git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@2098 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
1352 lines
46 KiB
PHP
1352 lines
46 KiB
PHP
<?php
|
|
/** DomFramework
|
|
@package domframework
|
|
@author Dominique Fournier <dominique@fournier38.fr> */
|
|
|
|
// dblayer.php
|
|
|
|
/* Documentation :
|
|
The dbLayer provide an abstraction layer on PDO to be easier on all the CRUD
|
|
(Create, Read, Update, Delete) operations, accross all the databases engines.
|
|
To use it, extends in your code this class, and define the attributes :
|
|
- public $table : name of the table you want to use
|
|
- public $fields : description of all the fields in the database like :
|
|
public $fields = array (
|
|
"id"=>array ("integer", "not null", "autoincrement"),
|
|
"zone"=>array ("varchar", "255", "not null"),
|
|
"viewname"=>array ("varchar", "255"),
|
|
"viewclients"=>array ("varchar", "255"),
|
|
"comment"=>array ("varchar", "1024"),
|
|
"opendate"=>array ("datetime", "not null"),
|
|
"closedate"=>array ("datetime"),
|
|
);
|
|
- public $primary = "id" ; the primary key of the table (in text). Actually
|
|
the dbLayer abstraction don't supports primary key on multiples columns
|
|
- public $unique = array ("column", array ("column1", "column2");)
|
|
- public $foreign : Add the support of foreign keys. Must be defined like :
|
|
public $foreign = array (
|
|
"localfield"=>array ("foreign Table", "foreign key",
|
|
"ON UPDATE CASCADE ON DELETE CASCADE"),
|
|
);
|
|
|
|
Optionnaly, you can add the
|
|
- public $debug = TRUE : enable the debug on screen (NOT FOR PROD !!)
|
|
*/
|
|
|
|
require_once ("domframework/verify.php");
|
|
|
|
/** Permit abstraction on the differents SQL databases available */
|
|
class dblayer extends PDO
|
|
{
|
|
/** The table name to use */
|
|
public $table = null;
|
|
/** The tableprefix text to prepend to table name (Should finish by _)
|
|
Just allow chars ! */
|
|
public $tableprefix = "";
|
|
/** The fields with the definition of type, and special parameters */
|
|
public $fields = array ();
|
|
/** The primary field */
|
|
public $primary = null;
|
|
/** An array to define the unique fields (or array of unique fields) */
|
|
public $unique = null;
|
|
/** An array to define the foreign keys of the field */
|
|
public $foreign = array ();
|
|
/** The verify unitary stack
|
|
@param string $field The name of the field to test
|
|
@param string $val The value of the field to test */
|
|
public function verifyOne ($field, $val) {}
|
|
/** The verify global stack
|
|
@param array $datas The associative array of contents */
|
|
public function verifyAll ($datas) {}
|
|
/** Debug of the SQL */
|
|
public $debug = FALSE;
|
|
/** The connecting DSN */
|
|
private $dsn = null;
|
|
/** The field group delimiter */
|
|
private $sep = "";
|
|
/** Titles */
|
|
public $titles = array ();
|
|
|
|
// TODO !!
|
|
/** Create Table creation from $this->fields with engine abstraction
|
|
Example in sqlite3 id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
|
|
in MySQL id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT, */
|
|
|
|
// TODO !!
|
|
/** Create automatic creation of $fields from .schema of sqlite3/
|
|
show create table `NomTable`; for MySQL
|
|
SQLite3 : PRAGMA TABLE_INFO('yourtable');
|
|
MYSQL : SHOW COLUMNS FROM yourtable;*/
|
|
|
|
// TODO !!
|
|
/** Allow to modify tables if the definition is changed
|
|
Attention : SQLite don't supports adding Foreign keys without deleting all
|
|
the table, and re-import the datas (http://www.sqlite.org/omitted.html) */
|
|
|
|
/** Limit to one instance of the connection to the same database */
|
|
private static $instance = null;
|
|
|
|
/** Connection to the database engine
|
|
See http://fr2.php.net/manual/en/pdo.construct.php for the $dsn format
|
|
@param string $dsn PDO Data Source Name
|
|
@param string|null $username Username to connect
|
|
@param string|null $password Password to connect
|
|
@param string|null $driver_options Driver options to the database */
|
|
public function __construct ($dsn, $username=null, $password=null,
|
|
$driver_options=null)
|
|
{
|
|
$driver = @explode (":", $dsn);
|
|
if (! isset ($driver[0]))
|
|
throw new Exception (_("No valid DSN provided"), 500);
|
|
// Force specifics initialisations
|
|
switch ($driver[0])
|
|
{
|
|
case "sqlite":
|
|
// Look at the right to write in database and in the directory
|
|
$file = substr ($dsn, 7);
|
|
if (! is_writeable (dirname ($file)))
|
|
throw new Exception (dgettext("domframework",
|
|
"The directory for SQLite database is write protected"),
|
|
500);
|
|
if (file_exists ($file) && ! is_writeable ($file))
|
|
throw new Exception (dgettext("domframework",
|
|
"The SQLite database file is write protected"),
|
|
500);
|
|
if (function_exists ("posix_getuid") &&
|
|
file_exists ($file) &&
|
|
fileowner ($file) === posix_getuid ())
|
|
chmod ($file, 0666);
|
|
try
|
|
{
|
|
parent::__construct ($dsn, $username, $password, $driver_options);
|
|
parent::setAttribute (PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
|
}
|
|
catch (Exception $e)
|
|
{
|
|
throw new Exception ("PDO error : ".$e->getMessage(), 500);
|
|
}
|
|
|
|
// Force ForeignKeys support (disabled by default)
|
|
$this->exec("PRAGMA foreign_keys = ON");
|
|
$this->sep = "`";
|
|
break;
|
|
case "mysql":
|
|
try
|
|
{
|
|
$driver_options[PDO::MYSQL_ATTR_FOUND_ROWS] = 1;
|
|
parent::__construct ($dsn, $username, $password, $driver_options);
|
|
parent::setAttribute (PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
|
}
|
|
catch (Exception $e)
|
|
{
|
|
throw new Exception ("PDO error : ".$e->getMessage(), 500);
|
|
}
|
|
|
|
// Set the coding to UTF8
|
|
$this->exec("SET CHARACTER SET utf8");
|
|
$this->sep = "`";
|
|
break;
|
|
case "pgsql":
|
|
try
|
|
{
|
|
parent::__construct ($dsn, $username, $password, $driver_options);
|
|
parent::setAttribute (PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
|
}
|
|
catch (Exception $e)
|
|
{
|
|
throw new Exception ("PDO error : ".$e->getMessage(), 500);
|
|
}
|
|
|
|
// Set the coding to UTF8
|
|
$this->exec("SET NAMES 'utf8'");
|
|
$this->sep = "\"";
|
|
break;
|
|
default:
|
|
throw new Exception (dgettext("domframework",
|
|
"Unknown PDO driver provided"), 500);
|
|
}
|
|
$this->dsn = $dsn;
|
|
}
|
|
|
|
/** Return the connected database name from DSN used to connect */
|
|
public function databasename ()
|
|
{
|
|
if ($this->sep === "")
|
|
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->sep === "")
|
|
throw new Exception (dgettext("domframework", "Database not connected"),
|
|
500);
|
|
switch ($this->getAttribute(PDO::ATTR_DRIVER_NAME))
|
|
{
|
|
case "sqlite":
|
|
$req = "SELECT name FROM sqlite_master WHERE type='table'";
|
|
$st = $this->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->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->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;
|
|
}
|
|
|
|
/** Verify if the provided datas can be inserted/updated in the database.
|
|
@param $datas An array containing the datas to verify with keys
|
|
@param $updatekey the key to update
|
|
@return an array with the errors in array($key=>array($priority,$message))
|
|
*/
|
|
public function verify ($datas, $updatekey=false)
|
|
{
|
|
if ($this->debug) echo "== Entering verify\n";
|
|
$errors = array ();
|
|
foreach ($this->fields as $key=>$params)
|
|
{
|
|
if ($this->debug) echo " verify ($key)\n";
|
|
if ($updatekey === false)
|
|
{
|
|
// Don't check if there is an update : the database is already filled
|
|
// For autoincrement, in INSERT mode, force the value to null
|
|
if (in_array ("autoincrement", $params))
|
|
$datas[$key] = null;
|
|
if (in_array ("not null", $params) && !array_key_exists ($key, $datas))
|
|
{
|
|
$errors[$key] = array ("error", sprintf (dgettext("domframework",
|
|
"Mandatory field '%s' not provided"),
|
|
$key));
|
|
continue;
|
|
}
|
|
if (in_array ("not null", $params) && $datas[$key] === "")
|
|
{
|
|
$errors[$key] = array ("error", sprintf (dgettext("domframework",
|
|
"Mandatory field '%s' is empty"),
|
|
$key));
|
|
continue;
|
|
}
|
|
}
|
|
|
|
// Do not verify the non provided datas (if they are not mandatory)
|
|
if (!array_key_exists ($key, $datas))
|
|
continue;
|
|
|
|
// Verify the fields, if $verify is defined, before doing insertion
|
|
$verify = $this->verifyOne ($key, $datas[$key]);
|
|
if (is_array ($verify) && count ($verify))
|
|
{
|
|
$errors[$key] = array ($verify[0], $verify[1]);
|
|
//." ". dgettext("domframework","in")." ".$key);
|
|
continue;
|
|
}
|
|
|
|
// Check for type inconsistencies if the value is provided
|
|
if (is_null ($datas[$key]))
|
|
{
|
|
// Skipped the removed autoincrement keys
|
|
continue;
|
|
}
|
|
elseif (! is_string ($datas[$key]) && ! is_integer ($datas[$key]))
|
|
{
|
|
$errors[$key] = array ("error", sprintf (
|
|
dgettext("domframework",
|
|
"Errors in consistency : '%s' is not an integer or a string [is %s]"),
|
|
$key, gettype ($datas[$key])));
|
|
continue;
|
|
}
|
|
elseif ($datas[$key] !== "" && $params[0] === "integer")
|
|
{
|
|
if (strspn ($datas[$key], "0123456789") !== strlen ($datas[$key]))
|
|
{
|
|
$errors[$key] = array ("error", sprintf (
|
|
dgettext("domframework",
|
|
"Errors in consistency : '%s' is not an integer"),
|
|
$key));
|
|
continue;
|
|
}
|
|
}
|
|
elseif ($datas[$key] !== "" && $params[0] === "varchar")
|
|
{
|
|
if (! isset ($params[1]))
|
|
{
|
|
$errors[$key] = array ("error", sprintf (
|
|
dgettext("domframework",
|
|
"The length of varchar field '%s' is not provided"),
|
|
$key));
|
|
continue;
|
|
}
|
|
if (strlen ($datas[$key]) > $params[1])
|
|
{
|
|
$errors[$key] = array ("error", sprintf (
|
|
dgettext("domframework",
|
|
"Errors in consistency : '%s' data is too long"),
|
|
$key));
|
|
continue;
|
|
}
|
|
}
|
|
elseif ($datas[$key] !== "" && $params[0] === "datetime")
|
|
{
|
|
// The date format must be in ANSI SQL : YYYY-MM-DD HH:MM:SS
|
|
$d = DateTime::createFromFormat("Y-m-d H:i:s", $datas[$key]);
|
|
if (!$d || $d->format("Y-m-d H:i:s") !== $datas[$key])
|
|
{
|
|
$errors[$key] = array ("error", sprintf (
|
|
dgettext("domframework",
|
|
"Incorrect datetime provided for field '%s'"),
|
|
$key));
|
|
continue;
|
|
}
|
|
}
|
|
elseif ($datas[$key] !== "" && $params[0] === "date")
|
|
{
|
|
// The date format must be in ANSI SQL : YYYY-MM-DD
|
|
$d = DateTime::createFromFormat("Y-m-d", $datas[$key]);
|
|
if (!$d || $d->format("Y-m-d") !== $datas[$key])
|
|
{
|
|
$errors[$key] = array ("error", sprintf (
|
|
dgettext("domframework",
|
|
"Incorrect date provided for field '%s'"),
|
|
$key));
|
|
continue;
|
|
}
|
|
}
|
|
elseif ($datas[$key] !== "")
|
|
{
|
|
$errors[$key] = array ("error", sprintf (dgettext("domframework",
|
|
"Unknown field type for '%s'"), $key));
|
|
continue;
|
|
}
|
|
else
|
|
{
|
|
// Nothing to do if the value is empty : just save it
|
|
}
|
|
}
|
|
|
|
if (count ($errors) !== 0)
|
|
return $errors;
|
|
|
|
if ($this->debug) echo " verify inconsistency\n";
|
|
// Check for inconsistency
|
|
$verify = $this->verifyAll ($datas);
|
|
if (count ($verify))
|
|
return $verify;
|
|
|
|
$datasOK = array ();
|
|
foreach ($this->fields as $field=>$desc)
|
|
{
|
|
if (isset ($datas[$field]))
|
|
$datasOK[$field] = $datas[$field];
|
|
}
|
|
|
|
if ($updatekey !== false)
|
|
{
|
|
if ($this->debug) echo " verify in updatekey\n";
|
|
// Check if the unique constrain is valid before doing the insertion
|
|
// 1. Read the actual state
|
|
$before = $this->read (array (array ($this->primary, $updatekey)));
|
|
if (count ($before) === 0)
|
|
return array ("error", dgettext("domframework",
|
|
"Entry to modify unavailable"));
|
|
$before = reset ($before);
|
|
// 2. Map the proposal entries into the before state
|
|
$after = $before;
|
|
foreach ($datasOK as $field=>$val)
|
|
$after[$field] = $val;
|
|
}
|
|
else
|
|
{
|
|
if ($this->debug) echo " verify NO updatekey\n";
|
|
$after = $datasOK;
|
|
|
|
}
|
|
// Check if the unique constrain is valid before doing the insertion
|
|
if ($this->unique === null && $this->primary !== null)
|
|
$this->unique = array ($this->primary);
|
|
// TODO : If Unique is not defined nor primary
|
|
// FIXME : If unique is defined, could I test if the primary key is defined
|
|
// in a parameter of the unique and if it is not add it automatically
|
|
foreach ($this->unique as $k=>$columns)
|
|
{
|
|
if ($this->primary === null)
|
|
{
|
|
return array (dgettext("domframework",
|
|
"No field primary defined for tests in primary"));
|
|
}
|
|
|
|
if (is_array ($columns))
|
|
{
|
|
// Multiple columns in unique
|
|
if ($this->debug) echo " verify unique multiple $k\n";
|
|
$select = array ();
|
|
if ($updatekey !== false)
|
|
$select[] = array ($this->primary, $updatekey, "!=");
|
|
foreach ($columns as $col)
|
|
{
|
|
if (!array_key_exists ($col, $after)) continue;
|
|
$select[] = array ($col, $after[$col]);
|
|
}
|
|
// If there is only the primary key, there is no chance to have a
|
|
// conflict
|
|
// Before updating, check if the new values are not creating an error
|
|
$rc = $this->read ($select, array ($this->primary));
|
|
if (count ($rc) > 0)
|
|
{
|
|
$errors[] = array ("error", dgettext("domframework",
|
|
"An entry with these values already exists"));
|
|
continue;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// One column in unique
|
|
if ($this->debug) echo " verify unique one column $k\n";
|
|
if (!array_key_exists ($columns, $after)) continue;
|
|
$select = array ();
|
|
if ($updatekey !== false)
|
|
{
|
|
// This line have a problem to update a tuple with the same values
|
|
// if ($columns === $this->primary)
|
|
$select[] = array ($this->primary, $updatekey, "!=");
|
|
}
|
|
$select[] = array ($columns, $after[$columns]);
|
|
$rc = $this->read ($select,
|
|
array ($this->primary));
|
|
if (count ($rc) > 0)
|
|
{
|
|
$errors[] = array ("error", dgettext("domframework",
|
|
"An entry with this value already exists"));
|
|
continue;
|
|
}
|
|
}
|
|
}
|
|
|
|
// Check if the foreign keys constrains are valid before doing the insertion
|
|
foreach ($this->foreign as $foreign=>$data)
|
|
{
|
|
if ($updatekey === false)
|
|
{
|
|
// Before doing the insert, check the foreign keys. In update, they are
|
|
// not mandatory and are not checked for existancy.
|
|
if ($this->debug) echo " verify foreign $foreign\n";
|
|
if (! isset ($datas[$foreign]))
|
|
{
|
|
$errors[] = array ("error", sprintf (dgettext("domframework",
|
|
"The foreign column '%s' is not provided"),
|
|
$foreign));
|
|
return $errors;
|
|
}
|
|
if (! isset ($datas[$foreign][0]))
|
|
{
|
|
$errors[] = array ("error", sprintf (dgettext("domframework",
|
|
"The field type for column '%s' is not provided"),
|
|
$foreign));
|
|
return $errors;
|
|
continue;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (! array_key_exists ($foreign, $data))
|
|
continue;
|
|
}
|
|
$table = $data[0];
|
|
$column = $data[1];
|
|
$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->prepare ($req);
|
|
$val = $datas[$foreign];
|
|
$key = $column;
|
|
if ($this->debug) echo "DEBUG BIND : ".$this->fields[$foreign][0]."\n";
|
|
if ($this->debug) echo "DEBUG BIND : $column(".md5 ($column)."->".
|
|
var_export ($val, TRUE)."\n";
|
|
if ($val === null)
|
|
$st->bindValue (":".md5 ($key), $val, PDO::PARAM_NULL);
|
|
elseif ($this->fields[$foreign][0] === "integer")
|
|
$st->bindValue (":".md5 ($key), $val, PDO::PARAM_INT);
|
|
elseif ($this->fields[$foreign][0] === "varchar")
|
|
$st->bindValue (":".md5 ($key), $val, PDO::PARAM_STR);
|
|
elseif ($this->fields[$foreign][0] === "datetime")
|
|
$st->bindValue (":".md5 ($key), $val, PDO::PARAM_STR);
|
|
elseif ($this->fields[$foreign][0] === "date")
|
|
$st->bindValue (":".md5 ($key), $val, PDO::PARAM_STR);
|
|
else
|
|
{
|
|
throw new Exception ("TO BE DEVELOPPED : ".$this->fields[$foreign][0],
|
|
500);
|
|
}
|
|
$st->execute ();
|
|
$res = array ();
|
|
while ($d = $st->fetch (PDO::FETCH_ASSOC))
|
|
$res[] = $d;
|
|
if (count ($res) === 0)
|
|
{
|
|
$errors[] = array ("error", sprintf (dgettext("domframework",
|
|
"The foreign key '%s' doesn't exists"),
|
|
$column));
|
|
continue;
|
|
}
|
|
}
|
|
return $errors;
|
|
}
|
|
|
|
/** 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 */
|
|
public function create ($datas)
|
|
{
|
|
return $this->insert ($datas);
|
|
}
|
|
|
|
/** Insert a new line of datas in the table. Datas must be an indexed array
|
|
@param array $datas Datas to be recorded (column=>value)*/
|
|
public function insert ($datas)
|
|
{
|
|
if ($this->debug) echo "== Entering insert\n";
|
|
if ($this->sep === "")
|
|
throw new Exception (dgettext("domframework", "Database not connected"),
|
|
500);
|
|
if ($this->unique === null)
|
|
throw new Exception (dgettext("domframework",
|
|
"Unique fields of table are not defined"), 500);
|
|
if (! is_array ($this->unique))
|
|
throw new Exception (dgettext("domframework",
|
|
"The unique configuration is not an array"), 500);
|
|
if (!is_array ($datas))
|
|
throw new Exception (dgettext("domframework",
|
|
"The datas provided to create are not array"),
|
|
405);
|
|
foreach ($this->fields as $key=>$params)
|
|
{
|
|
if (in_array ("autoincrement", $params))
|
|
$datas[$key] = null;
|
|
}
|
|
if (!in_array ($this->primary, $this->unique))
|
|
$this->unique[] = $this->primary;
|
|
$datasOK = array ();
|
|
$errors = $this->verify ($datas);
|
|
if (count ($errors) !== 0)
|
|
{
|
|
$errors = reset ($errors);
|
|
throw new Exception ($errors[1], 405);
|
|
}
|
|
foreach ($this->fields as $field=>$desc)
|
|
{
|
|
if (isset ($datas[$field]))
|
|
$datasOK[$field] = $datas[$field];
|
|
}
|
|
$binds = array_keys ($datasOK);
|
|
array_walk ($binds, function(&$value, $key) {
|
|
$value = md5 ($value);
|
|
});
|
|
$datasOK = $this->hookpreinsert ($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";
|
|
$st = $this->prepare ($req);
|
|
foreach ($datasOK as $key=>$val)
|
|
{
|
|
if ($this->debug) echo "DEBUG BIND : $key(".md5 ($key).")->".
|
|
var_export ($val, TRUE)."\n";
|
|
if ($val === null)
|
|
$st->bindValue (":".md5 ($key), $val, PDO::PARAM_NULL);
|
|
elseif ($this->fields[$key][0] === "integer")
|
|
$st->bindValue (":".md5 ($key), $val, PDO::PARAM_INT);
|
|
elseif ($this->fields[$key][0] === "varchar")
|
|
$st->bindValue (":".md5 ($key), $val, PDO::PARAM_STR);
|
|
elseif ($this->fields[$key][0] === "datetime")
|
|
$st->bindValue (":".md5 ($key), $val, PDO::PARAM_STR);
|
|
elseif ($this->fields[$key][0] === "date")
|
|
$st->bindValue (":".md5 ($key), $val, PDO::PARAM_STR);
|
|
else
|
|
throw new Exception ("TO BE DEVELOPPED : ".$this->fields[$key][0], 500);
|
|
}
|
|
|
|
try
|
|
{
|
|
$st->execute ();
|
|
}
|
|
catch (Exception $e)
|
|
{
|
|
echo "dblayer execute exception : ".$e->getMessage()."\n";
|
|
exit;
|
|
}
|
|
$lastID = $this->lastInsertId();
|
|
$lastID = $this->hookpostinsert ($datasOK, $lastID);
|
|
return $lastID;
|
|
}
|
|
|
|
/** Read the table content based on a select filter, ordered by order
|
|
operator and the associated select value
|
|
@param array|null $select Rows to select with
|
|
$select = array (array ($key, $val, $operator), ...)
|
|
$key=>column, $val=>value to found, $operator=>'LIKE', =...
|
|
@param array|null $display Columns displayed
|
|
$display = array ($col1, $col2...);
|
|
@param array|null $order Sort the columns by orientation
|
|
$order = array (array ($key, $orientation), ...)
|
|
$key=>column, $orientation=ASC/DESC
|
|
@param bool|null $whereOr The WHERE parameters are separated by OR instead
|
|
of AND
|
|
@param array|null $foreignSelect Add a filter on foreign keys */
|
|
public function read ($select=null, $display=null, $order=null,
|
|
$whereOr=false, $foreignSelect=null)
|
|
{
|
|
if ($this->debug) echo "== Entering read\n";
|
|
if ($this->sep === "")
|
|
throw new Exception (dgettext("domframework", "Database not connected"),
|
|
500);
|
|
if ($select !== null && !is_array ($select))
|
|
throw new Exception (dgettext("domframework",
|
|
"Select information provided is not an array"),
|
|
405);
|
|
if ($display !== null && !is_array ($display))
|
|
throw new Exception (dgettext("domframework",
|
|
"Display information provided is not an array"),
|
|
405);
|
|
if ($order !== null && !is_array ($order))
|
|
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 (dgettext("domframework",
|
|
"Field '%s' not allowed"), $f), 500);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
$display = array_keys ($this->fields);
|
|
}
|
|
|
|
$foreignSelectCols = array ();
|
|
if ($foreignSelect !== null)
|
|
{
|
|
foreach ($foreignSelect as $s)
|
|
$foreignSelectCols[] = $s[0];
|
|
}
|
|
$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 || $foreignSelect !== null)
|
|
{
|
|
$req .= " WHERE (";
|
|
}
|
|
if ($select !== null)
|
|
{
|
|
// TODO Allow a field=>value in plus of array("field","value")
|
|
foreach ($select as $n=>$s)
|
|
{
|
|
// The foreign keys can not be in the select too (conflict)
|
|
if (in_array ($s[0],$foreignSelectCols))
|
|
continue;
|
|
if ($n > 0)
|
|
{
|
|
if ($whereOr === false)
|
|
$req .= " AND";
|
|
else
|
|
$req .= " OR ";
|
|
}
|
|
if (!isset ($s[2]))
|
|
$s[2] = "=";
|
|
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 .= " $this->sep".$s[0]."$this->sep ".$s[2]." :".md5 ($s[0]);
|
|
}
|
|
$req .=")";
|
|
}
|
|
if ($select !== null && $foreignSelect !== null)
|
|
$req .= " AND (";
|
|
if ($foreignSelect !== null)
|
|
{
|
|
// TODO Allow a field=>value in plus of array("field","value")
|
|
foreach ($foreignSelect as $n=>$s)
|
|
{
|
|
if ($n > 0)
|
|
{
|
|
$req .= " AND";
|
|
}
|
|
if (!isset ($s[2]))
|
|
$s[2] = "=";
|
|
$req .= " $this->sep".$s[0]."$this->sep ".$s[2]." :".md5 ($s[0]);
|
|
}
|
|
$req .=")";
|
|
}
|
|
|
|
if ($order !== null)
|
|
{
|
|
$req .= " ORDER BY ";
|
|
foreach ($order as $n=>$o)
|
|
{
|
|
if ($n > 0)
|
|
$req .= ",";
|
|
$req .= $this->sep.$o[0].$this->sep;
|
|
if (isset ($o[1]) && $o[1] === "DESC")
|
|
$req .= " DESC";
|
|
else
|
|
$req .= " ASC";
|
|
}
|
|
}
|
|
|
|
if ($this->debug) echo "DEBUG : $req\n";
|
|
try
|
|
{
|
|
$st = $this->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]."(".md5 ($s[0]).")->".
|
|
var_export ($s[1], TRUE)."\n";
|
|
$st->bindValue (":".md5 ($s[0]), $s[1]);
|
|
}
|
|
}
|
|
if ($foreignSelect !== null)
|
|
{
|
|
foreach ($foreignSelect as $s)
|
|
{
|
|
if ($this->debug) echo "DEBUG BIND : ".$s[0]."(".md5 ($s[0]).")->".
|
|
var_export ($s[1], TRUE)."\n";
|
|
$st->bindValue (":".md5 ($s[0]), $s[1]);
|
|
}
|
|
}
|
|
|
|
$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;
|
|
return $res;
|
|
}
|
|
|
|
/** Update the key tuple with the provided datas
|
|
Return the number of rows modified
|
|
@param string|integer $updatekey The key applied on primary key to be
|
|
updated
|
|
@param array $datas The values to be updated
|
|
@return the number of lines modified */
|
|
public function update ($updatekey, $datas)
|
|
{
|
|
if ($this->debug) echo "== Entering update\n";
|
|
if ($this->sep === "")
|
|
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);
|
|
if (count ($datas) === 0)
|
|
throw new Exception (dgettext("domframework",
|
|
"No data to update provided"), 500);
|
|
$datasOK = array ();
|
|
$errors = $this->verify ($datas, $updatekey);
|
|
if (count ($errors) !== 0)
|
|
{
|
|
if (is_array ($errors))
|
|
throw new Exception ($errors[0][1], 405);
|
|
throw new Exception ($errors[1], 405);
|
|
}
|
|
foreach ($this->fields as $field=>$desc)
|
|
{
|
|
if (isset ($datas[$field]))
|
|
$datasOK[$field] = $datas[$field];
|
|
}
|
|
|
|
$datasOK = $this->hookpreupdate ($updatekey, $datasOK);
|
|
$req = "UPDATE $this->sep".$this->tableprefix."$this->table$this->sep SET ";
|
|
$i = 0;
|
|
foreach ($datasOK as $key=>$val)
|
|
{
|
|
if ($i>0) $req .= ",";
|
|
$req .= "$this->sep$key$this->sep=:".md5 ($key);
|
|
$i++;
|
|
}
|
|
|
|
$req .= " WHERE $this->sep$this->primary$this->sep=:".
|
|
md5 ("PRIMARY".$this->primary);
|
|
if ($this->debug) echo "DEBUG : $req\n";
|
|
$st = $this->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).")->".
|
|
var_export ($val, TRUE)." ";
|
|
if ($val === null)
|
|
{
|
|
if ($this->debug) echo "(null)\n";
|
|
$st->bindValue (":".md5 ($key), $val, PDO::PARAM_NULL);
|
|
}
|
|
elseif ($fields[$key][0] === "integer")
|
|
{
|
|
if ($this->debug) echo "(integer)\n";
|
|
$st->bindValue (":".md5 ($key), $val, PDO::PARAM_INT);
|
|
}
|
|
elseif ($fields[$key][0] === "varchar")
|
|
{
|
|
if ($this->debug) echo "(varchar)\n";
|
|
$st->bindValue (":".md5 ($key), "$val", PDO::PARAM_STR);
|
|
}
|
|
elseif ($fields[$key][0] === "datetime")
|
|
{
|
|
if ($this->debug) echo "(datetime)\n";
|
|
$st->bindValue (":".md5 ($key), $val, PDO::PARAM_STR);
|
|
}
|
|
elseif ($fields[$key][0] === "date")
|
|
{
|
|
if ($this->debug) echo "(date)\n";
|
|
$st->bindValue (":".md5 ($key), $val, PDO::PARAM_STR);
|
|
}
|
|
else
|
|
{
|
|
if ($this->debug) echo "(UNKNOWN)\n";
|
|
throw new Exception ("TO BE DEVELOPPED : ".$fields[$key][0], 500);
|
|
}
|
|
}
|
|
|
|
$st->execute ();
|
|
$nbLinesUpdated = $st->rowCount ();
|
|
$nbLinesUpdated = $this->hookpostupdate ($updatekey, $datasOK,
|
|
$nbLinesUpdated);
|
|
return $nbLinesUpdated;
|
|
}
|
|
|
|
/** Delete a tuple identified by its primary key
|
|
Return the number of deleted rows (can be 0 !)
|
|
@param strin|integer $deletekey The key of primary key to be deleted */
|
|
public function delete ($deletekey)
|
|
{
|
|
if ($this->debug) echo "== Entering delete\n";
|
|
if ($this->sep === "")
|
|
throw new Exception (dgettext("domframework", "Database not connected"));
|
|
$deletekey = $this->hookpredelete ($deletekey);
|
|
$req = "DELETE FROM $this->sep$this->tableprefix$this->table$this->sep ";
|
|
$req .= "WHERE $this->primary = :primary";
|
|
$st = $this->prepare ($req);
|
|
if ($this->debug) echo "DEBUG : $req\n";
|
|
if ($this->debug) echo "DEBUG BIND : primary->".
|
|
var_export ($deletekey, TRUE)."\n";
|
|
$st->bindValue (":primary", $deletekey);
|
|
$st->execute ();
|
|
$nbLinesDeleted = $st->rowCount();
|
|
$nbLinesDeleted = $this->hookpostdelete ($deletekey, $nbLinesDeleted);
|
|
return $nbLinesDeleted;
|
|
}
|
|
|
|
/** Translation of fields */
|
|
public function titles ()
|
|
{
|
|
if ($this->debug) echo "== Entering titles\n";
|
|
if (count ($this->fields) === 0)
|
|
throw new Exception (dgettext("domframework", "No Field defined"), 500);
|
|
$arr = array ();
|
|
if (count ($this->titles) !== 0)
|
|
{
|
|
foreach ($this->titles as $field=>$v)
|
|
$arr[$field] = $field;
|
|
}
|
|
else
|
|
{
|
|
foreach ($this->fields as $field=>$v)
|
|
$arr[$field] = $field;
|
|
}
|
|
return $arr;
|
|
}
|
|
|
|
/** Drop the table */
|
|
public function dropTable ()
|
|
{
|
|
if ($this->debug) echo "== Entering dropTables\n";
|
|
if ($this->sep === "")
|
|
throw new Exception (dgettext("domframework", "Database not connected"));
|
|
$sql = "DROP TABLE $this->sep$this->tableprefix$this->table$this->sep";
|
|
if ($this->debug)
|
|
echo "$sql\n";
|
|
return $this->exec($sql);
|
|
}
|
|
|
|
/** Create the table defined by the differents fields.
|
|
Define the SQL syntax based on SQL engines
|
|
$table = "dns zones";
|
|
$fields = array (
|
|
"id"=>array ("integer", "not null", "autoincrement"),
|
|
"zo ne"=>array ("varchar", "255", "not null"),
|
|
"vie wname"=>array ("varchar", "255"),
|
|
"view clients"=>array ("varchar", "255"),
|
|
"comme nt"=>array ("varchar", "1024"),
|
|
"opendate"=>array ("datetime", "not null"),
|
|
"closedate"=>array ("datetime"),
|
|
);
|
|
$primary = "id";
|
|
$unique = array ("id", array ("zo ne", "vie wname"));
|
|
$foreign = array ("zone"=>"table.field",...);
|
|
*/
|
|
public function createTable ()
|
|
{
|
|
if ($this->debug) echo "== Entering createTable\n";
|
|
if ($this->sep === "")
|
|
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->getAttribute(PDO::ATTR_DRIVER_NAME))
|
|
{
|
|
case "sqlite":
|
|
$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 .= "$this->sep$field$this->sep ";
|
|
// Type of field : in $params[0]
|
|
if (!isset ($params[0]))
|
|
throw new Exception (sprintf (
|
|
dgettext("domframework",
|
|
"No database type defined for field '%s'"),
|
|
$field), 500);
|
|
|
|
switch ($params[0])
|
|
{
|
|
case "integer":
|
|
$sql .= "INTEGER";
|
|
$params = array_slice ($params, 1);
|
|
break;
|
|
case "varchar":
|
|
if (!isset ($params[1]))
|
|
throw new Exception (dgettext("domframework",
|
|
"No Size provided for varchar field"), 500);
|
|
$sql .= "VARCHAR(".$params[1].")";
|
|
$params = array_slice ($params, 2);
|
|
break;
|
|
case "datetime":
|
|
$sql .= "DATETIME";
|
|
$params = array_slice ($params, 1);
|
|
break;
|
|
case "date":
|
|
$sql .= "DATE";
|
|
$params = array_slice ($params, 1);
|
|
break;
|
|
default:
|
|
throw new Exception (sprintf (
|
|
dgettext("domframework",
|
|
"Unknown type '%s' provided for field '%s'"),
|
|
$params[0], $field), 500);
|
|
}
|
|
// Primary key
|
|
if ($this->primary === $field)
|
|
$sql .= " PRIMARY KEY";
|
|
// Others parameters for field
|
|
// Sort to put the autoincrement field in front of params, if it is
|
|
// present
|
|
sort ($params);
|
|
foreach ($params as $p)
|
|
{
|
|
switch ($p)
|
|
{
|
|
case "not null": $sql .= " NOT NULL"; break;
|
|
case "autoincrement": $sql .= " AUTOINCREMENT";break;
|
|
default:
|
|
throw new Exception (dgettext("domframework",
|
|
"Unknown additionnal parameter for field"),
|
|
500);
|
|
}
|
|
}
|
|
$i ++;
|
|
}
|
|
// Unique fields
|
|
if ($this->unique !== null)
|
|
{
|
|
if (!is_array ($this->unique))
|
|
throw new Exception (dgettext("domframework",
|
|
"The Unique field definition is not an array"),
|
|
500);
|
|
foreach ($this->unique as $u)
|
|
{
|
|
$sql .= ",\n UNIQUE ($this->sep";
|
|
if (is_array ($u))
|
|
$sql .=implode ("$this->sep,$this->sep", $u);
|
|
else
|
|
$sql .= $u;
|
|
$sql .="$this->sep)";
|
|
}
|
|
}
|
|
// Foreign keys
|
|
$i = 0;
|
|
foreach ($this->foreign as $field=>$k)
|
|
{
|
|
$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++;
|
|
}
|
|
$sql .=")";
|
|
break;
|
|
case "mysql":
|
|
$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 .= "$this->sep$field$this->sep ";
|
|
// Type of field : in $params[0]
|
|
if (!isset ($params[0]))
|
|
throw new Exception (dgettext("domframework",
|
|
"No database type defined for field"), 500);
|
|
switch ($params[0])
|
|
{
|
|
case "integer":
|
|
$sql .= "INTEGER";
|
|
$params = array_slice ($params, 1);
|
|
break;
|
|
case "varchar":
|
|
if (!isset ($params[1]))
|
|
throw new Exception (dgettext("domframework",
|
|
"No Size provided for varchar field"), 500);
|
|
$sql .= "VARCHAR(".$params[1].")";
|
|
$params = array_slice ($params, 2);
|
|
break;
|
|
case "datetime":
|
|
$sql .= "DATETIME";
|
|
$params = array_slice ($params, 1);
|
|
break;
|
|
case "date":
|
|
$sql .= "DATE";
|
|
$params = array_slice ($params, 1);
|
|
break;
|
|
default:
|
|
throw new Exception (sprintf (
|
|
dgettext("domframework",
|
|
"Unknown type provided for field '%s'"),
|
|
$field), 500);
|
|
}
|
|
// Primary key
|
|
if ($this->primary === $field)
|
|
$sql .= " PRIMARY KEY";
|
|
// Others parameters for field
|
|
// Sort to put the autoincrement field in front of params, if it is
|
|
// present
|
|
sort ($params);
|
|
foreach ($params as $p)
|
|
{
|
|
switch ($p)
|
|
{
|
|
case "not null": $sql .= " NOT NULL"; break;
|
|
case "autoincrement": $sql .= " AUTO_INCREMENT";break;
|
|
default:
|
|
throw new Exception (sprintf (
|
|
dgettext("domframework",
|
|
"Unknown additionnal parameter for field '%s'"),
|
|
$field), 500);
|
|
}
|
|
}
|
|
$i ++;
|
|
}
|
|
// Unique fields
|
|
if ($this->unique !== null)
|
|
{
|
|
foreach ($this->unique as $u)
|
|
{
|
|
$sql .= ",\n UNIQUE ($this->sep";
|
|
if (is_array ($u))
|
|
$sql .=implode ("$this->sep,$this->sep", $u);
|
|
else
|
|
$sql .= $u;
|
|
$sql .="$this->sep)";
|
|
}
|
|
}
|
|
// Foreign keys
|
|
$i = 0;
|
|
foreach ($this->foreign as $field=>$k)
|
|
{
|
|
$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)
|
|
$sql .= ",";
|
|
$i++;
|
|
}
|
|
$sql .=") ENGINE=InnoDB DEFAULT CHARSET=utf8;";
|
|
break;
|
|
case "pgsql":
|
|
$sql = "CREATE TABLE \"$this->tableprefix$this->table\" (\n";
|
|
$i = 0;
|
|
foreach ($this->fields as $field=>$params)
|
|
{
|
|
if ($i > 0)
|
|
$sql .= ",\n";
|
|
// Name of field
|
|
$sql .= "\"$field\" ";
|
|
if (in_array ("autoincrement", $params))
|
|
$sql .= "SERIAL";
|
|
else
|
|
{
|
|
// Type of field : in $params[0]
|
|
if (!isset ($params[0]))
|
|
throw new Exception (sprintf (
|
|
dgettext("domframework",
|
|
"No database type defined for field '%s'"),
|
|
$field), 500);
|
|
switch ($params[0])
|
|
{
|
|
case "integer":
|
|
$sql .= "INTEGER";
|
|
$params = array_slice ($params, 1);
|
|
break;
|
|
case "varchar":
|
|
if (!isset ($params[1]))
|
|
throw new Exception (sprintf (
|
|
dgettext("domframework",
|
|
"No Size provided for varchar field '%s'"),
|
|
$field), 500);
|
|
$sql .= "VARCHAR(".$params[1].")";
|
|
$params = array_slice ($params, 2);
|
|
break;
|
|
case "datetime":
|
|
$sql .= "timestamp with time zone";
|
|
$params = array_slice ($params, 1);
|
|
break;
|
|
case "date":
|
|
$sql .= "DATE";
|
|
$params = array_slice ($params, 1);
|
|
break;
|
|
default:
|
|
throw new Exception (sprintf (
|
|
dgettext("domframework",
|
|
"Unknown type provided for field '%s'"),
|
|
$field), 500);
|
|
}
|
|
// Primary key
|
|
if ($this->primary === $field)
|
|
$sql .= " PRIMARY KEY";
|
|
// Others parameters for field
|
|
// Sort to put the autoincrement field in front of params, if it is
|
|
// present
|
|
sort ($params);
|
|
foreach ($params as $p)
|
|
{
|
|
switch ($p)
|
|
{
|
|
case "not null": $sql .= " NOT NULL"; break;
|
|
default:
|
|
throw new Exception (sprintf (
|
|
dgettext("domframework",
|
|
"Unknown additionnal parameter for field '%s'"),
|
|
$field), 500);
|
|
}
|
|
}
|
|
}
|
|
$i ++;
|
|
}
|
|
// Unique fields
|
|
if ($this->unique !== null)
|
|
{
|
|
foreach ($this->unique as $u)
|
|
{
|
|
$sql .= ",\n UNIQUE (\"";
|
|
if (is_array ($u))
|
|
$sql .=implode ("\",\"", $u);
|
|
else
|
|
$sql .= $u;
|
|
$sql .="\")";
|
|
}
|
|
}
|
|
// Foreign keys
|
|
$i = 0;
|
|
foreach ($this->foreign as $field=>$k)
|
|
{
|
|
$sql .= ",\n FOREIGN KEY(\"$field\") REFERENCES \"".$k[0]."\"(\"".
|
|
$k[1]."\")";
|
|
if (isset ($k[2]))
|
|
$sql .= " ".$k[2];
|
|
if ($i > 0)
|
|
$sql .= ",";
|
|
$i++;
|
|
}
|
|
$sql .=")";
|
|
break;
|
|
default:
|
|
throw new Exception (dgettext("domframework",
|
|
"PDO Engine not supported in dbLayer"), 500);
|
|
}
|
|
|
|
if ($this->debug)
|
|
echo "$sql\n";
|
|
return $this->exec($sql);
|
|
}
|
|
|
|
/** This function permit to send a SQL request to the database to do a SELECT
|
|
Return the an array with the datas */
|
|
public function directRead ($sql)
|
|
{
|
|
if ($this->debug) echo "== Entering directRead\n";
|
|
$st = $this->prepare ($sql);
|
|
$st->execute ();
|
|
$res = array ();
|
|
while ($d = $st->fetch (PDO::FETCH_ASSOC))
|
|
$res[] = $d;
|
|
return $res;
|
|
}
|
|
|
|
/** Hook preinsert
|
|
This hook is run before inserting a new data in the database, after the
|
|
verification
|
|
@param array the data to insert in the database
|
|
@return the modified datas */
|
|
public function hookpreinsert ($data)
|
|
{
|
|
return $data;
|
|
}
|
|
|
|
/** Hook postinsert
|
|
This hook is run after successfuly insert a new data in the database
|
|
@return the modified lastID */
|
|
public function hookpostinsert ($data, $lastID)
|
|
{
|
|
return $lastID;
|
|
}
|
|
|
|
/** Hook preupdate
|
|
This hook is run before updating a data in the database, after the
|
|
verification
|
|
@return the modified datas */
|
|
public function hookpreupdate ($updatekey, $data)
|
|
{
|
|
return $data;
|
|
}
|
|
|
|
/** Hook postupdate
|
|
This hook is run after successfuly update a data in the database
|
|
@return the modified $nbLinesUpdated */
|
|
public function hookpostupdate ($updatekey, $data, $nbLinesUpdated)
|
|
{
|
|
return $nbLinesUpdated;
|
|
}
|
|
|
|
/** Hook predelete
|
|
This hook is run before deleting a data in the database
|
|
@return the modified $deletekey */
|
|
public function hookpredelete ($deletekey)
|
|
{
|
|
return $deletekey;
|
|
}
|
|
|
|
/** Hook postdelete
|
|
This hook is run after successfuly deleting a data in the database
|
|
@return $nbLinesUpdated */
|
|
public function hookpostdelete ($deletekey, $nbLinesDeleted)
|
|
{
|
|
return $nbLinesDeleted;
|
|
}
|
|
}
|
|
|
|
/** POC :
|
|
error_reporting (E_ALL);
|
|
require_once ("domframework/dbLayer.php");
|
|
class zone extends dbLayer
|
|
{
|
|
// The database must be initialized with
|
|
// CREATE TABLE `dns_zones` (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
|
|
// zone VARCHAR(255) NOT NULL,
|
|
// viewname VARCHAR(255),
|
|
// viewclients VARCHAR(255),
|
|
// comment VARCHAR(1024),
|
|
// opendate DATETIME NOT NULL,
|
|
// closedate DATETIME,
|
|
// UNIQUE (zone,viewname));
|
|
protected $table = "dns_zones";
|
|
protected $fields = array (
|
|
"id"=>array ("integer", "not null", "autoincrement"),
|
|
"zone"=>array ("varchar", "255", "not null"),
|
|
"viewname"=>array ("varchar", "255"),
|
|
"viewclients"=>array ("varchar", "255"),
|
|
"comment"=>array ("varchar", "1024"),
|
|
"opendate"=>array ("datetime", "not null"),
|
|
"closedate"=>array ("datetime"),
|
|
);
|
|
protected $primary = "id";
|
|
protected $unique = array ("id", array ("zone", "viewname"));
|
|
}
|
|
|
|
ini_set ("date.timezone", "Europe/Paris");
|
|
$zone = new zone ("sqlite:datas/database.db");
|
|
$last = $zone->create (array ("zone"=>"testZone",
|
|
"opendate"=>date("Y-m-d H:i:s")));
|
|
//print_r ($zone->read ());
|
|
$zone->update (2040, array ("zone"=>"testZone2"));
|
|
print_r ($zone->delete ($last));
|
|
*/
|