From 8254777c76209a8f91777110d48f64144ae9a9b6 Mon Sep 17 00:00:00 2001 From: Jephte Clain Date: Tue, 28 May 2024 17:56:42 +0400 Subject: [PATCH] modifs.mineures sans commentaires --- src/db/sqlite/_query.php | 89 ++++++++++++++++++++++++++-------- tests/db/sqlite/_queryTest.php | 24 +++++++-- 2 files changed, 88 insertions(+), 25 deletions(-) diff --git a/src/db/sqlite/_query.php b/src/db/sqlite/_query.php index 2bbe609..169453e 100644 --- a/src/db/sqlite/_query.php +++ b/src/db/sqlite/_query.php @@ -83,43 +83,92 @@ class _query { } else { ## associatif # paramètre - $param = $key; - if ($params !== null && array_key_exists($param, $params)) { - $i = 1; + $i = false; + if ($params !== null && array_key_exists($key, $params)) { + $i = 2; while (array_key_exists("$key$i", $params)) { $i++; } - $param = "$key$i"; } # value ou [operator, value] + $condprefix = $condsep = $condsuffix = null; if (is_array($cond)) { - #XXX implémenter le support de ["between", lower, upper] - # et aussi ["in", values] - $op = null; - $value = null; - $condkeys = array_keys($cond); - if (array_key_exists("op", $cond)) $op = $cond["op"]; - if (array_key_exists("value", $cond)) $value = $cond["value"]; $condkey = 0; - if ($op === null && array_key_exists($condkey, $condkeys)) { + $condkeys = array_keys($cond); + $op = null; + if (array_key_exists("op", $cond)) { + $op = $cond["op"]; + } elseif (array_key_exists($condkey, $condkeys)) { $op = $cond[$condkeys[$condkey]]; $condkey++; } - if ($value === null && array_key_exists($condkey, $condkeys)) { - $value = $cond[$condkeys[$condkey]]; - $condkey++; + $op = strtolower($op); + switch ($op) { + case "between": + $condsep = " and "; + $values = null; + if (array_key_exists("lower", $cond)) { + $values[] = $cond["lower"]; + } elseif (array_key_exists($condkey, $condkeys)) { + $values[] = $cond[$condkeys[$condkey]]; + $condkey++; + } + if (array_key_exists("upper", $cond)) { + $values[] = $cond["upper"]; + } elseif (array_key_exists($condkey, $condkeys)) { + $values[] = $cond[$condkeys[$condkey]]; + $condkey++; + } + break; + case "in": + $condprefix = "("; + $condsep = ", "; + $condsuffix = ")"; + $values = null; + if (array_key_exists("values", $cond)) { + $values = cl::with($cond["values"]); + } elseif (array_key_exists($condkey, $condkeys)) { + $values = cl::with($cond[$condkeys[$condkey]]); + $condkey++; + } + break; + case "null": + case "is null": + $op = "is null"; + $values = null; + break; + case "not null": + case "is not null": + $op = "is not null"; + $values = null; + break; + default: + $values = null; + if (array_key_exists("value", $cond)) { + $values = [$cond["value"]]; + } elseif (array_key_exists($condkey, $condkeys)) { + $values = [$cond[$condkeys[$condkey]]]; + $condkey++; + } } } elseif ($cond !== null) { $op = "="; - $value = $cond; + $values = [$cond]; } else { $op = "is null"; - $value = null; + $values = null; } $cond = [$key, $op]; - if ($value !== null) { - $cond[] = ":$param"; - $params[$param] = $value; + if ($values !== null) { + $parts = []; + foreach ($values as $value) { + $param = "$key$i"; + $parts[] = ":$param"; + $params[$param] = $value; + if ($i === false) $i = 2; + else $i++; + } + $cond[] = $condprefix.implode($condsep, $parts).$condsuffix; } $condsql[] = implode(" ", $cond); } diff --git a/tests/db/sqlite/_queryTest.php b/tests/db/sqlite/_queryTest.php index aac8f75..8eed3fb 100644 --- a/tests/db/sqlite/_queryTest.php +++ b/tests/db/sqlite/_queryTest.php @@ -42,13 +42,28 @@ class _queryTest extends TestCase { $sql = $params = null; _query::parse_conds([["int" => 42, "string" => "value"], ["int" => 24, "string" => "eulav"]], $sql, $params); - self::assertSame(["((int = :int and string = :string) and (int = :int1 and string = :string1))"], $sql); - self::assertSame(["int" => 42, "string" => "value", "int1" => 24, "string1" => "eulav"], $params); + self::assertSame(["((int = :int and string = :string) and (int = :int2 and string = :string2))"], $sql); + self::assertSame(["int" => 42, "string" => "value", "int2" => 24, "string2" => "eulav"], $params); $sql = $params = null; _query::parse_conds(["int" => ["is null"], "string" => ["<>", "value"]], $sql, $params); self::assertSame(["(int is null and string <> :string)"], $sql); self::assertSame(["string" => "value"], $params); + + $sql = $params = null; + _query::parse_conds(["col" => ["between", "lower", "upper"]], $sql, $params); + self::assertSame(["col between :col and :col2"], $sql); + self::assertSame(["col" => "lower", "col2" => "upper"], $params); + + $sql = $params = null; + _query::parse_conds(["col" => ["in", "one"]], $sql, $params); + self::assertSame(["col in (:col)"], $sql); + self::assertSame(["col" => "one"], $params); + + $sql = $params = null; + _query::parse_conds(["col" => ["in", ["one", "two"]]], $sql, $params); + self::assertSame(["col in (:col, :col2)"], $sql); + self::assertSame(["col" => "one", "col2" => "two"], $params); } function testParseValues(): void { @@ -84,8 +99,7 @@ class _queryTest extends TestCase { $sql = $params = null; _query::parse_set_values([["int" => 42, "string" => "value"], ["int" => 24, "string" => "eulav"]], $sql, $params); - self::assertSame(["int = :int", "string = :string", "int = :int1", "string = :string1"], $sql); - self::assertSame(["int" => 42, "string" => "value", "int1" => 24, "string1" => "eulav"], $params); + self::assertSame(["int = :int", "string = :string", "int = :int2", "string = :string2"], $sql); + self::assertSame(["int" => 42, "string" => "value", "int2" => 24, "string2" => "eulav"], $params); } - }