Changes to make mysql cast boolean vaules to the native php boolean datatype

This commit is contained in:
José Lorenzo Rodríguez 2011-01-01 22:07:27 -04:30
parent 04c602e3d0
commit da152e0e82
4 changed files with 45 additions and 33 deletions

View file

@ -213,10 +213,15 @@ class DboMysql extends DboSource {
while ($j < $numFields) {
$column = $results->getColumnMeta($j);
if (!empty($column['table']) && strpos($column['name'], $this->virtualFieldSeparator) === false) {
$this->map[$index++] = array($column['table'], $column['name']);
if (!empty($column['native_type'])) {
$type = $column['native_type'];
} else {
$this->map[$index++] = array(0, $column['name']);
$type = ($column['len'] == 1) ? 'boolean' : 'string';
}
if (!empty($column['table']) && strpos($column['name'], $this->virtualFieldSeparator) === false) {
$this->map[$index++] = array($column['table'], $column['name'], $type);
} else {
$this->map[$index++] = array(0, $column['name'], $type);
}
$j++;
}
@ -231,8 +236,11 @@ class DboMysql extends DboSource {
if ($row = $this->_result->fetch()) {
$resultRow = array();
foreach ($this->map as $col => $meta) {
list($table, $column) = $meta;
list($table, $column, $type) = $meta;
$resultRow[$table][$column] = $row[$col];
if ($type == 'boolean') {
$resultRow[$table][$column] = $this->boolean($resultRow[$table][$column]);
}
}
return $resultRow;
} else {

View file

@ -194,14 +194,13 @@ 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 $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) {
function value($data, $column = null) {
if (is_array($data) && !empty($data)) {
return array_map(
array(&$this, 'value'),
$data, array_fill(0, count($data), $column), array_fill(0, count($data), $safe)
$data, array_fill(0, count($data), $column)
);
} elseif (is_object($data) && isset($data->type)) {
if ($data->type == 'identifier') {
@ -226,7 +225,7 @@ class DboSource extends DataSource {
return $this->_connection->quote($data, PDO::PARAM_LOB);
break;
case 'boolean':
return $this->_connection->quote($this->boolean($data), PDO::PARAM_BOOL);
return $this->_connection->quote($this->boolean($data, true), PDO::PARAM_BOOL);
break;
case 'string':
case 'text':
@ -2698,15 +2697,11 @@ class DboSource extends DataSource {
* @param mixed $data Value to be translated
* @return int Converted boolean value
*/
public function boolean($data) {
if ($data === true || $data === false) {
if ($data === true) {
return 1;
}
return 0;
} else {
return (int) !empty($data);
public function boolean($data, $quote = false) {
if ($quote) {
return !empty($data) ? '1' : '0';
}
return !empty($data);
}
/**

View file

@ -1421,7 +1421,7 @@ class ModelIntegrationTest extends BaseModelTest {
'id' => '3',
'something_id' => '3',
'something_else_id' => '1',
'doomed' => '1',
'doomed' => true,
'created' => '2007-03-18 10:43:23',
'updated' => '2007-03-18 10:45:31'
)))),
@ -1446,7 +1446,7 @@ class ModelIntegrationTest extends BaseModelTest {
'id' => '1',
'something_id' => '1',
'something_else_id' => '2',
'doomed' => '1',
'doomed' => true,
'created' => '2007-03-18 10:39:23',
'updated' => '2007-03-18 10:41:31'
)))),
@ -1471,7 +1471,7 @@ class ModelIntegrationTest extends BaseModelTest {
'id' => '2',
'something_id' => '2',
'something_else_id' => '3',
'doomed' => '0',
'doomed' => false,
'created' => '2007-03-18 10:41:23',
'updated' => '2007-03-18 10:43:31'
)))));
@ -1497,7 +1497,7 @@ class ModelIntegrationTest extends BaseModelTest {
'created' => '2007-03-18 10:41:23',
'updated' => '2007-03-18 10:43:31',
'JoinThing' => array(
'doomed' => '1',
'doomed' => true,
'something_id' => '1',
'something_else_id' => '2'
)))),
@ -1519,7 +1519,7 @@ class ModelIntegrationTest extends BaseModelTest {
'created' => '2007-03-18 10:43:23',
'updated' => '2007-03-18 10:45:31',
'JoinThing' => array(
'doomed' => '0',
'doomed' => false,
'something_id' => '2',
'something_else_id' => '3'
)))),
@ -1541,7 +1541,7 @@ class ModelIntegrationTest extends BaseModelTest {
'created' => '2007-03-18 10:39:23',
'updated' => '2007-03-18 10:41:31',
'JoinThing' => array(
'doomed' => '1',
'doomed' => true,
'something_id' => '3',
'something_else_id' => '1'
)))));
@ -1566,7 +1566,7 @@ class ModelIntegrationTest extends BaseModelTest {
'created' => '2007-03-18 10:41:23',
'updated' => '2007-03-18 10:43:31',
'JoinThing' => array(
'doomed' => '1',
'doomed' => true,
'something_id' => '1',
'something_else_id' => '2'
))));
@ -1583,7 +1583,7 @@ class ModelIntegrationTest extends BaseModelTest {
'Something' => array('id' => 1),
'SomethingElse' => array(3, array(
'something_else_id' => 1,
'doomed' => '1'
'doomed' => true
))));
$ts = date('Y-m-d H:i:s');
@ -1608,7 +1608,7 @@ class ModelIntegrationTest extends BaseModelTest {
'created' => '2007-03-18 10:39:23',
'updated' => '2007-03-18 10:41:31',
'JoinThing' => array(
'doomed' => '1',
'doomed' => true,
'something_id' => '1',
'something_else_id' => '1'
)),
@ -1620,7 +1620,7 @@ class ModelIntegrationTest extends BaseModelTest {
'created' => '2007-03-18 10:41:23',
'updated' => '2007-03-18 10:43:31',
'JoinThing' => array(
'doomed' => '1',
'doomed' => true,
'something_id' => '1',
'something_else_id' => '2'
)),
@ -1632,7 +1632,7 @@ class ModelIntegrationTest extends BaseModelTest {
'created' => '2007-03-18 10:43:23',
'updated' => '2007-03-18 10:45:31',
'JoinThing' => array(
'doomed' => '0',
'doomed' => false,
'something_id' => '1',
'something_else_id' => '3'
))));

View file

@ -56,8 +56,8 @@ class ModelReadTest extends BaseModelTest {
$result = $Something->JoinThing->find('all', array('conditions' => array('something_else_id' => 2)));
$this->assertEqual((bool)$result[0]['JoinThing']['doomed'], true);
$this->assertEqual((bool)$result[1]['JoinThing']['doomed'], false);
$this->assertEqual($result[0]['JoinThing']['doomed'], true);
$this->assertEqual($result[1]['JoinThing']['doomed'], false);
$result = $Something->find('first');
@ -5100,7 +5100,7 @@ class ModelReadTest extends BaseModelTest {
array(
'id' => 2,
'syfile_id' => 2,
'published' => 0,
'published' => false,
'name' => 'Item 2',
'ItemsPortfolio' => array(
'id' => 2,
@ -5120,7 +5120,7 @@ class ModelReadTest extends BaseModelTest {
array(
'id' => 6,
'syfile_id' => 6,
'published' => 0,
'published' => false,
'name' => 'Item 6',
'ItemsPortfolio' => array(
'id' => 6,
@ -7403,7 +7403,7 @@ class ModelReadTest extends BaseModelTest {
$Post->Author->virtualFields = array('false' => '1 = 2');
$result = $Post->find('first');
$this->assertEqual($result['Post']['two'], 2);
$this->assertEqual($result['Author']['false'], '0');
$this->assertFalse((bool)$result['Author']['false']);
$result = $Post->find('first',array('fields' => array('author_id')));
$this->assertFalse(isset($result['Post']['two']));
@ -7462,11 +7462,20 @@ class ModelReadTest extends BaseModelTest {
$Post->virtualFields = array('other_field' => 'COUNT(Post.id) + 1');
$result = $Post->field('other_field');
$this->assertEqual($result, 4);
}
/**
* testVirtualFieldsMysql()
*
* Test correct fetching of virtual fields
* currently is not possible to do Relation.virtualField
*
*/
public function testVirtualFieldsMysql() {
if ($this->skipIf($this->db->config['driver'] != 'mysql', 'The rest of virtualFieds test is not compatible with Postgres')) {
return;
}
ClassRegistry::flush();
$this->loadFixtures('Post', 'Author');
$Post = ClassRegistry::init('Post');
$Post->create();