Permalink
Browse files

Fixing complex expression handling in DboSource::fields(). Making AS …

…a variable controlled by implementing dbo.

Test cases added.
Fixes #38
  • Loading branch information...
1 parent 5a971dd commit cca3281f8c9a543e8f7554a679004d351ac2d79d @markstory markstory committed Aug 19, 2009
@@ -78,8 +78,8 @@ class DboSource extends DataSource {
* @access protected
*/
var $_commands = array(
- 'begin' => 'BEGIN',
- 'commit' => 'COMMIT',
+ 'begin' => 'BEGIN',
+ 'commit' => 'COMMIT',
'rollback' => 'ROLLBACK'
);
/**
@@ -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 . '(', '(', $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('/\w+\s+AS\s+/', $data[$i])) {
- $quoted = $this->endQuote . ' AS ' . $this->startQuote;
- $data[$i] = str_replace(' AS ', $quoted, $data[$i]);
+ if (preg_match('/\s+' . $alias . '\s*/', $data[$i])) {
+ if (preg_match('/\w+\s+' . $alias . '\s*/', $data[$i])) {
+ $quoted = $this->endQuote . ' ' . $alias . $this->startQuote;
+ $data[$i] = str_replace(' ' . $alias, $quoted, $data[$i]);
} else {
- $quoted = ' AS ' . $this->startQuote;
- $data[$i] = str_replace(' AS ', $quoted, $data[$i]) . $this->endQuote;
+ $quoted = $alias . $this->startQuote;
+ $data[$i] = str_replace($alias, $quoted, $data[$i]) . $this->endQuote;
}
}
@@ -1680,7 +1681,9 @@ function fields(&$model, $alias = null, $fields = array(), $quote = true) {
if ($count >= 1 && !in_array($fields[0], array('*', 'COUNT(*)'))) {
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 = '';
if (strpos($fields[$i], 'DISTINCT') !== false) {
@@ -2696,6 +2696,10 @@ function testFieldParsing() {
);
$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');
$expected = array(
'`Post`.`id`', '`Post`.`client_id`', '`Post`.`name`', '`Post`.`login`',
@@ -2773,7 +2777,7 @@ function testFieldParsing() {
$expected = array(
'`Foo`.`id`',
'`Foo`.`title`',
- '(user_count + discussion_count + post_count) AS `score`'
+ '(user_count + discussion_count + post_count) AS score'
);
$this->assertEqual($result, $expected);
}

0 comments on commit cca3281

Please sign in to comment.