diff --git a/Tests/dblayerooComplet.php b/Tests/dblayerooComplet.php index 625de43..945c9eb 100644 --- a/Tests/dblayerooComplet.php +++ b/Tests/dblayerooComplet.php @@ -702,4 +702,104 @@ class test_dblayeroo_{ENGINE} extends PHPUnit_Framework_TestCase $db2->disconnect (); $this->assertSame ("order3", $res); } + + public function test_displayAdd_NotFull1 () + { + $db1 = $this->db1 (); + $db1->displayAdd ("group"); + $res = $db1->displayGet (); + $db1->disconnect (); + $this->assertSame (array ("order1" => $db1->sep()."group".$db1->sep()), + $res); + } + + public function test_displayAdd_NotFull2 () + { + $db1 = $this->db1 (); + $db1->displayAdd ("distinct group"); + $res = $db1->displayGet (); + $db1->disconnect (); + $this->assertSame (array ( + "order1" => "DISTINCT ".$db1->sep()."group".$db1->sep()), $res); + } + + public function test_displayAdd_NotFull3 () + { + $db1 = $this->db1 (); + $db1->displayAdd ("group_concat ( group ) "); + $res = $db1->displayGet (); + $db1->disconnect (); + $this->assertSame (array ( + "order1" => "GROUP_CONCAT(".$db1->sep()."group".$db1->sep().")"), $res); + } + + public function test_displayAdd_NotFull4 () + { + $db1 = $this->db1 (); + $db1->displayAdd ("group_concat (distinct group ) "); + $res = $db1->displayGet (); + $db1->disconnect (); + $this->assertSame (array ( + "order1" => "GROUP_CONCAT(DISTINCT ". + $db1->sep()."group".$db1->sep().")"), $res); + } + + public function test_displayAdd_Full1 () + { + $db1 = $this->db1 (); + $db1->displayAdd ("group"); + $res = $db1->displayGet (true); + $db1->disconnect (); + $this->assertSame (array ( + "order1" => $db1->sep()."groupedoo".$db1->sep().".". + $db1->sep()."group".$db1->sep()), + $res); + } + + public function test_displayAdd_Full2 () + { + $db1 = $this->db1 (); + $db1->displayAdd ("distinct group"); + $res = $db1->displayGet (true); + $db1->disconnect (); + $this->assertSame (array ( + "order1" => "DISTINCT ".$db1->sep()."groupedoo".$db1->sep().".". + $db1->sep()."group".$db1->sep()), $res); + } + + public function test_displayAdd_Full3 () + { + $db1 = $this->db1 (); + $db1->displayAdd ("group_concat ( group ) "); + $res = $db1->displayGet (true); + $db1->disconnect (); + $this->assertSame (array ( + "order1" => "GROUP_CONCAT(".$db1->sep()."groupedoo".$db1->sep().".". + $db1->sep()."group".$db1->sep().")"), $res); + } + + public function test_displayAdd_Full4 () + { + $db1 = $this->db1 (); + $db1->displayAdd ("group_concat (distinct group ) "); + $res = $db1->displayGet (true); + $db1->disconnect (); + $this->assertSame (array ( + "order1" => "GROUP_CONCAT(DISTINCT ". + $db1->sep()."groupedoo".$db1->sep().".". + $db1->sep()."group".$db1->sep().")"), $res); + } + + public function test_displayAdd_Multiple1 () + { + $db1 = $this->db1 (); + $db1->displayAdd ("group"); + $db1->displayAdd ("where"); + $res = $db1->displayGet (); + $db1->disconnect (); + $this->assertSame (array ("order1" => $db1->sep()."group".$db1->sep(), + "order2" => $db1->sep()."where".$db1->sep(),), + $res); + } + } diff --git a/dblayeroo.php b/dblayeroo.php index 0b2dbf4..1ae9cda 100644 --- a/dblayeroo.php +++ b/dblayeroo.php @@ -1231,6 +1231,15 @@ class dblayeroo } /* }}} */ + /** Get the sep property + */ + public function sep () + { + /* {{{ */ + return $this->sep; + /* }}} */ + } + /** Get/Set the dsn property * @param string|null $dsn Set the DSN property of PDO */ @@ -1557,23 +1566,32 @@ class dblayeroo if ($func === "GROUP_CONCAT" && $this->driver === "psql") $func = "string_agg"; } - $display = "$func($name)"; } - if (! array_key_exists ($name, $this->fields)) + $fieldName = $name; + $distinct = ""; + if (stripos ($name, "DISTINCT ") === 0) + { + $distinct = "DISTINCT "; + $fieldName = substr ($name, strlen ("DISTINCT ")); + } + if (! array_key_exists ($fieldName, $this->fields)) $this->DBException (sprintf ( - "Invalid field to display '%s' : not defined in table", $name)); + "Invalid field to display '%s' : not defined in table", $fieldName)); $getSortOrder = $this->getSortOrder(); if (! isset ($func)) - $this->displayColumn[$getSortOrder] = $this->sep.$display.$this->sep; + $this->displayColumn[$getSortOrder] = + $distinct.$this->sep.$fieldName.$this->sep; else $this->displayColumn[$getSortOrder] = - "$func($this->sep$name$this->sep$separator)"; + "$func($distinct$this->sep$fieldName$this->sep$separator)"; if ($this->groupByExpression !== array () && ! isset ($aggregateFunction)) { // Not a aggregate function, but groupBy is set : add the new field name - $this->groupByExpression[$getSortOrder] = $this->sep.$name.$this->sep; + $this->groupByExpression[$getSortOrder] = + $this->sep.$fieldName.$this->sep; } unset ($aggregateFunction); + unset ($func); } return $this; } @@ -1581,10 +1599,19 @@ class dblayeroo /** Return the name of the display field, with $this->sep * Add the table prefix/name if full is set - * Allow name, $this->sep.$name.$this->sep, func(name), - * func($this->sep.$name.$this->sep), - * func($this->sep.$this->tableprefix.$this->table.$this->sep.".". - * $this->sep.$name.$this->sep) + * Allow : + * name, + * DISTINCT name, + * $this->sep.$name.$this->sep, + * DISTINCT $this->sep.$name.$this->sep, + * func(name), + * func(DISTINCT name), + * func($this->sep.$name.$this->sep), + * func(DISTINCT $this->sep.$name.$this->sep), + * func($this->sep.$this->tableprefix.$this->table.$this->sep.".". + * $this->sep.$name.$this->sep) + * func(DISTINCT $this->sep.$this->tableprefix.$this->table.$this->sep.".". + * $this->sep.$name.$this->sep) * @param string $name The name of the field * @param boolean|null $full Add the table prefix/name if set */ @@ -1597,6 +1624,12 @@ class dblayeroo $func = strtoupper (trim (substr ($name, 0, $pos))); $name = trim (substr ($name, $pos+1, -1)); } + $distinct = ""; + if (strpos ($name, "DISTINCT ") === 0) + { + $distinct = "DISTINCT "; + $name = substr ($name, strlen ("DISTINCT ")); + } if ($name[0] !== $this->sep) $name = $this->sep.$name; if (! isset ($func) && substr ($name, -1) !== $this->sep) @@ -1604,12 +1637,14 @@ class dblayeroo if ($full !== false) $name = $this->sep.$this->tableprefix.$this->table.$this->sep.".".$name; if (isset ($func)) - $name = "$func($name)"; + $name = "$func($distinct$name)"; + else + $name = "$distinct$name"; return $name; } /* }}} */ - /** Get the columns set in the query by displayColumn. If the $full parameter + /** Get the columns set in the query by displayAdd. If the $full parameter * is set, add the table prefix/name to the result. * If the join object is set, ask to it the columns too * @param boolean $full Add the table prefix/name if set