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
This commit is contained in:
107
dblayeroo.php
107
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
|
/** Do the real join
|
||||||
* @param string $joinType The join type to use ("INNER", "LEFT", "RIGHT")
|
* @param string $joinType The join type to use ("INNER", "LEFT", "RIGHT")
|
||||||
* @param object $object The dblayeroo object to use for searching the join
|
* @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)");
|
$this->DBException ("Invalid joinArray provided (not array)");
|
||||||
if (empty ($joinArray))
|
if (empty ($joinArray))
|
||||||
$this->DBException ("Invalid joinArray provided (empty array)");
|
$this->DBException ("Invalid joinArray provided (empty array)");
|
||||||
|
$newJoinArray = array ();
|
||||||
foreach ($joinArray as $fieldLocal=>$fieldToJoin)
|
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 (
|
$this->DBException (sprintf (
|
||||||
"Invalid field to join '%s' : not defined in Local table",
|
"Invalid field to join '%s' : not defined in Local table",
|
||||||
$fieldLocal));
|
$fieldLocal));
|
||||||
if (substr ($fieldToJoin, 0, 7) === "CONCAT(")
|
if (substr ($fieldToJoin, 0, 7) === "CONCAT(")
|
||||||
{
|
$fieldToJoin = $this->concat ($fieldToJoin, $object);
|
||||||
if (substr ($fieldToJoin, -1) !== ")")
|
elseif (array_key_exists ($fieldToJoin, $object->fields))
|
||||||
$this->DBException ("CONCAT without ending parenthesis");
|
$fieldToJoin =
|
||||||
$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.$object->tableprefix.$object->table.$this->sep.".".
|
||||||
$this->sep.$part.$this->sep;
|
$this->sep.$fieldToJoin.$this->sep;
|
||||||
}
|
else
|
||||||
$new .= ")";
|
|
||||||
$joinArray[$fieldLocal] = $new;
|
|
||||||
}
|
|
||||||
elseif (! array_key_exists ($fieldToJoin, $object->fields))
|
|
||||||
$this->DBException (sprintf (
|
$this->DBException (sprintf (
|
||||||
"Invalid field to join '%s' : not defined in Distant table",
|
"Invalid field to join '%s' : not defined in Distant table",
|
||||||
$fieldToJoin));
|
$fieldToJoin));
|
||||||
else
|
$newJoinArray[$fieldLocal] = $fieldToJoin;
|
||||||
$joinArray[$fieldLocal] =
|
|
||||||
$this->sep.$object->tableprefix.$object->table.$this->sep.".".
|
|
||||||
$this->sep.$fieldToJoin.$this->sep;
|
|
||||||
}
|
}
|
||||||
if (! isset ($object->table) || $object->table === null ||
|
if (! isset ($object->table) || $object->table === null ||
|
||||||
trim ($object->table) === "")
|
trim ($object->table) === "")
|
||||||
@@ -2090,14 +2111,11 @@ class dblayeroo
|
|||||||
$this->DBException ("No tableprefix defined in the Join object");
|
$this->DBException ("No tableprefix defined in the Join object");
|
||||||
$this->joinObject[] = $object;
|
$this->joinObject[] = $object;
|
||||||
$tmp = "";
|
$tmp = "";
|
||||||
foreach ($joinArray as $fieldLocal=>$fieldToJoin)
|
foreach ($newJoinArray as $fieldLocal=>$fieldToJoin)
|
||||||
{
|
{
|
||||||
if ($tmp !== "")
|
if ($tmp !== "")
|
||||||
$tmp .= " AND ";
|
$tmp .= " AND ";
|
||||||
$tmp .=
|
$tmp .= "$fieldLocal=$fieldToJoin";
|
||||||
$this->sep.$this->tableprefix.$this->table.$this->sep.".".
|
|
||||||
$this->sep.$fieldLocal.$this->sep.
|
|
||||||
"=$fieldToJoin";
|
|
||||||
}
|
}
|
||||||
// Correct the displayQuery in the main display fields with the display
|
// Correct the displayQuery in the main display fields with the display
|
||||||
// fields of object
|
// fields of object
|
||||||
@@ -3219,8 +3237,27 @@ class dblayeroo
|
|||||||
// FETCH_ASSOC doesn't work in empty left join (return NULL instead of
|
// FETCH_ASSOC doesn't work in empty left join (return NULL instead of
|
||||||
// the filled value)
|
// the filled value)
|
||||||
$fieldsAll = $this->fieldsAll (false);
|
$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
|
// Remove the table name as there is no collisions risk
|
||||||
// In case of Alias, remove the $this->sep too
|
// In case of Alias, remove the $this->sep too
|
||||||
$columns = array ();
|
$columns = array ();
|
||||||
|
|||||||
Reference in New Issue
Block a user