Skip to content

Commit

Permalink
Fixing complex expression handling in DboSource::fields(). Making AS …
Browse files Browse the repository at this point in the history
…a variable controlled by implementing dbo.

Test cases added.
Fixes #38
  • Loading branch information
markstory committed Aug 19, 2009
1 parent 5a971dd commit cca3281
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
21 changes: 12 additions & 9 deletions cake/libs/model/datasources/dbo_source.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ class DboSource extends DataSource {
* @access protected * @access protected
*/ */
var $_commands = array( var $_commands = array(
'begin' => 'BEGIN', 'begin' => 'BEGIN',
'commit' => 'COMMIT', 'commit' => 'COMMIT',
'rollback' => 'ROLLBACK' 'rollback' => 'ROLLBACK'
); );
/** /**
Expand Down Expand Up @@ -409,14 +409,15 @@ function name($data) {
$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;
} }
} }


Expand Down Expand Up @@ -1680,7 +1681,9 @@ function fields(&$model, $alias = null, $fields = array(), $quote = true) {


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) {
Expand Down
6 changes: 5 additions & 1 deletion cake/tests/cases/libs/model/datasources/dbo_source.test.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2696,6 +2696,10 @@ function testFieldParsing() {
); );
$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`',
Expand Down Expand Up @@ -2773,7 +2777,7 @@ function testFieldParsing() {
$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);
} }
Expand Down

0 comments on commit cca3281

Please sign in to comment.