BUG : dblayer : allow to prepare the SQL with fields containing spaces

BUG : dblayer : add more unit tests


git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@1809 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
This commit is contained in:
2014-09-11 13:05:53 +00:00
parent b496dadc62
commit 50c328cdd6
2 changed files with 365 additions and 171 deletions

View File

@@ -16,5 +16,113 @@ class test_dblayer extends PHPUnit_Framework_TestCase
$dbconfig["tableprefix"] = "";
}
// Test with column name 'group', 'object', 'where', 'with space'
// Test with table name 'group', 'object', 'where', 'with space'
// For the 3 DB engines
public function test_createTable ()
{
// Create a table named group
$configuration = new configuration ();
$dbconfig = $configuration->get ("database");
if (! isset ($dbconfig["tableprefix"]))
$dbconfig["tableprefix"] = "";
$db = new dblayer ($dbconfig["dsn"], $dbconfig["username"],
$dbconfig["password"], $dbconfig["driver_options"]);
$db->table = "grouped";
$db->fields = array ("group"=>array ("varchar", "255", "not null"),
"object"=>array ("varchar", "255", "not null"),
"where"=>array ("varchar", "255", "not null"),
"with space"=>array ("varchar", "255", "not null"));
$res = $db->createTable ();
$this->assertSame (0, $res);
}
public function test_insert ()
{
$configuration = new configuration ();
$dbconfig = $configuration->get ("database");
if (! isset ($dbconfig["tableprefix"]))
$dbconfig["tableprefix"] = "";
$db = new dblayer ($dbconfig["dsn"], $dbconfig["username"],
$dbconfig["password"], $dbconfig["driver_options"]);
$db->table = "grouped";
$db->fields = array ("group"=>array ("varchar", "255", "not null"),
"object"=>array ("varchar", "255", "not null"),
"where"=>array ("varchar", "255", "not null"),
"with space"=>array ("varchar", "255", "not null"));
$db->unique = array ();
$res = $db->insert (array ("group"=>"gr ou\"p",
"object"=>"/éobj%",
"where"=>"\$'\"",
"with space"=>"with space"));
$this->assertSame ("1", $res);
}
public function test_read1 ()
{
$configuration = new configuration ();
$dbconfig = $configuration->get ("database");
if (! isset ($dbconfig["tableprefix"]))
$dbconfig["tableprefix"] = "";
$db = new dblayer ($dbconfig["dsn"], $dbconfig["username"],
$dbconfig["password"], $dbconfig["driver_options"]);
$db->table = "grouped";
$db->fields = array ("group"=>array ("varchar", "255", "not null"),
"object"=>array ("varchar", "255", "not null"),
"where"=>array ("varchar", "255", "not null"),
"with space"=>array ("varchar", "255", "not null"));
$db->unique = array ();
$res = $db->read (array (array ("group", "gr ou\"p"),
array ("object","/éobj%"),
array ("where","\$'\""),
array ("with space","with space")));
$this->assertSame (array (0=>array ("group"=>"gr ou\"p",
"object"=>"/éobj%",
"where"=>"\$'\"",
"with space"=>"with space")), $res);
}
public function test_update1 ()
{
$configuration = new configuration ();
$dbconfig = $configuration->get ("database");
if (! isset ($dbconfig["tableprefix"]))
$dbconfig["tableprefix"] = "";
$db = new dblayer ($dbconfig["dsn"], $dbconfig["username"],
$dbconfig["password"], $dbconfig["driver_options"]);
$db->table = "grouped";
$db->fields = array ("group"=>array ("varchar", "255", "not null"),
"object"=>array ("varchar", "255", "not null"),
"where"=>array ("varchar", "255", "not null"),
"with space"=>array ("varchar", "255", "not null"));
$db->unique = array ();
$db->primary = "group";
$db->update ("gr ou\"p", array ("object"=>"%éàoppp",
"with space"=>"WITH SPACE"));
}
public function test_read2 ()
{
$configuration = new configuration ();
$dbconfig = $configuration->get ("database");
if (! isset ($dbconfig["tableprefix"]))
$dbconfig["tableprefix"] = "";
$db = new dblayer ($dbconfig["dsn"], $dbconfig["username"],
$dbconfig["password"], $dbconfig["driver_options"]);
$db->table = "grouped";
$db->fields = array ("group"=>array ("varchar", "255", "not null"),
"object"=>array ("varchar", "255", "not null"),
"where"=>array ("varchar", "255", "not null"),
"with space"=>array ("varchar", "255", "not null"));
$db->unique = array ();
$res = $db->read (array (array ("group", "gr ou\"p"),
array ("object","%éàoppp"),
array ("where","\$'\""),
array ("with space","WITH SPACE")));
$this->assertSame (array (0=>array ("group"=>"gr ou\"p",
"object"=>"%éàoppp",
"where"=>"\$'\"",
"with space"=>"WITH SPACE")), $res);
}
}

