BUG : dblayer : UPDATE can now update the table primary key too

dblayer : add support of the differents field separator (choosed by DB engine)
Add more unit tests


git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@1813 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
This commit is contained in:
2014-09-12 14:14:26 +00:00
parent b2264440a4
commit 2c21043a9e
2 changed files with 61 additions and 52 deletions

View File

@@ -25,6 +25,13 @@ class test_dblayer_{ENGINE} extends PHPUnit_Framework_TestCase
"driver_options" => null, "driver_options" => null,
"tableprefix" => "", "tableprefix" => "",
), ),
"pgsql" => array (
"dsn" => "pgsql:host=127.0.0.1;port=5432;dbname=dbname",
"username" => "root",
"password" => "root",
"driver_options" => null,
"tableprefix" => "",
),
); );
public function test_dropTable () public function test_dropTable ()

View File

@@ -59,6 +59,8 @@ class dblayer extends PDO
public $debug = FALSE; public $debug = FALSE;
/** The connecting DSN */ /** The connecting DSN */
private $dsn = null; private $dsn = null;
/** The field group delimiter */
private $sep = "";
// TODO !! // TODO !!
/** Create Table creation from $this->fields with engine abstraction /** Create Table creation from $this->fields with engine abstraction
@@ -114,14 +116,17 @@ class dblayer extends PDO
chmod ($file, 0666); chmod ($file, 0666);
// Force ForeignKeys support (disabled by default) // Force ForeignKeys support (disabled by default)
$this->db->exec("PRAGMA foreign_keys = ON"); $this->db->exec("PRAGMA foreign_keys = ON");
$this->sep = "`";
break; break;
case "mysql": case "mysql":
// Set the coding to UTF8 // Set the coding to UTF8
$this->db->exec("SET CHARACTER SET utf8"); $this->db->exec("SET CHARACTER SET utf8");
$this->sep = "`";
break; break;
case "pgsql": case "pgsql":
// Set the coding to UTF8 // Set the coding to UTF8
$this->db->exec("SET CHARACTER SET utf8"); $this->db->exec("SET NAMES 'utf8'");
$this->sep = "\"";
break; break;
} }
$this->dsn = $dsn; $this->dsn = $dsn;
@@ -327,7 +332,7 @@ class dblayer extends PDO
{ {
$table = $data[0]; $table = $data[0];
$column = $data[1]; $column = $data[1];
$req = "SELECT $column FROM `$this->tableprefix$table` ". $req = "SELECT $column FROM $this->sep$this->tableprefix$table$this->sep ".
"WHERE \"$column\"=:".md5 ($column); "WHERE \"$column\"=:".md5 ($column);
if ($this->debug) echo "DEBUG : $req\n"; if ($this->debug) echo "DEBUG : $req\n";
$st = $this->db->prepare ($req); $st = $this->db->prepare ($req);
@@ -361,8 +366,10 @@ class dblayer extends PDO
array_walk ($binds, function(&$value, $key) { array_walk ($binds, function(&$value, $key) {
$value = md5 ($value); $value = md5 ($value);
}); });
$req = "INSERT INTO `$this->tableprefix$this->table` "; $req = "INSERT INTO $this->sep$this->tableprefix$this->table$this->sep ";
$req .= "(`".implode ("`,`", array_keys ($datasOK))."`)"; $req .= "($this->sep".
implode ("$this->sep,$this->sep", array_keys ($datasOK)).
"$this->sep)";
$req .= " VALUES "; $req .= " VALUES ";
$req .= "(:".implode (",:", $binds).")"; $req .= "(:".implode (",:", $binds).")";
if ($this->debug) echo "DEBUG : $req\n"; if ($this->debug) echo "DEBUG : $req\n";
@@ -433,9 +440,10 @@ class dblayer extends PDO
$display = array_keys ($this->fields); $display = array_keys ($this->fields);
} }
$req = "SELECT `"; $req = "SELECT $this->sep";
$req .= implode ("`,`", $display); $req .= implode ("$this->sep,$this->sep", $display);
$req .= "` FROM `$this->tableprefix$this->table`"; $req .= "$this->sep ";
$req .= "FROM $this->sep$this->tableprefix$this->table$this->sep";
if ($select !== null) if ($select !== null)
{ {
$req .= " WHERE "; $req .= " WHERE ";
@@ -457,7 +465,7 @@ class dblayer extends PDO
// name is 'group' // name is 'group'
// Don't put single quotes : don't work with SQLite // Don't put single quotes : don't work with SQLite
// TODO : Test for PostgreSQL (Tested for SQLite and MySQL) // TODO : Test for PostgreSQL (Tested for SQLite and MySQL)
$req .= " `".$s[0]."` ".$s[2]." :".md5 ($s[0]); $req .= " $this->sep".$s[0]."$this->sep ".$s[2]." :".md5 ($s[0]);
} }
} }
@@ -661,8 +669,9 @@ class dblayer extends PDO
continue; continue;
$table = $data[0]; $table = $data[0];
$column = $data[1]; $column = $data[1];
$req = "SELECT `$column` FROM `$this->tableprefix$table` ". $req = "SELECT $this->sep$column$this->sep ".
"WHERE `$column`=:".md5 ($column); "FROM $this->sep$this->tableprefix$table$this->sep ".
"WHERE $this->sep$column$this->sep=:".md5 ($column);
if ($this->debug) echo "DEBUG : $req\n"; if ($this->debug) echo "DEBUG : $req\n";
$st = $this->db->prepare ($req); $st = $this->db->prepare ($req);
$val = $datasOK[$foreign]; $val = $datasOK[$foreign];
@@ -692,19 +701,24 @@ class dblayer extends PDO
$column), 405); $column), 405);
} }
$datasOK[$this->primary] = $updatekey; $req = "UPDATE $this->sep".$this->tableprefix."$this->table$this->sep SET ";
$req = "UPDATE `".$this->tableprefix."$this->table` SET ";
$i = 0; $i = 0;
foreach ($datasOK as $key=>$val) foreach ($datasOK as $key=>$val)
{ {
if ($i>0) $req .= ","; if ($i>0) $req .= ",";
$req .= "`$key`=:".md5 ($key); $req .= "$this->sep$key$this->sep=:".md5 ($key);
$i++; $i++;
} }
$req .= " WHERE `$this->primary`=:".md5 ($this->primary); $req .= " WHERE $this->sep$this->primary$this->sep=:".
md5 ("PRIMARY".$this->primary);
if ($this->debug) echo "DEBUG : $req\n"; if ($this->debug) echo "DEBUG : $req\n";
$st = $this->db->prepare ($req); $st = $this->db->prepare ($req);
// Add the primary key to field list temporaly. It will permit to update the
// primary key
$fields = $this->fields;
$datasOK["PRIMARY".$this->primary] = $updatekey;
$fields["PRIMARY".$this->primary] = $this->fields[$this->primary];
foreach ($datasOK as $key=>$val) foreach ($datasOK as $key=>$val)
{ {
if ($this->debug) echo "DEBUG BIND : $key(".md5 ($key).")->". if ($this->debug) echo "DEBUG BIND : $key(".md5 ($key).")->".
@@ -714,22 +728,22 @@ class dblayer extends PDO
if ($this->debug) echo "(null)\n"; if ($this->debug) echo "(null)\n";
$st->bindValue (":".md5 ($key), $val, PDO::PARAM_NULL); $st->bindValue (":".md5 ($key), $val, PDO::PARAM_NULL);
} }
elseif ($this->fields[$key][0] === "integer") elseif ($fields[$key][0] === "integer")
{ {
if ($this->debug) echo "(integer)\n"; if ($this->debug) echo "(integer)\n";
$st->bindValue (":".md5 ($key), $val, PDO::PARAM_INT); $st->bindValue (":".md5 ($key), $val, PDO::PARAM_INT);
} }
elseif ($this->fields[$key][0] === "varchar") elseif ($fields[$key][0] === "varchar")
{ {
if ($this->debug) echo "(varchar)\n"; if ($this->debug) echo "(varchar)\n";
$st->bindValue (":".md5 ($key), $val, PDO::PARAM_STR); $st->bindValue (":".md5 ($key), "$val", PDO::PARAM_STR);
} }
elseif ($this->fields[$key][0] === "datetime") elseif ($fields[$key][0] === "datetime")
{ {
if ($this->debug) echo "(datetime)\n"; if ($this->debug) echo "(datetime)\n";
$st->bindValue (":".md5 ($key), $val, PDO::PARAM_STR); $st->bindValue (":".md5 ($key), $val, PDO::PARAM_STR);
} }
elseif ($this->fields[$key][0] === "date") elseif ($fields[$key][0] === "date")
{ {
if ($this->debug) echo "(date)\n"; if ($this->debug) echo "(date)\n";
$st->bindValue (":".md5 ($key), $val, PDO::PARAM_STR); $st->bindValue (":".md5 ($key), $val, PDO::PARAM_STR);
@@ -737,7 +751,7 @@ class dblayer extends PDO
else else
{ {
if ($this->debug) echo "(UNKNOWN)\n"; if ($this->debug) echo "(UNKNOWN)\n";
throw new Exception ("TO BE DEVELOPPED : ".$this->fields[$key][0], 500); throw new Exception ("TO BE DEVELOPPED : ".$fields[$key][0], 500);
} }
} }
@@ -752,7 +766,7 @@ class dblayer extends PDO
{ {
if ($this->db === null) if ($this->db === null)
throw new Exception (dgettext("domframework", "Database not connected")); throw new Exception (dgettext("domframework", "Database not connected"));
$req = "DELETE FROM `$this->tableprefix$this->table` "; $req = "DELETE FROM $this->sep$this->tableprefix$this->table$this->sep ";
$req .= "WHERE $this->primary = :primary"; $req .= "WHERE $this->primary = :primary";
$st = $this->db->prepare ($req); $st = $this->db->prepare ($req);
if ($this->debug) echo "DEBUG : $req\n"; if ($this->debug) echo "DEBUG : $req\n";
@@ -779,23 +793,7 @@ class dblayer extends PDO
{ {
if ($this->db === null) if ($this->db === null)
throw new Exception (dgettext("domframework", "Database not connected")); throw new Exception (dgettext("domframework", "Database not connected"));
switch ($this->db->getAttribute(PDO::ATTR_DRIVER_NAME)) $sql = "DROP TABLE $this->sep$this->tableprefix$this->table$this->sep";
{
case "sqlite":
$sql = "DROP TABLE `$this->tableprefix$this->table`";
break;
case "mysql":
$sql = "DROP TABLE `$this->tableprefix$this->table`";
break;
case "pgsql":
$sql = "DROP TABLE `$this->tableprefix$this->table`";
break;
default:
throw new Exception (sprintf (dgettext("domframework",
"Unknown DB engine for drop table '%s'"),
$this->db->getAttribute(PDO::ATTR_DRIVER_NAME)),
500);
}
if ($this->debug) if ($this->debug)
echo "$sql\n"; echo "$sql\n";
return $this->db->exec($sql); return $this->db->exec($sql);
@@ -827,14 +825,15 @@ class dblayer extends PDO
switch ($this->db->getAttribute(PDO::ATTR_DRIVER_NAME)) switch ($this->db->getAttribute(PDO::ATTR_DRIVER_NAME))
{ {
case "sqlite": case "sqlite":
$sql = "CREATE TABLE `$this->tableprefix$this->table` (\n"; $sql = "CREATE TABLE $this->sep$this->tableprefix$this->table$this->sep ".
"(\n";
$i = 0; $i = 0;
foreach ($this->fields as $field=>$params) foreach ($this->fields as $field=>$params)
{ {
if ($i > 0) if ($i > 0)
$sql .= ",\n"; $sql .= ",\n";
// Name of field // Name of field
$sql .= "`$field` "; $sql .= "$this->sep$field$this->sep ";
// Type of field : in $params[0] // Type of field : in $params[0]
if (!isset ($params[0])) if (!isset ($params[0]))
throw new Exception (sprintf ( throw new Exception (sprintf (
@@ -899,20 +898,21 @@ class dblayer extends PDO
500); 500);
foreach ($this->unique as $u) foreach ($this->unique as $u)
{ {
$sql .= ",\n UNIQUE (`"; $sql .= ",\n UNIQUE ($this->sep";
if (is_array ($u)) if (is_array ($u))
$sql .=implode ("`,`", $u); $sql .=implode ("$this->sep,$this->sep", $u);
else else
$sql .= $u; $sql .= $u;
$sql .="`)"; $sql .="$this->sep)";
} }
} }
// Foreign keys // Foreign keys
$i = 0; $i = 0;
foreach ($this->foreign as $field=>$k) foreach ($this->foreign as $field=>$k)
{ {
$sql .= ",\n FOREIGN KEY(`$field`) REFERENCES `".$k[0]."`(`". $sql .= ",\n FOREIGN KEY($this->sep$field$this->sep) ".
$k[1]."`)"; "REFERENCES $this->sep".$k[0]."$this->sep($this->sep".
$k[1]."$this->sep)";
if (isset ($k[2])) if (isset ($k[2]))
$sql .= " ".$k[2]; $sql .= " ".$k[2];
$i++; $i++;
@@ -920,14 +920,15 @@ class dblayer extends PDO
$sql .=")"; $sql .=")";
break; break;
case "mysql": case "mysql":
$sql = "CREATE TABLE `$this->tableprefix$this->table` (\n"; $sql = "CREATE TABLE $this->sep$this->tableprefix$this->table$this->sep ".
"(\n";
$i = 0; $i = 0;
foreach ($this->fields as $field=>$params) foreach ($this->fields as $field=>$params)
{ {
if ($i > 0) if ($i > 0)
$sql .= ",\n"; $sql .= ",\n";
// Name of field // Name of field
$sql .= "`$field` "; $sql .= "$this->sep$field$this->sep ";
// Type of field : in $params[0] // Type of field : in $params[0]
if (!isset ($params[0])) if (!isset ($params[0]))
throw new Exception (dgettext("domframework", throw new Exception (dgettext("domframework",
@@ -986,20 +987,21 @@ class dblayer extends PDO
{ {
foreach ($this->unique as $u) foreach ($this->unique as $u)
{ {
$sql .= ",\n UNIQUE (`"; $sql .= ",\n UNIQUE ($this->sep";
if (is_array ($u)) if (is_array ($u))
$sql .=implode ("`,`", $u); $sql .=implode ("$this->sep,$this->sep", $u);
else else
$sql .= $u; $sql .= $u;
$sql .="`)"; $sql .="$this->sep)";
} }
} }
// Foreign keys // Foreign keys
$i = 0; $i = 0;
foreach ($this->foreign as $field=>$k) foreach ($this->foreign as $field=>$k)
{ {
$sql .= ",\n FOREIGN KEY(`$field`) REFERENCES `".$k[0]."`(`". $sql .= ",\n FOREIGN KEY($this->sep$field$this->sep) ".
$k[1]."`)"; "REFERENCES $this->sep".$k[0]."$this->sep($this->sep".
$k[1]."$this->sep)";
if (isset ($k[2])) if (isset ($k[2]))
$sql .= " ".$k[2]; $sql .= " ".$k[2];
if ($i > 0) if ($i > 0)