dblayer : bug in read with OR if the select fields are multiple time the same

git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@2757 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
This commit is contained in:
2016-06-09 13:11:26 +00:00
parent 3bda1f7c63
commit 230f8b7603
2 changed files with 53 additions and 19 deletions

View File

@@ -40,7 +40,8 @@ class test_dblayer_{ENGINE} extends PHPUnit_Framework_TestCase
$dbconfig = $this->confs["{ENGINE}"];
$db = new dblayer ($dbconfig["dsn"], $dbconfig["username"],
$dbconfig["password"], $dbconfig["driver_options"]);
foreach (array ("users", "grouped", "multiple", "multiple2", "users3") as
foreach (array ("users", "grouped", "multiple", "multiple2", "users3",
"readOR") as
$table)
{
$db->table = $table;
@@ -362,4 +363,25 @@ class test_dblayer_{ENGINE} extends PHPUnit_Framework_TestCase
$db->disconnect ();
$this->assertSame (0, $res);
}
/** Check the OR feature on the same column with different value */
public function test_readOR1 ()
{
$dbconfig = $this->confs["{ENGINE}"];
$db = new dblayer ($dbconfig["dsn"], $dbconfig["username"],
$dbconfig["password"], $dbconfig["driver_options"]);
$db->table = "readOR";
$db->fields = array ("id"=>array ("integer"),
"user"=>array ("varchar", "255", "not null"));
$db->primary = "id";
$db->unique = array ("id");
$db->createTable ();
$db->insert (array ("id"=>"0", "user"=>"test0"));
$db->insert (array ("id"=>"1", "user"=>"test1"));
$res = $db->read (array (array ("id", 0), array ("id", 1)), array ("user"),
null, true);
$db->disconnect ();
$this->assertSame (array (0=>array ("user"=>"test0"),
1=>array ("user"=>"test1")), $res);
}
}

View File

@@ -581,6 +581,8 @@ class dblayer
if (count ($errors) !== 0)
{
$errors = reset ($errors);
if (! is_array ($errors))
$errors = array (0=>"error", 1=>$errors);
throw new Exception ($errors[1], 405);
}
foreach ($this->fields as $field=>$desc)
@@ -634,18 +636,20 @@ class dblayer
}
/** Read the table content based on a select filter, ordered by order
operator and the associated select value
@param array|null $select Rows to select with
$select = array (array ($key, $val, $operator), ...)
$key=>column, $val=>value to found, $operator=>'LIKE', =...
@param array|null $display Columns displayed
$display = array ($col1, $col2...);
@param array|null $order Sort the columns by orientation
$order = array (array ($key, $orientation), ...)
$key=>column, $orientation=ASC/DESC
@param bool|null $whereOr The WHERE parameters are separated by OR instead
of AND
@param array|null $foreignSelect Add a filter on foreign keys */
* operator and the associated select value
* @param array|null $select Rows to select with
* $select = array (array ($key, $val, $operator), ...)
* $key=>column, $val=>value to found, $operator=>'LIKE', =...
* @param array|null $display Columns displayed
* $display = array ($col1, $col2...);
* @param array|null $order Sort the columns by orientation
* $order = array (array ($key, $orientation), ...)
* $key=>column, $orientation=ASC/DESC
* @param bool|null $whereOr The WHERE parameters are separated by OR instead
* of AND
* @param array|null $foreignSelect Add a filter on foreign keys
* @return array array ([0] => array (column=>value, column=>value),);
*/
public function read ($select=null, $display=null, $order=null,
$whereOr=false, $foreignSelect=null)
{
@@ -704,6 +708,10 @@ class dblayer
// The foreign keys can not be in the select too (conflict)
if (in_array ($s[0],$foreignSelectCols))
continue;
if (! array_key_exists (0, $s))
throw new Exception ("Select field for key $n not provided", 406);
if (! array_key_exists (1, $s))
throw new Exception ("Select value for key $n not provided", 406);
if ($n > 0)
{
if ($whereOr === false)
@@ -720,7 +728,7 @@ class dblayer
// name is 'group'
// Don't put single quotes : don't work with SQLite
// TODO : Test for PostgreSQL (Tested for SQLite and MySQL)
$req .= " $this->sep".$s[0]."$this->sep ".$s[2]." :".md5 ($s[0]);
$req .= " $this->sep".$s[0]."$this->sep ".$s[2]." :".md5 ($s[0].$s[1]);
}
$req .=")";
}
@@ -731,13 +739,17 @@ class dblayer
// TODO Allow a field=>value in plus of array("field","value")
foreach ($foreignSelect as $n=>$s)
{
if (! array_key_exists (0, $s))
throw new Exception ("Foreign field for key $n not provided", 406);
if (! array_key_exists (1, $s))
throw new Exception ("Foreign value for key $n not provided", 406);
if ($n > 0)
{
$req .= " AND";
}
if (!isset ($s[2]))
$s[2] = "=";
$req .= " $this->sep".$s[0]."$this->sep ".$s[2]." :".md5 ($s[0]);
$req .= " $this->sep".$s[0]."$this->sep ".$s[2]." :".md5 ($s[0].$s[1]);
}
$req .=")";
}
@@ -773,18 +785,18 @@ class dblayer
{
foreach ($select as $s)
{
if ($this->debug) echo "DEBUG BIND : ".$s[0]."(".md5 ($s[0]).")->".
if ($this->debug) echo "DEBUG BIND : ".$s[0]."(".md5 ($s[0].$s[1]).")->".
var_export ($s[1], TRUE)."\n";
$st->bindValue (":".md5 ($s[0]), $s[1]);
$st->bindValue (":".md5 ($s[0].$s[1]), $s[1]);
}
}
if ($foreignSelect !== null)
{
foreach ($foreignSelect as $s)
{
if ($this->debug) echo "DEBUG BIND : ".$s[0]."(".md5 ($s[0]).")->".
if ($this->debug) echo "DEBUG BIND : ".$s[0]."(".md5 ($s[0].$s[1]).")->".
var_export ($s[1], TRUE)."\n";
$st->bindValue (":".md5 ($s[0]), $s[1]);
$st->bindValue (":".md5 ($s[0].$s[1]), $s[1]);
}
}