Merge pull request #19035 from kamil-tekiela/Refactor-import-4

Split $createDb from buildSql() method
This commit is contained in:
Maurício Meneghini Fauth 2024-03-08 15:46:33 -03:00 committed by GitHub
commit ac77797d89
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
11 changed files with 74 additions and 68 deletions

View File

@ -8600,6 +8600,11 @@ parameters:
count: 1
path: src/Import/Import.php
-
message: "#^Method PhpMyAdmin\\\\Import\\\\Import\\:\\:createDatabase\\(\\) should return array\\<string\\> but returns mixed\\.$#"
count: 1
path: src/Import/Import.php
-
message: "#^Method PhpMyAdmin\\\\Import\\\\Import\\:\\:detectSize\\(\\) should return int\\|string but returns mixed\\.$#"
count: 3
@ -11395,11 +11400,6 @@ parameters:
count: 1
path: src/Plugins/Import/ImportCsv.php
-
message: "#^Parameter \\#1 \\$dbName of method PhpMyAdmin\\\\Import\\\\Import\\:\\:buildSql\\(\\) expects string, mixed given\\.$#"
count: 1
path: src/Plugins/Import/ImportCsv.php
-
message: "#^Parameter \\#1 \\$serverMessage of static method PhpMyAdmin\\\\Html\\\\Generator\\:\\:mysqlDie\\(\\) expects string, mixed given\\.$#"
count: 1
@ -11440,6 +11440,11 @@ parameters:
count: 1
path: src/Plugins/Import/ImportCsv.php
-
message: "#^Parameter \\#4 \\$sqlData of method PhpMyAdmin\\\\Import\\\\Import\\:\\:createDatabase\\(\\) expects array\\<string\\>, mixed given\\.$#"
count: 1
path: src/Plugins/Import/ImportCsv.php
-
message: "#^Cannot cast mixed to string\\.$#"
count: 5

View File

@ -6428,8 +6428,6 @@
<MixedAssignment>
<code><![CDATA[$active]]></code>
<code><![CDATA[$cellValue]]></code>
<code><![CDATA[$charset]]></code>
<code><![CDATA[$collation]]></code>
<code><![CDATA[$importPlugin]]></code>
</MixedAssignment>
<MixedInferredReturnType>
@ -6440,10 +6438,6 @@
<code><![CDATA[getProperties]]></code>
</MixedMethodCall>
<MixedOperand>
<code><![CDATA[$charset]]></code>
<code><![CDATA[$charset]]></code>
<code><![CDATA[$collation]]></code>
<code><![CDATA[$collation]]></code>
<code><![CDATA[$importPlugin->getProperties()->getExtension()]]></code>
<code><![CDATA[$size[self::D]]]></code>
<code><![CDATA[ImportSettings::$maximumTime]]></code>
@ -8702,13 +8696,9 @@
<code><![CDATA[$GLOBALS['errorUrl']]]></code>
<code><![CDATA[$fields[]]]></code>
</MixedAssignment>
<PossiblyInvalidArgument>
<code><![CDATA[$dbName]]></code>
</PossiblyInvalidArgument>
<PossiblyInvalidCast>
<code><![CDATA[$_REQUEST['csv_new_db_name']]]></code>
<code><![CDATA[$_REQUEST['csv_new_tbl_name']]]></code>
<code><![CDATA[$dbName]]></code>
</PossiblyInvalidCast>
<PossiblyInvalidOperand>
<code><![CDATA[$maxLines]]></code>

View File

