diff --git a/model/SQLQuery.php b/model/SQLQuery.php
index 85a2b241b..6040ea580 100644
--- a/model/SQLQuery.php
+++ b/model/SQLQuery.php
@@ -148,15 +148,18 @@ class SQLQuery {
*
*
* // pass fields to select as single parameter array
- * $query->setSelect(array("Col1","Col2"))->setFrom("MyTable");
+ * $query->setSelect(array('"Col1"', '"Col2"'))->setFrom('"MyTable"');
*
* // pass fields to select as multiple parameters
- * $query->setSelect("Col1", "Col2")->setFrom("MyTable");
+ * $query->setSelect('"Col1"', '"Col2"')->setFrom('"MyTable"');
+ *
+ * // Set a list of selected fields as aliases
+ * $query->setSelect(array('Name' => '"Col1"', 'Details' => '"Col2"')->setFrom('"MyTable"');
*
*
- * @param string|array $fields
+ * @param string|array $fields Field names should be ANSI SQL quoted. Array keys should be unquoted.
* @param boolean $clear Clear existing select fields?
- * @return SQLQuery
+ * @return $this Self reference
*/
public function setSelect($fields) {
$this->select = array();
@@ -171,17 +174,10 @@ class SQLQuery {
/**
* Add to the list of columns to be selected by the query.
*
- *
- * // pass fields to select as single parameter array
- * $query->addSelect(array("Col1","Col2"))->setFrom("MyTable");
+ * @see setSelect for example usage
*
- * // pass fields to select as multiple parameters
- * $query->addSelect("Col1", "Col2")->setFrom("MyTable");
- *
- *
- * @param string|array $fields
- * @param boolean $clear Clear existing select fields?
- * @return SQLQuery
+ * @param string|array $fields Field names should be ANSI SQL quoted. Array keys should be unquoted.
+ * @return $this Self reference
*/
public function addSelect($fields) {
if (func_num_args() > 1) {
@@ -191,7 +187,7 @@ class SQLQuery {
}
foreach($fields as $idx => $field) {
- if(preg_match('/^(.*) +AS +"?([^"]*)"?/i', $field, $matches)) {
+ if(preg_match('/^(.*) +AS +"([^"]*)"/i', $field, $matches)) {
Deprecation::notice("3.0", "Use selectField() to specify column aliases");
$this->selectField($matches[1], $matches[2]);
} else {
@@ -210,8 +206,8 @@ class SQLQuery {
/**
* Select an additional field.
*
- * @param $field String The field to select (escaped SQL statement)
- * @param $alias String The alias of that field (escaped SQL statement).
+ * @param string $field The field to select (ansi quoted SQL identifier or statement)
+ * @param string|null $alias The alias of that field (unquoted SQL identifier).
* Defaults to the unquoted column name of the $field parameter.
* @return SQLQuery
*/
@@ -500,8 +496,8 @@ class SQLQuery {
* @example $sql->setOrderBy("Column", "DESC");
* @example $sql->setOrderBy(array("Column" => "ASC", "ColumnTwo" => "DESC"));
*
- * @param string|array $orderby Clauses to add (escaped SQL statement)
- * @param string $dir Sort direction, ASC or DESC
+ * @param string|array $clauses Clauses to add (escaped SQL statement)
+ * @param string $direction Sort direction, ASC or DESC
*
* @return SQLQuery
*/
@@ -520,7 +516,7 @@ class SQLQuery {
* @example $sql->addOrderBy(array("Column" => "ASC", "ColumnTwo" => "DESC"));
*
* @param string|array $clauses Clauses to add (escaped SQL statements)
- * @param string $dir Sort direction, ASC or DESC
+ * @param string $direction Sort direction, ASC or DESC
*
* @return SQLQuery
*/
@@ -596,9 +592,9 @@ class SQLQuery {
/**
* Extract the direction part of a single-column order by clause.
*
- * @param String
- * @param String
- * @return Array A two element array: array($column, $direction)
+ * @param string $value
+ * @param string $defaultDirection
+ * @return array A two element array: array($column, $direction)
*/
private function getDirectionFromString($value, $defaultDirection = null) {
if(preg_match('/^(.*)(asc|desc)$/i', $value, $matches)) {
@@ -1015,7 +1011,7 @@ class SQLQuery {
$clone->setSelect(array("count($column)"));
}
- $clone->setGroupBy(array());;
+ $clone->setGroupBy(array());
return $clone->execute()->value();
}
diff --git a/tests/model/SQLQueryTest.php b/tests/model/SQLQueryTest.php
index 42a4f42e0..8ae14870d 100755
--- a/tests/model/SQLQueryTest.php
+++ b/tests/model/SQLQueryTest.php
@@ -575,6 +575,35 @@ class SQLQueryTest extends SapphireTest {
}
}
+ public function testSelect() {
+ $query = new SQLQuery('"Title"', '"MyTable"');
+ $query->addSelect('"TestField"');
+ $this->assertEquals(
+ 'SELECT "Title", "TestField" FROM "MyTable"',
+ $query->sql()
+ );
+
+ // Test replacement of select
+ $query->setSelect(array(
+ 'Field' => '"Field"',
+ 'AnotherAlias' => '"AnotherField"'
+ ));
+ $this->assertEquals(
+ 'SELECT "Field", "AnotherField" AS "AnotherAlias" FROM "MyTable"',
+ $query->sql()
+ );
+
+ // Check that ' as ' selects don't get mistaken as aliases
+ $query->addSelect(array(
+ 'Relevance' => "MATCH (Title, MenuTitle) AGAINST ('Two as One')"
+ ));
+ $this->assertEquals(
+ 'SELECT "Field", "AnotherField" AS "AnotherAlias", MATCH (Title, MenuTitle) AGAINST (' .
+ '\'Two as One\') AS "Relevance" FROM "MyTable"',
+ $query->sql()
+ );
+ }
+
/**
* Test passing in a LIMIT with OFFSET clause string.
*/