modifs.mineures sans commentaires

This commit is contained in:
Jephté Clain 2024-05-28 17:56:42 +04:00
parent 55ea8cdc81
commit 8254777c76
2 changed files with 88 additions and 25 deletions

View File

@ -83,43 +83,92 @@ class _query {
} else { } else {
## associatif ## associatif
# paramètre # paramètre
$param = $key; $i = false;
if ($params !== null && array_key_exists($param, $params)) { if ($params !== null && array_key_exists($key, $params)) {
$i = 1; $i = 2;
while (array_key_exists("$key$i", $params)) { while (array_key_exists("$key$i", $params)) {
$i++; $i++;
} }
$param = "$key$i";
} }
# value ou [operator, value] # value ou [operator, value]
$condprefix = $condsep = $condsuffix = null;
if (is_array($cond)) { 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; $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]]; $op = $cond[$condkeys[$condkey]];
$condkey++; $condkey++;
} }
if ($value === null && array_key_exists($condkey, $condkeys)) { $op = strtolower($op);
$value = $cond[$condkeys[$condkey]]; switch ($op) {
$condkey++; 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) { } elseif ($cond !== null) {
$op = "="; $op = "=";
$value = $cond; $values = [$cond];
} else { } else {
$op = "is null"; $op = "is null";
$value = null; $values = null;
} }
$cond = [$key, $op]; $cond = [$key, $op];
if ($value !== null) { if ($values !== null) {
$cond[] = ":$param"; $parts = [];
$params[$param] = $value; 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); $condsql[] = implode(" ", $cond);
} }

View File

@ -42,13 +42,28 @@ class _queryTest extends TestCase {
$sql = $params = null; $sql = $params = null;
_query::parse_conds([["int" => 42, "string" => "value"], ["int" => 24, "string" => "eulav"]], $sql, $params); _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 = :int and string = :string) and (int = :int2 and string = :string2))"], $sql);
self::assertSame(["int" => 42, "string" => "value", "int1" => 24, "string1" => "eulav"], $params); self::assertSame(["int" => 42, "string" => "value", "int2" => 24, "string2" => "eulav"], $params);
$sql = $params = null; $sql = $params = null;
_query::parse_conds(["int" => ["is null"], "string" => ["<>", "value"]], $sql, $params); _query::parse_conds(["int" => ["is null"], "string" => ["<>", "value"]], $sql, $params);
self::assertSame(["(int is null and string <> :string)"], $sql); self::assertSame(["(int is null and string <> :string)"], $sql);
self::assertSame(["string" => "value"], $params); 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 { function testParseValues(): void {
@ -84,8 +99,7 @@ class _queryTest extends TestCase {
$sql = $params = null; $sql = $params = null;
_query::parse_set_values([["int" => 42, "string" => "value"], ["int" => 24, "string" => "eulav"]], $sql, $params); _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 = :int", "string = :string", "int = :int2", "string = :string2"], $sql);
self::assertSame(["int" => 42, "string" => "value", "int1" => 24, "string1" => "eulav"], $params); self::assertSame(["int" => 42, "string" => "value", "int2" => 24, "string2" => "eulav"], $params);
} }
} }