@ -888,7 +888,6 @@ class Import
* @param ImportTable[] $tables
* @param array{0:ColumnType[], 1:(int|string)[], 2?:true[]}[]|null $analyses Analyses of the tables
* @param string[]|null $additionalSql Additional SQL to be executed
* @param mixed[]|null $options Associative array of options
* @param string[] $sqlData List of SQL to be executed
*/
public function buildSql(
@ -896,24 +895,11 @@ class Import
array $tables,
array|null $analyses = null,
array|null $additionalSql = null,
bool $createDb = true,
array|null $options = null,
array &$sqlData = [],
): void {
/* Needed to quell the beast that is Message */
ImportSettings::$importNotice = '';
/* Take care of the options */
$collation = $options['db_collation'] ?? 'utf8_general_ci';
$charset = $options['db_charset'] ?? 'utf8';
if ($createDb) {
$sql = 'CREATE DATABASE IF NOT EXISTS ' . Util::backquote($dbName)
. ' DEFAULT CHARACTER SET ' . $charset . ' COLLATE ' . $collation
. ';';
$this->runQuery($sql, $sqlData);
}
/* Run the $additional_sql statements supplied by the caller plug-in */
if ($additionalSql != null) {
/* Clean the SQL first */
@ -977,8 +963,7 @@ class Import
$tempSQLStr .= ', ';
}
$tempSQLStr .= ') DEFAULT CHARACTER SET ' . $charset
. ' COLLATE ' . $collation . ';';
$tempSQLStr .= ')';
/**
* Each SQL statement is executed immediately
@ -994,7 +979,6 @@ class Import
*
* Only one insert query is formed for each table
*/
$tempSQLStr = '';
$colCount = 0;
$dbi = DatabaseInterface::getInstance();
foreach ($tables as $i => $table) {
@ -1066,8 +1050,6 @@ class Import
unset($table->rows[$j]);
}
$tempSQLStr .= ';';
/**
* Each SQL statement is executed immediately
* after it is formed so that we don't have
@ -1076,9 +1058,6 @@ class Import
$this->runQuery($tempSQLStr, $sqlData);
}
/* No longer needed */
unset($tempSQLStr);
/**
* A work in progress
*/
@ -1404,4 +1383,18 @@ class Import
return $importFileName;
}
/**
* @param string[] $sqlData List of SQL statements to be executed
*
* @return string[]
*/
public function createDatabase(string $dbName, string $charset, string $collation, array $sqlData): array
{
$sql = 'CREATE DATABASE IF NOT EXISTS ' . Util::backquote($dbName)
. ' DEFAULT CHARACTER SET ' . $charset . ' COLLATE ' . $collation;
$this->runQuery($sql, $sqlData);
return $sqlData;
}
}

View File

@ -22,6 +22,7 @@ use PhpMyAdmin\Properties\Options\Items\NumberPropertyItem;
use PhpMyAdmin\Properties\Options\Items\TextPropertyItem;
use PhpMyAdmin\Properties\Plugins\ImportPluginProperties;
use PhpMyAdmin\Util;
use Webmozart\Assert\Assert;
use function __;
use function array_pad;
@ -590,6 +591,7 @@ class ImportCsv extends AbstractImportCsv
*/
if (isset($_REQUEST['csv_new_db_name']) && (string) $_REQUEST['csv_new_db_name'] !== '') {
$newDb = $_REQUEST['csv_new_db_name'];
Assert::string($newDb);
} else {
$result = $dbi->fetchResult('SHOW DATABASES');
@ -599,7 +601,11 @@ class ImportCsv extends AbstractImportCsv
$dbName = Current::$database !== '' ? Current::$database : $newDb;
$createDb = Current::$database === '';
$this->import->buildSql($dbName, [$table], [$analysis], createDb:$createDb, sqlData:$sqlStatements);
if ($createDb) {
$sqlStatements = $this->import->createDatabase($dbName, 'utf8', 'utf8_general_ci', $sqlStatements);
}
$this->import->buildSql($dbName, [$table], [$analysis], sqlData: $sqlStatements);
}
// Commit any possible data in buffers

View File

@ -305,11 +305,16 @@ class ImportMediawiki extends ImportPlugin
// Obtain the best-fit MySQL types for each column
$analysis = $this->import->analyzeTable($table);
$dbName = Current::$database !== '' ? Current::$database : 'mediawiki_DB';
if (Current::$database === '') {
$sqlStatements = $this->import->createDatabase($dbName, 'utf8', 'utf8_general_ci', $sqlStatements);
}
$this->import->buildSql(
Current::$database !== '' ? Current::$database : 'mediawiki_DB',
$dbName,
[$table],
[$analysis],
createDb: Current::$database === '',
sqlData: $sqlStatements,
);
}

View File

@ -167,8 +167,12 @@ class ImportOds extends ImportPlugin
$dbName = Current::$database !== '' ? Current::$database : 'ODS_DB';
$createDb = Current::$database === '';
if ($createDb) {
$sqlStatements = $this->import->createDatabase($dbName, 'utf8', 'utf8_general_ci', $sqlStatements);
}
/* Created and execute necessary SQL statements from data */
$this->import->buildSql($dbName, $tables, $analyses, createDb:$createDb, sqlData:$sqlStatements);
$this->import->buildSql($dbName, $tables, $analyses, sqlData: $sqlStatements);
/* Commit any possible data in buffers */
$this->import->runQuery('', $sqlStatements);

View File

@ -288,7 +288,11 @@ class ImportShp extends ImportPlugin
// Created and execute necessary SQL statements from data
$sqlStatements = [];
$this->import->buildSql($dbName, [$table], [$analysis], createDb:$createDb, sqlData:$sqlStatements);
if ($createDb) {
$sqlStatements = $this->import->createDatabase($dbName, 'utf8', 'utf8_general_ci', []);
}
$this->import->buildSql($dbName, [$table], [$analysis], sqlData: $sqlStatements);
ImportSettings::$finished = true;
$GLOBALS['error'] = false;

View File

@ -240,21 +240,23 @@ class ImportXml extends ImportPlugin
$create = null;
}
/* Created and execute necessary SQL statements from data */
$sqlStatements = [];
/* Set database name to the currently selected one, if applicable */
if (Current::$database !== '') {
/* Override the database name in the XML file, if one is selected */
$dbName = Current::$database;
$options = null;
} else {
/* Set database collation/charset */
$options = ['db_collation' => $collation, 'db_charset' => $charset];
$sqlStatements = $this->import->createDatabase(
$dbName,
$charset ?? 'utf8',
$collation ?? 'utf8_general_ci',
[],
);
}
$createDb = Current::$database === '';
/* Created and execute necessary SQL statements from data */
$sqlStatements = [];
$this->import->buildSql($dbName, $tables, $analyses, $create, $createDb, $options, $sqlStatements);
$this->import->buildSql($dbName, $tables, $analyses, $create, $sqlStatements);
/* Commit any possible data in buffers */
$this->import->runQuery('', $sqlStatements);

View File

@ -261,10 +261,10 @@ class ImportCsvTest extends AbstractTestCase
$this->object->doImport();
self::assertSame(
'CREATE DATABASE IF NOT EXISTS `CSV_DB 1` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;;'
. 'CREATE TABLE IF NOT EXISTS `CSV_DB 1`.`db_test` (`COL 1` varchar(5), `COL 2` varchar(5))'
. ' DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;;INSERT INTO `CSV_DB 1`.`db_test`'
. ' (`COL 1`, `COL 2`) VALUES (\'Row 1\', \'Row 2\'),' . "\n" . ' (\'123\', \'456\');;',
'CREATE DATABASE IF NOT EXISTS `CSV_DB 1` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;'
. 'CREATE TABLE IF NOT EXISTS `CSV_DB 1`.`db_test` (`COL 1` varchar(5), `COL 2` varchar(5));'
. 'INSERT INTO `CSV_DB 1`.`db_test`'
. ' (`COL 1`, `COL 2`) VALUES (\'Row 1\', \'Row 2\'),' . "\n" . ' (\'123\', \'456\');',
$GLOBALS['sql_query'],
);
@ -306,10 +306,10 @@ class ImportCsvTest extends AbstractTestCase
$this->object->doImport();
self::assertSame(
'CREATE DATABASE IF NOT EXISTS `CSV_DB 1` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;;'
. 'CREATE TABLE IF NOT EXISTS `CSV_DB 1`.`db_test` (`Row 1` int(3), `Row 2` int(3))'
. ' DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;;INSERT INTO `CSV_DB 1`.`db_test`'
. ' (`Row 1`, `Row 2`) VALUES (123, 456);;',
'CREATE DATABASE IF NOT EXISTS `CSV_DB 1` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;'
. 'CREATE TABLE IF NOT EXISTS `CSV_DB 1`.`db_test` (`Row 1` int(3), `Row 2` int(3));'
. 'INSERT INTO `CSV_DB 1`.`db_test`'
. ' (`Row 1`, `Row 2`) VALUES (123, 456);',
$GLOBALS['sql_query'],
);

