Issue followup by danblack, stefan.r: Fixed Database Schema field/column default value is not properly quoted via PDO::quote().

8.0.x
Alex Pott 2014-10-14 10:10:12 +01:00
parent 221a48c14e
commit cfd30d63e8
4 changed files with 23 additions and 23 deletions
core
lib/Drupal/Core/Database
modules/system/src/Tests/Database

View File

@ -169,13 +169,7 @@ class Schema extends DatabaseSchema {
// $spec['default'] can be NULL, so we explicitly check for the key here.
if (array_key_exists('default', $spec)) {
if (is_string($spec['default'])) {
$spec['default'] = $this->connection->quote($spec['default']);
}
elseif (!isset($spec['default'])) {
$spec['default'] = 'NULL';
}
$sql .= ' DEFAULT ' . $spec['default'];
$sql .= ' DEFAULT ' . $this->escapeDefaultValue($spec['default']);
}
if (empty($spec['not null']) && !isset($spec['default'])) {
@ -386,14 +380,7 @@ class Schema extends DatabaseSchema {
throw new SchemaObjectDoesNotExistException(t("Cannot set default value of field @table.@field: field doesn't exist.", array('@table' => $table, '@field' => $field)));
}
if (!isset($default)) {
$default = 'NULL';
}
else {
$default = is_string($default) ? "'$default'" : $default;
}
$this->connection->query('ALTER TABLE {' . $table . '} ALTER COLUMN `' . $field . '` SET DEFAULT ' . $default);
$this->connection->query('ALTER TABLE {' . $table . '} ALTER COLUMN `' . $field . '` SET DEFAULT ' . $this->escapeDefaultValue($default));
}
public function fieldSetNoDefault($table, $field) {

View File

@ -272,8 +272,8 @@ class Schema extends DatabaseSchema {
$sql .= ' NULL';
}
}
if (isset($spec['default'])) {
$default = is_string($spec['default']) ? $this->connection->quote($spec['default']) : $spec['default'];
if (array_key_exists('default', $spec)) {
$default = $this->escapeDefaultValue($spec['default']);
$sql .= " default $default";
}
@ -498,12 +498,7 @@ class Schema extends DatabaseSchema {
throw new SchemaObjectDoesNotExistException(t("Cannot set default value of field @table.@field: field doesn't exist.", array('@table' => $table, '@field' => $field)));
}
if (!isset($default)) {
$default = 'NULL';
}
else {
$default = is_string($default) ? $this->connection->quote($default) : $default;
}
$default = $this->escapeDefaultValue($default);
$this->connection->query('ALTER TABLE {' . $table . '} ALTER COLUMN "' . $field . '" SET DEFAULT ' . $default);
}

View File

@ -722,4 +722,21 @@ abstract class Schema implements PlaceholderInterface {
public function prepareComment($comment, $length = NULL) {
return $this->connection->quote($comment);
}
/**
* Return an escaped version of its parameter to be used as a default value
* on a column.
*
* @param mixed $value
* The value to be escaped (int, float, null or string).
*
* @return string|int|float
* The escaped value.
*/
protected function escapeDefaultValue($value) {
if (is_null($value)) {
return 'NULL';
}
return is_string($value) ? $this->connection->quote($value) : $value;
}
}

View File

@ -292,6 +292,7 @@ class SchemaTest extends KernelTestBase {
array('not null' => FALSE, 'default' => substr('"thing"', 0, $length)),
array('not null' => FALSE, 'default' => substr("\"'hing", 0, $length)),
array('not null' => TRUE, 'initial' => 'd'),
array('not null' => FALSE, 'default' => NULL),
array('not null' => TRUE, 'initial' => 'd', 'default' => '7'),
);