dblayeroo : manage correctely the columns names in joins

git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@3501 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
This commit is contained in:
2017-04-10 14:33:38 +00:00
parent 0172aad420
commit 56cbeaa891

View File

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