Commit 55d94637 authored by Dries's avatar Dries
Browse files

- Patch #370240 by Josh Waihi, stuzza, mfb, Arancaytar: PDO should quote index...

- Patch #370240 by Josh Waihi, stuzza, mfb, Arancaytar: PDO should quote index names in table declarations.
parent 9b4013fd
...@@ -226,12 +226,12 @@ protected function createKeysSql($spec) { ...@@ -226,12 +226,12 @@ protected function createKeysSql($spec) {
} }
if (!empty($spec['unique keys'])) { if (!empty($spec['unique keys'])) {
foreach ($spec['unique keys'] as $key => $fields) { foreach ($spec['unique keys'] as $key => $fields) {
$keys[] = 'UNIQUE KEY ' . $key . ' (' . $this->createKeysSqlHelper($fields) . ')'; $keys[] = 'UNIQUE KEY `' . $key . '` (' . $this->createKeysSqlHelper($fields) . ')';
} }
} }
if (!empty($spec['indexes'])) { if (!empty($spec['indexes'])) {
foreach ($spec['indexes'] as $index => $fields) { foreach ($spec['indexes'] as $index => $fields) {
$keys[] = 'INDEX ' . $index . ' (' . $this->createKeysSqlHelper($fields) . ')'; $keys[] = 'INDEX `' . $index . '` (' . $this->createKeysSqlHelper($fields) . ')';
} }
} }
...@@ -242,10 +242,10 @@ protected function createKeySql($fields) { ...@@ -242,10 +242,10 @@ protected function createKeySql($fields) {
$ret = array(); $ret = array();
foreach ($fields as $field) { foreach ($fields as $field) {
if (is_array($field)) { if (is_array($field)) {
$ret[] = $field[0] . '(' . $field[1] . ')'; $ret[] = '`' . $field[0] . '`(' . $field[1] . ')';
} }
else { else {
$ret[] = $field; $ret[] = '`' . $field . '`';
} }
} }
return implode(', ', $ret); return implode(', ', $ret);
...@@ -255,10 +255,10 @@ protected function createKeysSqlHelper($fields) { ...@@ -255,10 +255,10 @@ protected function createKeysSqlHelper($fields) {
$ret = array(); $ret = array();
foreach ($fields as $field) { foreach ($fields as $field) {
if (is_array($field)) { if (is_array($field)) {
$ret[] = $field[0] . '(' . $field[1] . ')'; $ret[] = '`' . $field[0] . '`(' . $field[1] . ')';
} }
else { else {
$ret[] = $field; $ret[] = '`' . $field . '`';
} }
} }
return implode(', ', $ret); return implode(', ', $ret);
...@@ -297,7 +297,7 @@ public function addField(&$ret, $table, $field, $spec, $keys_new = array()) { ...@@ -297,7 +297,7 @@ public function addField(&$ret, $table, $field, $spec, $keys_new = array()) {
} }
public function dropField(&$ret, $table, $field) { public function dropField(&$ret, $table, $field) {
$ret[] = update_sql('ALTER TABLE {' . $table . '} DROP ' . $field); $ret[] = update_sql('ALTER TABLE {' . $table . '} DROP `' . $field . '`');
} }
public function fieldSetDefault(&$ret, $table, $field, $default) { public function fieldSetDefault(&$ret, $table, $field, $default) {
...@@ -308,11 +308,11 @@ public function fieldSetDefault(&$ret, $table, $field, $default) { ...@@ -308,11 +308,11 @@ public function fieldSetDefault(&$ret, $table, $field, $default) {
$default = is_string($default) ? "'$default'" : $default; $default = is_string($default) ? "'$default'" : $default;
} }
$ret[] = update_sql('ALTER TABLE {' . $table . '} ALTER COLUMN ' . $field . ' SET DEFAULT ' . $default); $ret[] = update_sql('ALTER TABLE {' . $table . '} ALTER COLUMN `' . $field . '` SET DEFAULT ' . $default);
} }
public function fieldSetNoDefault(&$ret, $table, $field) { public function fieldSetNoDefault(&$ret, $table, $field) {
$ret[] = update_sql('ALTER TABLE {' . $table . '} ALTER COLUMN ' . $field . ' DROP DEFAULT'); $ret[] = update_sql('ALTER TABLE {' . $table . '} ALTER COLUMN `' . $field . '` DROP DEFAULT');
} }
public function addPrimaryKey(&$ret, $table, $fields) { public function addPrimaryKey(&$ret, $table, $fields) {
...@@ -324,20 +324,20 @@ public function dropPrimaryKey(&$ret, $table) { ...@@ -324,20 +324,20 @@ public function dropPrimaryKey(&$ret, $table) {
} }
public function addUniqueKey(&$ret, $table, $name, $fields) { public function addUniqueKey(&$ret, $table, $name, $fields) {
$ret[] = update_sql('ALTER TABLE {' . $table . '} ADD UNIQUE KEY ' . $name . ' (' . $this->createKeySql($fields) . ')'); $ret[] = update_sql('ALTER TABLE {' . $table . '} ADD UNIQUE KEY `' . $name . '` (' . $this->createKeySql($fields) . ')');
} }
public function dropUniqueKey(&$ret, $table, $name) { public function dropUniqueKey(&$ret, $table, $name) {
$ret[] = update_sql('ALTER TABLE {' . $table . '} DROP KEY ' . $name); $ret[] = update_sql('ALTER TABLE {' . $table . '} DROP KEY `' . $name . '`');
} }
public function addIndex(&$ret, $table, $name, $fields) { public function addIndex(&$ret, $table, $name, $fields) {
$query = 'ALTER TABLE {' . $table . '} ADD INDEX ' . $name . ' (' . $this->createKeySql($fields) . ')'; $query = 'ALTER TABLE {' . $table . '} ADD INDEX `' . $name . '` (' . $this->createKeySql($fields) . ')';
$ret[] = update_sql($query); $ret[] = update_sql($query);
} }
public function dropIndex(&$ret, $table, $name) { public function dropIndex(&$ret, $table, $name) {
$ret[] = update_sql('ALTER TABLE {' . $table . '} DROP INDEX ' . $name); $ret[] = update_sql('ALTER TABLE {' . $table . '} DROP INDEX `' . $name . '`');
} }
public function changeField(&$ret, $table, $field, $field_new, $spec, $keys_new = array()) { public function changeField(&$ret, $table, $field, $field_new, $spec, $keys_new = array()) {
......
...@@ -260,7 +260,7 @@ protected function _createKeySql($fields) { ...@@ -260,7 +260,7 @@ protected function _createKeySql($fields) {
$ret[] = 'substr(' . $field[0] . ', 1, ' . $field[1] . ')'; $ret[] = 'substr(' . $field[0] . ', 1, ' . $field[1] . ')';
} }
else { else {
$ret[] = $field; $ret[] = '"' . $field . '"';
} }
} }
return implode(', ', $ret); return implode(', ', $ret);
...@@ -355,7 +355,7 @@ public function addField(&$ret, $table, $field, $spec, $new_keys = array()) { ...@@ -355,7 +355,7 @@ public function addField(&$ret, $table, $field, $spec, $new_keys = array()) {
* The field to be dropped. * The field to be dropped.
*/ */
public function dropField(&$ret, $table, $field) { public function dropField(&$ret, $table, $field) {
$ret[] = update_sql('ALTER TABLE {' . $table . '} DROP COLUMN ' . $field); $ret[] = update_sql('ALTER TABLE {' . $table . '} DROP COLUMN "' . $field . '"');
} }
/** /**
...@@ -378,7 +378,7 @@ public function fieldSetDefault(&$ret, $table, $field, $default) { ...@@ -378,7 +378,7 @@ public function fieldSetDefault(&$ret, $table, $field, $default) {
$default = is_string($default) ? "'$default'" : $default; $default = is_string($default) ? "'$default'" : $default;
} }
$ret[] = update_sql('ALTER TABLE {' . $table . '} ALTER COLUMN ' . $field . ' SET DEFAULT ' . $default); $ret[] = update_sql('ALTER TABLE {' . $table . '} ALTER COLUMN "' . $field . '" SET DEFAULT ' . $default);
} }
/** /**
...@@ -392,7 +392,7 @@ public function fieldSetDefault(&$ret, $table, $field, $default) { ...@@ -392,7 +392,7 @@ public function fieldSetDefault(&$ret, $table, $field, $default) {
* The field to be altered. * The field to be altered.
*/ */
public function fieldSetNoDefault(&$ret, $table, $field) { public function fieldSetNoDefault(&$ret, $table, $field) {
$ret[] = update_sql('ALTER TABLE {' . $table . '} ALTER COLUMN ' . $field . ' DROP DEFAULT'); $ret[] = update_sql('ALTER TABLE {' . $table . '} ALTER COLUMN "' . $field . '" DROP DEFAULT');
} }
/** /**
...@@ -435,7 +435,7 @@ public function dropPrimaryKey(&$ret, $table) { ...@@ -435,7 +435,7 @@ public function dropPrimaryKey(&$ret, $table) {
*/ */
function addUniqueKey(&$ret, $table, $name, $fields) { function addUniqueKey(&$ret, $table, $name, $fields) {
$name = '{' . $table . '}_' . $name . '_key'; $name = '{' . $table . '}_' . $name . '_key';
$ret[] = update_sql('ALTER TABLE {' . $table . '} ADD CONSTRAINT ' . $name . ' UNIQUE (' . implode(',', $fields) . ')'); $ret[] = update_sql('ALTER TABLE {' . $table . '} ADD CONSTRAINT "' . $name . '" UNIQUE (' . implode(',', $fields) . ')');
} }
/** /**
...@@ -450,7 +450,7 @@ function addUniqueKey(&$ret, $table, $name, $fields) { ...@@ -450,7 +450,7 @@ function addUniqueKey(&$ret, $table, $name, $fields) {
*/ */
public function dropUniqueKey(&$ret, $table, $name) { public function dropUniqueKey(&$ret, $table, $name) {
$name = '{' . $table . '}_' . $name . '_key'; $name = '{' . $table . '}_' . $name . '_key';
$ret[] = update_sql('ALTER TABLE {' . $table . '} DROP CONSTRAINT ' . $name); $ret[] = update_sql('ALTER TABLE {' . $table . '} DROP CONSTRAINT "' . $name . '"');
} }
/** /**
...@@ -578,7 +578,7 @@ public function changeField(&$ret, $table, $field, $field_new, $spec, $new_keys ...@@ -578,7 +578,7 @@ public function changeField(&$ret, $table, $field, $field_new, $spec, $new_keys
} }
protected function _createIndexSql($table, $name, $fields) { protected function _createIndexSql($table, $name, $fields) {
$query = 'CREATE INDEX {' . $table . '}_' . $name . '_idx ON {' . $table . '} ('; $query = 'CREATE INDEX "{' . $table . '}_' . $name . '_idx" ON {' . $table . '} (';
$query .= $this->_createKeySql($fields) . ')'; $query .= $this->_createKeySql($fields) . ')';
return $query; return $query;
} }
......
...@@ -47,12 +47,12 @@ protected function createIndexSql($tablename, $schema) { ...@@ -47,12 +47,12 @@ protected function createIndexSql($tablename, $schema) {
$sql = array(); $sql = array();
if (!empty($schema['unique keys'])) { if (!empty($schema['unique keys'])) {
foreach ($schema['unique keys'] as $key => $fields) { foreach ($schema['unique keys'] as $key => $fields) {
$sql[] = 'CREATE UNIQUE INDEX {' . $tablename . '}_' . $key . ' ON {' . $tablename . '} (' . $this->createKeySql($fields) . "); \n"; $sql[] = 'CREATE UNIQUE INDEX "{' . $tablename . '}_' . $key . '" ON {' . $tablename . '} (' . $this->createKeySql($fields) . "); \n";
} }
} }
if (!empty($schema['indexes'])) { if (!empty($schema['indexes'])) {
foreach ($schema['indexes'] as $index => $fields) { foreach ($schema['indexes'] as $index => $fields) {
$sql[] = 'CREATE INDEX {' . $tablename . '}_' . $index . ' ON {' . $tablename . '} (' . $this->createKeySql($fields) . "); \n"; $sql[] = 'CREATE INDEX "{' . $tablename . '}_' . $index . '" ON {' . $tablename . '} (' . $this->createKeySql($fields) . "); \n";
} }
} }
return $sql; return $sql;
......
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