dblayeroo: Update of multiple not unique fields should not be an exception

git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@3825 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
This commit is contained in:
2017-07-24 19:05:11 +00:00
parent ea8bbcf37b
commit e98686d91c
2 changed files with 57 additions and 20 deletions

View File

@@ -829,4 +829,29 @@ class test_dblayeroo_{ENGINE} extends PHPUnit_Framework_TestCase
$res); $res);
} }
public function test_update5 ()
{
// Manage to update the non unique fields
// the "where" column is not unique, so it allow to have twice the same
// value
$db1 = $this->db1 ();
$res = $db1->update()
->setValues(array ("where"=>"where2"))
->execute ();
$db1->disconnect ();
$this->assertSame (2, $res);
}
public function test_update6 ()
{
// Manage to update the primary / unique fields
// There is 2 lines in the DB, so the unique key "group" can not be updated
// with the same value twice (the result can not be unique)
$db1 = $this->db1 ();
$this->setExpectedException ("Exception");
$res = $db1->update()
->setValues(array ("group"=>"group3"))
->execute ();
$db1->disconnect ();
}
} }

View File

@@ -2539,36 +2539,48 @@ class dblayeroo
if (! in_array ($this->primary, $uniques)) if (! in_array ($this->primary, $uniques))
$uniques = array_merge (array ($this->primary), $uniques); $uniques = array_merge (array ($this->primary), $uniques);
$setValues = $values; $setValues = $values;
if (! array_key_exists ($this->primary, $setValues)) $foundImpactedColumns = 0;
$setValues[$this->primary] = null;
foreach ($uniques as $k=>$columns) foreach ($uniques as $k=>$columns)
{ {
if ($update !== false && ! isset ($resUpdate)) if ($update)
{ {
// Can not update multiple UNIQUE rows with the same value // Can not update multiple UNIQUE rows with the same value
$this->debugLog ("CLONE because of update"); $cols = explode (",", $columns);
$objTmp = clone $this; foreach ($cols as $col)
$objTmp->debugDepth++; {
$objTmp->clearRequest (); if (!key_exists ($col, $setValues))
$objTmp->Select (); continue;
$objTmp->displayAdd ($this->primary); // One column to set is a unique column : check if the where clause
$objTmp->displayAdd ($columns); // is filtering more than one entry. If there is more than one
$objTmp->whereValues = $this->whereValues; // entry, generate an exception
$objTmp->whereExpression = $this->whereExpression; $this->debugLog ("CLONE because of update");
$objTmp->limitLines (3); $objTmp = clone $this;
$resUpdate = $objTmp->execute (); $objTmp->debugDepth++;
unset ($objTmp); $objTmp->clearRequest ();
/* if (count ($resUpdate) > 1) $objTmp->Select ();
$this->DBException (sprintf (dgettext ("domframework", $objTmp->displayAdd ($this->primary);
"Can't update multiple rows with unique value"), $columns)); $objTmp->displayAdd ($columns);
*/ $objTmp->whereValues = $this->whereValues;
if (count ($resUpdate) === 0) $objTmp->whereExpression = $this->whereExpression;
$objTmp->limitLines (3);
$resUpdate = $objTmp->execute ();
unset ($objTmp);
if (count ($resUpdate) > 1)
$this->DBException (sprintf (dgettext ("domframework",
"Can't update multiple rows with unique value on col '%s'"),
$col));
elseif (count ($resUpdate) === 1)
$foundImpactedColumns++;
}
if ($foundImpactedColumns === 0)
{ {
// There is no row available with the WHERE clause provided // There is no row available with the WHERE clause provided
// Skip all the UNIQUE tests as there will not have any modification // Skip all the UNIQUE tests as there will not have any modification
break; break;
} }
} }
if (! array_key_exists ($this->primary, $setValues))
$setValues[$this->primary] = null;
$this->debugLog ("CLONE to check primary and unique constraint"); $this->debugLog ("CLONE to check primary and unique constraint");
$objTmp = clone $this; $objTmp = clone $this;