dblayeroo: Add DISTINCT feature to displayAdd
git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@3819 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
This commit is contained in:
@@ -702,4 +702,104 @@ class test_dblayeroo_{ENGINE} extends PHPUnit_Framework_TestCase
|
|||||||
$db2->disconnect ();
|
$db2->disconnect ();
|
||||||
$this->assertSame ("order3", $res);
|
$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);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1231,6 +1231,15 @@ class dblayeroo
|
|||||||
}
|
}
|
||||||
/* }}} */
|
/* }}} */
|
||||||
|
|
||||||
|
/** Get the sep property
|
||||||
|
*/
|
||||||
|
public function sep ()
|
||||||
|
{
|
||||||
|
/* {{{ */
|
||||||
|
return $this->sep;
|
||||||
|
/* }}} */
|
||||||
|
}
|
||||||
|
|
||||||
/** Get/Set the dsn property
|
/** Get/Set the dsn property
|
||||||
* @param string|null $dsn Set the DSN property of PDO
|
* @param string|null $dsn Set the DSN property of PDO
|
||||||
*/
|
*/
|
||||||
@@ -1557,23 +1566,32 @@ class dblayeroo
|
|||||||
if ($func === "GROUP_CONCAT" && $this->driver === "psql")
|
if ($func === "GROUP_CONCAT" && $this->driver === "psql")
|
||||||
$func = "string_agg";
|
$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 (
|
$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();
|
$getSortOrder = $this->getSortOrder();
|
||||||
if (! isset ($func))
|
if (! isset ($func))
|
||||||
$this->displayColumn[$getSortOrder] = $this->sep.$display.$this->sep;
|
$this->displayColumn[$getSortOrder] =
|
||||||
|
$distinct.$this->sep.$fieldName.$this->sep;
|
||||||
else
|
else
|
||||||
$this->displayColumn[$getSortOrder] =
|
$this->displayColumn[$getSortOrder] =
|
||||||
"$func($this->sep$name$this->sep$separator)";
|
"$func($distinct$this->sep$fieldName$this->sep$separator)";
|
||||||
if ($this->groupByExpression !== array () && ! isset ($aggregateFunction))
|
if ($this->groupByExpression !== array () && ! isset ($aggregateFunction))
|
||||||
{
|
{
|
||||||
// Not a aggregate function, but groupBy is set : add the new field name
|
// 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 ($aggregateFunction);
|
||||||
|
unset ($func);
|
||||||
}
|
}
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
@@ -1581,10 +1599,19 @@ class dblayeroo
|
|||||||
|
|
||||||
/** Return the name of the display field, with $this->sep
|
/** Return the name of the display field, with $this->sep
|
||||||
* Add the table prefix/name if full is set
|
* Add the table prefix/name if full is set
|
||||||
* Allow name, $this->sep.$name.$this->sep, func(name),
|
* Allow :
|
||||||
* func($this->sep.$name.$this->sep),
|
* name,
|
||||||
* func($this->sep.$this->tableprefix.$this->table.$this->sep.".".
|
* DISTINCT name,
|
||||||
* $this->sep.$name.$this->sep)
|
* $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 string $name The name of the field
|
||||||
* @param boolean|null $full Add the table prefix/name if set
|
* @param boolean|null $full Add the table prefix/name if set
|
||||||
*/
|
*/
|
||||||
@@ -1597,6 +1624,12 @@ class dblayeroo
|
|||||||
$func = strtoupper (trim (substr ($name, 0, $pos)));
|
$func = strtoupper (trim (substr ($name, 0, $pos)));
|
||||||
$name = trim (substr ($name, $pos+1, -1));
|
$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)
|
if ($name[0] !== $this->sep)
|
||||||
$name = $this->sep.$name;
|
$name = $this->sep.$name;
|
||||||
if (! isset ($func) && substr ($name, -1) !== $this->sep)
|
if (! isset ($func) && substr ($name, -1) !== $this->sep)
|
||||||
@@ -1604,12 +1637,14 @@ class dblayeroo
|
|||||||
if ($full !== false)
|
if ($full !== false)
|
||||||
$name = $this->sep.$this->tableprefix.$this->table.$this->sep.".".$name;
|
$name = $this->sep.$this->tableprefix.$this->table.$this->sep.".".$name;
|
||||||
if (isset ($func))
|
if (isset ($func))
|
||||||
$name = "$func($name)";
|
$name = "$func($distinct$name)";
|
||||||
|
else
|
||||||
|
$name = "$distinct$name";
|
||||||
return $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.
|
* is set, add the table prefix/name to the result.
|
||||||
* If the join object is set, ask to it the columns too
|
* If the join object is set, ask to it the columns too
|
||||||
* @param boolean $full Add the table prefix/name if set
|
* @param boolean $full Add the table prefix/name if set
|
||||||
|
|||||||
Reference in New Issue
Block a user