Making DboSource::fields() accept an expression object. Fixes issues with sql parsing over quoting special SQL syntax.

Tests added
Fixes #66
This commit is contained in:
Mark Story 2009-12-13 15:52:07 -05:00
parent 47a9401d43
commit 02330b2d9c
2 changed files with 17 additions and 1 deletions

View file

@ -1683,7 +1683,9 @@ class DboSource extends DataSource {
if ($count >= 1 && !in_array($fields[0], array('*', 'COUNT(*)'))) {
for ($i = 0; $i < $count; $i++) {
if (preg_match('/^\(.*\)\s' . $this->alias . '.*/i', $fields[$i])){
if (is_object($fields[$i]) && isset($fields[$i]->type) && $fields[$i]->type === 'expression') {
$fields[$i] = $fields[$i]->value;
} elseif (preg_match('/^\(.*\)\s' . $this->alias . '.*/i', $fields[$i])){
continue;
} elseif (!preg_match('/^.+\\(.*\\)/', $fields[$i])) {
$prepend = '';

View file

@ -2781,6 +2781,20 @@ class DboSourceTest extends CakeTestCase {
);
$this->assertEqual($result, $expected);
}
/**
* test that fields() will accept objects made from DboSource::expression
*
* @return void
*/
function testFieldsWithExpression() {
$expression =& $this->testDb->expression("CASE Sample.id WHEN 1 THEN 'Id One' ELSE 'Other Id' END AS case_col");
$result = $this->testDb->fields($this->Model, null, array("id", $expression));
$expected = array(
'`TestModel`.`id`',
"CASE Sample.id WHEN 1 THEN 'Id One' ELSE 'Other Id' END AS case_col"
);
$this->assertEqual($result, $expected);
}
/**
* testMergeAssociations method
*