From 7e69a0e978f0f3e596fa279b00c649b04de0e041 Mon Sep 17 00:00:00 2001 From: Dominique Fournier Date: Sun, 23 Mar 2014 19:58:55 +0000 Subject: [PATCH] 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 --- dblayer.php | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/dblayer.php b/dblayer.php index fa4926f..73e3fc0 100644 --- a/dblayer.php +++ b/dblayer.php @@ -101,8 +101,8 @@ class dblayer extends PDO $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); + _("The provided values for columns '%s' already exists"), + implode (",", $columns)), 405); } else { @@ -111,7 +111,7 @@ class dblayer extends PDO array ($this->primary)); if (count ($rc) > 0) throw new Exception (sprintf ( - _("The provided value for column '%s' already exists"), + _("The column '%s' with this value already exists"), $columns), 405); } } @@ -241,6 +241,16 @@ class dblayer extends PDO throw new Exception (_("Don't receive any field to display"), 501); // 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) { if (is_array ($columns)) @@ -249,8 +259,8 @@ class dblayer extends PDO $select[] = array ($this->primary, $updatekey, "!="); foreach ($columns as $col) { - if (!array_key_exists ($col, $datasOK)) continue; - $select[] = array ($col, $datasOK[$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 @@ -260,20 +270,23 @@ class dblayer extends PDO $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); + _("The provided values 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, $updatekey, "!=")), + $select = array (); + if ($columns !== $this->primary) + $select[] = array ($this->primary, $updatekey, "!="); + $select[] = array ($columns, $datasOK[$columns]); + $rc = $this->read ($select, array ($this->primary)); if (count ($rc) > 0) throw new Exception (sprintf ( - _("The provided value for column '%s' already exists"), - $columns), 405); + _("An entry already exists with this value in the column '%s'"), + $columns), 405); } }