Issue #3130579 by daffie, markdorison, pradhumanjainOSL, dragan_bp, jcisio,...
Issue #3130579 by daffie, markdorison, pradhumanjainOSL, dragan_bp, jcisio, mondrake, artem_sylchuk, Sutharsan, alexpott, jsst, catch: Make Drupal\Core\Database\Schema work with reserved keywords for naming
(cherry picked from commit 9363b0e7)
$this->connection->query('ALTER TABLE {'.$table.'} ALTER "'.$field.'" TYPE '.$field_def.' USING convert_from("'.$field.'"'.", 'UTF8')");
$this->connection->query('ALTER TABLE {'.$table.'} ALTER ['.$field.'] TYPE '.$field_def.' USING convert_from(['.$field.']'.", 'UTF8')");
}
else{
$this->connection->query('ALTER TABLE {'.$table.'} ALTER "'.$field.'" TYPE '.$field_def.' USING "'.$field.'"::'.$field_def);
$this->connection->query('ALTER TABLE {'.$table.'} ALTER ['.$field.'] TYPE '.$field_def.' USING ['.$field.']::'.$field_def);
}
}
else{
@@ -936,7 +937,7 @@ public function changeField($table, $field, $field_new, $spec, $new_keys = []) {
// Convert to a bytea type by using the SQL replace() function to
// convert any single backslashes in the field content to double
// backslashes ('\' to '\\').
$this->connection->query('ALTER TABLE {'.$table.'} ALTER "'.$field.'" TYPE '.$field_def.' USING decode(replace("'.$field.'"'.", E'\\\\', E'\\\\\\\\'), 'escape');");
$this->connection->query('ALTER TABLE {'.$table.'} ALTER ['.$field.'] TYPE '.$field_def.' USING decode(replace("'.$field.'"'.", E'\\\\', E'\\\\\\\\'), 'escape');");
}
}
@@ -947,7 +948,7 @@ public function changeField($table, $field, $field_new, $spec, $new_keys = []) {
else{
$null_action='DROP NOT NULL';
}
$this->connection->query('ALTER TABLE {'.$table.'} ALTER "'.$field.'" '.$null_action);
$this->connection->query('ALTER TABLE {'.$table.'} ALTER ['.$field.'] '.$null_action);
@@ -32,7 +32,7 @@ public function tableExists($table) {
$info=$this->getPrefixInfo($table);
// Don't use {} around sqlite_master table.
return(bool)$this->connection->query('SELECT 1 FROM '.$info['schema'].'.sqlite_master WHERE type = :type AND name = :name',[':type'=>'table',':name'=>$info['table']])->fetchField();
return(bool)$this->connection->query('SELECT 1 FROM ['.$info['schema'].'].sqlite_master WHERE type = :type AND name = :name',[':type'=>'table',':name'=>$info['table']])->fetchField();
}
/**
@@ -64,12 +64,12 @@ protected function createIndexSql($tablename, $schema) {
$info=$this->getPrefixInfo($tablename);
if(!empty($schema['unique keys'])){
foreach($schema['unique keys']as$key=>$fields){
$sql[]='CREATE UNIQUE INDEX '.$info['schema'].'.'.$info['table'].'_'.$key.' ON '.$info['table'].' ('.$this->createKeySql($fields).")\n";
$sql[]='CREATE UNIQUE INDEX ['.$info['schema'].'].['.$info['table'].'_'.$key.'] ON ['.$info['table'].'] ('.$this->createKeySql($fields).")\n";
}
}
if(!empty($schema['indexes'])){
foreach($schema['indexes']as$key=>$fields){
$sql[]='CREATE INDEX '.$info['schema'].'.'.$info['table'].'_'.$key.' ON '.$info['table'].' ('.$this->createKeySql($fields).")\n";
$sql[]='CREATE INDEX ['.$info['schema'].'].['.$info['table'].'_'.$key.'] ON ['.$info['table'].'] ('.$this->createKeySql($fields).")\n";
}
}
return$sql;
@@ -106,10 +106,10 @@ protected function createKeySql($fields) {
$return=[];
foreach($fieldsas$field){
if(is_array($field)){
$return[]=$field[0];
$return[]='['.$field[0].']';
}
else{
$return[]=$field;
$return[]='['.$field.']';
}
}
returnimplode(', ',$return);
@@ -272,7 +272,7 @@ public function renameTable($table, $new_name) {
// the table with curly braces in case the db_prefix contains a reference
// to a database outside of our existing database.
$info=$this->getPrefixInfo($new_name);
$this->connection->query('ALTER TABLE {'.$table.'} RENAME TO '.$info['table']);
$this->connection->query('ALTER TABLE {'.$table.'} RENAME TO ['.$info['table'].']');
// Drop the indexes, there is no RENAME INDEX command in SQLite.
if(!empty($schema['unique keys'])){
@@ -483,7 +483,7 @@ protected function introspectSchema($table) {
@@ -706,7 +706,7 @@ public function dropIndex($table, $name) {
$info=$this->getPrefixInfo($table);
$this->connection->query('DROP INDEX '.$info['schema'].'.'.$info['table'].'_'.$name);
$this->connection->query('DROP INDEX ['.$info['schema'].'].['.$info['table'].'_'.$name.']');
returnTRUE;
}
@@ -738,7 +738,7 @@ public function dropUniqueKey($table, $name) {
$info=$this->getPrefixInfo($table);
$this->connection->query('DROP INDEX '.$info['schema'].'.'.$info['table'].'_'.$name);
$this->connection->query('DROP INDEX ['.$info['schema'].'].['.$info['table'].'_'.$name.']');
returnTRUE;
}
@@ -817,7 +817,7 @@ public function findTables($table_expression) {
// Can't use query placeholders for the schema because the query would
// have to be :prefixsqlite_master, which does not work. We also need to
// ignore the internal SQLite tables.
$result=$this->connection->query("SELECT name FROM ".$schema.".sqlite_master WHERE type = :type AND name LIKE :table_name AND name NOT LIKE :pattern",[
$result=$this->connection->query("SELECT name FROM [".$schema."].sqlite_master WHERE type = :type AND name LIKE :table_name AND name NOT LIKE :pattern",[