Refactor $sql_data

Signed-off-by: Kamil Tekiela <tekiela246@gmail.com>
This commit is contained in:
Kamil Tekiela 2022-03-17 19:32:22 +00:00
parent ff4614d581
commit d043ceedc6
13 changed files with 113 additions and 137 deletions

View File

@ -100,7 +100,6 @@ final class ImportController extends AbstractController
$GLOBALS['reset_charset'] = $GLOBALS['reset_charset'] ?? null;
$GLOBALS['result'] = $GLOBALS['result'] ?? null;
$GLOBALS['import_file_name'] = $GLOBALS['import_file_name'] ?? null;
$GLOBALS['sql_data'] = $GLOBALS['sql_data'] ?? null;
$GLOBALS['import_notice'] = $GLOBALS['import_notice'] ?? null;
$GLOBALS['read_multiply'] = $GLOBALS['read_multiply'] ?? null;
$GLOBALS['my_die'] = $GLOBALS['my_die'] ?? null;
@ -597,10 +596,7 @@ final class ImportController extends AbstractController
// This array contain the data like number of valid sql queries in the statement
// and complete valid sql statement (which affected for rows)
$GLOBALS['sql_data'] = [
'valid_sql' => [],
'valid_queries' => 0,
];
$queriesToBeExecuted = [];
if (! $GLOBALS['error']) {
/**
@ -624,7 +620,7 @@ final class ImportController extends AbstractController
// Do the real import
$default_fk_check = ForeignKey::handleDisableCheckInit();
try {
$import_plugin->doImport($importHandle ?? null, $GLOBALS['sql_data']);
$queriesToBeExecuted = $import_plugin->doImport($importHandle ?? null);
ForeignKey::handleDisableCheckCleanup($default_fk_check);
} catch (Throwable $e) {
ForeignKey::handleDisableCheckCleanup($default_fk_check);
@ -741,16 +737,15 @@ final class ImportController extends AbstractController
}
if ($GLOBALS['go_sql']) {
if (! empty($GLOBALS['sql_data']) && ($GLOBALS['sql_data']['valid_queries'] > 1)) {
if ($queriesToBeExecuted !== []) {
$_SESSION['is_multi_query'] = true;
$sql_queries = $GLOBALS['sql_data']['valid_sql'];
} else {
$sql_queries = [$GLOBALS['sql_query']];
$queriesToBeExecuted = [$GLOBALS['sql_query']];
}
$html_output = '';
foreach ($sql_queries as $GLOBALS['sql_query']) {
foreach ($queriesToBeExecuted as $GLOBALS['sql_query']) {
// parse sql query
[
$analyzed_sql_results,

View File

@ -61,7 +61,6 @@ class SqlController extends AbstractController
$GLOBALS['disp_query'] = $GLOBALS['disp_query'] ?? null;
$GLOBALS['extra_data'] = $GLOBALS['extra_data'] ?? null;
$GLOBALS['message_to_show'] = $GLOBALS['message_to_show'] ?? null;
$GLOBALS['sql_data'] = $GLOBALS['sql_data'] ?? null;
$GLOBALS['disp_message'] = $GLOBALS['disp_message'] ?? null;
$GLOBALS['complete_query'] = $GLOBALS['complete_query'] ?? null;
$GLOBALS['is_gotofile'] = $GLOBALS['is_gotofile'] ?? null;
@ -216,7 +215,7 @@ class SqlController extends AbstractController
$GLOBALS['import_text'] ?? null,
$GLOBALS['extra_data'] ?? null,
$GLOBALS['message_to_show'] ?? null,
$GLOBALS['sql_data'] ?? null,
null,
$GLOBALS['goto'],
isset($GLOBALS['disp_query']) ? $GLOBALS['display_query'] : null,
$GLOBALS['disp_message'] ?? null,

View File

@ -108,11 +108,10 @@ class Import
* Runs query inside import buffer. This is needed to allow displaying
* of last SELECT, SHOW or HANDLER results and similar nice stuff.
*
* @param string $sql query to run
* @param string $full query to display, this might be commented
* @param array $sqlData SQL parse data storage
* @param string $sql query to run
* @param string[] $sqlData SQL parse data storage
*/
public function executeQuery(string $sql, string $full, array &$sqlData): void
public function executeQuery(string $sql, array &$sqlData): void
{
$GLOBALS['my_die'] = $GLOBALS['my_die'] ?? null;
$GLOBALS['error'] = $GLOBALS['error'] ?? null;
@ -132,7 +131,7 @@ class Import
}
$GLOBALS['my_die'][] = [
'sql' => $full,
'sql' => $sql,
'error' => $GLOBALS['dbi']->getError(),
];
@ -156,9 +155,7 @@ class Import
}
if (($aNumRows > 0) || $isUseQuery) {
$sqlData['valid_sql'][] = $sql;
$sqlData['valid_queries'] = $sqlData['valid_queries'] ?? 0;
$sqlData['valid_queries']++;
$sqlData[] = $sql;
}
}
@ -184,13 +181,11 @@ class Import
* Runs query inside import buffer. This is needed to allow displaying
* of last SELECT, SHOW or HANDLER results and similar nice stuff.
*
* @param string $sql query to run
* @param array $sqlData SQL parse data storage
* @param string $sql query to run
* @param string[] $sqlData SQL parse data storage
*/
public function runQuery(
string $sql = '',
array &$sqlData = []
): void {
public function runQuery(string $sql, array &$sqlData): void
{
$GLOBALS['go_sql'] = $GLOBALS['go_sql'] ?? null;
$GLOBALS['complete_query'] = $GLOBALS['complete_query'] ?? null;
$GLOBALS['display_query'] = $GLOBALS['display_query'] ?? null;
@ -241,31 +236,20 @@ class Import
}
$GLOBALS['sql_query'] = $this->importRunBuffer;
$sqlData['valid_sql'][] = $this->importRunBuffer;
$sqlData['valid_full'][] = $this->importRunBuffer;
$sqlData['valid_queries'] = $sqlData['valid_queries'] ?? 0;
$sqlData['valid_queries']++;
$sqlData[] = $this->importRunBuffer;
} elseif ($GLOBALS['run_query']) {
/* Handle rollback from go_sql */
if ($GLOBALS['go_sql'] && isset($sqlData['valid_full'])) {
$queries = $sqlData['valid_sql'];
$fulls = $sqlData['valid_full'];
$count = $sqlData['valid_queries'];
if ($GLOBALS['go_sql'] && $sqlData !== []) {
$queries = $sqlData;
$sqlData = [];
$GLOBALS['go_sql'] = false;
$sqlData['valid_sql'] = [];
$sqlData['valid_queries'] = 0;
unset($sqlData['valid_full']);
for ($i = 0; $i < $count; $i++) {
$this->executeQuery($queries[$i], $fulls[$i], $sqlData);
foreach ($queries as $query) {
$this->executeQuery($query, $sqlData);
}
}
$this->executeQuery(
$this->importRunBuffer,
$this->importRunBuffer,
$sqlData
);
$this->executeQuery($this->importRunBuffer, $sqlData);
}
} elseif (! empty($this->importRunBuffer)) {
if ($GLOBALS['go_sql']) {
@ -972,7 +956,7 @@ class Import
* @param array|null $analyses Analyses of the tables
* @param array|null $additionalSql Additional SQL statements to be executed
* @param array|null $options Associative array of options
* @param array $sqlData 2-element array with sql data
* @param string[] $sqlData 2-element array with sql data
*/
public function buildSql(
string $dbName,

View File

@ -153,10 +153,8 @@ class ImportCsv extends AbstractImportCsv
/**
* Handles the whole import logic
*
* @param array $sql_data 2-element array with sql data
*/
public function doImport(?File $importHandle = null, array &$sql_data = []): void
public function doImport(?File $importHandle = null): array
{
$GLOBALS['error'] = $GLOBALS['error'] ?? null;
$GLOBALS['message'] = $GLOBALS['message'] ?? null;
@ -197,6 +195,8 @@ class ImportCsv extends AbstractImportCsv
$GLOBALS['csv_columns']
);
$sqlStatements = [];
// Defaults for parser
$i = 0;
$len = 0;
@ -544,7 +544,7 @@ class ImportCsv extends AbstractImportCsv
* @todo maybe we could add original line to verbose
* SQL in comment
*/
$this->import->runQuery($sql, $sql_data);
$this->import->runQuery($sql, $sqlStatements);
}
$line++;
@ -627,16 +627,16 @@ class ImportCsv extends AbstractImportCsv
$create = null;
/* Created and execute necessary SQL statements from data */
$this->import->buildSql($db_name, $tables, $analyses, $create, $options, $sql_data);
$this->import->buildSql($db_name, $tables, $analyses, $create, $options, $sqlStatements);
unset($tables, $analyses);
}
// Commit any possible data in buffers
$this->import->runQuery('', $sql_data);
$this->import->runQuery('', $sqlStatements);
if (count($values) == 0 || $GLOBALS['error'] !== false) {
return;
return $sqlStatements;
}
$GLOBALS['message'] = Message::error(
@ -644,6 +644,8 @@ class ImportCsv extends AbstractImportCsv
);
$GLOBALS['message']->addParam($line);
$GLOBALS['error'] = true;
return $sqlStatements;
}
private function buildErrorsForParams(

View File

@ -86,10 +86,8 @@ class ImportLdi extends AbstractImportCsv
/**
* Handles the whole import logic
*
* @param array $sql_data 2-element array with sql data
*/
public function doImport(?File $importHandle = null, array &$sql_data = []): void
public function doImport(?File $importHandle = null): array
{
$GLOBALS['finished'] = $GLOBALS['finished'] ?? null;
$GLOBALS['import_file'] = $GLOBALS['import_file'] ?? null;
@ -104,6 +102,7 @@ class ImportLdi extends AbstractImportCsv
$GLOBALS['skip_queries'] = $GLOBALS['skip_queries'] ?? null;
$GLOBALS['ldi_columns'] = $GLOBALS['ldi_columns'] ?? null;
$sqlStatements = [];
$compression = '';
if ($importHandle !== null) {
$compression = $importHandle->getCompression();
@ -116,7 +115,7 @@ class ImportLdi extends AbstractImportCsv
);
$GLOBALS['error'] = true;
return;
return [];
}
$sql = 'LOAD DATA';
@ -186,9 +185,11 @@ class ImportLdi extends AbstractImportCsv
$sql .= ')';
}
$this->import->runQuery($sql, $sql_data);
$this->import->runQuery('', $sql_data);
$this->import->runQuery($sql, $sqlStatements);
$this->import->runQuery('', $sqlStatements);
$GLOBALS['finished'] = true;
return $sqlStatements;
}
public function isAvailable(): bool

