Correct bug in uniq constrain violation test in update for dblayer

git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@1240 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
This commit is contained in:
2014-03-23 19:58:55 +00:00
parent bd575e67ed
commit 7e69a0e978

View File

@@ -101,7 +101,7 @@ class dblayer extends PDO
$rc = $this->read ($select, array ($this->primary)); $rc = $this->read ($select, array ($this->primary));
if (count ($rc) > 0) if (count ($rc) > 0)
throw new Exception (sprintf ( throw new Exception (sprintf (
_("The provided value for columns '%s' already exists"), _("The provided values for columns '%s' already exists"),
implode (",", $columns)), 405); implode (",", $columns)), 405);
} }
else else
@@ -111,7 +111,7 @@ class dblayer extends PDO
array ($this->primary)); array ($this->primary));
if (count ($rc) > 0) if (count ($rc) > 0)
throw new Exception (sprintf ( throw new Exception (sprintf (
_("The provided value for column '%s' already exists"), _("The column '%s' with this value already exists"),
$columns), 405); $columns), 405);
} }
} }
@@ -241,6 +241,16 @@ class dblayer extends PDO
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 // 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)
throw new Exception (_("Entry to modify unavailable"), 404);
$before = reset ($before);
// 2. Map the proposal entries into the before state
$after = $before;
foreach ($datasOK as $field=>$val)
$after[$field] = $val;
// 3. Check for constrain violation on unique columns
foreach ($this->unique as $columns) foreach ($this->unique as $columns)
{ {
if (is_array ($columns)) if (is_array ($columns))
@@ -249,8 +259,8 @@ class dblayer extends PDO
$select[] = array ($this->primary, $updatekey, "!="); $select[] = array ($this->primary, $updatekey, "!=");
foreach ($columns as $col) foreach ($columns as $col)
{ {
if (!array_key_exists ($col, $datasOK)) continue; if (!array_key_exists ($col, $after)) continue;
$select[] = array ($col, $datasOK[$col]); $select[] = array ($col, $after[$col]);
} }
// If there is only the primary key, there is no chance to have a // If there is only the primary key, there is no chance to have a
@@ -260,19 +270,22 @@ class dblayer extends PDO
$rc = $this->read ($select, array ($this->primary)); $rc = $this->read ($select, array ($this->primary));
if (count ($rc) > 0) if (count ($rc) > 0)
throw new Exception (sprintf ( throw new Exception (sprintf (
_("The provided value for columns '%s' already exists"), _("The provided values for columns '%s' already exists"),
implode (",", $columns)), 405); implode (",", $columns)), 405);
} }
} }
else else
{ {
if (!array_key_exists ($columns, $datasOK)) continue; if (!array_key_exists ($columns, $datasOK)) continue;
$rc = $this->read (array (array ($columns, $datasOK[$columns]), $select = array ();
array ($this->primary, $updatekey, "!=")), if ($columns !== $this->primary)
$select[] = array ($this->primary, $updatekey, "!=");
$select[] = array ($columns, $datasOK[$columns]);
$rc = $this->read ($select,
array ($this->primary)); array ($this->primary));
if (count ($rc) > 0) if (count ($rc) > 0)
throw new Exception (sprintf ( throw new Exception (sprintf (
_("The provided value for column '%s' already exists"), _("An entry already exists with this value in the column '%s'"),
$columns), 405); $columns), 405);
} }
} }