View File

@ -185,28 +185,26 @@ class ImportOdsTest extends AbstractTestCase
// The process could probably detect that all the values for columns V to BL are empty
// That would make the empty columns not needed and would create a cleaner structure
$endOfSql = ');;';
$endOfSql = ');';
if (! $odsEmptyRowsMode) {
$fullCols = 'NULL' . str_repeat(', NULL', 18);// 19 empty cells
$endOfSql = '),' . "\n" . ' (' . $fullCols . '),' . "\n" . ' (' . $fullCols . ');;';
$endOfSql = '),' . "\n" . ' (' . $fullCols . '),' . "\n" . ' (' . $fullCols . ');';
}
//Test function called
$this->object->doImport($importHandle);
self::assertSame(
'CREATE DATABASE IF NOT EXISTS `ODS_DB` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;;'
'CREATE DATABASE IF NOT EXISTS `ODS_DB` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;'
. 'CREATE TABLE IF NOT EXISTS `ODS_DB`.`Shop` ('
. '`Artikelnummer` varchar(7), `Name` varchar(41), `keywords` varchar(15), `EK_Preis` varchar(21),'
. ' `Preis` varchar(23), `Details` varchar(10), `addInfo` varchar(22), `Einheit` varchar(3),'
. ' `Wirkstoff` varchar(10), `verkuerztHaltbar` varchar(21), `kuehlkette` varchar(7),'
. ' `Gebinde` varchar(71), `Verbrauchsnachweis` varchar(7), `Genehmigungspflichtig` varchar(7),'
. ' `Gefahrstoff` varchar(11), `GefahrArbeitsbereich` varchar(14), `Verwendungszweck` varchar(10),'
. ' `Verbrauch` varchar(10), `showLagerbestand` varchar(7)) '
. 'DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;;'
. 'CREATE TABLE IF NOT EXISTS `ODS_DB`.`Feuille 1` (`value` varchar(19)) '
. 'DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;;'
. ' `Verbrauch` varchar(10), `showLagerbestand` varchar(7));'
. 'CREATE TABLE IF NOT EXISTS `ODS_DB`.`Feuille 1` (`value` varchar(19));'
. 'INSERT INTO `ODS_DB`.`Shop` ('
. '`Artikelnummer`, `Name`, `keywords`, `EK_Preis`, `Preis`, `Details`, `addInfo`, `Einheit`,'
. ' `Wirkstoff`, `verkuerztHaltbar`, `kuehlkette`, `Gebinde`, `Verbrauchsnachweis`,'
@ -247,7 +245,7 @@ class ImportOdsTest extends AbstractTestCase
. ' (\'true\'),' . "\n"
. ' (\'12\')'
. ($odsEmptyRowsMode ? '' : ',' . "\n" . ' (NULL)')
. ($odsEmptyRowsMode ? ';;' : ',' . "\n" . ' (NULL);;'),
. ($odsEmptyRowsMode ? ';' : ',' . "\n" . ' (NULL);'),
$GLOBALS['sql_query'],
);

View File

@ -181,8 +181,7 @@ class ImportShpTest extends AbstractTestCase
if (extension_loaded('dbase')) {
self::assertStringContainsString(
'CREATE TABLE IF NOT EXISTS `SHP_DB`.`TBL_NAME` '
. '(`SPATIAL` geometry, `ID` int(2), `AUTHORITY` varchar(25), `NAME` varchar(42)) '
. 'DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;',
. '(`SPATIAL` geometry, `ID` int(2), `AUTHORITY` varchar(25), `NAME` varchar(42));',
$GLOBALS['sql_query'],
);