View File

@ -63,15 +63,15 @@ class ImportMediawiki extends ImportPlugin
/**
* Handles the whole import logic
*
* @param array $sql_data 2-element array with sql data
*/
public function doImport(?File $importHandle = null, array &$sql_data = []): void
public function doImport(?File $importHandle = null): array
{
$GLOBALS['error'] = $GLOBALS['error'] ?? null;
$GLOBALS['timeout_passed'] = $GLOBALS['timeout_passed'] ?? null;
$GLOBALS['finished'] = $GLOBALS['finished'] ?? null;
$sqlStatements = [];
// Defaults for parser
// The buffer that will be used to store chunks read from the imported file
@ -233,7 +233,7 @@ class ImportMediawiki extends ImportPlugin
];
// Import the current table data into the database
$this->importDataOneTable($current_table, $sql_data);
$this->importDataOneTable($current_table, $sqlStatements);
// Reset table name
$cur_table_name = '';
@ -281,23 +281,25 @@ class ImportMediawiki extends ImportPlugin
}
}
}
return $sqlStatements;
}
/**
* Imports data from a single table
*
* @param array $table containing all table info:
* <code> $table[0] - string
* containing table name
* $table[1] - array[] of
* table headers $table[2] -
* array[][] of table content
* rows </code>
* @param array $sql_data 2-element array with sql data
* @param array $table containing all table info:
* <code> $table[0] - string
* containing table name
* $table[1] - array[] of
* table headers $table[2] -
* array[][] of table content
* rows </code>
* @param string[] $sqlStatements List of SQL statements to be executed
*
* @global bool $analyze whether to scan for column types
*/
private function importDataOneTable(array $table, array &$sql_data): void
private function importDataOneTable(array $table, array &$sqlStatements): void
{
$analyze = $this->getAnalyze();
if ($analyze) {
@ -319,11 +321,11 @@ class ImportMediawiki extends ImportPlugin
$analyses = [];
$analyses[] = $this->import->analyzeTable($tables[0]);
$this->executeImportTables($tables, $analyses, $sql_data);
$this->executeImportTables($tables, $analyses, $sqlStatements);
}
// Commit any possible data in buffers
$this->import->runQuery('', $sql_data);
$this->import->runQuery('', $sqlStatements);
}
/**
@ -367,20 +369,20 @@ class ImportMediawiki extends ImportPlugin
* Sets the database name and additional options and calls Import::buildSql()
* Used in PMA_importDataAllTables() and $this->importDataOneTable()
*
* @param array $tables structure:
* array(
* array(table_name, array() column_names, array()()
* rows)
* )
* @param array $analyses structure:
* $analyses = array(
* array(array() column_types, array() column_sizes)
* )
* @param array $sql_data 2-element array with sql data
* @param array $tables structure:
* array(
* array(table_name, array() column_names, array()()
* rows)
* )
* @param array $analyses structure:
* $analyses = array(
* array(array() column_types, array() column_sizes)
* )
* @param string[] $sqlStatements List of SQL statements to be executed
*
* @global string $db name of the database to import in
*/
private function executeImportTables(array &$tables, array &$analyses, array &$sql_data): void
private function executeImportTables(array &$tables, array &$analyses, array &$sqlStatements): void
{
// $db_name : The currently selected database name, if applicable
// No backquotes
@ -392,7 +394,7 @@ class ImportMediawiki extends ImportPlugin
$create = null;
// Create and execute necessary SQL statements from data
$this->import->buildSql($db_name, $tables, $analyses, $create, $options, $sql_data);
$this->import->buildSql($db_name, $tables, $analyses, $create, $options, $sqlStatements);
}
/**

View File

@ -98,15 +98,14 @@ class ImportOds extends ImportPlugin
/**
* Handles the whole import logic
*
* @param array $sql_data 2-element array with sql data
*/
public function doImport(?File $importHandle = null, array &$sql_data = []): void
public function doImport(?File $importHandle = null): array
{
$GLOBALS['error'] = $GLOBALS['error'] ?? null;
$GLOBALS['timeout_passed'] = $GLOBALS['timeout_passed'] ?? null;
$GLOBALS['finished'] = $GLOBALS['finished'] ?? null;
$sqlStatements = [];
$buffer = '';
/**
@ -223,12 +222,14 @@ class ImportOds extends ImportPlugin
$create = null;
/* Created and execute necessary SQL statements from data */
$this->import->buildSql($db_name, $tables, $analyses, $create, $options, $sql_data);
$this->import->buildSql($db_name, $tables, $analyses, $create, $options, $sqlStatements);
unset($tables, $analyses);
/* Commit any possible data in buffers */
$this->import->runQuery('', $sql_data);
$this->import->runQuery('', $sqlStatements);
return $sqlStatements;
}
/**

View File

@ -74,10 +74,8 @@ class ImportShp extends ImportPlugin
/**
* Handles the whole import logic
*
* @param array $sql_data 2-element array with sql data
*/
public function doImport(?File $importHandle = null, array &$sql_data = []): void
public function doImport(?File $importHandle = null): array
{
$GLOBALS['error'] = $GLOBALS['error'] ?? null;
$GLOBALS['import_file'] = $GLOBALS['import_file'] ?? null;
@ -86,7 +84,7 @@ class ImportShp extends ImportPlugin
$GLOBALS['finished'] = false;
if ($importHandle === null || $this->zipExtension === null) {
return;
return [];
}
/** @see ImportShp::readFromBuffer() */
@ -104,7 +102,7 @@ class ImportShp extends ImportPlugin
);
$GLOBALS['message']->addParam($importHandle->getError());
return;
return [];
}
}
@ -170,7 +168,7 @@ class ImportShp extends ImportPlugin
);
$GLOBALS['message']->addParam($shp->lastError);
return;
return [];
}
switch ($shp->shapeType) {
@ -200,7 +198,7 @@ class ImportShp extends ImportPlugin
);
$GLOBALS['message']->addParam($shp->getShapeName());
return;
return [];
}
if (isset($gis_type)) {
@ -248,7 +246,7 @@ class ImportShp extends ImportPlugin
__('The imported file does not contain any data!')
);
return;
return [];
}
// Column names for spatial column and the rest of the columns,
@ -299,7 +297,8 @@ class ImportShp extends ImportPlugin
// Created and execute necessary SQL statements from data
$null_param = null;
$this->import->buildSql($db_name, $tables, $analyses, $null_param, $options, $sql_data);
$sqlStatements = [];
$this->import->buildSql($db_name, $tables, $analyses, $null_param, $options, $sqlStatements);
unset($tables, $analyses);
@ -307,7 +306,9 @@ class ImportShp extends ImportPlugin
$GLOBALS['error'] = false;
// Commit any possible data in buffers
$this->import->runQuery('', $sql_data);
$this->import->runQuery('', $sqlStatements);
return $sqlStatements;
}
/**

View File

@ -93,10 +93,8 @@ class ImportSql extends ImportPlugin
/**
* Handles the whole import logic
*
* @param array $sql_data 2-element array with sql data
*/
public function doImport(?File $importHandle = null, array &$sql_data = []): void
public function doImport(?File $importHandle = null): array
{
$GLOBALS['error'] = $GLOBALS['error'] ?? null;
$GLOBALS['timeout_passed'] = $GLOBALS['timeout_passed'] ?? null;
@ -116,6 +114,8 @@ class ImportSql extends ImportPlugin
*/
$GLOBALS['finished'] = false;
$sqlStatements = [];
while (! $GLOBALS['error'] && ! $GLOBALS['timeout_passed']) {
// Getting the first statement, the remaining data and the last
// delimiter.
@ -146,7 +146,7 @@ class ImportSql extends ImportPlugin
}
// Executing the query.
$this->import->runQuery($statement, $sql_data);
$this->import->runQuery($statement, $sqlStatements);
}
// Extracting remaining statements.
@ -156,11 +156,13 @@ class ImportSql extends ImportPlugin
continue;
}
$this->import->runQuery($statement, $sql_data);
$this->import->runQuery($statement, $sqlStatements);
}
// Finishing.
$this->import->runQuery('', $sql_data);
$this->import->runQuery('', $sqlStatements);
return $sqlStatements;
}
/**

View File

@ -55,10 +55,8 @@ class ImportXml extends ImportPlugin
/**
* Handles the whole import logic
*
* @param array $sql_data 2-element array with sql data
*/
public function doImport(?File $importHandle = null, array &$sql_data = []): void
public function doImport(?File $importHandle = null): array
{
$GLOBALS['error'] = $GLOBALS['error'] ?? null;
$GLOBALS['timeout_passed'] = $GLOBALS['timeout_passed'] ?? null;
@ -117,7 +115,7 @@ class ImportXml extends ImportPlugin
unset($xml);
$GLOBALS['finished'] = false;
return;
return [];
}
/**
@ -180,7 +178,7 @@ class ImportXml extends ImportPlugin
unset($xml);
$GLOBALS['finished'] = false;
return;
return [];
}
/**
@ -354,11 +352,14 @@ class ImportXml extends ImportPlugin
}
/* Created and execute necessary SQL statements from data */
$this->import->buildSql($db_name, $tables, $analyses, $create, $options, $sql_data);
$sqlStatements = [];
$this->import->buildSql($db_name, $tables, $analyses, $create, $options, $sqlStatements);
unset($analyses, $tables, $create);
/* Commit any possible data in buffers */
$this->import->runQuery('', $sql_data);
$this->import->runQuery('', $sqlStatements);
return $sqlStatements;
}
}

