From 56cbeaa891eb54fdd47c05ffc635c42eceb3fe43 Mon Sep 17 00:00:00 2001 From: Dominique Fournier Date: Mon, 10 Apr 2017 14:33:38 +0000 Subject: [PATCH] dblayeroo : manage correctely the columns names in joins git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@3501 bf3deb0d-5f1a-0410-827f-c0cc1f45334c --- dblayeroo.php | 146 ++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 129 insertions(+), 17 deletions(-) diff --git a/dblayeroo.php b/dblayeroo.php index 7b1cf8e..55c3784 100644 --- a/dblayeroo.php +++ b/dblayeroo.php @@ -57,6 +57,7 @@ class dblayeroo */ public function __construct ($dsn, $username=null, $password=null, $driver_options=null) + /* {{{ */ { $driver = @explode (":", $dsn); if (! isset ($driver[0])) @@ -155,39 +156,49 @@ class dblayeroo } return self::$instance[$this->dsn]; } + /* }}} */ /** This function disconnect the database. It is normally only used in phpunit * unit tests */ public function disconnect () + /* {{{ */ { unset (self::$instance[$this->dsn]); } + /* }}} */ /** Start a new Transaction */ public function beginTransaction () + /* {{{ */ { return self::$instance[$this->dsn]->beginTransaction (); } + /* }}} */ /** Commit (validate) a transaction */ public function commit () + /* {{{ */ { return self::$instance[$this->dsn]->commit (); } + /* }}} */ /** RollBack a transaction */ public function rollback () + /* {{{ */ { return self::$instance[$this->dsn]->rollback (); } + /* }}} */ /** Return the connected database name from DSN used to connect */ public function databasename () + /* {{{ */ { if ($this->sep === "") $this->DBException (dgettext ("domframework", "Database not connected")); @@ -202,10 +213,12 @@ class dblayeroo return $dsnExplode["dbname"]; return NULL; } + /* }}} */ /** Return all the tables available in the database */ public function listTables () + /* {{{ */ { if ($this->sep === "") $this->DBException (dgettext ("domframework", "Database not connected")); @@ -245,6 +258,7 @@ class dblayeroo } return $res; } + /* }}} */ /** Create the table defined by the differents fields. * Define the SQL syntax based on SQL engines @@ -263,6 +277,7 @@ class dblayeroo * $foreign = array ("zone"=>"table.field",...); */ public function createTable () + /* {{{ */ { $this->debugLog ("Entering createTable"); if ($this->sep === "") @@ -544,10 +559,12 @@ class dblayeroo $this->debugLog ($sql); return self::$instance[$this->dsn]->exec ($sql); } + /* }}} */ /** Drop the table */ public function dropTable () + /* {{{ */ { $this->debugLog ("Entering dropTable ()"); if ($this->sep === "") @@ -559,11 +576,13 @@ class dblayeroo $this->debugLog ($sql); return self::$instance[$this->dsn]->exec ($sql); } + /* }}} */ /** Get the informations about a table * @param string $tableName The table to examine */ public function getTableSchema ($tableName) + /* {{{ */ { $this->debugLog ("Entering getTableSchema (",$tableName,")"); switch (self::$instance[$this->dsn]->getAttribute(\PDO::ATTR_DRIVER_NAME)) @@ -817,11 +836,13 @@ class dblayeroo "PDO Engine not supported in dbLayeroo"), 500); } } + /* }}} */ /** Return the type of the provided field * @param string $field The field to get the type */ private function fieldTypeComplete ($field) + /* {{{ */ { // $this->debugLog ("Entering fieldTypeComplete (",$field,")"); if (! array_key_exists ($field, $this->fields)) @@ -840,22 +861,26 @@ class dblayeroo $type = str_replace (" ", "", $type); return $type; } + /* }}} */ /** Return the type of the provided field. For varchar(255), return only * varchar * @param string $field The field to get the type */ private function fieldTypeLight ($field) + /* {{{ */ { $type = $this->fieldTypeComplete ($field); list ($type, ) = explode ("(", $type); return $type; } + /* }}} */ /** Return the length of a field (generally a varchar) * @param string $field The field to get the type */ private function fieldLength ($field) + /* {{{ */ { $type = $this->fieldTypeComplete ($field); $pos = strpos ($type, "("); @@ -868,6 +893,7 @@ class dblayeroo "fieldLength : Length equal to Zero for field '%s'", $field)); return $length; } + /* }}} */ ///////////////////////////// /// GETTERS / SETTERS /// @@ -876,6 +902,7 @@ class dblayeroo * @param string|null $table The table to use */ public function table ($table=null) + /* {{{ */ { $this->debugLog ("Entering table (",$table,")"); if ($table === null) @@ -887,11 +914,13 @@ class dblayeroo $this->table = $table; return $this; } + /* }}} */ /** Get/Set the tableprefix property * @param string|null $tableprefix The prefix to append */ public function tableprefix ($tableprefix=null) + /* {{{ */ { $this->debugLog ("Entering tableprefix (",$tableprefix,")"); if ($tableprefix === null) @@ -904,6 +933,7 @@ class dblayeroo $this->tableprefix = $tableprefix; return $this; } + /* }}} */ /** Get/Set the fields property * The fields to define are in the format: @@ -911,6 +941,7 @@ class dblayeroo * @param array|null $fields The fields to define */ public function fields ($fields=null) + /* {{{ */ { $this->debugLog ("Entering fields (VALUE)"); if ($fields === null) @@ -943,11 +974,13 @@ class dblayeroo $this->fields = $fields; return $this; } + /* }}} */ /** Get/Set the primary property * @param string|null $primary The primary key to use */ public function primary ($primary=null) + /* {{{ */ { $this->debugLog ("Entering primary (",$primary,")"); if ($primary === null) @@ -961,11 +994,13 @@ class dblayeroo $this->primary = $primary; return $this; } + /* }}} */ /** Get/Set the unique property * @param array|null $unique The unique fields constraint to add */ public function unique ($unique=null) + /* {{{ */ { $this->debugLog ("Entering unique (VALUE)"); if ($unique === null) @@ -1000,11 +1035,13 @@ class dblayeroo $this->unique = $unique; return $this; } + /* }}} */ /** Get/Set the foreign property * @param array|null $foreign The definition of the foreign constraint */ public function foreign ($foreign=null) + /* {{{ */ { $this->debugLog ("Entering foreign (VALUE)"); if ($foreign === null) @@ -1051,11 +1088,13 @@ class dblayeroo $this->foreign = $foreign; return $this; } + /* }}} */ /** Get/Set the debug property * @param integer|null $debug Set the debug value */ public function debug ($debug=null) + /* {{{ */ { $this->debugLog ("Entering debug (",$debug,")"); if ($debug === null) @@ -1065,11 +1104,13 @@ class dblayeroo $this->debug = $debug; return $this; } + /* }}} */ /** Get/Set the dsn property * @param string|null $dsn Set the DSN property of PDO */ public function dsn ($dsn=null) + /* {{{ */ { $this->debugLog ("Entering dsn (",$dsn,")"); if ($dsn === null) @@ -1079,11 +1120,13 @@ class dblayeroo $this->dsn = $dsn; return $this; } + /* }}} */ /** Get/Set the titles property * @param array|null $titles The titles of the fields */ public function titles ($titles=null) + /* {{{ */ { $this->debugLog ("Entering titles (VALUE)"); if ($titles === null) @@ -1105,6 +1148,7 @@ class dblayeroo $this->titles = $titles; return $this; } + /* }}} */ ///////////////////////////////////// /// MANAGE THE REQUEST BY OOP /// @@ -1152,6 +1196,7 @@ class dblayeroo /** Reinit the SQL request */ public function clearRequest () + /* {{{ */ { $this->debugLog ("Entering clearRequest ()"); $this->command = ""; @@ -1167,12 +1212,14 @@ class dblayeroo $this->setForeignObj = array (); unset ($this->fieldsTmp); } + /* }}} */ /** Define a new foreign object * @param object $object The dblayeroo object to use for foreign constraint * checks */ public function setForeignObj ($object) + /* {{{ */ { $this->debugLog ("Entering setForeignObj (OBJECT)"); if (! is_object ($object)) @@ -1185,12 +1232,14 @@ class dblayeroo "Invalid object provided to setForeignObj (no table defined)"); $this->setForeignObj[$object->tableprefix.$object->table] = $object; } + /* }}} */ /** Define the command to execute. Can be * "SELECT", "INSERT", "DELETE", "UPDATE". * @param string $command The command to execute */ public function command ($command) + /* {{{ */ { $this->debugLog ("Entering command (",$command,")"); $allowed = array ("SELECT", "INSERT", "DELETE", "UPDATE"); @@ -1202,52 +1251,64 @@ class dblayeroo $this->command = $command; return $this; } + /* }}} */ /** Alias of command ("SELECT") */ public function select () + /* {{{ */ { $this->command = "SELECT"; return $this; } + /* }}} */ /** Alias of command ("INSERT") */ public function insert () + /* {{{ */ { $this->command = "INSERT"; return $this; } + /* }}} */ /** Alias of command ("DELETE") */ public function delete () + /* {{{ */ { $this->command = "DELETE"; return $this; } + /* }}} */ /** Alias of command ("UPDATE") */ public function update () + /* {{{ */ { $this->command = "UPDATE"; return $this; } + /* }}} */ /** Set the DISTINCT option */ public function setDistinct () + /* {{{ */ { $this->distinct = "DISTINCT"; return $this; } + /* }}} */ /** Set the columns to display for the next SELECT request * @param array|string $columnNames The columns name, separated by comma * By default, display all the columns */ public function displayColumn ($columnNames) + /* {{{ */ { $this->debugLog ("Entering displayColumn (",$columnNames,")"); if (! is_string ($columnNames) && ! is_array ($columnNames)) @@ -1266,6 +1327,7 @@ class dblayeroo } return $this; } + /* }}} */ /** Do a inner join between two dblayer objects * The join array is a associated array with local field as key and distant @@ -1275,10 +1337,12 @@ class dblayeroo * @param array $joinArray The values to search for join */ public function joinInner ($object, $joinArray) + /* {{{ */ { $this->debugLog ("Entering joinInner (OBJECT, JOINARRAY)"); return $this->joinReal ("INNER", $object, $joinArray); } + /* }}} */ /** Do a left join between two dblayer objects * The join array is a associated array with local field as key and distant @@ -1288,10 +1352,12 @@ class dblayeroo * @param array $joinArray The values to search for join */ public function joinLeft ($object, $joinArray) + /* {{{ */ { $this->debugLog ("Entering joinLeft (OBJECT, JOINARRAY)"); return $this->joinReal ("LEFT", $object, $joinArray); } + /* }}} */ /** Do a right join between two dblayer objects * The join array is a associated array with local field as key and distant @@ -1301,10 +1367,12 @@ class dblayeroo * @param array $joinArray The values to search for join */ public function joinRight ($object, $joinArray) + /* {{{ */ { $this->debugLog ("Entering joinRight (OBJECT, JOINARRAY)"); return $this->joinReal ("RIGHT", $object, $joinArray); } + /* }}} */ /** Do the real join * @param string $joinType The join type to use ("INNER", "LEFT", "RIGHT") @@ -1313,6 +1381,7 @@ class dblayeroo * @param array $joinArray The values to search for join */ private function joinReal ($joinType, $object, $joinArray) + /* {{{ */ { $this->debugLog ("Entering joinReal (",$joinType,", OBJECT, JOINARRAY)"); if (! is_string ($joinType)) @@ -1396,6 +1465,7 @@ class dblayeroo $this->fieldsTmp = array_merge ($this->fieldsTmp, $tmp); return $this; } + /* }}} */ /** Set a new WHERE expression value * @param string $field The field to check @@ -1404,6 +1474,7 @@ class dblayeroo * @param string|null $value The value to search ("" if not provided) */ public function whereAdd ($field, $operator, $value = "") + /* {{{ */ { $this->debugLog ("Entering whereAdd (",$field,", ",$operator,", ",$value, ")"); @@ -1445,10 +1516,12 @@ class dblayeroo "type"=>$this->fieldTypeLight ($field)); return $this; } + /* }}} */ /** Add a new AND to the WHERE expression */ public function whereAddAND () + /* {{{ */ { $this->debugLog ("Entering whereAddAND ()"); if (count ($this->whereExpression) === 0) @@ -1456,10 +1529,12 @@ class dblayeroo $this->whereExpression[] = "AND"; return $this; } + /* }}} */ /** Add a new OR to the WHERE expression */ public function whereAddOR () + /* {{{ */ { $this->debugLog ("Entering whereAddOR ()"); if (count ($this->whereExpression) === 0) @@ -1467,30 +1542,36 @@ class dblayeroo $this->whereExpression[] = "OR"; return $this; } + /* }}} */ /** Add a new Open Parenthesis to the WHERE expression */ public function whereAddParenthesisOpen () + /* {{{ */ { $this->debugLog ("Entering whereAddParenthesisOpen ()"); $this->whereExpression[] = "("; return $this; } + /* }}} */ /** Add a new Close Parenthesis to the WHERE expression */ public function whereAddParenthesisClose () + /* {{{ */ { $this->debugLog ("Entering whereAddParenthesisClose ()"); $this->whereExpression[] = ")"; return $this; } + /* }}} */ /** Add a new ORDER sort * @param string $field The field to sort * @param string|null $sort The sort order ("ASC", "DESC"); */ public function orderAdd ($field, $sort = "ASC") + /* {{{ */ { $this->debugLog ("Entering orderAdd (",$field,", ",$sort,")"); if (! is_string ($field)) @@ -1506,6 +1587,7 @@ class dblayeroo $this->orderExpression[] = $this->sep.$field.$this->sep." ".$sort; return $this; } + /* }}} */ /** Define a LIMIT for the request. * To use only the nbLines, put a 0 on startLine @@ -1513,6 +1595,7 @@ class dblayeroo * @param integer $nbLines The number of lines to return */ public function limit ($startLine, $nbLines) + /* {{{ */ { $this->debugLog ("Entering limit (",$startLine,", ",$nbLines,")"); if (! preg_match ("/^\d+$/", $startLine)) @@ -1529,11 +1612,13 @@ class dblayeroo $this->limitExpression = "$nbLines"; return $this; } + /* }}} */ /** Define a LIMIT for the request. * @param integer $nbLines The number of lines to return */ public function limitLines ($nbLines) + /* {{{ */ { $this->debugLog ("Entering limitLines (",$nbLines,")"); if (! preg_match ("/^\d+$/", $nbLines)) @@ -1543,6 +1628,7 @@ class dblayeroo $this->limitExpression = "$nbLines"; return $this; } + /* }}} */ /** Set INSERT/UPDATE values * - The provided array must be an associative array, the field name must be @@ -1550,6 +1636,7 @@ class dblayeroo * @param array $values The values to INSERT or UPDATE */ public function setValues ($values) + /* {{{ */ { $this->debugLog ("Entering setValues (",$values,")"); if (! is_array ($values)) @@ -1571,10 +1658,12 @@ class dblayeroo $this->setType = $tmpType; return $this; } + /* }}} */ /** Create the SQL request */ private function createRequest () + /* {{{ */ { if ($this->sep === "") $this->DBException (dgettext ("domframework", "Database not connected")); @@ -1599,9 +1688,7 @@ class dblayeroo if (isset ($this->fieldsTmp)) $displayColumns = implode (",", array_keys ($this->fieldsTmp)); else - $displayColumns = $this->sep. - implode ($this->sep.",".$this->sep, array_keys ($this->fields)). - $this->sep; + $displayColumns = "*"; } $sql .= " $displayColumns FROM $this->sep$this->tableprefix". "$this->table$this->sep"; @@ -1679,6 +1766,7 @@ class dblayeroo } return $sql; } + /* }}} */ /** Prepare the request with the associated entries. * If textForm is true, return a string to display what will be done @@ -1688,6 +1776,7 @@ class dblayeroo * really the request */ private function prepareRequest ($sql, $textForm) + /* {{{ */ { $text = ""; if (!$textForm) @@ -1765,10 +1854,12 @@ class dblayeroo else return $st; } + /* }}} */ /** Return the query that will be executed */ public function getDisplayQuery () + /* {{{ */ { $text = ""; $sql = $this->createRequest (); @@ -1778,6 +1869,7 @@ class dblayeroo $text .= "\n$prep"; return $text; } + /* }}} */ /** Check the provided values which will be inserted or updated against the * database structure. @@ -1787,6 +1879,7 @@ class dblayeroo * @return array The errors found by field */ public function verify ($values, $update = false) + /* {{{ */ { $update = !! $update; $errors = array (); @@ -1959,7 +2052,7 @@ class dblayeroo // overwrite with the same value) // If it is not the same, produce an error $objTmp->whereAdd ($this->primary, "!=", - $resUpdate[0][$this->table.".".$this->primary]); + $resUpdate[0][$this->primary]); } if (count ($errors) == 0 && count ($objTmp->execute ())) $this->DBException (dgettext ("domframework", @@ -1991,11 +2084,13 @@ class dblayeroo } return $errors; } + /* }}} */ /** Check the values before doing really the modification of the database * @param boolean|null $update if true UPDATE request, else INSERT request */ public function checkValues ($update = false) + /* {{{ */ { $this->debugLog ("Entering checkValues (",$update,")"); $update = !! $update; @@ -2005,6 +2100,7 @@ class dblayeroo $this->DBException (reset ($errors)); $this->debugLog ("End of checkValues (",$update,") : Nothing in error"); } + /* }}} */ /** Execute the pre-defined query * Return the content array if SELECT command is choosed @@ -2012,6 +2108,7 @@ class dblayeroo * Return the number of modified lines for UPDATE/DELETE command */ public function execute () + /* {{{ */ { $this->debugLog ("Entering execute ()"); switch ($this->command) @@ -2043,12 +2140,30 @@ class dblayeroo // name to the value by index. // FETCH_ASSOC doesn't work in empty left join (return NULL instead of // the filled value) - if (isset ($this->fieldsTmp)) + if (count ($this->displayColumn)) + { + if (isset ($this->fieldsTmp)) + { + $columns = $this->displayColumn; + } + else + { + // Remove the columns names as there is no collisions risk + $columns = array (); + foreach ($this->displayColumn as $col) + { + $columns[] = substr ($col, 1 + strrpos ($col, $this->sep, -2), -1); + } + } + } + elseif (isset ($this->fieldsTmp)) + { $columns = array_keys ($this->fieldsTmp); - elseif (count ($this->displayColumn)) - $columns = $this->displayColumn; + } else + { $columns = array_keys ($this->fields); + } foreach ($result as $rownb=>$row) { foreach ($row as $colNb=>$val) @@ -2060,17 +2175,9 @@ class dblayeroo "integer") $val = intval ($val); } - elseif (count ($this->displayColumn)) + elseif (isset ($this->fields)) { - $fieldWithoutTable = substr ($columns[$colNb], - strlen ($this->sep.$this->table.$this->sep.".".$this->sep), -1); - if (strtolower ($this->fields[$fieldWithoutTable][0]) === "integer") - $val = intval ($val); - } - else - { - if (strtolower ($this->fields[$columns[$colNb]][0]) === - "integer") + if (strtolower ($this->fields[$columns[$colNb]][0]) === "integer") $val = intval ($val); } $colName = str_replace ($this->sep, "", $columns[$colNb]); @@ -2106,11 +2213,13 @@ class dblayeroo $this->DBException ("execute : command not defined : no RC"); } } + /* }}} */ /** Error management * @param string $message The message to throw in the exception */ public function DBException ($message) + /* {{{ */ { $backtrace = debug_backtrace (); if (! array_key_exists (1, $backtrace)) @@ -2125,11 +2234,13 @@ class dblayeroo } throw new \Exception ($message, 500); } + /* }}} */ /** Debug function * @param mixed ...$message The message to display in debug */ private function debugLog ($message) + /* {{{ */ { if ((!!$this->debug) === false) return; @@ -2151,4 +2262,5 @@ class dblayeroo } echo "\n"; } + /* }}} */ }