Add a unique constrain in the tables in dblayer. Block the creation/update if the constrain is forced.
git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@1232 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
This commit is contained in:
97
dblayer.php
97
dblayer.php
@@ -18,6 +18,7 @@ To use it, extends in your code this class, and define the attributes :
|
|||||||
);
|
);
|
||||||
- protected $primary = "id" ; the primary key of the table (in text). Actually
|
- protected $primary = "id" ; the primary key of the table (in text). Actually
|
||||||
the dbLayer abstraction don't supports primary key on multiples columns
|
the dbLayer abstraction don't supports primary key on multiples columns
|
||||||
|
- protected $unique = array ("column", array ("column1", "column2");)
|
||||||
|
|
||||||
Optionnaly, you can add the
|
Optionnaly, you can add the
|
||||||
- protected $debug = TRUE : enable the debug on screen (NOT FOR PROD !!)
|
- protected $debug = TRUE : enable the debug on screen (NOT FOR PROD !!)
|
||||||
@@ -28,6 +29,7 @@ class dblayer extends PDO
|
|||||||
{
|
{
|
||||||
protected $fields = array ();
|
protected $fields = array ();
|
||||||
protected $primary = null;
|
protected $primary = null;
|
||||||
|
protected $unique = null;
|
||||||
protected $db = null;
|
protected $db = null;
|
||||||
public $debug = FALSE;
|
public $debug = FALSE;
|
||||||
/** Return all the tables available in the database */
|
/** Return all the tables available in the database */
|
||||||
@@ -36,7 +38,8 @@ class dblayer extends PDO
|
|||||||
$driver = $this->getAttribute(PDO::ATTR_DRIVER_NAME);
|
$driver = $this->getAttribute(PDO::ATTR_DRIVER_NAME);
|
||||||
$rc = @include_once ("dbLayer".ucfirst ($driver).".php");
|
$rc = @include_once ("dbLayer".ucfirst ($driver).".php");
|
||||||
if ($rc === FALSE)
|
if ($rc === FALSE)
|
||||||
throw new Exception ("dbLayer driver $driver not available");
|
throw new Exception (sprintf (_("dbLayer driver %s not available"),
|
||||||
|
$driver), 500);
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO !!
|
// TODO !!
|
||||||
@@ -63,9 +66,14 @@ class dblayer extends PDO
|
|||||||
function create ($datas)
|
function create ($datas)
|
||||||
{
|
{
|
||||||
if ($this->db === null)
|
if ($this->db === null)
|
||||||
throw new Exception ("Database not connected");
|
throw new Exception (_("Database not connected"), 500);
|
||||||
|
if ($this->unique === null)
|
||||||
|
throw new Exception (_("Unique fields of table are not defined"), 500);
|
||||||
if (!is_array ($datas))
|
if (!is_array ($datas))
|
||||||
throw new Exception ("The datas provided to create are not array");
|
throw new Exception (_("The datas provided to create are not array"),
|
||||||
|
405);
|
||||||
|
if (!in_array ($this->primary, $this->unique))
|
||||||
|
$this->unique[] = $this->primary;
|
||||||
$datasOK = array ();
|
$datasOK = array ();
|
||||||
// Check for missing parameters
|
// Check for missing parameters
|
||||||
foreach ($this->fields as $key=>$params)
|
foreach ($this->fields as $key=>$params)
|
||||||
@@ -73,12 +81,43 @@ class dblayer extends PDO
|
|||||||
if (in_array ("autoincrement", $params))
|
if (in_array ("autoincrement", $params))
|
||||||
$datas[$key] = null;
|
$datas[$key] = null;
|
||||||
if (in_array ("not null", $params) && !array_key_exists ($key, $datas))
|
if (in_array ("not null", $params) && !array_key_exists ($key, $datas))
|
||||||
throw new Exception ("Mandatory field '$key' not provided");
|
throw new Exception ("Mandatory field '$key' not provided", 405);
|
||||||
// TODO : Check for type inconsistancies before create $datasOK
|
// TODO : Check for type inconsistancies before create $datasOK
|
||||||
if (array_key_exists ($key, $datas))
|
if (array_key_exists ($key, $datas))
|
||||||
$datasOK[$key] = $datas[$key];
|
$datasOK[$key] = $datas[$key];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check if the unique constrain is valid before doing the insertion
|
||||||
|
foreach ($this->unique as $columns)
|
||||||
|
{
|
||||||
|
if (is_array ($columns))
|
||||||
|
{
|
||||||
|
$select = array ();
|
||||||
|
foreach ($columns as $col)
|
||||||
|
{
|
||||||
|
if (!array_key_exists ($col, $datasOK)) continue;
|
||||||
|
$select[] = array ($col, $datasOK[$col]);
|
||||||
|
}
|
||||||
|
$rc = $this->read ($select, array ($this->primary));
|
||||||
|
if (count ($rc) > 0)
|
||||||
|
throw new Exception (sprintf (
|
||||||
|
_("The provided value for columns '%s' already exists"),
|
||||||
|
implode (",", $columns)), 405);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (!array_key_exists ($columns, $datasOK)) continue;
|
||||||
|
$rc = $this->read (array (array ($columns, $datasOK[$columns])),
|
||||||
|
array ($this->primary));
|
||||||
|
if (count ($rc) > 0)
|
||||||
|
throw new Exception (sprintf (
|
||||||
|
_("The provided value for column '%s' already exists"),
|
||||||
|
$columns), 405);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO : Check if the foreign keys constrains are valid before doing the
|
||||||
|
// insertion
|
||||||
$req = "INSERT INTO `".$this->table."` ";
|
$req = "INSERT INTO `".$this->table."` ";
|
||||||
$req .= "(".implode (",", array_keys ($datasOK)).")";
|
$req .= "(".implode (",", array_keys ($datasOK)).")";
|
||||||
$req .= " VALUES ";
|
$req .= " VALUES ";
|
||||||
@@ -97,7 +136,7 @@ class dblayer extends PDO
|
|||||||
elseif ($this->fields[$key][0] === "datetime")
|
elseif ($this->fields[$key][0] === "datetime")
|
||||||
$st->bindValue (":$key", $val, PDO::PARAM_STR);
|
$st->bindValue (":$key", $val, PDO::PARAM_STR);
|
||||||
else
|
else
|
||||||
throw new Exception ("TO BE DEVELOPPED : ".$this->fields[$key][0]);
|
throw new Exception ("TO BE DEVELOPPED : ".$this->fields[$key][0], 500);
|
||||||
}
|
}
|
||||||
|
|
||||||
$st->execute ();
|
$st->execute ();
|
||||||
@@ -116,13 +155,22 @@ class dblayer extends PDO
|
|||||||
function read ($select=null, $display=null, $order=null)
|
function read ($select=null, $display=null, $order=null)
|
||||||
{
|
{
|
||||||
if ($this->db === null)
|
if ($this->db === null)
|
||||||
throw new Exception ("Database not connected");
|
throw new Exception (_("Database not connected"), 500);
|
||||||
|
if ($select !== null && !is_array ($select))
|
||||||
|
throw new Exception (_("Select information provided is not an array"),
|
||||||
|
405);
|
||||||
|
if ($display !== null && !is_array ($display))
|
||||||
|
throw new Exception (_("Display information provided is not an array"),
|
||||||
|
405);
|
||||||
|
if ($order !== null && !is_array ($order))
|
||||||
|
throw new Exception (_("Order information provided is not an array"),
|
||||||
|
405);
|
||||||
if ($display !== null)
|
if ($display !== null)
|
||||||
{
|
{
|
||||||
foreach ($display as $f)
|
foreach ($display as $f)
|
||||||
{
|
{
|
||||||
if (!in_array ($f, array_keys ($this->fields)))
|
if (!in_array ($f, array_keys ($this->fields)))
|
||||||
throw new Exception ("Field $f not allowed");
|
throw new Exception (sprintf (_("Field %s not allowed"), $f), 506);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$req = "SELECT ";
|
$req = "SELECT ";
|
||||||
@@ -180,7 +228,7 @@ class dblayer extends PDO
|
|||||||
function update ($updatekey, $datas)
|
function update ($updatekey, $datas)
|
||||||
{
|
{
|
||||||
if ($this->db === null)
|
if ($this->db === null)
|
||||||
throw new Exception ("Database not connected");
|
throw new Exception (_("Database not connected"), 500);
|
||||||
$datasOK = array ();
|
$datasOK = array ();
|
||||||
// Check for missing parameters
|
// Check for missing parameters
|
||||||
foreach ($this->fields as $key=>$params)
|
foreach ($this->fields as $key=>$params)
|
||||||
@@ -192,6 +240,36 @@ class dblayer extends PDO
|
|||||||
if (count ($datasOK) === 0)
|
if (count ($datasOK) === 0)
|
||||||
throw new Exception (_("Don't receive any field to display"), 501);
|
throw new Exception (_("Don't receive any field to display"), 501);
|
||||||
|
|
||||||
|
// Check if the unique constrain is valid before doing the insertion
|
||||||
|
foreach ($this->unique as $columns)
|
||||||
|
{
|
||||||
|
if (is_array ($columns))
|
||||||
|
{
|
||||||
|
$select = array ();
|
||||||
|
foreach ($columns as $col)
|
||||||
|
{
|
||||||
|
if (!array_key_exists ($col, $datasOK)) continue;
|
||||||
|
$select[] = array ($col, $datasOK[$col]);
|
||||||
|
}
|
||||||
|
$rc = $this->read ($select, array ($this->primary));
|
||||||
|
if (count ($rc) > 0)
|
||||||
|
throw new Exception (sprintf (
|
||||||
|
_("The provided value for columns '%s' already exists"),
|
||||||
|
implode (",", $columns)), 405);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (!array_key_exists ($columns, $datasOK)) continue;
|
||||||
|
$rc = $this->read (array (array ($columns, $datasOK[$columns])),
|
||||||
|
array ($this->primary));
|
||||||
|
if (count ($rc) > 0)
|
||||||
|
throw new Exception (sprintf (
|
||||||
|
_("The provided value for column '%s' already exists"),
|
||||||
|
$columns), 405);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO : Check if the foreign keys constrains are valid before doing the
|
||||||
$req = "UPDATE `".$this->table."` SET ";
|
$req = "UPDATE `".$this->table."` SET ";
|
||||||
$i = 0;
|
$i = 0;
|
||||||
foreach ($datasOK as $key=>$val)
|
foreach ($datasOK as $key=>$val)
|
||||||
@@ -219,7 +297,7 @@ class dblayer extends PDO
|
|||||||
elseif ($this->fields[$key][0] === "datetime")
|
elseif ($this->fields[$key][0] === "datetime")
|
||||||
$st->bindValue (":$key", $val, PDO::PARAM_STR);
|
$st->bindValue (":$key", $val, PDO::PARAM_STR);
|
||||||
else
|
else
|
||||||
throw new Exception ("TO BE DEVELOPPED : ".$this->fields[$key][0]);
|
throw new Exception ("TO BE DEVELOPPED : ".$this->fields[$key][0], 500);
|
||||||
}
|
}
|
||||||
|
|
||||||
$st->execute ();
|
$st->execute ();
|
||||||
@@ -269,6 +347,7 @@ class zone extends dbLayer
|
|||||||
"closedate"=>array ("datetime"),
|
"closedate"=>array ("datetime"),
|
||||||
);
|
);
|
||||||
protected $primary = "id";
|
protected $primary = "id";
|
||||||
|
protected $unique = array ("id", array ("zone", "viewname"));
|
||||||
}
|
}
|
||||||
|
|
||||||
ini_set ("date.timezone", "Europe/Paris");
|
ini_set ("date.timezone", "Europe/Paris");
|
||||||
|
|||||||
Reference in New Issue
Block a user