View File

@ -94,18 +94,17 @@ class Import[Name] extends ImportPlugin
/**
* Handles the whole import logic
*
* @param array &$sql_data 2-element array with sql data
*
* @return void
* @return array A list of SQL statements to be executed
*/
public function doImport(&$sql_data = [])
public function doImport(?File $importHandle = null): array
{
// get globals (others are optional)
global $error, $timeout_passed, $finished;
$sqlStatements = [];
$buffer = '';
while (! ($finished && $i >= $len) && ! $error && ! $timeout_passed) {
$data = $this->import->getNextChunk();
$data = $this->import->getNextChunk($importHandle);
if ($data === false) {
// subtract data we didn't handle yet and stop processing
$GLOBALS['offset'] -= strlen($buffer);
@ -119,10 +118,12 @@ class Import[Name] extends ImportPlugin
$buffer .= $data;
}
// PARSE $buffer here, post sql queries using:
$this->import->runQuery($sql, $verbose_sql_with_comments, $sql_data);
$this->import->runQuery($sql, $sqlStatements);
} // End of import loop
// Commit any possible data in buffers
$this->import->runQuery('', '', $sql_data);
$this->import->runQuery('', $sqlStatements);
return $sqlStatements;
}
/* optional: */

View File

@ -46,10 +46,8 @@ abstract class ImportPlugin implements Plugin
/**
* Handles the whole import logic
*
* @param array $sql_data 2-element array with sql data
*/
abstract public function doImport(?File $importHandle = null, array &$sql_data = []): void;
abstract public function doImport(?File $importHandle = null): array;
/**
* Gets the import specific format plugin properties

View File

@ -565,11 +565,7 @@ class ImportTest extends AbstractTestCase
$this->import->runQuery('SELECT 2', $sqlData);
$this->assertSame([
'valid_sql' => ['SELECT 1;'],
'valid_full' => ['SELECT 1;'],
'valid_queries' => 1,
], $sqlData);
$this->assertSame(['SELECT 1;'], $sqlData);
$this->assertSame('SELECT 1;', $GLOBALS['sql_query']);
$this->assertSame('SELECT 1;', $GLOBALS['complete_query']);
$this->assertSame('SELECT 1;', $GLOBALS['display_query']);
@ -577,15 +573,8 @@ class ImportTest extends AbstractTestCase
$this->import->runQuery('', $sqlData);
$this->assertSame([
'valid_sql' => [
'SELECT 1;',
'SELECT 2;',
],
'valid_full' => [
'SELECT 1;',
'SELECT 2;',
],
'valid_queries' => 2,
'SELECT 1;',
'SELECT 2;',
], $sqlData);
$this->assertSame('SELECT 2;', $GLOBALS['sql_query']);