Skip to content

Commit

Permalink
Clean up SQL generation in MySQL platform
Browse files Browse the repository at this point in the history
  • Loading branch information
morozov committed Dec 2, 2021
1 parent 71b2e68 commit 3fdac36
Showing 1 changed file with 25 additions and 37 deletions.
62 changes: 25 additions & 37 deletions src/Platforms/MySQLPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,10 @@ public function getListTableConstraintsSQL($table)
public function getListTableIndexesSQL($table, $database = null)
{
if ($database !== null) {
$database = $this->quoteStringLiteral($database);
$table = $this->quoteStringLiteral($table);

return 'SELECT NON_UNIQUE AS Non_Unique, INDEX_NAME AS Key_name, COLUMN_NAME AS Column_Name,' .
' SUB_PART AS Sub_Part, INDEX_TYPE AS Index_Type' .
' FROM information_schema.STATISTICS WHERE TABLE_NAME = ' . $table .
' AND TABLE_SCHEMA = ' . $database .
' FROM information_schema.STATISTICS WHERE TABLE_NAME = ' . $this->quoteStringLiteral($table) .
' AND TABLE_SCHEMA = ' . $this->quoteStringLiteral($database) .
' ORDER BY SEQ_IN_INDEX ASC';
}

Expand All @@ -173,9 +170,7 @@ public function getListTableIndexesSQL($table, $database = null)
*/
public function getListViewsSQL($database)
{
$database = $this->quoteStringLiteral($database);

return 'SELECT * FROM information_schema.VIEWS WHERE TABLE_SCHEMA = ' . $database;
return 'SELECT * FROM information_schema.VIEWS WHERE TABLE_SCHEMA = ' . $this->quoteStringLiteral($database);
}

/**
Expand All @@ -186,25 +181,16 @@ public function getListViewsSQL($database)
*/
public function getListTableForeignKeysSQL($table, $database = null)
{
$table = $this->quoteStringLiteral($table);

if ($database !== null) {
$database = $this->quoteStringLiteral($database);
}

$sql = 'SELECT DISTINCT k.`CONSTRAINT_NAME`, k.`COLUMN_NAME`, k.`REFERENCED_TABLE_NAME`, ' .
'k.`REFERENCED_COLUMN_NAME`, k.`ORDINAL_POSITION` /*!50116 , c.update_rule, c.delete_rule */ ' .
'FROM information_schema.key_column_usage k /*!50116 ' .
'INNER JOIN information_schema.referential_constraints c ON ' .
' c.constraint_name = k.constraint_name AND ' .
' c.table_name = ' . $table . ' */ WHERE k.table_name = ' . $table;

$databaseNameSql = $database ?? 'DATABASE()';

return $sql . ' AND k.table_schema = ' . $databaseNameSql
. ' /*!50116 AND c.constraint_schema = ' . $databaseNameSql . ' */'
. ' AND k.`REFERENCED_COLUMN_NAME` is not NULL'
. ' ORDER BY k.`ORDINAL_POSITION`';
return 'SELECT k.CONSTRAINT_NAME, k.COLUMN_NAME, k.REFERENCED_TABLE_NAME, ' .
'k.REFERENCED_COLUMN_NAME /*!50116 , c.update_rule, c.delete_rule */ ' .
'FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE k /*!50116 ' .
'INNER JOIN INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS c ON ' .
'c.CONSTRAINT_NAME = k.CONSTRAINT_NAME AND ' .
'c.TABLE_NAME = k.TABLE_NAME AND ' .
'c.CONSTRAINT_SCHEMA = k.CONSTRAINT_SCHEMA */ ' .
'WHERE k.TABLE_NAME = ' . $this->quoteStringLiteral($table) . ' ' .
'AND k.TABLE_SCHEMA = ' . $this->getDatabaseNameSQL($database) . ' ' .
'ORDER BY k.ORDINAL_POSITION';
}

/**
Expand Down Expand Up @@ -342,18 +328,11 @@ public function getListTablesSQL()
*/
public function getListTableColumnsSQL($table, $database = null)
{
$table = $this->quoteStringLiteral($table);

if ($database !== null) {
$database = $this->quoteStringLiteral($database);
} else {
$database = 'DATABASE()';
}

return 'SELECT COLUMN_NAME AS Field, COLUMN_TYPE AS Type, IS_NULLABLE AS `Null`, ' .
'COLUMN_KEY AS `Key`, COLUMN_DEFAULT AS `Default`, EXTRA AS Extra, COLUMN_COMMENT AS Comment, ' .
'CHARACTER_SET_NAME AS CharacterSet, COLLATION_NAME AS Collation ' .
'FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = ' . $database . ' AND TABLE_NAME = ' . $table .
'FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = ' . $this->getDatabaseNameSQL($database) .
' AND TABLE_NAME = ' . $this->quoteStringLiteral($table) .
' ORDER BY ORDINAL_POSITION ASC';
}

Expand All @@ -373,7 +352,7 @@ public function getListTableMetadataSQL(string $table, ?string $database = null)
WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_SCHEMA = %s AND TABLE_NAME = %s
SQL
,
$database !== null ? $this->quoteStringLiteral($database) : 'DATABASE()',
$this->getDatabaseNameSQL($database),
$this->quoteStringLiteral($table)
);
}
Expand Down Expand Up @@ -1184,4 +1163,13 @@ public function supportsColumnLengthIndexes(): bool
{
return true;
}

private function getDatabaseNameSQL(?string $databaseName): string
{
if ($databaseName !== null) {
return $this->quoteStringLiteral($databaseName);
}

return $this->getCurrentDatabaseExpression();
}
}

0 comments on commit 3fdac36

Please sign in to comment.