From d3f9aaeaa27e0b1e27860850e6d55c2705bd448b Mon Sep 17 00:00:00 2001 From: Dominique Fournier Date: Fri, 22 Mar 2019 13:08:13 +0000 Subject: [PATCH] dblayeroo : Clean the columns name if there is only one table displayed, even if join is used git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@5162 bf3deb0d-5f1a-0410-827f-c0cc1f45334c --- dblayeroo.php | 109 +++++++++++++++++++++++++++++++++----------------- 1 file changed, 73 insertions(+), 36 deletions(-) diff --git a/dblayeroo.php b/dblayeroo.php index e4d2937..c4cbf1e 100644 --- a/dblayeroo.php +++ b/dblayeroo.php @@ -2011,6 +2011,42 @@ class dblayeroo } /* }}} */ + /** Convert a CONCAT('text',field) to CONCAT('text',`table`.`field`) or + * generate an exception if the field is not available in table + * If the provided string is not a concat, return the original one + * @param string $concat The string to examine + * @param object $object The dblayer object to use + */ + private function concat ($concat, $object) + // {{{ + { + if (substr ($concat, 0, 7) !== "CONCAT(") + return $concat; + if (substr ($concat, -1) !== ")") + $this->DBException ("CONCAT without ending parenthesis"); + $tmp = substr ($concat, 7, -1); + $new = "CONCAT("; + foreach (explode (",", $tmp) as $part) + { + if ($new !== "CONCAT(") + $new .= ","; + if (substr ($part, 0, 1) === "'" && + substr ($part, -1) === "'") + $new .= $part; + elseif (! array_key_exists ($part, $object->fields)) + $this->DBException (sprintf ( + "Invalid field to join in CONCAT : ". + "'%s' not defined in Distant table", $part)); + else + $new .= + $this->sep.$object->tableprefix.$object->table.$this->sep.".". + $this->sep.$part.$this->sep; + } + $new .= ")"; + return $new; + } + // }}} + /** Do the real join * @param string $joinType The join type to use ("INNER", "LEFT", "RIGHT") * @param object $object The dblayeroo object to use for searching the join @@ -2040,45 +2076,30 @@ class dblayeroo $this->DBException ("Invalid joinArray provided (not array)"); if (empty ($joinArray)) $this->DBException ("Invalid joinArray provided (empty array)"); + $newJoinArray = array (); foreach ($joinArray as $fieldLocal=>$fieldToJoin) { - if (! array_key_exists ($fieldLocal, $this->fields)) + if (substr ($fieldToJoin, 0, 7) === "CONCAT(") + $fieldLocal = $this->concat ($fieldToJoin, $object); + elseif (array_key_exists ($fieldToJoin, $object->fields)) + $fieldLocal = + $this->sep.$object->tableprefix.$object->table.$this->sep.".". + $this->sep.$fieldToJoin.$this->sep; + else $this->DBException (sprintf ( "Invalid field to join '%s' : not defined in Local table", $fieldLocal)); if (substr ($fieldToJoin, 0, 7) === "CONCAT(") - { - if (substr ($fieldToJoin, -1) !== ")") - $this->DBException ("CONCAT without ending parenthesis"); - $tmp = substr ($fieldToJoin, 7, -1); - $new = "CONCAT("; - foreach (explode (",", $tmp) as $part) - { - if ($new !== "CONCAT(") - $new .= ","; - if (substr ($part, 0, 1) === "'" && - substr ($part, -1) === "'") - $new .= $part; - elseif (! array_key_exists ($part, $object->fields)) - $this->DBException (sprintf ( - "Invalid field to join in CONCAT : ". - "'%s' not defined in Distant table", $part)); - else - $new .= - $this->sep.$object->tableprefix.$object->table.$this->sep.".". - $this->sep.$part.$this->sep; - } - $new .= ")"; - $joinArray[$fieldLocal] = $new; - } - elseif (! array_key_exists ($fieldToJoin, $object->fields)) + $fieldToJoin = $this->concat ($fieldToJoin, $object); + elseif (array_key_exists ($fieldToJoin, $object->fields)) + $fieldToJoin = + $this->sep.$object->tableprefix.$object->table.$this->sep.".". + $this->sep.$fieldToJoin.$this->sep; + else $this->DBException (sprintf ( "Invalid field to join '%s' : not defined in Distant table", $fieldToJoin)); - else - $joinArray[$fieldLocal] = - $this->sep.$object->tableprefix.$object->table.$this->sep.".". - $this->sep.$fieldToJoin.$this->sep; + $newJoinArray[$fieldLocal] = $fieldToJoin; } if (! isset ($object->table) || $object->table === null || trim ($object->table) === "") @@ -2090,14 +2111,11 @@ class dblayeroo $this->DBException ("No tableprefix defined in the Join object"); $this->joinObject[] = $object; $tmp = ""; - foreach ($joinArray as $fieldLocal=>$fieldToJoin) + foreach ($newJoinArray as $fieldLocal=>$fieldToJoin) { if ($tmp !== "") $tmp .= " AND "; - $tmp .= - $this->sep.$this->tableprefix.$this->table.$this->sep.".". - $this->sep.$fieldLocal.$this->sep. - "=$fieldToJoin"; + $tmp .= "$fieldLocal=$fieldToJoin"; } // Correct the displayQuery in the main display fields with the display // fields of object @@ -3219,8 +3237,27 @@ class dblayeroo // FETCH_ASSOC doesn't work in empty left join (return NULL instead of // the filled value) $fieldsAll = $this->fieldsAll (false); - if ($this->joinObject === null && count ($this->displayGet (false))) + // If the displayed fields are all in the same table, remove the table + // name in the columns + $cleanable = true; + $cleanTable = ""; + foreach ($this->displayGet (true) as $display) { + preg_match_all ("#".$this->sep."(.+)".$this->sep."\.#", $display, + $matches); + if ($cleanTable === "") + $cleanTable = $matches[1][0]; + elseif ($cleanTable !== $matches[1][0]) + { + $cleanable = false; + break; + } + } + //if ($this->joinObject === null && count ($this->displayGet (false))) + if ($cleanable) + { + // Force the fields to local ones as there is no use of external fields + $fieldsAll = $this->fields; // Remove the table name as there is no collisions risk // In case of Alias, remove the $this->sep too $columns = array ();