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:
@@ -25,6 +25,13 @@ class test_dblayer_{ENGINE} extends PHPUnit_Framework_TestCase
|
||||
"driver_options" => null,
|
||||
"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 ()
|
||||
|
||||
106
dblayer.php
106
dblayer.php
@@ -59,6 +59,8 @@ class dblayer extends PDO
|
||||
public $debug = FALSE;
|
||||
/** The connecting DSN */
|
||||
private $dsn = null;
|
||||
/** The field group delimiter */
|
||||
private $sep = "";
|
||||
|
||||
// TODO !!
|
||||
/** Create Table creation from $this->fields with engine abstraction
|
||||
@@ -114,14 +116,17 @@ class dblayer extends PDO
|
||||
chmod ($file, 0666);
|
||||
// Force ForeignKeys support (disabled by default)
|
||||
$this->db->exec("PRAGMA foreign_keys = ON");
|
||||
$this->sep = "`";
|
||||
break;
|
||||
case "mysql":
|
||||
// Set the coding to UTF8
|
||||
$this->db->exec("SET CHARACTER SET utf8");
|
||||
$this->sep = "`";
|
||||
break;
|
||||
case "pgsql":
|
||||
// Set the coding to UTF8
|
||||
$this->db->exec("SET CHARACTER SET utf8");
|
||||
$this->db->exec("SET NAMES 'utf8'");
|
||||
$this->sep = "\"";
|
||||
break;
|
||||
}
|
||||
$this->dsn = $dsn;
|
||||
@@ -327,7 +332,7 @@ class dblayer extends PDO
|
||||
{
|
||||
$table = $data[0];
|
||||
$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);
|
||||
if ($this->debug) echo "DEBUG : $req\n";
|
||||
$st = $this->db->prepare ($req);
|
||||
@@ -361,8 +366,10 @@ class dblayer extends PDO
|
||||
array_walk ($binds, function(&$value, $key) {
|
||||
$value = md5 ($value);
|
||||
});
|
||||
$req = "INSERT INTO `$this->tableprefix$this->table` ";
|
||||
$req .= "(`".implode ("`,`", array_keys ($datasOK))."`)";
|
||||
$req = "INSERT INTO $this->sep$this->tableprefix$this->table$this->sep ";
|
||||
$req .= "($this->sep".
|
||||
implode ("$this->sep,$this->sep", array_keys ($datasOK)).
|
||||
"$this->sep)";
|
||||
$req .= " VALUES ";
|
||||
$req .= "(:".implode (",:", $binds).")";
|
||||
if ($this->debug) echo "DEBUG : $req\n";
|
||||
@@ -433,9 +440,10 @@ class dblayer extends PDO
|
||||
$display = array_keys ($this->fields);
|
||||
}
|
||||
|
||||
$req = "SELECT `";
|
||||
$req .= implode ("`,`", $display);
|
||||
$req .= "` FROM `$this->tableprefix$this->table`";
|
||||
$req = "SELECT $this->sep";
|
||||
$req .= implode ("$this->sep,$this->sep", $display);
|
||||
$req .= "$this->sep ";
|
||||
$req .= "FROM $this->sep$this->tableprefix$this->table$this->sep";
|
||||
if ($select !== null)
|
||||
{
|
||||
$req .= " WHERE ";
|
||||
@@ -457,7 +465,7 @@ class dblayer extends PDO
|
||||
// name is 'group'
|
||||
// Don't put single quotes : don't work with SQLite
|
||||
// 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;
|
||||
$table = $data[0];
|
||||
$column = $data[1];
|
||||
$req = "SELECT `$column` FROM `$this->tableprefix$table` ".
|
||||
"WHERE `$column`=:".md5 ($column);
|
||||
$req = "SELECT $this->sep$column$this->sep ".
|
||||
"FROM $this->sep$this->tableprefix$table$this->sep ".
|
||||
"WHERE $this->sep$column$this->sep=:".md5 ($column);
|
||||
if ($this->debug) echo "DEBUG : $req\n";
|
||||
$st = $this->db->prepare ($req);
|
||||
$val = $datasOK[$foreign];
|
||||
@@ -692,19 +701,24 @@ class dblayer extends PDO
|
||||
$column), 405);
|
||||
}
|
||||
|
||||
$datasOK[$this->primary] = $updatekey;
|
||||
$req = "UPDATE `".$this->tableprefix."$this->table` SET ";
|
||||
$req = "UPDATE $this->sep".$this->tableprefix."$this->table$this->sep SET ";
|
||||
$i = 0;
|
||||
foreach ($datasOK as $key=>$val)
|
||||
{
|
||||
if ($i>0) $req .= ",";
|
||||
$req .= "`$key`=:".md5 ($key);
|
||||
$req .= "$this->sep$key$this->sep=:".md5 ($key);
|
||||
$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";
|
||||
$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)
|
||||
{
|
||||
if ($this->debug) echo "DEBUG BIND : $key(".md5 ($key).")->".
|
||||
@@ -714,22 +728,22 @@ class dblayer extends PDO
|
||||
if ($this->debug) echo "(null)\n";
|
||||
$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";
|
||||
$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";
|
||||
$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";
|
||||
$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";
|
||||
$st->bindValue (":".md5 ($key), $val, PDO::PARAM_STR);
|
||||
@@ -737,7 +751,7 @@ class dblayer extends PDO
|
||||
else
|
||||
{
|
||||
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)
|
||||
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";
|
||||
$st = $this->db->prepare ($req);
|
||||
if ($this->debug) echo "DEBUG : $req\n";
|
||||
@@ -779,23 +793,7 @@ class dblayer extends PDO
|
||||
{
|
||||
if ($this->db === null)
|
||||
throw new Exception (dgettext("domframework", "Database not connected"));
|
||||
switch ($this->db->getAttribute(PDO::ATTR_DRIVER_NAME))
|
||||
{
|
||||
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);
|
||||
}
|
||||
$sql = "DROP TABLE $this->sep$this->tableprefix$this->table$this->sep";
|
||||
if ($this->debug)
|
||||
echo "$sql\n";
|
||||
return $this->db->exec($sql);
|
||||
@@ -827,14 +825,15 @@ class dblayer extends PDO
|
||||
switch ($this->db->getAttribute(PDO::ATTR_DRIVER_NAME))
|
||||
{
|
||||
case "sqlite":
|
||||
$sql = "CREATE TABLE `$this->tableprefix$this->table` (\n";
|
||||
$sql = "CREATE TABLE $this->sep$this->tableprefix$this->table$this->sep ".
|
||||
"(\n";
|
||||
$i = 0;
|
||||
foreach ($this->fields as $field=>$params)
|
||||
{
|
||||
if ($i > 0)
|
||||
$sql .= ",\n";
|
||||
// Name of field
|
||||
$sql .= "`$field` ";
|
||||
$sql .= "$this->sep$field$this->sep ";
|
||||
// Type of field : in $params[0]
|
||||
if (!isset ($params[0]))
|
||||
throw new Exception (sprintf (
|
||||
@@ -899,20 +898,21 @@ class dblayer extends PDO
|
||||
500);
|
||||
foreach ($this->unique as $u)
|
||||
{
|
||||
$sql .= ",\n UNIQUE (`";
|
||||
$sql .= ",\n UNIQUE ($this->sep";
|
||||
if (is_array ($u))
|
||||
$sql .=implode ("`,`", $u);
|
||||
$sql .=implode ("$this->sep,$this->sep", $u);
|
||||
else
|
||||
$sql .= $u;
|
||||
$sql .="`)";
|
||||
$sql .="$this->sep)";
|
||||
}
|
||||
}
|
||||
// Foreign keys
|
||||
$i = 0;
|
||||
foreach ($this->foreign as $field=>$k)
|
||||
{
|
||||
$sql .= ",\n FOREIGN KEY(`$field`) REFERENCES `".$k[0]."`(`".
|
||||
$k[1]."`)";
|
||||
$sql .= ",\n FOREIGN KEY($this->sep$field$this->sep) ".
|
||||
"REFERENCES $this->sep".$k[0]."$this->sep($this->sep".
|
||||
$k[1]."$this->sep)";
|
||||
if (isset ($k[2]))
|
||||
$sql .= " ".$k[2];
|
||||
$i++;
|
||||
@@ -920,14 +920,15 @@ class dblayer extends PDO
|
||||
$sql .=")";
|
||||
break;
|
||||
case "mysql":
|
||||
$sql = "CREATE TABLE `$this->tableprefix$this->table` (\n";
|
||||
$sql = "CREATE TABLE $this->sep$this->tableprefix$this->table$this->sep ".
|
||||
"(\n";
|
||||
$i = 0;
|
||||
foreach ($this->fields as $field=>$params)
|
||||
{
|
||||
if ($i > 0)
|
||||
$sql .= ",\n";
|
||||
// Name of field
|
||||
$sql .= "`$field` ";
|
||||
$sql .= "$this->sep$field$this->sep ";
|
||||
// Type of field : in $params[0]
|
||||
if (!isset ($params[0]))
|
||||
throw new Exception (dgettext("domframework",
|
||||
@@ -986,20 +987,21 @@ class dblayer extends PDO
|
||||
{
|
||||
foreach ($this->unique as $u)
|
||||
{
|
||||
$sql .= ",\n UNIQUE (`";
|
||||
$sql .= ",\n UNIQUE ($this->sep";
|
||||
if (is_array ($u))
|
||||
$sql .=implode ("`,`", $u);
|
||||
$sql .=implode ("$this->sep,$this->sep", $u);
|
||||
else
|
||||
$sql .= $u;
|
||||
$sql .="`)";
|
||||
$sql .="$this->sep)";
|
||||
}
|
||||
}
|
||||
// Foreign keys
|
||||
$i = 0;
|
||||
foreach ($this->foreign as $field=>$k)
|
||||
{
|
||||
$sql .= ",\n FOREIGN KEY(`$field`) REFERENCES `".$k[0]."`(`".
|
||||
$k[1]."`)";
|
||||
$sql .= ",\n FOREIGN KEY($this->sep$field$this->sep) ".
|
||||
"REFERENCES $this->sep".$k[0]."$this->sep($this->sep".
|
||||
$k[1]."$this->sep)";
|
||||
if (isset ($k[2]))
|
||||
$sql .= " ".$k[2];
|
||||
if ($i > 0)
|
||||
|
||||
Reference in New Issue
Block a user