Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Move shared code down to Driver.
There isn't any platform specific SQL in _value so pushing it down to
the Driver saves duplication, inheritance and makes drivers more useful.
  • Loading branch information
markstory committed May 12, 2013
1 parent edc5b5c commit f13f32f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 59 deletions.
28 changes: 28 additions & 0 deletions lib/Cake/Database/Driver.php
Expand Up @@ -135,6 +135,34 @@ public function supportsQuoting() {
return true;
}

/**
* Escapes values for use in schema definitions.
*
* @param mixed $value The value to escape.
* @return string String for use in schema definitions.
*/
public function schemaValue($value) {
if (is_null($value)) {
return 'NULL';
}
if ($value === false) {
return 'FALSE';
}
if ($value === true) {
return 'TRUE';
}
if (is_float($value)) {
return str_replace(',', '.', strval($value));
}
if ((is_int($value) || $value === '0') || (
is_numeric($value) && strpos($value, ',') === false &&
$value[0] != '0' && strpos($value, 'e') === false)
) {
return $value;
}
return $this->_connection->quote($value, \PDO::PARAM_STR);
}

/**
* Returns last id generated for a table or sequence in database
*
Expand Down
32 changes: 2 additions & 30 deletions lib/Cake/Database/Schema/MysqlSchema.php
Expand Up @@ -240,7 +240,7 @@ public function columnSql(Table $table, $name) {
unset($data['default']);
}
if (isset($data['default']) && $data['type'] !== 'timestamp') {
$out .= ' DEFAULT ' . $this->_value($data['default']);
$out .= ' DEFAULT ' . $this->_driver->schemaValue($data['default']);
}
if (
isset($data['default']) &&
Expand All @@ -250,39 +250,11 @@ public function columnSql(Table $table, $name) {
$out .= ' DEFAULT CURRENT_TIMESTAMP';
}
if (isset($data['comment'])) {
$out .= ' COMMENT ' . $this->_value($data['comment']);
$out .= ' COMMENT ' . $this->_driver->schemaValue($data['comment']);
}
return $out;
}

/**
* Escapes values for use in schema definitions.
*
* @param mixed $value The value to escape.
* @return string String for use in schema definitions.
*/
protected function _value($value) {
if (is_null($value)) {
return 'NULL';
}
if ($value === false) {
return 'FALSE';
}
if ($value === true) {
return 'TRUE';
}
if (is_float($value)) {
return str_replace(',', '.', strval($value));
}
if ((is_int($value) || $value === '0') || (
is_numeric($value) && strpos($value, ',') === false &&
$value[0] != '0' && strpos($value, 'e') === false)
) {
return $value;
}
return $this->_driver->quote($value, \PDO::PARAM_STR);
}

/**
* Generate the SQL fragment for a single index in MySQL
*
Expand Down
30 changes: 1 addition & 29 deletions lib/Cake/Database/Schema/SqliteSchema.php
Expand Up @@ -188,39 +188,11 @@ public function columnSql(Table $table, $name) {
unset($data['default']);
}
if (isset($data['default'])) {
$out .= ' DEFAULT ' . $this->_value($data['default']);
$out .= ' DEFAULT ' . $this->_driver->schemaValue($data['default']);
}
return $out;
}

/**
* Escapes values for use in schema definitions.
*
* @param mixed $value The value to escape.
* @return string String for use in schema definitions.
*/
protected function _value($value) {
if (is_null($value)) {
return 'NULL';
}
if ($value === false) {
return 'FALSE';
}
if ($value === true) {
return 'TRUE';
}
if (is_float($value)) {
return str_replace(',', '.', strval($value));
}
if ((is_int($value) || $value === '0') || (
is_numeric($value) && strpos($value, ',') === false &&
$value[0] != '0' && strpos($value, 'e') === false)
) {
return $value;
}
return $this->_driver->quote($value, \PDO::PARAM_STR);
}

/**
* Generate the SQL fragment for a single index in MySQL
*
Expand Down

0 comments on commit f13f32f

Please sign in to comment.