Commit a23a4711 authored by Steve Müller's avatar Steve Müller Committed by Marco Pivetta

escape identifiers in metadata SQL properly when used as string literal

Closes #2436
parent bce4a363
...@@ -251,6 +251,8 @@ class DB2Platform extends AbstractPlatform ...@@ -251,6 +251,8 @@ class DB2Platform extends AbstractPlatform
*/ */
public function getListTableColumnsSQL($table, $database = null) public function getListTableColumnsSQL($table, $database = null)
{ {
$table = $this->quoteStringLiteral($table);
// We do the funky subquery and join syscat.columns.default this crazy way because // We do the funky subquery and join syscat.columns.default this crazy way because
// as of db2 v10, the column is CLOB(64k) and the distinct operator won't allow a CLOB, // as of db2 v10, the column is CLOB(64k) and the distinct operator won't allow a CLOB,
// it wants shorter stuff like a varchar. // it wants shorter stuff like a varchar.
...@@ -283,7 +285,7 @@ class DB2Platform extends AbstractPlatform ...@@ -283,7 +285,7 @@ class DB2Platform extends AbstractPlatform
ON (c.tabschema = k.tabschema ON (c.tabschema = k.tabschema
AND c.tabname = k.tabname AND c.tabname = k.tabname
AND c.colname = k.colname) AND c.colname = k.colname)
WHERE UPPER(c.tabname) = UPPER('" . $table . "') WHERE UPPER(c.tabname) = UPPER(" . $table . ")
ORDER BY c.colno ORDER BY c.colno
) subq ) subq
JOIN syscat.columns cols JOIN syscat.columns cols
...@@ -315,6 +317,8 @@ class DB2Platform extends AbstractPlatform ...@@ -315,6 +317,8 @@ class DB2Platform extends AbstractPlatform
*/ */
public function getListTableIndexesSQL($table, $currentDatabase = null) public function getListTableIndexesSQL($table, $currentDatabase = null)
{ {
$table = $this->quoteStringLiteral($table);
return "SELECT idx.INDNAME AS key_name, return "SELECT idx.INDNAME AS key_name,
idxcol.COLNAME AS column_name, idxcol.COLNAME AS column_name,
CASE CASE
...@@ -328,7 +332,7 @@ class DB2Platform extends AbstractPlatform ...@@ -328,7 +332,7 @@ class DB2Platform extends AbstractPlatform
FROM SYSCAT.INDEXES AS idx FROM SYSCAT.INDEXES AS idx
JOIN SYSCAT.INDEXCOLUSE AS idxcol JOIN SYSCAT.INDEXCOLUSE AS idxcol
ON idx.INDSCHEMA = idxcol.INDSCHEMA AND idx.INDNAME = idxcol.INDNAME ON idx.INDSCHEMA = idxcol.INDSCHEMA AND idx.INDNAME = idxcol.INDNAME
WHERE idx.TABNAME = UPPER('" . $table . "') WHERE idx.TABNAME = UPPER(" . $table . ")
ORDER BY idxcol.COLSEQ ASC"; ORDER BY idxcol.COLSEQ ASC";
} }
...@@ -337,6 +341,8 @@ class DB2Platform extends AbstractPlatform ...@@ -337,6 +341,8 @@ class DB2Platform extends AbstractPlatform
*/ */
public function getListTableForeignKeysSQL($table) public function getListTableForeignKeysSQL($table)
{ {
$table = $this->quoteStringLiteral($table);
return "SELECT fkcol.COLNAME AS local_column, return "SELECT fkcol.COLNAME AS local_column,
fk.REFTABNAME AS foreign_table, fk.REFTABNAME AS foreign_table,
pkcol.COLNAME AS foreign_column, pkcol.COLNAME AS foreign_column,
...@@ -360,7 +366,7 @@ class DB2Platform extends AbstractPlatform ...@@ -360,7 +366,7 @@ class DB2Platform extends AbstractPlatform
ON fk.REFKEYNAME = pkcol.CONSTNAME ON fk.REFKEYNAME = pkcol.CONSTNAME
AND fk.REFTABSCHEMA = pkcol.TABSCHEMA AND fk.REFTABSCHEMA = pkcol.TABSCHEMA
AND fk.REFTABNAME = pkcol.TABNAME AND fk.REFTABNAME = pkcol.TABNAME
WHERE fk.TABNAME = UPPER('" . $table . "') WHERE fk.TABNAME = UPPER(" . $table . ")
ORDER BY fkcol.COLSEQ ASC"; ORDER BY fkcol.COLSEQ ASC";
} }
......
...@@ -153,11 +153,14 @@ class MySqlPlatform extends AbstractPlatform ...@@ -153,11 +153,14 @@ class MySqlPlatform extends AbstractPlatform
public function getListTableIndexesSQL($table, $currentDatabase = null) public function getListTableIndexesSQL($table, $currentDatabase = null)
{ {
if ($currentDatabase) { if ($currentDatabase) {
$currentDatabase = $this->quoteStringLiteral($currentDatabase);
$table = $this->quoteStringLiteral($table);
return "SELECT TABLE_NAME AS `Table`, NON_UNIQUE AS Non_Unique, INDEX_NAME AS Key_name, ". return "SELECT TABLE_NAME AS `Table`, NON_UNIQUE AS Non_Unique, INDEX_NAME AS Key_name, ".
"SEQ_IN_INDEX AS Seq_in_index, COLUMN_NAME AS Column_Name, COLLATION AS Collation, ". "SEQ_IN_INDEX AS Seq_in_index, COLUMN_NAME AS Column_Name, COLLATION AS Collation, ".
"CARDINALITY AS Cardinality, SUB_PART AS Sub_Part, PACKED AS Packed, " . "CARDINALITY AS Cardinality, SUB_PART AS Sub_Part, PACKED AS Packed, " .
"NULLABLE AS `Null`, INDEX_TYPE AS Index_Type, COMMENT AS Comment " . "NULLABLE AS `Null`, INDEX_TYPE AS Index_Type, COMMENT AS Comment " .
"FROM information_schema.STATISTICS WHERE TABLE_NAME = '" . $table . "' AND TABLE_SCHEMA = '" . $currentDatabase . "'"; "FROM information_schema.STATISTICS WHERE TABLE_NAME = " . $table . " AND TABLE_SCHEMA = " . $currentDatabase;
} }
return 'SHOW INDEX FROM ' . $table; return 'SHOW INDEX FROM ' . $table;
...@@ -168,7 +171,9 @@ class MySqlPlatform extends AbstractPlatform ...@@ -168,7 +171,9 @@ class MySqlPlatform extends AbstractPlatform
*/ */
public function getListViewsSQL($database) public function getListViewsSQL($database)
{ {
return "SELECT * FROM information_schema.VIEWS WHERE TABLE_SCHEMA = '".$database."'"; $database = $this->quoteStringLiteral($database);
return "SELECT * FROM information_schema.VIEWS WHERE TABLE_SCHEMA = " . $database;
} }
/** /**
...@@ -176,14 +181,23 @@ class MySqlPlatform extends AbstractPlatform ...@@ -176,14 +181,23 @@ class MySqlPlatform extends AbstractPlatform
*/ */
public function getListTableForeignKeysSQL($table, $database = null) public function getListTableForeignKeysSQL($table, $database = null)
{ {
$table = $this->quoteStringLiteral($table);
if (null !== $database) {
$database = $this->quoteStringLiteral($database);
}
$sql = "SELECT DISTINCT k.`CONSTRAINT_NAME`, k.`COLUMN_NAME`, k.`REFERENCED_TABLE_NAME`, ". $sql = "SELECT DISTINCT k.`CONSTRAINT_NAME`, k.`COLUMN_NAME`, k.`REFERENCED_TABLE_NAME`, ".
"k.`REFERENCED_COLUMN_NAME` /*!50116 , c.update_rule, c.delete_rule */ ". "k.`REFERENCED_COLUMN_NAME` /*!50116 , c.update_rule, c.delete_rule */ ".
"FROM information_schema.key_column_usage k /*!50116 ". "FROM information_schema.key_column_usage k /*!50116 ".
"INNER JOIN information_schema.referential_constraints c ON ". "INNER JOIN information_schema.referential_constraints c ON ".
" c.constraint_name = k.constraint_name AND ". " c.constraint_name = k.constraint_name AND ".
" c.table_name = '$table' */ WHERE k.table_name = '$table'"; " c.table_name = $table */ WHERE k.table_name = $table";
$databaseNameSql = null === $database ? "'$database'" : 'DATABASE()'; // @TODO: This needs fixing. The condition has to be inverted.
// When fixed, AbstractMySQLPlatformTestCase::testQuotesDatabaseNameInListTableForeignKeysSQL test
// has to be completed.
$databaseNameSql = null === $database ? $database : 'DATABASE()';
$sql .= " AND k.table_schema = $databaseNameSql /*!50116 AND c.constraint_schema = $databaseNameSql */"; $sql .= " AND k.table_schema = $databaseNameSql /*!50116 AND c.constraint_schema = $databaseNameSql */";
$sql .= " AND k.`REFERENCED_COLUMN_NAME` is not NULL"; $sql .= " AND k.`REFERENCED_COLUMN_NAME` is not NULL";
...@@ -355,8 +369,10 @@ class MySqlPlatform extends AbstractPlatform ...@@ -355,8 +369,10 @@ class MySqlPlatform extends AbstractPlatform
*/ */
public function getListTableColumnsSQL($table, $database = null) public function getListTableColumnsSQL($table, $database = null)
{ {
$table = $this->quoteStringLiteral($table);
if ($database) { if ($database) {
$database = "'" . $database . "'"; $database = $this->quoteStringLiteral($database);
} else { } else {
$database = 'DATABASE()'; $database = 'DATABASE()';
} }
...@@ -364,7 +380,7 @@ class MySqlPlatform extends AbstractPlatform ...@@ -364,7 +380,7 @@ class MySqlPlatform extends AbstractPlatform
return "SELECT COLUMN_NAME AS Field, COLUMN_TYPE AS Type, IS_NULLABLE AS `Null`, ". 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, " . "COLUMN_KEY AS `Key`, COLUMN_DEFAULT AS `Default`, EXTRA AS Extra, COLUMN_COMMENT AS Comment, " .
"CHARACTER_SET_NAME AS CharacterSet, COLLATION_NAME AS Collation ". "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 = " . $database . " AND TABLE_NAME = " . $table;
} }
/** /**
...@@ -1044,4 +1060,14 @@ class MySqlPlatform extends AbstractPlatform ...@@ -1044,4 +1060,14 @@ class MySqlPlatform extends AbstractPlatform
return 'LONGBLOB'; return 'LONGBLOB';
} }
/**
* {@inheritdoc}
*/
public function quoteStringLiteral($str)
{
$str = str_replace('\\', '\\\\', $str); // MySQL requires backslashes to be escaped aswell.
return parent::quoteStringLiteral($str);
}
} }
...@@ -375,9 +375,10 @@ class OraclePlatform extends AbstractPlatform ...@@ -375,9 +375,10 @@ class OraclePlatform extends AbstractPlatform
public function getListSequencesSQL($database) public function getListSequencesSQL($database)
{ {
$database = $this->normalizeIdentifier($database); $database = $this->normalizeIdentifier($database);
$database = $this->quoteStringLiteral($database->getName());
return "SELECT sequence_name, min_value, increment_by FROM sys.all_sequences ". return "SELECT sequence_name, min_value, increment_by FROM sys.all_sequences ".
"WHERE SEQUENCE_OWNER = '" . $database->getName() . "'"; "WHERE SEQUENCE_OWNER = " . $database;
} }
/** /**
...@@ -418,16 +419,35 @@ class OraclePlatform extends AbstractPlatform ...@@ -418,16 +419,35 @@ class OraclePlatform extends AbstractPlatform
public function getListTableIndexesSQL($table, $currentDatabase = null) public function getListTableIndexesSQL($table, $currentDatabase = null)
{ {
$table = $this->normalizeIdentifier($table); $table = $this->normalizeIdentifier($table);
$table = $this->quoteStringLiteral($table->getName());
return "SELECT uind.index_name AS name, " .
" uind.index_type AS type, " . return "SELECT uind_col.index_name AS name,
" decode( uind.uniqueness, 'NONUNIQUE', 0, 'UNIQUE', 1 ) AS is_unique, " . (
" uind_col.column_name AS column_name, " . SELECT uind.index_type
" uind_col.column_position AS column_pos, " . FROM user_indexes uind
" (SELECT ucon.constraint_type FROM user_constraints ucon WHERE ucon.constraint_name = uind.index_name) AS is_primary ". WHERE uind.index_name = uind_col.index_name
"FROM user_indexes uind, user_ind_columns uind_col " . ) AS type,
"WHERE uind.index_name = uind_col.index_name AND uind_col.table_name = '" . $table->getName() . "' " . decode(
"ORDER BY uind_col.column_position ASC"; (
SELECT uind.uniqueness
FROM user_indexes uind
WHERE uind.index_name = uind_col.index_name
),
'NONUNIQUE',
0,
'UNIQUE',
1
) AS is_unique,
uind_col.column_name AS column_name,
uind_col.column_position AS column_pos,
(
SELECT ucon.constraint_type
FROM user_constraints ucon
WHERE ucon.constraint_name = uind_col.index_name
) AS is_primary
FROM user_ind_columns uind_col
WHERE uind_col.table_name = " . $table . "
ORDER BY uind_col.column_position ASC";
} }
/** /**
...@@ -590,7 +610,8 @@ END;'; ...@@ -590,7 +610,8 @@ END;';
*/ */
public function getListTableForeignKeysSQL($table) public function getListTableForeignKeysSQL($table)
{ {
$table = $table = $this->normalizeIdentifier($table); $table = $this->normalizeIdentifier($table);
$table = $this->quoteStringLiteral($table->getName());
return "SELECT alc.constraint_name, return "SELECT alc.constraint_name,
alc.DELETE_RULE, alc.DELETE_RULE,
...@@ -609,8 +630,8 @@ LEFT JOIN user_cons_columns r_cols ...@@ -609,8 +630,8 @@ LEFT JOIN user_cons_columns r_cols
AND cols.position = r_cols.position AND cols.position = r_cols.position
WHERE alc.constraint_name = cols.constraint_name WHERE alc.constraint_name = cols.constraint_name
AND alc.constraint_type = 'R' AND alc.constraint_type = 'R'
AND alc.table_name = '" . $table->getName() . "' AND alc.table_name = " . $table . "
ORDER BY alc.constraint_name ASC, cols.position ASC"; ORDER BY cols.constraint_name ASC, cols.position ASC";
} }
/** /**
...@@ -619,8 +640,9 @@ LEFT JOIN user_cons_columns r_cols ...@@ -619,8 +640,9 @@ LEFT JOIN user_cons_columns r_cols
public function getListTableConstraintsSQL($table) public function getListTableConstraintsSQL($table)
{ {
$table = $this->normalizeIdentifier($table); $table = $this->normalizeIdentifier($table);
$table = $this->quoteStringLiteral($table->getName());
return "SELECT * FROM user_constraints WHERE table_name = '" . $table->getName() . "'"; return "SELECT * FROM user_constraints WHERE table_name = " . $table;
} }
/** /**
...@@ -629,6 +651,7 @@ LEFT JOIN user_cons_columns r_cols ...@@ -629,6 +651,7 @@ LEFT JOIN user_cons_columns r_cols
public function getListTableColumnsSQL($table, $database = null) public function getListTableColumnsSQL($table, $database = null)
{ {
$table = $this->normalizeIdentifier($table); $table = $this->normalizeIdentifier($table);
$table = $this->quoteStringLiteral($table->getName());
$tabColumnsTableName = "user_tab_columns"; $tabColumnsTableName = "user_tab_columns";
$colCommentsTableName = "user_col_comments"; $colCommentsTableName = "user_col_comments";
...@@ -636,14 +659,22 @@ LEFT JOIN user_cons_columns r_cols ...@@ -636,14 +659,22 @@ LEFT JOIN user_cons_columns r_cols
if (null !== $database) { if (null !== $database) {
$database = $this->normalizeIdentifier($database); $database = $this->normalizeIdentifier($database);
$database = $this->quoteStringLiteral($database->getName());
$tabColumnsTableName = "all_tab_columns"; $tabColumnsTableName = "all_tab_columns";
$colCommentsTableName = "all_col_comments"; $colCommentsTableName = "all_col_comments";
$ownerCondition = "AND c.owner = '" . $database->getName() . "'"; $ownerCondition = "AND c.owner = " . $database;
} }
return "SELECT c.*, d.comments FROM $tabColumnsTableName c ". return "SELECT c.*,
"INNER JOIN " . $colCommentsTableName . " d ON d.TABLE_NAME = c.TABLE_NAME AND d.COLUMN_NAME = c.COLUMN_NAME ". (
"WHERE c.table_name = '" . $table->getName() . "' ".$ownerCondition." ORDER BY c.column_name"; SELECT d.comments
FROM $colCommentsTableName d
WHERE d.TABLE_NAME = c.TABLE_NAME
AND d.COLUMN_NAME = c.COLUMN_NAME
) AS comments
FROM $tabColumnsTableName c
WHERE c.table_name = " . $table . " $ownerCondition
ORDER BY c.column_name";
} }
/** /**
...@@ -1125,4 +1156,14 @@ LEFT JOIN user_cons_columns r_cols ...@@ -1125,4 +1156,14 @@ LEFT JOIN user_cons_columns r_cols
{ {
return 'BLOB'; return 'BLOB';
} }
/**
* {@inheritdoc}
*/
public function quoteStringLiteral($str)
{
$str = str_replace('\\', '\\\\', $str); // Oracle requires backslashes to be escaped aswell.
return parent::quoteStringLiteral($str);
}
} }
...@@ -78,6 +78,8 @@ class PostgreSQL92Platform extends PostgreSQL91Platform ...@@ -78,6 +78,8 @@ class PostgreSQL92Platform extends PostgreSQL91Platform
*/ */
public function getCloseActiveDatabaseConnectionsSQL($database) public function getCloseActiveDatabaseConnectionsSQL($database)
{ {
return "SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = '$database'"; $database = $this->quoteStringLiteral($database);
return "SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = $database";
} }
} }
...@@ -318,7 +318,7 @@ class PostgreSqlPlatform extends AbstractPlatform ...@@ -318,7 +318,7 @@ class PostgreSqlPlatform extends AbstractPlatform
public function getListTableConstraintsSQL($table) public function getListTableConstraintsSQL($table)
{ {
$table = new Identifier($table); $table = new Identifier($table);
$table = $table->getName(); $table = $this->quoteStringLiteral($table->getName());
return "SELECT return "SELECT
quote_ident(relname) as relname quote_ident(relname) as relname
...@@ -327,7 +327,7 @@ class PostgreSqlPlatform extends AbstractPlatform ...@@ -327,7 +327,7 @@ class PostgreSqlPlatform extends AbstractPlatform
WHERE oid IN ( WHERE oid IN (
SELECT indexrelid SELECT indexrelid
FROM pg_index, pg_class FROM pg_index, pg_class
WHERE pg_class.relname = '$table' WHERE pg_class.relname = $table
AND pg_class.oid = pg_index.indrelid AND pg_class.oid = pg_index.indrelid
AND (indisunique = 't' OR indisprimary = 't') AND (indisunique = 't' OR indisprimary = 't')
)"; )";
...@@ -364,13 +364,14 @@ class PostgreSqlPlatform extends AbstractPlatform ...@@ -364,13 +364,14 @@ class PostgreSqlPlatform extends AbstractPlatform
$whereClause = $namespaceAlias.".nspname NOT IN ('pg_catalog', 'information_schema', 'pg_toast') AND "; $whereClause = $namespaceAlias.".nspname NOT IN ('pg_catalog', 'information_schema', 'pg_toast') AND ";
if (strpos($table, ".") !== false) { if (strpos($table, ".") !== false) {
list($schema, $table) = explode(".", $table); list($schema, $table) = explode(".", $table);
$schema = "'" . $schema . "'"; $schema = $this->quoteStringLiteral($schema);
} else { } else {
$schema = "ANY(string_to_array((select replace(replace(setting,'\"\$user\"',user),' ','') from pg_catalog.pg_settings where name = 'search_path'),','))"; $schema = "ANY(string_to_array((select replace(replace(setting,'\"\$user\"',user),' ','') from pg_catalog.pg_settings where name = 'search_path'),','))";
} }
$table = new Identifier($table); $table = new Identifier($table);
$whereClause .= "$classAlias.relname = '" . $table->getName() . "' AND $namespaceAlias.nspname = $schema"; $table = $this->quoteStringLiteral($table->getName());
$whereClause .= "$classAlias.relname = " . $table . " AND $namespaceAlias.nspname = $schema";
return $whereClause; return $whereClause;
} }
...@@ -445,7 +446,9 @@ class PostgreSqlPlatform extends AbstractPlatform ...@@ -445,7 +446,9 @@ class PostgreSqlPlatform extends AbstractPlatform
*/ */
public function getCloseActiveDatabaseConnectionsSQL($database) public function getCloseActiveDatabaseConnectionsSQL($database)
{ {
return "SELECT pg_terminate_backend(procpid) FROM pg_stat_activity WHERE datname = '$database'"; $database = $this->quoteStringLiteral($database);
return "SELECT pg_terminate_backend(procpid) FROM pg_stat_activity WHERE datname = $database";
} }
/** /**
...@@ -1172,4 +1175,14 @@ class PostgreSqlPlatform extends AbstractPlatform ...@@ -1172,4 +1175,14 @@ class PostgreSqlPlatform extends AbstractPlatform
{ {
return 'BYTEA'; return 'BYTEA';
} }
/**
* {@inheritdoc}
*/
public function quoteStringLiteral($str)
{
$str = str_replace('\\', '\\\\', $str); // PostgreSQL requires backslashes to be escaped aswell.
return parent::quoteStringLiteral($str);
}
} }
...@@ -724,7 +724,7 @@ class SQLAnywherePlatform extends AbstractPlatform ...@@ -724,7 +724,7 @@ class SQLAnywherePlatform extends AbstractPlatform
if (strpos($table, '.') !== false) { if (strpos($table, '.') !== false) {
list($user, $table) = explode('.', $table); list($user, $table) = explode('.', $table);
$user = "'" . $user . "'"; $user = $this->quoteStringLiteral($user);
} }
return "SELECT col.column_name, return "SELECT col.column_name,
...@@ -756,13 +756,16 @@ class SQLAnywherePlatform extends AbstractPlatform ...@@ -756,13 +756,16 @@ class SQLAnywherePlatform extends AbstractPlatform
if (strpos($table, '.') !== false) { if (strpos($table, '.') !== false) {
list($user, $table) = explode('.', $table); list($user, $table) = explode('.', $table);
$user = "'" . $user . "'"; $user = $this->quoteStringLiteral($user);
$table = $this->quoteStringLiteral($table);
} else {
$table = $this->quoteStringLiteral($table);
} }
return "SELECT con.* return "SELECT con.*
FROM SYS.SYSCONSTRAINT AS con FROM SYS.SYSCONSTRAINT AS con
JOIN SYS.SYSTAB AS tab ON con.table_object_id = tab.object_id JOIN SYS.SYSTAB AS tab ON con.table_object_id = tab.object_id
WHERE tab.table_name = '$table' WHERE tab.table_name = $table
AND tab.creator = USER_ID($user)"; AND tab.creator = USER_ID($user)";
} }
...@@ -775,7 +778,10 @@ class SQLAnywherePlatform extends AbstractPlatform ...@@ -775,7 +778,10 @@ class SQLAnywherePlatform extends AbstractPlatform
if (strpos($table, '.') !== false) { if (strpos($table, '.') !== false) {
list($user, $table) = explode('.', $table); list($user, $table) = explode('.', $table);
$user = "'" . $user . "'"; $user = $this->quoteStringLiteral($user);
$table = $this->quoteStringLiteral($table);
} else {
$table = $this->quoteStringLiteral($table);
} }
return "SELECT fcol.column_name AS local_column, return "SELECT fcol.column_name AS local_column,
...@@ -844,7 +850,7 @@ class SQLAnywherePlatform extends AbstractPlatform ...@@ -844,7 +850,7 @@ class SQLAnywherePlatform extends AbstractPlatform
ON fk.foreign_table_id = dt.foreign_table_id ON fk.foreign_table_id = dt.foreign_table_id
AND fk.foreign_index_id = dt.foreign_key_id AND fk.foreign_index_id = dt.foreign_key_id
AND dt.event = 'D' AND dt.event = 'D'
WHERE ftbl.table_name = '$table' WHERE ftbl.table_name = $table
AND ftbl.creator = USER_ID($user) AND ftbl.creator = USER_ID($user)
ORDER BY fk.foreign_index_id ASC, idxcol.sequence ASC"; ORDER BY fk.foreign_index_id ASC, idxcol.sequence ASC";
} }
...@@ -858,7 +864,10 @@ class SQLAnywherePlatform extends AbstractPlatform ...@@ -858,7 +864,10 @@ class SQLAnywherePlatform extends AbstractPlatform
if (strpos($table, '.') !== false) { if (strpos($table, '.') !== false) {
list($user, $table) = explode('.', $table); list($user, $table) = explode('.', $table);
$user = "'" . $user . "'"; $user = $this->quoteStringLiteral($user);
$table = $this->quoteStringLiteral($table);
} else {
$table = $this->quoteStringLiteral($table);
} }
return "SELECT idx.index_name AS key_name, return "SELECT idx.index_name AS key_name,
...@@ -893,7 +902,7 @@ class SQLAnywherePlatform extends AbstractPlatform ...@@ -893,7 +902,7 @@ class SQLAnywherePlatform extends AbstractPlatform
ON idxcol.table_id = col.table_id AND idxcol.column_id = col.column_id ON idxcol.table_id = col.table_id AND idxcol.column_id = col.column_id
JOIN SYS.SYSTAB AS tbl JOIN SYS.SYSTAB AS tbl
ON idx.table_id = tbl.table_id ON idx.table_id = tbl.table_id
WHERE tbl.table_name = '$table' WHERE tbl.table_name = $table
AND tbl.creator = USER_ID($user) AND tbl.creator = USER_ID($user)
AND idx.index_category != 2 -- exclude indexes implicitly created by foreign key constraints AND idx.index_category != 2 -- exclude indexes implicitly created by foreign key constraints
ORDER BY idx.index_id ASC, idxcol.sequence ASC"; ORDER BY idx.index_id ASC, idxcol.sequence ASC";
......
...@@ -928,12 +928,14 @@ class SQLServerPlatform extends AbstractPlatform ...@@ -928,12 +928,14 @@ class SQLServerPlatform extends AbstractPlatform
{ {
if (strpos($table, ".") !== false) { if (strpos($table, ".") !== false) {
list($schema, $table) = explode(".", $table); list($schema, $table) = explode(".", $table);
$schema = "'" . $schema . "'"; $schema = $this->quoteStringLiteral($schema);
$table = $this->quoteStringLiteral($table);
} else { } else {
$schema = "SCHEMA_NAME()"; $schema = "SCHEMA_NAME()";
$table = $this->quoteStringLiteral($table);
} }
return "({$tableColumn} = '{$table}' AND {$schemaColumn} = {$schema})"; return "({$tableColumn} = {$table} AND {$schemaColumn} = {$schema})";
} }
/** /**
......
...@@ -405,8 +405,9 @@ class SqlitePlatform extends AbstractPlatform ...@@ -405,8 +405,9 @@ class SqlitePlatform extends AbstractPlatform
public function getListTableConstraintsSQL($table) public function getListTableConstraintsSQL($table)
{ {
$table = str_replace('.', '__', $table); $table = str_replace('.', '__', $table);
$table = $this->quoteStringLiteral($table);
return "SELECT sql FROM sqlite_master WHERE type='index' AND tbl_name = '$table' AND sql NOT NULL ORDER BY name"; return "SELECT sql FROM sqlite_master WHERE type='index' AND tbl_name = $table AND sql NOT NULL ORDER BY name";
} }
/** /**
...@@ -415,8 +416,9 @@ class SqlitePlatform extends AbstractPlatform ...@@ -415,8 +416,9 @@ class SqlitePlatform extends AbstractPlatform
public function getListTableColumnsSQL($table, $currentDatabase = null) public function getListTableColumnsSQL($table, $currentDatabase = null)
{ {
$table = str_replace('.', '__', $table); $table = str_replace('.', '__', $table);
$table = $this->quoteStringLiteral($table);
return "PRAGMA table_info('$table')"; return "PRAGMA table_info($table)";
} }
/** /**
...@@ -425,8 +427,9 @@ class SqlitePlatform extends AbstractPlatform ...@@ -425,8 +427,9 @@ class SqlitePlatform extends AbstractPlatform
public function getListTableIndexesSQL($table, $currentDatabase = null) public function getListTableIndexesSQL($table, $currentDatabase = null)
{ {
$table = str_replace('.', '__', $table); $table = str_replace('.', '__', $table);
$table = $this->quoteStringLiteral($table);
return "PRAGMA index_list('$table')"; return "PRAGMA index_list($table)";
} }
/** /**
...@@ -758,8 +761,9 @@ class SqlitePlatform extends AbstractPlatform ...@@ -758,8 +761,9 @@ class SqlitePlatform extends AbstractPlatform
public function getListTableForeignKeysSQL($table, $database = null) public function getListTableForeignKeysSQL($table, $database = null)
{ {
$table = str_replace('.', '__', $table); $table = str_replace('.', '__', $table);
$table = $this->quoteStringLiteral($table);
return "PRAGMA foreign_key_list('$table')"; return "PRAGMA foreign_key_list($table)";
} }
/** /**
......
...@@ -731,4 +731,92 @@ abstract class AbstractMySQLPlatformTestCase extends AbstractPlatformTestCase ...@@ -731,4 +731,92 @@ abstract class AbstractMySQLPlatformTestCase extends AbstractPlatformTestCase
'ALTER TABLE mytable ADD CONSTRAINT fk_foo FOREIGN KEY (foo) REFERENCES foreign_table (id)', 'ALTER TABLE mytable ADD CONSTRAINT fk_foo FOREIGN KEY (foo) REFERENCES foreign_table (id)',
); );
} }
/**
* {@inheritdoc}
*/
public function getGeneratesDecimalTypeDeclarationSQL()
{
return array(
array(array(), 'NUMERIC(10, 0)'),
array(array('unsigned' => true), 'NUMERIC(10, 0) UNSIGNED'),
array(array('unsigned' => false), 'NUMERIC(10, 0)'),
array(array('precision' => 5), 'NUMERIC(5, 0)'),
array(array('scale' => 5), 'NUMERIC(10, 5)'),
array(array('precision' => 8, 'scale' => 2), 'NUMERIC(8, 2)'),
);
}
/**
* {@inheritdoc}
*/
public function getGeneratesFloatDeclarationSQL()
{
return array(
array(array(), 'DOUBLE PRECISION'),
array(array('unsigned' => true), 'DOUBLE PRECISION UNSIGNED'),
array(array('unsigned' => false), 'DOUBLE PRECISION'),
array(array('precision' => 5), 'DOUBLE PRECISION'),
array(array('scale' => 5), 'DOUBLE PRECISION'),
array(array('precision' => 8, 'scale' => 2), 'DOUBLE PRECISION'),
);
}
/**
* @group DBAL-2436
*/
public function testQuotesTableNameInListTableIndexesSQL()
{
$this->assertContains("'Foo''Bar\\\\'", $this->_platform->getListTableIndexesSQL("Foo'Bar\\", 'foo_db'), '', true);
}
/**
* @group DBAL-2436
*/
public function testQuotesDatabaseNameInListTableIndexesSQL()
{
$this->assertContains("'Foo''Bar\\\\'", $this->_platform->getListTableIndexesSQL('foo_table', "Foo'Bar\\"), '', true);
}
/**
* @group DBAL-2436
*/
public function testQuotesDatabaseNameInListViewsSQL()
{
$this->assertContains("'Foo''Bar\\\\'", $this->_platform->getListViewsSQL("Foo'Bar\\"), '', true);
}
/**
* @group DBAL-2436
*/
public function testQuotesTableNameInListTableForeignKeysSQL()
{
$this->assertContains("'Foo''Bar\\\\'", $this->_platform->getListTableForeignKeysSQL("Foo'Bar\\"), '', true);
}
/**
* @group DBAL-2436
*/
public function testQuotesDatabaseNameInListTableForeignKeysSQL()
{
$this->markTestIncomplete('Test does not work due to a bug in MySqlplatform::getListTableForeignKeysSQL');
$this->assertContains("'Foo''Bar\\\\'", $this->_platform->getListTableForeignKeysSQL('foo_table', "Foo'Bar\\"), '', true);
}
/**
* @group DBAL-2436
*/
public function testQuotesTableNameInListTableColumnsSQL()
{
$this->assertContains("'Foo''Bar\\\\'", $this->_platform->getListTableColumnsSQL("Foo'Bar\\"), '', true);
}
/**
* @group DBAL-2436
*/
public function testQuotesDatabaseNameInListTableColumnsSQL()
{
$this->assertContains("'Foo''Bar\\\\'", $this->_platform->getListTableColumnsSQL('foo_table', "Foo'Bar\\"), '', true);
}
} }
...@@ -818,4 +818,88 @@ abstract class AbstractPostgreSqlPlatformTestCase extends AbstractPlatformTestCa ...@@ -818,4 +818,88 @@ abstract class AbstractPostgreSqlPlatformTestCase extends AbstractPlatformTestCa
$this->_platform->getCloseActiveDatabaseConnectionsSQL('foo') $this->_platform->getCloseActiveDatabaseConnectionsSQL('foo')
); );
} }
/**
* @group DBAL-2436
*/
public function testQuotesTableNameInListTableForeignKeysSQL()
{
$this->assertContains("'Foo''Bar\\\\'", $this->_platform->getListTableForeignKeysSQL("Foo'Bar\\"), '', true);
}
/**
* @group DBAL-2436
*/
public function testQuotesSchemaNameInListTableForeignKeysSQL()
{
$this->assertContains(
"'Foo''Bar\\\\'",
$this->_platform->getListTableForeignKeysSQL("Foo'Bar\\.baz_table"),
'',
true
);
}
/**
* @group DBAL-2436
*/
public function testQuotesTableNameInListTableConstraintsSQL()
{
$this->assertContains("'Foo''Bar\\\\'", $this->_platform->getListTableConstraintsSQL("Foo'Bar\\"), '', true);
}
/**
* @group DBAL-2436
*/
public function testQuotesTableNameInListTableIndexesSQL()
{
$this->assertContains("'Foo''Bar\\\\'", $this->_platform->getListTableIndexesSQL("Foo'Bar\\"), '', true);
}
/**
* @group DBAL-2436
*/
public function testQuotesSchemaNameInListTableIndexesSQL()
{
$this->assertContains(
"'Foo''Bar\\\\'",
$this->_platform->getListTableIndexesSQL("Foo'Bar\\.baz_table"),
'',
true
);
}
/**
* @group DBAL-2436
*/
public function testQuotesTableNameInListTableColumnsSQL()
{
$this->assertContains("'Foo''Bar\\\\'", $this->_platform->getListTableColumnsSQL("Foo'Bar\\"), '', true);
}
/**
* @group DBAL-2436
*/
public function testQuotesSchemaNameInListTableColumnsSQL()
{
$this->assertContains(
"'Foo''Bar\\\\'",
$this->_platform->getListTableColumnsSQL("Foo'Bar\\.baz_table"),
'',
true
);
}
/**
* @group DBAL-2436
*/
public function testQuotesDatabaseNameInCloseActiveDatabaseConnectionsSQL()
{
$this->assertContains(
"'Foo''Bar\\\\'",
$this->_platform->getCloseActiveDatabaseConnectionsSQL("Foo'Bar\\"),
'',
true
);
}
} }
...@@ -1351,4 +1351,67 @@ abstract class AbstractSQLServerPlatformTestCase extends AbstractPlatformTestCas ...@@ -1351,4 +1351,67 @@ abstract class AbstractSQLServerPlatformTestCase extends AbstractPlatformTestCas
$sql = $this->_platform->modifyLimitQuery($querySql, 10); $sql = $this->_platform->modifyLimitQuery($querySql, 10);
$this->assertEquals(sprintf(static::$selectFromCtePattern, $alteredSql, 1, 10), $sql); $this->assertEquals(sprintf(static::$selectFromCtePattern, $alteredSql, 1, 10), $sql);
} }
/**
* @group DBAL-2436
*/
public function testQuotesTableNameInListTableColumnsSQL()
{
$this->assertContains("'Foo''Bar\\'", $this->_platform->getListTableColumnsSQL("Foo'Bar\\"), '', true);
}
/**
* @group DBAL-2436
*/
public function testQuotesSchemaNameInListTableColumnsSQL()
{
$this->assertContains(
"'Foo''Bar\\'",
$this->_platform->getListTableColumnsSQL("Foo'Bar\\.baz_table"),
'',
true
);
}
/**
* @group DBAL-2436
*/
public function testQuotesTableNameInListTableForeignKeysSQL()
{
$this->assertContains("'Foo''Bar\\'", $this->_platform->getListTableForeignKeysSQL("Foo'Bar\\"), '', true);
}
/**
* @group DBAL-2436
*/
public function testQuotesSchemaNameInListTableForeignKeysSQL()
{
$this->assertContains(
"'Foo''Bar\\'",
$this->_platform->getListTableForeignKeysSQL("Foo'Bar\\.baz_table"),
'',
true
);
}
/**
* @group DBAL-2436
*/
public function testQuotesTableNameInListTableIndexesSQL()
{
$this->assertContains("'Foo''Bar\\'", $this->_platform->getListTableIndexesSQL("Foo'Bar\\"), '', true);
}
/**
* @group DBAL-2436
*/
public function testQuotesSchemaNameInListTableIndexesSQL()
{
$this->assertContains(
"'Foo''Bar\\'",
$this->_platform->getListTableIndexesSQL("Foo'Bar\\.baz_table"),
'',
true
);
}
} }
...@@ -676,4 +676,28 @@ class DB2PlatformTest extends AbstractPlatformTestCase ...@@ -676,4 +676,28 @@ class DB2PlatformTest extends AbstractPlatformTestCase
'RENAME INDEX idx_foo TO idx_foo_renamed', 'RENAME INDEX idx_foo TO idx_foo_renamed',
); );
} }
/**
* @group DBAL-2436
*/
public function testQuotesTableNameInListTableColumnsSQL()
{
$this->assertContains("'Foo''Bar\\'", $this->_platform->getListTableColumnsSQL("Foo'Bar\\"), '', true);
}
/**
* @group DBAL-2436
*/
public function testQuotesTableNameInListTableIndexesSQL()
{
$this->assertContains("'Foo''Bar\\'", $this->_platform->getListTableIndexesSQL("Foo'Bar\\"), '', true);
}
/**
* @group DBAL-2436
*/
public function testQuotesTableNameInListTableForeignKeysSQL()
{
$this->assertContains("'Foo''Bar\\'", $this->_platform->getListTableForeignKeysSQL("Foo'Bar\\"), '', true);
}
} }
...@@ -741,4 +741,52 @@ EOD; ...@@ -741,4 +741,52 @@ EOD;
'ALTER INDEX idx_foo RENAME TO idx_foo_renamed', 'ALTER INDEX idx_foo RENAME TO idx_foo_renamed',
); );
} }
/**
* @group DBAL-2436
*/
public function testQuotesDatabaseNameInListSequencesSQL()
{
$this->assertContains("'Foo''Bar\\\\'", $this->_platform->getListSequencesSQL("Foo'Bar\\"), '', true);
}
/**
* @group DBAL-2436
*/
public function testQuotesTableNameInListTableIndexesSQL()
{
$this->assertContains("'Foo''Bar\\\\'", $this->_platform->getListTableIndexesSQL("Foo'Bar\\"), '', true);
}
/**
* @group DBAL-2436
*/
public function testQuotesTableNameInListTableForeignKeysSQL()
{
$this->assertContains("'Foo''Bar\\\\'", $this->_platform->getListTableForeignKeysSQL("Foo'Bar\\"), '', true);
}
/**
* @group DBAL-2436
*/
public function testQuotesTableNameInListTableConstraintsSQL()
{
$this->assertContains("'Foo''Bar\\\\'", $this->_platform->getListTableConstraintsSQL("Foo'Bar\\"), '', true);
}
/**
* @group DBAL-2436
*/
public function testQuotesTableNameInListTableColumnsSQL()
{
$this->assertContains("'Foo''Bar\\\\'", $this->_platform->getListTableColumnsSQL("Foo'Bar\\"), '', true);
}
/**
* @group DBAL-2436
*/
public function testQuotesDatabaseNameInListTableColumnsSQL()
{
$this->assertContains("'Foo''Bar\\\\'", $this->_platform->getListTableColumnsSQL('foo_table', "Foo'Bar\\"), '', true);
}
} }
...@@ -995,4 +995,80 @@ class SQLAnywherePlatformTest extends AbstractPlatformTestCase ...@@ -995,4 +995,80 @@ class SQLAnywherePlatformTest extends AbstractPlatformTestCase
'ALTER INDEX idx_foo ON mytable RENAME TO idx_foo_renamed', 'ALTER INDEX idx_foo ON mytable RENAME TO idx_foo_renamed',
); );
} }
/**
* @group DBAL-2436
*/
public function testQuotesSchemaNameInListTableColumnsSQL()
{
$this->assertContains(
"'Foo''Bar\\'",
$this->_platform->getListTableColumnsSQL("Foo'Bar\\.baz_table"),
'',
true
);
}
/**
* @group DBAL-2436
*/
public function testQuotesTableNameInListTableConstraintsSQL()
{
$this->assertContains("'Foo''Bar\\'", $this->_platform->getListTableConstraintsSQL("Foo'Bar\\"), '', true);
}
/**
* @group DBAL-2436
*/
public function testQuotesSchemaNameInListTableConstraintsSQL()
{
$this->assertContains(
"'Foo''Bar\\'",
$this->_platform->getListTableConstraintsSQL("Foo'Bar\\.baz_table"),
'',
true
);
}
/**
* @group DBAL-2436
*/
public function testQuotesTableNameInListTableForeignKeysSQL()
{
$this->assertContains("'Foo''Bar\\'", $this->_platform->getListTableForeignKeysSQL("Foo'Bar\\"), '', true);
}
/**
* @group DBAL-2436
*/
public function testQuotesSchemaNameInListTableForeignKeysSQL()
{
$this->assertContains(
"'Foo''Bar\\'",
$this->_platform->getListTableForeignKeysSQL("Foo'Bar\\.baz_table"),
'',
true
);
}
/**
* @group DBAL-2436
*/
public function testQuotesTableNameInListTableIndexesSQL()
{
$this->assertContains("'Foo''Bar\\'", $this->_platform->getListTableIndexesSQL("Foo'Bar\\"), '', true);
}
/**
* @group DBAL-2436
*/
public function testQuotesSchemaNameInListTableIndexesSQL()
{
$this->assertContains(
"'Foo''Bar\\'",
$this->_platform->getListTableIndexesSQL("Foo'Bar\\.baz_table"),
'',
true
);
}
} }
...@@ -678,4 +678,36 @@ class SqlitePlatformTest extends AbstractPlatformTestCase ...@@ -678,4 +678,36 @@ class SqlitePlatformTest extends AbstractPlatformTestCase
'CREATE INDEX idx_foo_renamed ON mytable (foo)', 'CREATE INDEX idx_foo_renamed ON mytable (foo)',
); );
} }
/**
* @group DBAL-2436
*/
public function testQuotesTableNameInListTableConstraintsSQL()
{
$this->assertContains("'Foo''Bar\\'", $this->_platform->getListTableConstraintsSQL("Foo'Bar\\"), '', true);
}
/**
* @group DBAL-2436
*/
public function testQuotesTableNameInListTableColumnsSQL()
{
$this->assertContains("'Foo''Bar\\'", $this->_platform->getListTableColumnsSQL("Foo'Bar\\"), '', true);
}
/**
* @group DBAL-2436
*/
public function testQuotesTableNameInListTableIndexesSQL()
{
$this->assertContains("'Foo''Bar\\'", $this->_platform->getListTableIndexesSQL("Foo'Bar\\"), '', true);
}
/**
* @group DBAL-2436
*/
public function testQuotesTableNameInListTableForeignKeysSQL()
{
$this->assertContains("'Foo''Bar\\'", $this->_platform->getListTableForeignKeysSQL("Foo'Bar\\"), '', true);
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment