diff --git a/core/lib/Drupal/Core/Database/Driver/pgsql/Connection.php b/core/lib/Drupal/Core/Database/Driver/pgsql/Connection.php index 118974c..816ceea 100644 --- a/core/lib/Drupal/Core/Database/Driver/pgsql/Connection.php +++ b/core/lib/Drupal/Core/Database/Driver/pgsql/Connection.php @@ -163,8 +163,9 @@ public function escapeField($field) { // need to be escaped. $escaped = $this->escapeTable($table) . '.' . $this->escapeAlias($column); } - elseif (preg_match('/[A-Z]/', $escaped)) { - // Quote the field name for case-sensitivity. + else { + // Quote the field name to preserve case-sensitivity, and in case it is + // a reserved word. $escaped = '"' . $escaped . '"'; } @@ -177,10 +178,9 @@ public function escapeField($field) { public function escapeAlias($field) { $escaped = preg_replace('/[^A-Za-z0-9_]+/', '', $field); - // Escape the alias in quotes for case-sensitivity. - if (preg_match('/[A-Z]/', $escaped)) { - $escaped = '"' . $escaped . '"'; - } + // Escape the alias in quotes to preserve case-sensitivity, and in case it + // is a reserved word. + $escaped = '"' . $escaped . '"'; return $escaped; } @@ -191,10 +191,9 @@ public function escapeAlias($field) { public function escapeTable($table) { $escaped = parent::escapeTable($table); - // Quote identifier to make it case-sensitive. - if (preg_match('/[A-Z]/', $escaped)) { - $escaped = '"' . $escaped . '"'; - } + // Quote identifier to preserve case-sensitivity, and in case it is a + // reserved word. + $escaped = '"' . $escaped . '"'; return $escaped; } diff --git a/core/lib/Drupal/Core/Database/Driver/pgsql/Insert.php b/core/lib/Drupal/Core/Database/Driver/pgsql/Insert.php index 1b13a77..7fb213f 100644 --- a/core/lib/Drupal/Core/Database/Driver/pgsql/Insert.php +++ b/core/lib/Drupal/Core/Database/Driver/pgsql/Insert.php @@ -117,6 +117,8 @@ public function __toString() { // Default fields are always placed first for consistency. $insert_fields = array_merge($this->defaultFields, $this->insertFields); + $insert_fields = array_map(function($f) { return $this->connection->escapeField($f); }, $insert_fields); + // If we're selecting from a SelectQuery, finish building the query and // pass it back, as any remaining options are irrelevant. if (!empty($this->fromQuery)) { @@ -124,7 +126,7 @@ public function __toString() { return $comments . 'INSERT INTO {' . $this->table . '}' . $insert_fields_string . $this->fromQuery; } - $query = $comments . 'INSERT INTO {' . $this->table . '} (' . implode(', ', array_map('Drupal\Core\Database\Driver\pgsql\Insert::escape', $insert_fields)) . ') VALUES '; + $query = $comments . 'INSERT INTO {' . $this->table . '} (' . implode(', ', $insert_fields) . ') VALUES '; $max_placeholder = 0; $values = array(); @@ -155,11 +157,4 @@ public function __toString() { return $query; } - /** - * Surrounds a column name with double quotes to support reserved words. - */ - private static function escape($column) { - return '"' . $column . '"'; - } - } diff --git a/core/lib/Drupal/Core/Database/Query/Update.php b/core/lib/Drupal/Core/Database/Query/Update.php index 4461ba0..5893b1e 100644 --- a/core/lib/Drupal/Core/Database/Query/Update.php +++ b/core/lib/Drupal/Core/Database/Query/Update.php @@ -256,13 +256,13 @@ public function __toString() { $data['expression']->compile($this->connection, $this); $data['expression'] = ' (' . $data['expression'] . ')'; } - $update_fields[] = $field . '=' . $data['expression']; + $update_fields[] = $this->connection->escapeField($field) . '=' . $data['expression']; unset($fields[$field]); } $max_placeholder = 0; foreach ($fields as $field => $value) { - $update_fields[] = $field . '=:db_update_placeholder_' . ($max_placeholder++); + $update_fields[] = $this->connection->escapeField($field) . '=:db_update_placeholder_' . ($max_placeholder++); } $query = $comments . 'UPDATE {' . $this->connection->escapeTable($this->table) . '} SET ' . implode(', ', $update_fields);