View File

@@ -60,65 +60,6 @@ class dblayer extends PDO
/** The connecting DSN */
private $dsn = null;
/** Return the connected database name from DSN used to connect */
public function databasename ()
{
if ($this->db === null)
throw new Exception (dgettext("domframework", "Database not connected"), 500);
$vals = explode (";", substr (strstr ($this->dsn, ":"), 1));
$dsnExplode = array ();
foreach ($vals as $val)
{
@list ($k, $v) = explode ("=", $val);
$dsnExplode[$k] = $v;
}
if (isset ($dsnExplode["dbname"]))
return $dsnExplode["dbname"];
return NULL;
}
/** Return all the tables available in the database */
public function listTables ()
{
if ($this->db === null)
throw new Exception (dgettext("domframework", "Database not connected"), 500);
switch ($this->db->getAttribute(PDO::ATTR_DRIVER_NAME))
{
case "sqlite":
$req = "SELECT name FROM sqlite_master WHERE type='table'";
$st = $this->db->prepare ($req);
$st->execute ();
$res = array ();
while ($d = $st->fetch (PDO::FETCH_ASSOC))
$res[] = $d["name"];
break;
case "mysql":
$req = "SELECT TABLE_NAME
FROM information_schema.tables
WHERE TABLE_SCHEMA='".$this->databasename()."'";
$st = $this->db->prepare ($req);
$st->execute ();
$res = array ();
while ($d = $st->fetch (PDO::FETCH_ASSOC))
$res[] = $d["TABLE_NAME"];
break;
case "pgsql":
$req = "SELECT *
FROM pg_tables
WHERE schemaname = 'public'";
$st = $this->db->prepare ($req);
$st->execute ();
$res = array ();
while ($d = $st->fetch (PDO::FETCH_ASSOC))
$res[] = $d["tablename"];
break;
default:
throw new Exception (dgettext("domframework",
"Unknown database driver in listTables"), 500);
}
return $res;
}
// TODO !!
/** Create Table creation from $this->fields with engine abstraction
Example in sqlite3 id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
@@ -161,11 +102,12 @@ class dblayer extends PDO
// Look at the right to write in database and in the directory
$file = substr ($dsn, 7);
if (! is_writeable (dirname ($file)))
throw new Exception (
_("The directory for SQLite database is write protected"),
500);
throw new Exception (dgettext("domframework",
"The directory for SQLite database is write protected"),
500);
if (file_exists ($file) && ! is_writeable ($file))
throw new Exception (_("The SQLite database file is write protected"),
throw new Exception (dgettext("domframework",
"The SQLite database file is write protected"),
500);
if (function_exists ("posix_getuid") &&
fileowner ($file) === posix_getuid ())
@@ -177,6 +119,67 @@ class dblayer extends PDO
$this->dsn = $dsn;
}
/** Return the connected database name from DSN used to connect */
public function databasename ()
{
if ($this->db === null)
throw new Exception (dgettext("domframework", "Database not connected"),
500);
$vals = explode (";", substr (strstr ($this->dsn, ":"), 1));
$dsnExplode = array ();
foreach ($vals as $val)
{
@list ($k, $v) = explode ("=", $val);
$dsnExplode[$k] = $v;
}
if (isset ($dsnExplode["dbname"]))
return $dsnExplode["dbname"];
return NULL;
}
/** Return all the tables available in the database */
public function listTables ()
{
if ($this->db === null)
throw new Exception (dgettext("domframework", "Database not connected"),
500);
switch ($this->db->getAttribute(PDO::ATTR_DRIVER_NAME))
{
case "sqlite":
$req = "SELECT name FROM sqlite_master WHERE type='table'";
$st = $this->db->prepare ($req);
$st->execute ();
$res = array ();
while ($d = $st->fetch (PDO::FETCH_ASSOC))
$res[] = $d["name"];
break;
case "mysql":
$req = "SELECT TABLE_NAME
FROM information_schema.tables
WHERE TABLE_SCHEMA='".$this->databasename()."'";
$st = $this->db->prepare ($req);
$st->execute ();
$res = array ();
while ($d = $st->fetch (PDO::FETCH_ASSOC))
$res[] = $d["TABLE_NAME"];
break;
case "pgsql":
$req = "SELECT *
FROM pg_tables
WHERE schemaname = 'public'";
$st = $this->db->prepare ($req);
$st->execute ();
$res = array ();
while ($d = $st->fetch (PDO::FETCH_ASSOC))
$res[] = $d["tablename"];
break;
default:
throw new Exception (dgettext("domframework",
"Unknown database driver in listTables"), 500);
}
return $res;
}
/** Create a new entry in the table. Datas must be an indexed array
@param array $datas Datas to be recorded (column=>value)
@obsolete 0.5 */
@@ -190,11 +193,14 @@ class dblayer extends PDO
public function insert ($datas)
{
if ($this->db === null)
throw new Exception (_("Database not connected"), 500);
throw new Exception (dgettext("domframework", "Database not connected"),
500);
if ($this->unique === null)
throw new Exception (_("Unique fields of table are not defined"), 500);
throw new Exception (dgettext("domframework",
"Unique fields of table are not defined"), 500);
if (!is_array ($datas))
throw new Exception (_("The datas provided to create are not array"),
throw new Exception (dgettext("domframework",
"The datas provided to create are not array"),
405);
if (!in_array ($this->primary, $this->unique))
$this->unique[] = $this->primary;
@@ -205,10 +211,12 @@ class dblayer extends PDO
if (in_array ("autoincrement", $params))
$datas[$key] = null;
if (in_array ("not null", $params) && !array_key_exists ($key, $datas))
throw new Exception (sprintf (_("Mandatory field '%s' not provided"),
throw new Exception (sprintf (dgettext("domframework",
"Mandatory field '%s' not provided"),
$key), 405);
if (in_array ("not null", $params) && $datas[$key] === "")
throw new Exception (sprintf (_("Mandatory field '%s' is empty"),
throw new Exception (sprintf (dgettext("domframework",
"Mandatory field '%s' is empty"),
$key), 405);
if (!array_key_exists ($key, $datas))
continue;
@@ -221,19 +229,22 @@ class dblayer extends PDO
{
if (strspn ($datas[$key], "0123456789") !== strlen ($datas[$key]))
throw new Exception (sprintf (
_("Errors in consistency : '%s' is not an integer"),
dgettext("domframework",
"Errors in consistency : '%s' is not an integer"),
$key), 405);
}
elseif ($datas[$key] !== "" && $params[0] === "varchar")
{
if (! isset ($params[1]))
throw new Exception (sprintf (
_("The length of varchar field '%s' is not provided"),
dgettext("domframework",
"The length of varchar field '%s' is not provided"),
$key), 500);
if (strlen ($datas[$key]) > $params[1])
throw new Exception (sprintf (
_("Errors in consistency : '%s' data is too long"),
$key), 405);
dgettext("domframework",
"Errors in consistency : '%s' data is too long"),
$key), 405);
}
elseif ($datas[$key] !== "" && $params[0] === "datetime")
{
@@ -241,8 +252,9 @@ class dblayer extends PDO
$d = DateTime::createFromFormat("Y-m-d H:i:s", $datas[$key]);
if (!$d || $d->format("Y-m-d H:i:s") !== $datas[$key])
throw new Exception (sprintf (
_("Incorrect datetime provided for field '%s'"),
$key), 500);
dgettext("domframework",
"Incorrect datetime provided for field '%s'"),
$key), 500);
}
elseif ($datas[$key] !== "" && $params[0] === "date")
{
@@ -250,11 +262,13 @@ class dblayer extends PDO
$d = DateTime::createFromFormat("Y-m-d", $datas[$key]);
if (!$d || $d->format("Y-m-d") !== $datas[$key])
throw new Exception (sprintf (
_("Incorrect date provided for field '%s'"),
$key), 500);
dgettext("domframework",
"Incorrect date provided for field '%s'"),
$key), 500);
}
elseif ($datas[$key] !== "")
throw new Exception (sprintf (_("Unknown field type for '%s'"), $key),
throw new Exception (sprintf (dgettext("domframework",
"Unknown field type for '%s'"), $key),
500);
else
{
@@ -266,7 +280,7 @@ class dblayer extends PDO
// Check for inconsistency
$verify = $this->verifyAll ($datas);
if (count ($verify))
throw new Exception (_("Errors in consistency : ").
throw new Exception (dgettext("domframework", "Errors in consistency : ").
print_r ($verify, TRUE), 405);
// Check if the unique constrain is valid before doing the insertion
@@ -283,8 +297,9 @@ class dblayer extends PDO
$rc = $this->read ($select, array ($this->primary));
if (count ($rc) > 0)
throw new Exception (sprintf (
_("The provided values for columns '%s' already exists"),
implode (",", $columns)), 405);
dgettext("domframework",
"The provided values for columns '%s' already exists"),
implode (",", $columns)), 405);
}
else
{
@@ -293,8 +308,9 @@ class dblayer extends PDO
array ($this->primary));
if (count ($rc) > 0)
throw new Exception (sprintf (
_("The column '%s' with this value already exists"),
$columns), 405);
dgettext("domframework",
"The column '%s' with this value already exists"),
$columns), 405);
}
}
@@ -304,23 +320,23 @@ class dblayer extends PDO
$table = $data[0];
$column = $data[1];
$req = "SELECT $column FROM `$this->tableprefix$table` ".
"WHERE $column=:$column";
"WHERE \"$column\"=:".md5 ($column);
if ($this->debug) echo "DEBUG : $req\n";
$st = $this->db->prepare ($req);
$val = $datasOK[$foreign];
$key = $column;
if ($this->debug) echo "DEBUG BIND : $column->".var_export ($val, TRUE).
"\n";
if ($this->debug) echo "DEBUG BIND : $column(".md5 ($column)."->".
var_export ($val, TRUE)."\n";
if ($val === null)
$st->bindValue (":$key", $val, PDO::PARAM_NULL);
$st->bindValue (":".md5 ($key), $val, PDO::PARAM_NULL);
elseif ($this->fields[$key][0] === "integer")
$st->bindValue (":$key", $val, PDO::PARAM_INT);
$st->bindValue (":".md5 ($key), $val, PDO::PARAM_INT);
elseif ($this->fields[$key][0] === "varchar")
$st->bindValue (":$key", $val, PDO::PARAM_STR);
$st->bindValue (":".md5 ($key), $val, PDO::PARAM_STR);
elseif ($this->fields[$key][0] === "datetime")
$st->bindValue (":$key", $val, PDO::PARAM_STR);
$st->bindValue (":".md5 ($key), $val, PDO::PARAM_STR);
elseif ($this->fields[$key][0] === "date")
$st->bindValue (":$key", $val, PDO::PARAM_STR);
$st->bindValue (":".md5 ($key), $val, PDO::PARAM_STR);
else
throw new Exception ("TO BE DEVELOPPED : ".$this->fields[$key][0], 500);
$st->execute ();
@@ -328,29 +344,35 @@ class dblayer extends PDO
while ($d = $st->fetch (PDO::FETCH_ASSOC))
$res[] = $d;
if (count ($res) === 0)
throw new Exception (sprintf (_("The foreign key '%s' doesn't exists"),
throw new Exception (sprintf (dgettext("domframework",
"The foreign key '%s' doesn't exists"),
$column), 405);
}
$binds = array_keys ($datasOK);
array_walk ($binds, function(&$value, $key) {
$value = md5 ($value);
});
$req = "INSERT INTO `$this->tableprefix$this->table` ";
$req .= "(".implode (",", array_keys ($datasOK)).")";
$req .= "(\"".implode ("\",\"", array_keys ($datasOK))."\")";
$req .= " VALUES ";
$req .= "(:".implode (",:", array_keys ($datasOK)).")";
$req .= "(:".implode (",:", $binds).")";
if ($this->debug) echo "DEBUG : $req\n";
$st = $this->db->prepare ($req);
foreach ($datasOK as $key=>$val)
{
if ($this->debug) echo "DEBUG BIND : $key->".var_export ($val, TRUE)."\n";
if ($this->debug) echo "DEBUG BIND : $key(".md5 ($key).")->".
var_export ($val, TRUE)."\n";
if ($val === null)
$st->bindValue (":$key", $val, PDO::PARAM_NULL);
$st->bindValue (":".md5 ($key), $val, PDO::PARAM_NULL);
elseif ($this->fields[$key][0] === "integer")
$st->bindValue (":$key", $val, PDO::PARAM_INT);
$st->bindValue (":".md5 ($key), $val, PDO::PARAM_INT);
elseif ($this->fields[$key][0] === "varchar")
$st->bindValue (":$key", $val, PDO::PARAM_STR);
$st->bindValue (":".md5 ($key), $val, PDO::PARAM_STR);
elseif ($this->fields[$key][0] === "datetime")
$st->bindValue (":$key", $val, PDO::PARAM_STR);
$st->bindValue (":".md5 ($key), $val, PDO::PARAM_STR);
elseif ($this->fields[$key][0] === "date")
$st->bindValue (":$key", $val, PDO::PARAM_STR);
$st->bindValue (":".md5 ($key), $val, PDO::PARAM_STR);
else
throw new Exception ("TO BE DEVELOPPED : ".$this->fields[$key][0], 500);
}
@@ -375,22 +397,27 @@ class dblayer extends PDO
$whereOr=false)
{
if ($this->db === null)
throw new Exception (_("Database not connected"), 500);
throw new Exception (dgettext("domframework", "Database not connected"),
500);
if ($select !== null && !is_array ($select))
throw new Exception (_("Select information provided is not an array"),
throw new Exception (dgettext("domframework",
"Select information provided is not an array"),
405);
if ($display !== null && !is_array ($display))
throw new Exception (_("Display information provided is not an array"),
throw new Exception (dgettext("domframework",
"Display information provided is not an array"),
405);
if ($order !== null && !is_array ($order))
throw new Exception (_("Order information provided is not an array"),
throw new Exception (dgettext("domframework",
"Order information provided is not an array"),
405);
if ($display !== null)
{
foreach ($display as $f)
{
if (!in_array ($f, array_keys ($this->fields)))
throw new Exception (sprintf (_("Field '%s' not allowed"), $f), 506);
throw new Exception (sprintf (dgettext("domframework",
"Field '%s' not allowed"), $f), 506);
}
}
else
@@ -398,9 +425,9 @@ class dblayer extends PDO
$display = array_keys ($this->fields);
}
$req = "SELECT ";
$req .= implode (",", $display);
$req .= " FROM `$this->tableprefix$this->table`";
$req = "SELECT \"";
$req .= implode ("\",\"", $display);
$req .= "\" FROM `$this->tableprefix$this->table`";
if ($select !== null)
{
$req .= " WHERE ";
@@ -415,7 +442,14 @@ class dblayer extends PDO
}
if (!isset ($s[2]))
$s[2] = "=";
$req .= " ".$s[0]." ".$s[2]." :".$s[0];
if (!isset ($s[0]))
throw new Exception (sprintf (dgettext("domframework",
"Select not found for id=%d"), $n), 500);
// The double-quotes are added for sqlite to escape the column if its
// 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]);
}
}
@@ -435,18 +469,32 @@ class dblayer extends PDO
}
if ($this->debug) echo "DEBUG : $req\n";
$st = $this->db->prepare ($req);
try
{
$st = $this->db->prepare ($req);
}
catch (Exception $e)
{
if ($this->debug) echo "DEBUG : PREPARE ERROR ! Return FALSE".
$e->getMessage()."\n";
throw new Exception ($e->getMessage(), 500);
}
if ($select !== NULL)
{
foreach ($select as $s)
{
if ($this->debug) echo "DEBUG BIND : ".$s[0]."->".
if ($this->debug) echo "DEBUG BIND : ".$s[0]."(".md5 ($s[0]).")->".
var_export ($s[1], TRUE)."\n";
$st->bindValue (":".$s[0], $s[1]);
$st->bindValue (":".md5 ($s[0]), $s[1]);
}
}
$st->execute ();
$rc = $st->execute ();
if ($rc === false)
{
if ($this->debug) echo "DEBUG : EXECUTE ERROR ! Return FALSE\n";
}
$res = array ();
while ($d = $st->fetch (PDO::FETCH_ASSOC))
$res[] = $d;
@@ -461,7 +509,12 @@ class dblayer extends PDO
public function update ($updatekey, $datas)
{
if ($this->db === null)
throw new Exception (_("Database not connected"), 500);
throw new Exception (dgettext("domframework", "Database not connected"),
500);
if (count ($this->fields) === 0)
throw new Exception (dgettext("domframework", "No Field defined"), 500);
if ($this->primary === null)
throw new Exception (dgettext("domframework", "No Primary defined"), 500);
$datasOK = array ();
// Check for missing parameters
foreach ($this->fields as $key=>$params)
@@ -475,19 +528,22 @@ class dblayer extends PDO
{
if (strspn ($datas[$key], "0123456789") !== strlen ($datas[$key]))
throw new Exception (sprintf (
_("Errors in consistency : '%s' is not an integer"),
$key), 405);
dgettext("domframework",
"Errors in consistency : '%s' is not an integer"),
$key), 405);
}
elseif ($datas[$key] !== "" && $params[0] === "varchar")
{
if (! isset ($params[1]))
throw new Exception (sprintf (
_("The length of varchar field '%s' is not provided"),
$key), 500);
dgettext("domframework",
"The length of varchar field '%s' is not provided"),
$key), 500);
if (strlen ($datas[$key]) > $params[1])
throw new Exception (sprintf (
_("Errors in consistency : '%s' data is too long"),
$key), 405);
dgettext("domframework",
"Errors in consistency : '%s' data is too long"),
$key), 405);
}
elseif ($datas[$key] !== "" && $params[0] === "datetime")
{
@@ -495,8 +551,9 @@ class dblayer extends PDO
$d = DateTime::createFromFormat("Y-m-d H:i:s", $datas[$key]);
if (!$d || $d->format("Y-m-d H:i:s") !== $datas[$key])
throw new Exception (sprintf (
_("Incorrect datetime provided for field '%s'"),
$key), 500);
dgettext("domframework",
"Incorrect datetime provided for field '%s'"),
$key), 500);
}
elseif ($datas[$key] !== "" && $params[0] === "date")
{
@@ -504,11 +561,13 @@ class dblayer extends PDO
$d = DateTime::createFromFormat("Y-m-d", $datas[$key]);
if (!$d || $d->format("Y-m-d") !== $datas[$key])
throw new Exception (sprintf (
_("Incorrect date provided for field '%s'"),
$key), 500);
dgettext("domframework",
"Incorrect date provided for field '%s'"),
$key), 500);
}
elseif ($datas[$key] !== "")
throw new Exception (sprintf (_("Unknown field type for '%s'"), $key),
throw new Exception (sprintf (dgettext("domframework",
"Unknown field type for '%s'"), $key),
500);
else
{
@@ -519,7 +578,8 @@ class dblayer extends PDO
}
if (count ($datasOK) === 0)
throw new Exception (_("Don't receive any field to display"), 501);
throw new Exception (dgettext("domframework",
"Don't receive any field to display"), 501);
// Check for type inconsistencies before using $datasOK
foreach ($datasOK as $key=>$params)
@@ -533,7 +593,8 @@ class dblayer extends PDO
// 1. Read the actual state
$before = $this->read (array (array ($this->primary, $updatekey)));
if (count ($before) === 0)
throw new Exception (_("Entry to modify unavailable"), 404);
throw new Exception (dgettext("domframework",
"Entry to modify unavailable"), 404);
$before = reset ($before);
// 2. Map the proposal entries into the before state
$after = $before;
@@ -560,8 +621,9 @@ class dblayer extends PDO
$rc = $this->read ($select, array ($this->primary));
if (count ($rc) > 0)
throw new Exception (sprintf (
_("The provided values for columns '%s' already exists"),
implode (",", $columns)), 405);
dgettext("domframework",
"The provided values for columns '%s' already exists"),
implode (",", $columns)), 405);
}
}
else
@@ -576,8 +638,9 @@ class dblayer extends PDO
array ($this->primary));
if (count ($rc) > 0)
throw new Exception (sprintf (
_("An entry already exists with this value in the column '%s'"),
$columns), 405);
dgettext("domframework",
"An entry already exists with this value in the column '%s'"),
$columns), 405);
}
}
@@ -591,23 +654,24 @@ class dblayer extends PDO
$table = $data[0];
$column = $data[1];
$req = "SELECT $column FROM `$this->tableprefix$table` ".
"WHERE $column=:$column";
"WHERE \"$column\"=:".md5 ($column);
if ($this->debug) echo "DEBUG : $req\n";
$st = $this->db->prepare ($req);
$val = $datasOK[$foreign];
$key = $column;
if ($this->debug) echo "DEBUG BIND : $column->".var_export ($val, TRUE).
if ($this->debug) echo "DEBUG BIND : $column(".md5 ($column).")->".
var_export ($val, TRUE).
"\n";
if ($val === null)
$st->bindValue (":$key", $val, PDO::PARAM_NULL);
$st->bindValue (":".md5 ($key), $val, PDO::PARAM_NULL);
elseif ($this->fields[$key][0] === "integer")
$st->bindValue (":$key", $val, PDO::PARAM_INT);
$st->bindValue (":".md5 ($key), $val, PDO::PARAM_INT);
elseif ($this->fields[$key][0] === "varchar")
$st->bindValue (":$key", $val, PDO::PARAM_STR);
$st->bindValue (":".md5 ($key), $val, PDO::PARAM_STR);
elseif ($this->fields[$key][0] === "datetime")
$st->bindValue (":$key", $val, PDO::PARAM_STR);
$st->bindValue (":".md5 ($key), $val, PDO::PARAM_STR);
elseif ($this->fields[$key][0] === "date")
$st->bindValue (":$key", $val, PDO::PARAM_STR);
$st->bindValue (":".md5 ($key), $val, PDO::PARAM_STR);
else
throw new Exception ("TO BE DEVELOPPED : ".$this->fields[$key][0], 500);
$st->execute ();
@@ -615,7 +679,8 @@ class dblayer extends PDO
while ($d = $st->fetch (PDO::FETCH_ASSOC))
$res[] = $d;
if (count ($res) === 0)
throw new Exception (sprintf (_("The foreign key '%s' doesn't exists"),
throw new Exception (sprintf (dgettext("domframework",
"The foreign key '%s' doesn't exists"),
$column), 405);
}
@@ -625,40 +690,41 @@ class dblayer extends PDO
foreach ($datasOK as $key=>$val)
{
if ($i>0) $req .= ",";
$req .= "$key=:$key";
$req .= "\"$key\"=:".md5 ($key);
$i++;
}
$req .= " WHERE $this->primary=:$this->primary";
$req .= " WHERE \"$this->primary\"=:".md5 ($this->primary);
if ($this->debug) echo "DEBUG : $req\n";
$st = $this->db->prepare ($req);
foreach ($datasOK as $key=>$val)
{
if ($this->debug) echo "DEBUG BIND : $key->".var_export ($val, TRUE)." ";
if ($this->debug) echo "DEBUG BIND : $key(".md5 ($key).")->".
var_export ($val, TRUE)." ";
if ($val === null)
{
if ($this->debug) echo "(null)\n";
$st->bindValue (":$key", $val, PDO::PARAM_NULL);
$st->bindValue (":".md5 ($key), $val, PDO::PARAM_NULL);
}
elseif ($this->fields[$key][0] === "integer")
{
if ($this->debug) echo "(integer)\n";
$st->bindValue (":$key", $val, PDO::PARAM_INT);
$st->bindValue (":".md5 ($key), $val, PDO::PARAM_INT);
}
elseif ($this->fields[$key][0] === "varchar")
{
if ($this->debug) echo "(varchar)\n";
$st->bindValue (":$key", $val, PDO::PARAM_STR);
$st->bindValue (":".md5 ($key), $val, PDO::PARAM_STR);
}
elseif ($this->fields[$key][0] === "datetime")
{
if ($this->debug) echo "(datetime)\n";
$st->bindValue (":$key", $val, PDO::PARAM_STR);
$st->bindValue (":".md5 ($key), $val, PDO::PARAM_STR);
}
elseif ($this->fields[$key][0] === "date")
{
if ($this->debug) echo "(date)\n";
$st->bindValue (":$key", $val, PDO::PARAM_STR);
$st->bindValue (":".md5 ($key), $val, PDO::PARAM_STR);
}
else
{
@@ -677,7 +743,7 @@ class dblayer extends PDO
public function delete ($deletekey)
{
if ($this->db === null)
throw new Exception (_("Database not connected"));
throw new Exception (dgettext("domframework", "Database not connected"));
$req = "DELETE FROM `$this->tableprefix$this->table` ";
$req .= "WHERE $this->primary = :primary";
$st = $this->db->prepare ($req);
@@ -692,6 +758,8 @@ class dblayer extends PDO
/** Translation of fields */
public function titles ()
{
if (count ($this->fields) === 0)
throw new Exception (dgettext("domframework", "No Field defined"), 500);
$arr = array ();
foreach ($this->fields as $field=>$v)
$arr[$field] = $field;
@@ -702,7 +770,7 @@ class dblayer extends PDO
public function dropTable ()
{
if ($this->db === null)
throw new Exception (_("Database not connected"));
throw new Exception (dgettext("domframework", "Database not connected"));
switch ($this->db->getAttribute(PDO::ATTR_DRIVER_NAME))
{
case "sqlite":
@@ -715,7 +783,8 @@ class dblayer extends PDO
$sql = "DROP TABLE `$this->tableprefix$this->table`";
break;
default:
throw new Exception (sprintf (_("Unknown DB engine for drop table '%s'"),
throw new Exception (sprintf (dgettext("domframework",
"Unknown DB engine for drop table '%s'"),
$this->db->getAttribute(PDO::ATTR_DRIVER_NAME)),
500);
}
@@ -743,7 +812,10 @@ class dblayer extends PDO
public function createTable ()
{
if ($this->db === null)
throw new Exception (_("Database not connected"), 500);
throw new Exception (dgettext("domframework", "Database not connected"),
500);
if (count ($this->fields) === 0)
throw new Exception (dgettext("domframework", "No Field defined"), 500);
switch ($this->db->getAttribute(PDO::ATTR_DRIVER_NAME))
{
case "sqlite":
@@ -758,8 +830,9 @@ class dblayer extends PDO
// Type of field : in $params[0]
if (!isset ($params[0]))
throw new Exception (sprintf (
_("No database type defined for field '%s'"),
$field), 500);
dgettext("domframework",
"No database type defined for field '%s'"),
$field), 500);
switch ($params[0])
{
@@ -769,7 +842,8 @@ class dblayer extends PDO
break;
case "varchar":
if (!isset ($params[1]))
throw new Exception (_("No Size provided for varchar field"), 500);
throw new Exception (dgettext("domframework",
"No Size provided for varchar field"), 500);
$sql .= "VARCHAR(".$params[1].")";
$params = array_slice ($params, 2);
break;
@@ -783,8 +857,9 @@ class dblayer extends PDO
break;
default:
throw new Exception (sprintf (
_("Unknown type '%s' provided for field '%s'"),
$params[0], $field), 500);
dgettext("domframework",
"Unknown type '%s' provided for field '%s'"),
$params[0], $field), 500);
}
// Primary key
if ($this->primary === $field)
@@ -800,7 +875,8 @@ class dblayer extends PDO
case "not null": $sql .= " NOT NULL"; break;
case "autoincrement": $sql .= " AUTOINCREMENT";break;
default:
throw new Exception (_("Unknown additionnal parameter for field"),
throw new Exception (dgettext("domframework",
"Unknown additionnal parameter for field"),
500);
}
}
@@ -810,7 +886,8 @@ class dblayer extends PDO
if ($this->unique !== null)
{
if (!is_array ($this->unique))
throw new Exception (_("The Unique field definition is not an array"),
throw new Exception (dgettext("domframework",
"The Unique field definition is not an array"),
500);
foreach ($this->unique as $u)
{
@@ -845,7 +922,8 @@ class dblayer extends PDO
$sql .= "`$field` ";
// Type of field : in $params[0]
if (!isset ($params[0]))
throw new Exception (_("No database type defined for field"), 500);
throw new Exception (dgettext("domframework",
"No database type defined for field"), 500);
switch ($params[0])
{
case "integer":
@@ -854,7 +932,8 @@ class dblayer extends PDO
break;
case "varchar":
if (!isset ($params[1]))
throw new Exception (_("No Size provided for varchar field"), 500);
throw new Exception (dgettext("domframework",
"No Size provided for varchar field"), 500);
$sql .= "VARCHAR(".$params[1].")";
$params = array_slice ($params, 2);
break;
@@ -868,8 +947,9 @@ class dblayer extends PDO
break;
default:
throw new Exception (sprintf (
_("Unknown type provided for field '%s'"),
$field), 500);
dgettext("domframework",
"Unknown type provided for field '%s'"),
$field), 500);
}
// Primary key
if ($this->primary === $field)
@@ -886,8 +966,9 @@ class dblayer extends PDO
case "autoincrement": $sql .= " AUTO_INCREMENT";break;
default:
throw new Exception (sprintf (
_("Unknown additionnal parameter for field '%s'"),
$field), 500);
dgettext("domframework",
"Unknown additionnal parameter for field '%s'"),
$field), 500);
}
}
$i ++;
@@ -935,8 +1016,9 @@ class dblayer extends PDO
// Type of field : in $params[0]
if (!isset ($params[0]))
throw new Exception (sprintf (
_("No database type defined for field '%s'"),
$field), 500);
dgettext("domframework",
"No database type defined for field '%s'"),
$field), 500);
switch ($params[0])
{
case "integer":
@@ -946,7 +1028,8 @@ class dblayer extends PDO
case "varchar":
if (!isset ($params[1]))
throw new Exception (sprintf (
_("No Size provided for varchar field '%s'"),
dgettext("domframework",
"No Size provided for varchar field '%s'"),
$field), 500);
$sql .= "VARCHAR(".$params[1].")";
$params = array_slice ($params, 2);
@@ -961,8 +1044,9 @@ class dblayer extends PDO
break;
default:
throw new Exception (sprintf (
_("Unknown type provided for field '%s'"),
$field), 500);
dgettext("domframework",
"Unknown type provided for field '%s'"),
$field), 500);
}
// Primary key
if ($this->primary === $field)
@@ -978,8 +1062,9 @@ class dblayer extends PDO
case "not null": $sql .= " NOT NULL"; break;
default:
throw new Exception (sprintf (
_("Unknown additionnal parameter for field '%s'"),
$field), 500);
dgettext("domframework",
"Unknown additionnal parameter for field '%s'"),
$field), 500);
}
}
}
@@ -1013,7 +1098,8 @@ class dblayer extends PDO
$sql .=")";
break;
default:
throw new Exception (_("PDO Engine not supported in dbLayer"), 500);
throw new Exception (dgettext("domframework",
"PDO Engine not supported in dbLayer"), 500);
}
if ($this->debug)