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" ; the primary key of the table (in text). Actually the dbLayer abstraction don't supports primary key on multiples columns Optionnaly, you can add the - protected $debug = TRUE : enable the debug on screen (NOT FOR PROD !!) */ /** Permit abstraction on the differents SQL databases available */ class dblayer extends PDO { protected $fields = array (); protected $primary = null; protected $db = null; public $debug = FALSE; /** Return all the tables available in the database */ function listTables () { $driver = $this->getAttribute(PDO::ATTR_DRIVER_NAME); $rc = @include_once ("dbLayer".ucfirst ($driver).".php"); if ($rc === FALSE) throw new Exception ("dbLayer driver $driver not available"); } // 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*/ /** Connection to the database engine See http://fr2.php.net/manual/en/pdo.construct.php for the $dsn format */ function __construct ($dsn, $username=null, $password=null, $driver_options=null) { $this->db = new PDO ($dsn, $username, $password, $driver_options); $this->db->setAttribute (PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } /** Create a new entry in the table. Datas must be an indexed array */ function create ($datas) { if ($this->db === null) throw new Exception ("Database not connected"); $datasOK = array (); // Check for missing parameters foreach ($this->fields as $key=>$params) { if (in_array ("autoincrement", $params)) $datas[$key] = null; if (in_array ("not null", $params) && !array_key_exists ($key, $datas)) throw new Exception ("Mandatory field '$key' not provided"); // TODO : Check for type inconsistancies before create $datasOK if (array_key_exists ($key, $datas)) $datasOK[$key] = $datas[$key]; } $req = "INSERT INTO `".$this->table."` "; $req .= "(".implode (",", array_keys ($datasOK)).")"; $req .= " VALUES "; $req .= "(:".implode (",:", array_keys ($datasOK)).")"; 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 ($val === null) $st->bindValue (":$key", $val, PDO::PARAM_NULL); elseif ($this->fields[$key][0] === "integer") $st->bindValue (":$key", $val, PDO::PARAM_INT); elseif ($this->fields[$key][0] === "varchar") $st->bindValue (":$key", $val, PDO::PARAM_STR); elseif ($this->fields[$key][0] === "datetime") $st->bindValue (":$key", $val, PDO::PARAM_STR); else throw new Exception ("TO BE DEVELOPPED : ".$this->fields[$key][0]); } $st->execute (); return $this->db->lastInsertId(); } /** Read all the table or only one line defined by the primary key */ function read ($selectkey=null) { if ($this->db === null) throw new Exception ("Database not connected"); $req = "SELECT "; $req .= implode (",", array_keys ($this->fields)); $req .= " FROM `".$this->table."`"; if ($selectkey !== NULL) $req .= " WHERE $this->primary=:primary"; if ($this->debug) echo "DEBUG : $req\n"; $st = $this->db->prepare ($req); if ($selectkey !== NULL) { if ($this->debug) echo "DEBUG BIND : primary->". var_export ($selectkey, TRUE)."\n"; $st->bindValue (":primary", $selectkey); } $st->execute (); $res = array (); while ($d = $st->fetch (PDO::FETCH_ASSOC)) $res[] = $d; return $res; } /** Update the key tuple with the provided datas */ function update ($updatekey, $datas) { if ($this->db === null) throw new Exception ("Database not connected"); $datasOK = array (); // Check for missing parameters foreach ($this->fields as $key=>$params) { // TODO : Check for type inconsistancies before create $datasOK if (array_key_exists ($key, $datas)) $datasOK[$key] = $datas[$key]; } $req = "UPDATE `".$this->table."` SET "; $i = 0; foreach ($datasOK as $key=>$val) { if ($i>0) $req .= ","; $req .= "$key=:$key"; $i++; } $req .= " WHERE $this->primary=:primary"; if ($this->debug) echo "DEBUG : $req\n"; $st = $this->db->prepare ($req); if ($this->debug) echo "DEBUG BIND : primary->". var_export ($updatekey, TRUE)."\n"; $st->bindValue (":primary", $updatekey); foreach ($datasOK as $key=>$val) { if ($this->debug) echo "DEBUG BIND : $key->".var_export ($val, TRUE)."\n"; if ($val === null) $st->bindValue (":$key", $val, PDO::PARAM_NULL); elseif ($this->fields[$key][0] === "integer") $st->bindValue (":$key", $val, PDO::PARAM_INT); elseif ($this->fields[$key][0] === "varchar") $st->bindValue (":$key", $val, PDO::PARAM_STR); elseif ($this->fields[$key][0] === "datetime") $st->bindValue (":$key", $val, PDO::PARAM_STR); else throw new Exception ("TO BE DEVELOPPED : ".$this->fields[$key][0]); } $st->execute (); } /** Delete a tuple identified by its primary key */ function delete ($deletekey) { if ($this->db === null) throw new Exception ("Database not connected"); $req = "DELETE FROM `".$this->table."` "; $req .= "WHERE $this->primary = :primary"; $st = $this->db->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 (); } } /** 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"; } 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)); */