From 39d34965c08068ca3af0128ca84e2920a29600ff Mon Sep 17 00:00:00 2001 From: Dominique Fournier Date: Tue, 25 Apr 2017 14:43:53 +0000 Subject: [PATCH] * dblayeroo: allow to sort the ORDER statements (the first added is the first use) * dblayeroo: allow to sort the Display statements (the first added is the first use) * dblayeroo: change displayColumns to displayAdd git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@3541 bf3deb0d-5f1a-0410-827f-c0cc1f45334c --- Tests/dblayerooComplet.php | 79 ++++++++++++++++++++++++++------------ dblayeroo.php | 69 ++++++++++++++++++++++++--------- 2 files changed, 104 insertions(+), 44 deletions(-) diff --git a/Tests/dblayerooComplet.php b/Tests/dblayerooComplet.php index d81ff30..7038164 100644 --- a/Tests/dblayerooComplet.php +++ b/Tests/dblayerooComplet.php @@ -542,9 +542,9 @@ class test_dblayeroo_{ENGINE} extends PHPUnit_Framework_TestCase // Filter on the db1, do not display db2 $db1 = $this->db1 (); $db2 = $this->db2 (); - $db2->displayColumn (); + $db2->displayAdd (); $res = $db1->select () - ->displayColumn ("group") + ->displayAdd ("group") ->joinLeft ($db2, array ("group"=>"group")) ->execute (); $db1->disconnect (); @@ -573,33 +573,33 @@ class test_dblayeroo_{ENGINE} extends PHPUnit_Framework_TestCase // Filter on the db1, display one column in db2 $db1 = $this->db1 (); $db2 = $this->db2 (); - $db2->displayColumn ("group"); + $db2->displayAdd ("group"); $res = $db1->select () - ->displayColumn ("group") + ->displayAdd ("group") ->joinLeft ($db2, array ("group"=>"group")) ->execute (); $db1->disconnect (); $db2->disconnect (); $this->assertSame (array ( array ( - 'groupedoo.group' => 'group1', 'usersoo.group' => 'group1', + 'groupedoo.group' => 'group1', ), array ( - 'groupedoo.group' => 'group1', 'usersoo.group' => 'group1', + 'groupedoo.group' => 'group1', ), array ( - 'groupedoo.group' => 'group1', 'usersoo.group' => 'group1', + 'groupedoo.group' => 'group1', ), array ( - 'groupedoo.group' => 'group1', 'usersoo.group' => 'group1', + 'groupedoo.group' => 'group1', ), array ( - 'groupedoo.group' => 'group2', 'usersoo.group' => NULL, + 'groupedoo.group' => 'group2', ), ), $res); } @@ -609,9 +609,9 @@ class test_dblayeroo_{ENGINE} extends PHPUnit_Framework_TestCase // Filter on the db1 and add order in full mode $db1 = $this->db1 (); $db2 = $this->db2 (); - $db2->displayColumn ("group"); + $db2->displayAdd ("group"); $res = $db1->select () - ->displayColumn ("group") + ->displayAdd ("group") ->joinLeft ($db2, array ("group"=>"group")) ->orderAdd ("group", "DESC") ->execute (); @@ -619,24 +619,24 @@ class test_dblayeroo_{ENGINE} extends PHPUnit_Framework_TestCase $db2->disconnect (); $this->assertSame (array ( array ( - 'groupedoo.group' => 'group2', 'usersoo.group' => NULL, + 'groupedoo.group' => 'group2', ), array ( - 'groupedoo.group' => 'group1', 'usersoo.group' => 'group1', + 'groupedoo.group' => 'group1', ), array ( - 'groupedoo.group' => 'group1', 'usersoo.group' => 'group1', + 'groupedoo.group' => 'group1', ), array ( - 'groupedoo.group' => 'group1', 'usersoo.group' => 'group1', + 'groupedoo.group' => 'group1', ), array ( - 'groupedoo.group' => 'group1', 'usersoo.group' => 'group1', + 'groupedoo.group' => 'group1', ), ), $res); } @@ -660,18 +660,18 @@ class test_dblayeroo_{ENGINE} extends PHPUnit_Framework_TestCase { // Two joins tables in left join $db1 = $this->db1 (); // Do not display anything from groupedoo - $db1->displayColumn ("group"); + $db1->displayAdd ("group") + ->orderAdd ("group", "DESC"); $db2 = $this->db2 (); // Display the gecos and group from usersoo - $db2->displayColumn ("gecos") + $db2->displayAdd ("gecos") ->orderAdd ("gecos", "ASC"); $db4 = $this->db4 (); // Display the name in rightsoo - $db4->displayColumn ("name"); + $db4->displayAdd ("name"); $db1->joinLeft ($db4, array ("group"=>"group")); $res = $db1->select () ->joinLeft ($db2, array ("group"=>"group")) - ->orderAdd ("group", "DESC") ->execute (); $db1->disconnect (); $db2->disconnect (); @@ -679,29 +679,58 @@ class test_dblayeroo_{ENGINE} extends PHPUnit_Framework_TestCase $this->assertSame (array ( array ( 'groupedoo.group' => 'group2', - 'rightsoo.name' => NULL, 'usersoo.gecos' => NULL, + 'rightsoo.name' => NULL, ), array ( 'groupedoo.group' => 'group1', - 'rightsoo.name' => 'RO', 'usersoo.gecos' => 'name', + 'rightsoo.name' => 'RO', ), array ( 'groupedoo.group' => 'group1', - 'rightsoo.name' => 'RO', 'usersoo.gecos' => 'name2', + 'rightsoo.name' => 'RO', ), array ( 'groupedoo.group' => 'group1', - 'rightsoo.name' => 'RO', 'usersoo.gecos' => 'name3', + 'rightsoo.name' => 'RO', ), array ( 'groupedoo.group' => 'group1', - 'rightsoo.name' => 'RO', 'usersoo.gecos' => 'name4', + 'rightsoo.name' => 'RO', ), ), $res); } + + public function test_sortOrder1 () + { + $db1 = $this->db1 (); + $res = $db1->getSortOrder (); + $db1->disconnect (); + $this->assertSame ("order1", $res); + } + + public function test_sortOrder2 () + { + $db1 = $this->db1 (); + $db1->getSortOrder (); + $res = $db1->getSortOrder (); + $db1->disconnect (); + $this->assertSame ("order2", $res); + } + + public function test_sortOrder3 () + { + $db1 = $this->db1 (); + $db2 = $this->db2 (); + $db1->getSortOrder (); + $db1->getSortOrder (); + $res = $db2->getSortOrder (); + $db1->disconnect (); + $db2->disconnect (); + $this->assertSame ("order3", $res); + } } diff --git a/dblayeroo.php b/dblayeroo.php index 48fcf71..2ee6cc9 100644 --- a/dblayeroo.php +++ b/dblayeroo.php @@ -165,6 +165,7 @@ class dblayeroo /* {{{ */ { unset (self::$instance[$this->dsn]); + self::$sortOrder = 0; } /* }}} */ @@ -1224,6 +1225,20 @@ class dblayeroo */ private $debugDepth = 1; + /** The sort order of select/order entries crossed the differents objects + */ + private static $sortOrder = 0; + + /** The method to get a new sorti order acrossed the differents objects + */ + public function getSortOrder () + /* {{{ */ + { + ++self::$sortOrder; + return "order".self::$sortOrder; + } + /* }}} */ + /** Reinit the SQL request */ public function clearRequest () @@ -1333,16 +1348,26 @@ class dblayeroo } /* }}} */ - /** Set the columns to display for the next SELECT request + /** Changing the name displayColumns to displayAdd + * @param array|string|null $columnNames The columns name, separated by comma + * @deprecated 0.36 + */ + public function displayColumns ($columnNames = array ()) + { + return $this->displayAdd ($columnNames); + } + + /** Set the columns to display for the next SELECT request. + * The columns are ordered by the first added to the last added * @param array|string|null $columnNames The columns name, separated by comma * By default, display all the columns if this method is not called * If the value is null or not provided or an empty array, do not display * any field */ - public function displayColumn ($columnNames = array ()) + public function displayAdd ($columnNames = array ()) /* {{{ */ { - $this->debugLog ("Entering displayColumn (",$columnNames,")"); + $this->debugLog ("Entering displayAdd (",$columnNames,")"); if (! is_string ($columnNames) && ! is_array ($columnNames)) $this->DBException ( "Invalid columnNames provided (not string and not array)"); @@ -1356,7 +1381,7 @@ class dblayeroo if (! array_key_exists ($name, $this->fields)) $this->DBException (sprintf ( "Invalid field to display '%s' : not defined in table", $name)); - $this->displayColumn[] = $this->sep.$name.$this->sep; + $this->displayColumn[$this->getSortOrder()] = $this->sep.$name.$this->sep; } return $this; } @@ -1385,7 +1410,7 @@ class dblayeroo { foreach (array_keys ($this->fields) as $name) { - $displayColumn[] = $this->sep.$name.$this->sep; + $displayColumn[$this->getSortOrder()] = $this->sep.$name.$this->sep; } } else @@ -1399,7 +1424,7 @@ class dblayeroo { foreach (array_keys ($this->fields) as $name) { - $displayColumn[] = $this->sep.$name.$this->sep; + $displayColumn[$this->getSortOrder()] = $this->sep.$name.$this->sep; } } else @@ -1407,19 +1432,20 @@ class dblayeroo $displayColumn = $this->displayColumn; } } - foreach ($displayColumn as $d) + foreach ($displayColumn as $pos=>$d) { if ($full !== false) - $columns[] = $this->sep.$this->tableprefix.$this->table.$this->sep.".". - $d; + $columns[$pos] = $this->sep.$this->tableprefix.$this->table.$this->sep. + ".".$d; else - $columns[] = $d; + $columns[$pos] = $d; } if ($this->joinObject) { foreach ($this->joinObject as $obj) $columns = array_merge ($columns, $obj->displayGet (true)); } + ksort ($columns, SORT_NATURAL); return $columns; } /* }}} */ @@ -1667,7 +1693,8 @@ class dblayeroo } /* }}} */ - /** Add a new ORDER sort + /** Add a new ORDER sort. The multiple ORDERS are used from the first added to + * the last added * @param string $field The field to sort * @param string|null $sort The sort order ("ASC", "DESC"); */ @@ -1685,7 +1712,8 @@ class dblayeroo if (! array_key_exists ($field, $this->fields)) $this->DBException (sprintf ( "Invalid field to orderAdd '%s' : not defined in table", $field)); - $this->orderExpression[] = $this->sep.$field.$this->sep." ".$sort; + $this->orderExpression[$this->getSortOrder()] = + $this->sep.$field.$this->sep." ".$sort; return $this; } /* }}} */ @@ -1702,18 +1730,20 @@ class dblayeroo $order = array (); if ($this->joinObject) $full = true; - foreach ($this->orderExpression as $o) + foreach ($this->orderExpression as $pos=>$o) { if ($full !== false) - $order[] = $this->sep.$this->tableprefix.$this->table.$this->sep.".".$o; + $order[$pos] = $this->sep.$this->tableprefix.$this->table.$this->sep. + ".".$o; else - $order[] = $o; + $order[$pos] = $o; } if ($this->joinObject) { foreach ($this->joinObject as $obj) $order = array_merge ($order, $obj->orderGet (true)); } + ksort ($order, SORT_NATURAL); return $order; } /* }}} */ @@ -2118,8 +2148,8 @@ class dblayeroo $objTmp->debugDepth++; $objTmp->clearRequest (); $objTmp->Select (); - $objTmp->displayColumn ($this->primary); - $objTmp->displayColumn ($columns); + $objTmp->displayAdd ($this->primary); + $objTmp->displayAdd ($columns); $objTmp->whereValues = $this->whereValues; $objTmp->whereExpression = $this->whereExpression; $objTmp->limitLines (3); @@ -2141,7 +2171,7 @@ class dblayeroo $objTmp->debugDepth++; $objTmp->clearRequest (); $objTmp->Select (); - $objTmp->displayColumn ($this->primary); + $objTmp->displayAdd ($this->primary); if (is_array ($columns)) { // Multiple columns in unique @@ -2205,7 +2235,7 @@ class dblayeroo $objTmp->debugDepth++; $objTmp->clearRequest (); $objTmp->Select (); - $objTmp->displayColumn ($objTmp->primary); + $objTmp->displayAdd ($objTmp->primary); $objTmp->whereAdd ($params[1], "=", $this->setValues[$field]); if (count ($objTmp->execute ()) === 0) $errors[$field] = sprintf (dgettext ("domframework", @@ -2284,6 +2314,7 @@ class dblayeroo { $columns = $this->displayGet (true); } + $columns = array_values ($columns); foreach ($result as $rownb=>$row) { foreach ($row as $colNb=>$val)