Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Qouting fields inside postgres functions when prepended a DISTINCT, c…

…loses #512
  • Loading branch information...
commit 1f1324f500f1dbe4c94c82d0bc38dd2c826e8cf2 1 parent 9334aea
@lorenzo lorenzo authored
View
26 cake/libs/model/datasources/dbo/dbo_postgres.php
@@ -455,6 +455,8 @@ 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]);
}
}
}
@@ -462,6 +464,30 @@ function fields(&$model, $alias = null, $fields = array(), $quote = true) {
}
/**
+ * 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.
*
* @param string $model Name of model to inspect
View
2  cake/libs/model/datasources/dbo_source.php
@@ -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
View
23 cake/tests/cases/libs/model/datasources/dbo/dbo_postgres.test.php
@@ -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;
@@ -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);
+ }
}
?>
Please sign in to comment.
Something went wrong with that request. Please try again.