Fixing complex expression handling in DboSource::fields(). Making AS a variable controlled by implementing dbo.

Test cases added.
Fixes #38
This commit is contained in:
mark_story 2009-08-19 13:03:36 -04:00
parent 5a971ddbf0
commit cca3281f8c
2 changed files with 17 additions and 10 deletions

View file

@ -409,14 +409,15 @@ class DboSource extends DataSource {
$data[$i] = str_replace($this->startQuote . $this->startQuote, $this->startQuote, $data[$i]); $data[$i] = str_replace($this->startQuote . $this->startQuote, $this->startQuote, $data[$i]);
$data[$i] = str_replace($this->startQuote . '(', '(', $data[$i]); $data[$i] = str_replace($this->startQuote . '(', '(', $data[$i]);
$data[$i] = str_replace(')' . $this->startQuote, ')', $data[$i]); $data[$i] = str_replace(')' . $this->startQuote, ')', $data[$i]);
$alias = !empty($this->alias) ? $this->alias : 'AS ';
if (preg_match('/\s+AS\s+/', $data[$i])) { if (preg_match('/\s+' . $alias . '\s*/', $data[$i])) {
if (preg_match('/\w+\s+AS\s+/', $data[$i])) { if (preg_match('/\w+\s+' . $alias . '\s*/', $data[$i])) {
$quoted = $this->endQuote . ' AS ' . $this->startQuote; $quoted = $this->endQuote . ' ' . $alias . $this->startQuote;
$data[$i] = str_replace(' AS ', $quoted, $data[$i]); $data[$i] = str_replace(' ' . $alias, $quoted, $data[$i]);
} else { } else {
$quoted = ' AS ' . $this->startQuote; $quoted = $alias . $this->startQuote;
$data[$i] = str_replace(' AS ', $quoted, $data[$i]) . $this->endQuote; $data[$i] = str_replace($alias, $quoted, $data[$i]) . $this->endQuote;
} }
} }
@ -1680,7 +1681,9 @@ class DboSource extends DataSource {
if ($count >= 1 && !in_array($fields[0], array('*', 'COUNT(*)'))) { if ($count >= 1 && !in_array($fields[0], array('*', 'COUNT(*)'))) {
for ($i = 0; $i < $count; $i++) { for ($i = 0; $i < $count; $i++) {
if (!preg_match('/^.+\\(.*\\)/', $fields[$i])) { if (preg_match('/^\(.*\)\s' . $this->alias . '.*/i', $fields[$i])){
continue;
} elseif (!preg_match('/^.+\\(.*\\)/', $fields[$i])) {
$prepend = ''; $prepend = '';
if (strpos($fields[$i], 'DISTINCT') !== false) { if (strpos($fields[$i], 'DISTINCT') !== false) {

View file

@ -2696,6 +2696,10 @@ class DboSourceTest extends CakeTestCase {
); );
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);
$result = $this->testDb->fields($this->Model, null, "(`Provider`.`star_total` / `Provider`.`total_ratings`) as `rating`");
$expected = array("(`Provider`.`star_total` / `Provider`.`total_ratings`) as `rating`");
$this->assertEqual($result, $expected);
$result = $this->testDb->fields($this->Model, 'Post'); $result = $this->testDb->fields($this->Model, 'Post');
$expected = array( $expected = array(
'`Post`.`id`', '`Post`.`client_id`', '`Post`.`name`', '`Post`.`login`', '`Post`.`id`', '`Post`.`client_id`', '`Post`.`name`', '`Post`.`login`',
@ -2773,7 +2777,7 @@ class DboSourceTest extends CakeTestCase {
$expected = array( $expected = array(
'`Foo`.`id`', '`Foo`.`id`',
'`Foo`.`title`', '`Foo`.`title`',
'(user_count + discussion_count + post_count) AS `score`' '(user_count + discussion_count + post_count) AS score'
); );
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);
} }