Refactoring DboSource::value() so postgres and mysq behavior is the same, updating test case

This commit is contained in:
José Lorenzo Rodríguez 2010-10-17 23:38:55 -04:30
parent 159776fc00
commit d9c9a32ff3
4 changed files with 64 additions and 131 deletions

View file

@ -199,54 +199,6 @@ class DboMysql extends DboSource {
}
}
/**
* Returns a quoted and escaped string of $data for use in an SQL statement.
*
* @param string $data String to be prepared for use in an SQL statement
* @param string $column The column into which this data will be inserted
* @param boolean $safe Whether or not numeric data should be handled automagically if no column data is provided
* @return string Quoted and escaped data
*/
function value($data, $column = null, $safe = false) {
$parent = parent::value($data, $column, $safe);
if ($parent != null) {
return $parent;
}
if ($data === null || (is_array($data) && empty($data))) {
return 'NULL';
}
if ($data === '' && $column !== 'integer' && $column !== 'float' && $column !== 'boolean') {
return $this->_connection->quote($data, PDO::PARAM_STR);
}
if (empty($column)) {
$column = $this->introspectType($data);
}
switch ($column) {
case 'boolean':
return $this->boolean((bool)$data);
break;
case 'integer':
case 'float':
if ($data === '') {
return 'NULL';
}
if (is_float($data)) {
return sprintf('%F', $data);
}
if ((is_int($data) || $data === '0') || (
is_numeric($data) && strpos($data, ',') === false &&
$data[0] != '0' && strpos($data, 'e') === false)
) {
return $data;
}
default:
return $this->_connection->quote($data, PDO::PARAM_STR);
break;
}
}
/**
* Returns a formatted error message from previous database operation.
*

View file

@ -256,61 +256,6 @@ class DboPostgres extends DboSource {
return $fields;
}
/**
* Returns a quoted and escaped string of $data for use in an SQL statement.
*
* @param string $data String to be prepared for use in an SQL statement
* @param string $column The column into which this data will be inserted
* @param boolean $read Value to be used in READ or WRITE context
* @return string Quoted and escaped
* @todo Add logic that formats/escapes data based on column type
*/
function value($data, $column = null, $read = true) {
$parent = parent::value($data, $column);
if ($parent != null) {
return $parent;
}
if ($data === null || (is_array($data) && empty($data))) {
return 'NULL';
}
if (empty($column)) {
$column = $this->introspectType($data);
}
switch($column) {
case 'binary':
$data = pg_escape_bytea($data);
break;
case 'boolean':
if ($data === true || $data === 't' || $data === 'true') {
return 'TRUE';
} elseif ($data === false || $data === 'f' || $data === 'false') {
return 'FALSE';
}
return (!empty($data) ? 'TRUE' : 'FALSE');
break;
case 'float':
if (is_float($data)) {
$data = sprintf('%F', $data);
}
case 'inet':
case 'integer':
case 'date':
case 'datetime':
case 'timestamp':
case 'time':
if ($data === '') {
return $read ? 'NULL' : 'DEFAULT';
}
default:
$data = pg_escape_string($data);
break;
}
return "'" . $data . "'";
}
/**
* Returns a formatted error message from previous database operation.
*

View file

@ -183,14 +183,14 @@ class DboSource extends DataSource {
*
* @param string $data String to be prepared for use in an SQL statement
* @param string $column The column into which this data will be inserted
* @param boolean $read Value to be used in READ or WRITE context
* @return mixed Prepared value or array of values.
* @param boolean $safe Whether or not numeric data should be handled automagically if no column data is provided
* @return string Quoted and escaped data
*/
public function value($data, $column = null, $read = true) {
function value($data, $column = null, $safe = false) {
if (is_array($data) && !empty($data)) {
return array_map(
array(&$this, 'value'),
$data, array_fill(0, count($data), $column), array_fill(0, count($data), $read)
$data, array_fill(0, count($data), $column), array_fill(0, count($data), $safe)
);
} elseif (is_object($data) && isset($data->type)) {
if ($data->type == 'identifier') {
@ -200,11 +200,46 @@ class DboSource extends DataSource {
}
} elseif (in_array($data, array('{$__cakeID__$}', '{$__cakeForeignKey__$}'), true)) {
return $data;
} else {
return null;
}
if ($data === null || (is_array($data) && empty($data))) {
return 'NULL';
}
if ($data === '' && $column !== 'integer' && $column !== 'float' && $column !== 'boolean') {
return $this->_connection->quote($data, PDO::PARAM_STR);
}
if (empty($column)) {
$column = $this->introspectType($data);
}
switch ($column) {
case 'binary':
$data = $this->_connection->quote($data, PDO::PARAM_LOB);
break;
case 'boolean':
return $this->boolean($data);
break;
case 'integer':
case 'float':
if ($data === '') {
return 'NULL';
}
if (is_float($data)) {
return sprintf('%F', $data);
}
if ((is_int($data) || $data === '0') || (
is_numeric($data) && strpos($data, ',') === false &&
$data[0] != '0' && strpos($data, 'e') === false)
) {
return $data;
}
default:
return $this->_connection->quote($data, PDO::PARAM_STR);
break;
}
}
/**
* Returns an object to represent a database identifier in a query
*
@ -462,6 +497,7 @@ class DboSource extends DataSource {
if ($cache) {
$this->_writeQueryCache($sql, $out, $params);
}
if (empty($out) && is_bool($this->_result)) {
return $this->_result;
}

View file

@ -330,30 +330,30 @@ class DboPostgresTest extends CakeTestCase {
* @return void
*/
function testValueQuoting() {
$this->assertIdentical($this->db2->value(1.2, 'float'), "'1.200000'");
$this->assertEqual($this->db2->value('1,2', 'float'), "'1,2'");
$this->assertEqual($this->Dbo->value(1.2, 'float'), "1.200000");
$this->assertEqual($this->Dbo->value('1,2', 'float'), "'1,2'");
$this->assertEqual($this->Dbo2->value('0', 'integer'), "'0'");
$this->assertEqual($this->Dbo2->value('', 'integer'), 'NULL');
$this->assertEqual($this->Dbo2->value('', 'float'), 'NULL');
$this->assertEqual($this->Dbo2->value('', 'integer', false), "DEFAULT");
$this->assertEqual($this->Dbo2->value('', 'float', false), "DEFAULT");
$this->assertEqual($this->Dbo2->value('0.0', 'float'), "'0.0'");
$this->assertEqual($this->Dbo->value('0', 'integer'), "0");
$this->assertEqual($this->Dbo->value('', 'integer'), 'NULL');
$this->assertEqual($this->Dbo->value('', 'float'), 'NULL');
$this->assertEqual($this->Dbo->value('', 'integer', false), "NULL");
$this->assertEqual($this->Dbo->value('', 'float', false), "NULL");
$this->assertEqual($this->Dbo->value('0.0', 'float'), "'0.0'");
$this->assertEqual($this->Dbo2->value('t', 'boolean'), "TRUE");
$this->assertEqual($this->Dbo2->value('f', 'boolean'), "FALSE");
$this->assertEqual($this->Dbo2->value(true), "TRUE");
$this->assertEqual($this->Dbo2->value(false), "FALSE");
$this->assertEqual($this->Dbo2->value('t'), "'t'");
$this->assertEqual($this->Dbo2->value('f'), "'f'");
$this->assertEqual($this->Dbo2->value('true', 'boolean'), 'TRUE');
$this->assertEqual($this->Dbo2->value('false', 'boolean'), 'FALSE');
$this->assertEqual($this->Dbo2->value('', 'boolean'), 'FALSE');
$this->assertEqual($this->Dbo2->value(0, 'boolean'), 'FALSE');
$this->assertEqual($this->Dbo2->value(1, 'boolean'), 'TRUE');
$this->assertEqual($this->Dbo2->value('1', 'boolean'), 'TRUE');
$this->assertEqual($this->Dbo2->value(null, 'boolean'), "NULL");
$this->assertEqual($this->Dbo2->value(array()), "NULL");
$this->assertEqual($this->Dbo->value('t', 'boolean'), true);
$this->assertEqual($this->Dbo->value('f', 'boolean'), false);
$this->assertEqual($this->Dbo->value(true), true);
$this->assertEqual($this->Dbo->value(false), false);
$this->assertEqual($this->Dbo->value('t'), "'t'");
$this->assertEqual($this->Dbo->value('f'), "'f'");
$this->assertEqual($this->Dbo->value('true', 'boolean'), true);
$this->assertEqual($this->Dbo->value('false', 'boolean'), false);
$this->assertEqual($this->Dbo->value('', 'boolean'), false);
$this->assertEqual($this->Dbo->value(0, 'boolean'), false);
$this->assertEqual($this->Dbo->value(1, 'boolean'), true);
$this->assertEqual($this->Dbo->value('1', 'boolean'), true);
$this->assertEqual($this->Dbo->value(null, 'boolean'), "NULL");
$this->assertEqual($this->Dbo->value(array()), "NULL");
}
/**