dblayer : Add the prefix support in tables name

dblayer : Add $table public attribute in the definition


git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@1591 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
This commit is contained in:
2014-07-30 06:31:04 +00:00
parent 8e5824a503
commit 7c935b49ef

View File

@@ -33,6 +33,11 @@ require_once ("domframework/verify.php");
/** Permit abstraction on the differents SQL databases available */ /** Permit abstraction on the differents SQL databases available */
class dblayer extends PDO class dblayer extends PDO
{ {
/** The table name to use */
public $table = null;
/** The prefix text to prepend to table name (Should finish by _)
Just allow chars ! */
public $prefix = "";
/** The fields with the definition of type, and special parameters */ /** The fields with the definition of type, and special parameters */
public $fields = array (); public $fields = array ();
/** The primary field */ /** The primary field */
@@ -276,7 +281,7 @@ class dblayer extends PDO
$column), 405); $column), 405);
} }
$req = "INSERT INTO `".$this->table."` "; $req = "INSERT INTO `".$this->prefix.$this->table."` ";
$req .= "(".implode (",", array_keys ($datasOK)).")"; $req .= "(".implode (",", array_keys ($datasOK)).")";
$req .= " VALUES "; $req .= " VALUES ";
$req .= "(:".implode (",:", array_keys ($datasOK)).")"; $req .= "(:".implode (",:", array_keys ($datasOK)).")";
@@ -339,7 +344,7 @@ class dblayer extends PDO
$req = "SELECT "; $req = "SELECT ";
$req .= implode (",", $display); $req .= implode (",", $display);
$req .= " FROM `".$this->table."`"; $req .= " FROM `".$this->prefix.$this->table."`";
if ($select !== null) if ($select !== null)
{ {
$req .= " WHERE "; $req .= " WHERE ";
@@ -499,7 +504,7 @@ class dblayer extends PDO
} }
$datasOK[$this->primary] = $updatekey; $datasOK[$this->primary] = $updatekey;
$req = "UPDATE `".$this->table."` SET "; $req = "UPDATE `".$this->prefix.$this->table."` SET ";
$i = 0; $i = 0;
foreach ($datasOK as $key=>$val) foreach ($datasOK as $key=>$val)
{ {
@@ -552,7 +557,7 @@ class dblayer extends PDO
{ {
if ($this->db === null) if ($this->db === null)
throw new Exception ("Database not connected"); throw new Exception ("Database not connected");
$req = "DELETE FROM `".$this->table."` "; $req = "DELETE FROM `".$this->prefix.$this->table."` ";
$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";
@@ -595,7 +600,7 @@ 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->table` (\n"; $sql = "CREATE TABLE `$this->prefix.$this->table` (\n";
$i = 0; $i = 0;
foreach ($this->fields as $field=>$params) foreach ($this->fields as $field=>$params)
{ {
@@ -676,7 +681,7 @@ class dblayer extends PDO
$sql .=")"; $sql .=")";
break; break;
case "mysql": case "mysql":
$sql = "CREATE TABLE `$this->table` (\n"; $sql = "CREATE TABLE `$this->prefix.$this->table` (\n";
$i = 0; $i = 0;
foreach ($this->fields as $field=>$params) foreach ($this->fields as $field=>$params)
{ {
@@ -755,7 +760,7 @@ class dblayer extends PDO
$sql .=") ENGINE=InnoDB DEFAULT CHARSET=utf8;"; $sql .=") ENGINE=InnoDB DEFAULT CHARSET=utf8;";
break; break;
case "pgsql": case "pgsql":
$sql = "CREATE TABLE \"$this->table\" (\n"; $sql = "CREATE TABLE \"$this->prefix.$this->table\" (\n";
$i = 0; $i = 0;
foreach ($this->fields as $field=>$params) foreach ($this->fields as $field=>$params)
{ {