Commit cfd30d63 authored by alexpott's avatar alexpott

Issue #2232425 followup by danblack, stefan.r: Fixed Database Schema...

Issue #2232425 followup by danblack, stefan.r: Fixed Database Schema field/column default value is not properly quoted via PDO::quote().
parent 221a48c1
...@@ -169,13 +169,7 @@ protected function createFieldSql($name, $spec) { ...@@ -169,13 +169,7 @@ protected function createFieldSql($name, $spec) {
// $spec['default'] can be NULL, so we explicitly check for the key here. // $spec['default'] can be NULL, so we explicitly check for the key here.
if (array_key_exists('default', $spec)) { if (array_key_exists('default', $spec)) {
if (is_string($spec['default'])) { $sql .= ' DEFAULT ' . $this->escapeDefaultValue($spec['default']);
$spec['default'] = $this->connection->quote($spec['default']);
}
elseif (!isset($spec['default'])) {
$spec['default'] = 'NULL';
}
$sql .= ' DEFAULT ' . $spec['default'];
} }
if (empty($spec['not null']) && !isset($spec['default'])) { if (empty($spec['not null']) && !isset($spec['default'])) {
...@@ -386,14 +380,7 @@ public function fieldSetDefault($table, $field, $default) { ...@@ -386,14 +380,7 @@ public function fieldSetDefault($table, $field, $default) {
throw new SchemaObjectDoesNotExistException(t("Cannot set default value of field @table.@field: field doesn't exist.", array('@table' => $table, '@field' => $field))); throw new SchemaObjectDoesNotExistException(t("Cannot set default value of field @table.@field: field doesn't exist.", array('@table' => $table, '@field' => $field)));
} }
if (!isset($default)) { $this->connection->query('ALTER TABLE {' . $table . '} ALTER COLUMN `' . $field . '` SET DEFAULT ' . $this->escapeDefaultValue($default));
$default = 'NULL';
}
else {
$default = is_string($default) ? "'$default'" : $default;
}
$this->connection->query('ALTER TABLE {' . $table . '} ALTER COLUMN `' . $field . '` SET DEFAULT ' . $default);
} }
public function fieldSetNoDefault($table, $field) { public function fieldSetNoDefault($table, $field) {
......
...@@ -272,8 +272,8 @@ protected function createFieldSql($name, $spec) { ...@@ -272,8 +272,8 @@ protected function createFieldSql($name, $spec) {
$sql .= ' NULL'; $sql .= ' NULL';
} }
} }
if (isset($spec['default'])) { if (array_key_exists('default', $spec)) {
$default = is_string($spec['default']) ? $this->connection->quote($spec['default']) : $spec['default']; $default = $this->escapeDefaultValue($spec['default']);
$sql .= " default $default"; $sql .= " default $default";
} }
...@@ -498,12 +498,7 @@ public function fieldSetDefault($table, $field, $default) { ...@@ -498,12 +498,7 @@ public function fieldSetDefault($table, $field, $default) {
throw new SchemaObjectDoesNotExistException(t("Cannot set default value of field @table.@field: field doesn't exist.", array('@table' => $table, '@field' => $field))); 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 = $this->escapeDefaultValue($default);
$default = 'NULL';
}
else {
$default = is_string($default) ? $this->connection->quote($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 ' . $default);
} }
......
...@@ -722,4 +722,21 @@ public function fieldNames($fields) { ...@@ -722,4 +722,21 @@ public function fieldNames($fields) {
public function prepareComment($comment, $length = NULL) { public function prepareComment($comment, $length = NULL) {
return $this->connection->quote($comment); 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;
}
} }
...@@ -292,6 +292,7 @@ function testSchemaAddField() { ...@@ -292,6 +292,7 @@ function testSchemaAddField() {
array('not null' => FALSE, 'default' => substr('"thing"', 0, $length)), array('not null' => FALSE, 'default' => substr('"thing"', 0, $length)),
array('not null' => FALSE, 'default' => substr("\"'hing", 0, $length)), array('not null' => FALSE, 'default' => substr("\"'hing", 0, $length)),
array('not null' => TRUE, 'initial' => 'd'), array('not null' => TRUE, 'initial' => 'd'),
array('not null' => FALSE, 'default' => NULL),
array('not null' => TRUE, 'initial' => 'd', 'default' => '7'), array('not null' => TRUE, 'initial' => 'd', 'default' => '7'),
); );
......
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