Skip to content

Commit

Permalink
Qouting fields inside postgres functions when prepended a DISTINCT, c…
Browse files Browse the repository at this point in the history
…loses #512
  • Loading branch information
lorenzo committed Mar 25, 2010
1 parent 9334aea commit 1f1324f
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 7 deletions.
26 changes: 26 additions & 0 deletions cake/libs/model/datasources/dbo/dbo_postgres.php
Expand Up @@ -455,12 +455,38 @@ function fields(&$model, $alias = null, $fields = array(), $quote = true) {
$build = explode('.', $fields[$i]);
$fields[$i] = $prepend . $this->name($build[0]) . '.' . $this->name($build[1]) . ' AS ' . $this->name($build[0] . '__' . $build[1]);
}
} else {
$fields[$i] = preg_replace_callback('/\(([\s\.\w]+)\)/', array(&$this, '__quoteFunctionField'), $fields[$i]);
}
}
}
return $fields;
}

/**
* Auxiliary function to quote matched `(Model.fields)` from a preg_replace_callback call
*
* @param string matched string
* @return string quoted strig
* @access private
*/
function __quoteFunctionField($match) {
$prepend = '';
if (strpos($match[1], 'DISTINCT') !== false) {
$prepend = 'DISTINCT ';
$match[1] = trim(str_replace('DISTINCT', '', $match[1]));
}
if (strpos($match[1], '.') === false) {
$match[1] = $this->name($alias . '.' . $match[1]);
} else {
$parts = explode('.', $match[1]);
if (!Set::numeric($parts)) {
$match[1] = $this->name($match[1]);
}
}
return '(' . $prepend .$match[1] . ')';
}

/**
* Returns an array of the indexes in given datasource name.
*
Expand Down
2 changes: 1 addition & 1 deletion cake/libs/model/datasources/dbo_source.php
Expand Up @@ -2327,7 +2327,7 @@ function __quoteFields($conditions) {
}

/**
* Auxiliary function to qoute matches `Model.fields` from a preg_replace_callback call
* Auxiliary function to quote matches `Model.fields` from a preg_replace_callback call
*
* @param string matched string
* @return string quoted strig
Expand Down
23 changes: 17 additions & 6 deletions cake/tests/cases/libs/model/datasources/dbo/dbo_postgres.test.php
Expand Up @@ -682,9 +682,9 @@ function testAlterIndexes() {
$this->db->query($this->db->dropSchema($schema1));
}

/**
* Test it is possible to use virtual field with postgresql
*/
/**
* Test it is possible to use virtual field with postgresql
*/
function testVirtualFields() {
$this->loadFixtures('Article', 'Comment');
$Article = new Article;
Expand All @@ -701,13 +701,24 @@ function testVirtualFields() {
$this->assertEqual($result['Article']['subquery'], 6);
}

/**
* Tests additional order options for postgres
*/
/**
* Tests additional order options for postgres
*/
function testOrderAdditionalParams() {
$result = $this->db->order(array('title' => 'DESC NULLS FIRST', 'body' => 'DESC'));
$expected = ' ORDER BY "title" DESC NULLS FIRST, "body" DESC';
$this->assertEqual($result, $expected);
}

/**
* Test it is possible to do a SELECT COUNT(DISTINCT Model.field) query in postgres and it gets correctly quoted
*/
function testQuoteDistinctInFunction() {
$this->loadFixtures('Article');
$Article = new Article;
$result = $this->db->fields($Article, null, array('COUNT(DISTINCT Article.id)'));
$expected = array('COUNT(DISTINCT "Article"."id")');
$this->assertEqual($result, $expected);
}
}
?>

0 comments on commit 1f1324f

Please sign in to comment.