From cf893964671af86b72fc142add7314d2375f6ada Mon Sep 17 00:00:00 2001 From: Jephte Clain Date: Thu, 9 May 2024 12:14:49 +0400 Subject: [PATCH] modifs.mineures sans commentaires --- src/db/sqlite/_Query.php | 29 +++++++++++++++++++++++------ tests/db/sqlite/SqliteTest.php | 16 ++++++++++++++++ 2 files changed, 39 insertions(+), 6 deletions(-) diff --git a/src/db/sqlite/_Query.php b/src/db/sqlite/_Query.php index 9e1ef41..c06a0b0 100644 --- a/src/db/sqlite/_Query.php +++ b/src/db/sqlite/_Query.php @@ -275,9 +275,13 @@ class _Query { if ($key === $index) { $userorderby[] = $value; $index++; - } elseif ($value !== null) { - if (is_bool($value)) $value = $value? "asc": "desc"; - $userorderby[] = "$key $value"; + } else { + if ($value === null) $value = false; + if (!is_bool($value)) { + $userorderby[] = "$key $value"; + } elseif ($value) { + $userorderby[] = $key; + } } } } @@ -297,8 +301,13 @@ class _Query { if ($key === $index) { $usergroupby[] = $value; $index++; - } elseif ($value !== null) { - $usergroupby[] = $key; + } else { + if ($value === null) $value = false; + if (!is_bool($value)) { + $usergroupby[] = "$key $value"; + } elseif ($value) { + $usergroupby[] = $key; + } } } } @@ -345,10 +354,14 @@ class _Query { static function parse_insert(array $query, ?array &$params=null): string { # fusionner d'abord toutes les parties séquentielles $usersql = $tmpsql = self::merge_seq($query); - # vérifier la présence des parties nécessaires + ### vérifier la présence des parties nécessaires $sql = []; if (($prefix = $query["prefix"] ?? null) !== null) $sql[] = $prefix; + + ## insert self::consume('insert\s*', $tmpsql); $sql[] = "insert"; + + ## into self::consume('into\s*', $tmpsql); $sql[] = "into"; $into = $query["into"] ?? null; if (self::consume('([a-z_][a-z0-9_]*)\s*', $tmpsql, $ms)) { @@ -359,6 +372,8 @@ class _Query { } else { throw new ValueException("expected table name: $usersql"); } + + ## cols & values $usercols = []; $uservalues = []; if (self::consume('\(([^)]*)\)\s*', $tmpsql, $ms)) { @@ -382,6 +397,8 @@ class _Query { if (self::consume('values\s+\(\s*(.*)\s*\)\s*', $tmpsql, $ms)) { if ($ms[1]) $uservalues[] = $ms[1]; } + + ## fin de la requête self::consume(';\s*', $tmpsql); if ($tmpsql) { throw new ValueException("unexpected value at end: $usersql"); diff --git a/tests/db/sqlite/SqliteTest.php b/tests/db/sqlite/SqliteTest.php index 55c7563..1186653 100644 --- a/tests/db/sqlite/SqliteTest.php +++ b/tests/db/sqlite/SqliteTest.php @@ -121,4 +121,20 @@ class SqliteTest extends TestCase { "plouf" => "jclain1", ], $sqlite->one(["select", "cols" => ["plouf" => "name"], "from" => "user", "where" => ["name" => "jclain1"]])); } + + function testSelectGroupBy() { + $sqlite = new Sqlite(":memory:", [ + "migrate" => "create table user (name varchar, amount integer)", + ]); + $sqlite->exec(["insert into user", "values" => ["name" => "jclain1", "amount" => 1]]); + $sqlite->exec(["insert into user", "values" => ["name" => "jclain2", "amount" => 1]]); + $sqlite->exec(["insert into user", "values" => ["name" => "jclain5", "amount" => 2]]); + $sqlite->exec(["insert into user", "values" => ["name" => "fclain7", "amount" => 2]]); + $sqlite->exec(["insert into user", "values" => ["name" => "fclain9", "amount" => 2]]); + $sqlite->exec(["insert into user", "values" => ["name" => "fclain10", "amount" => 3]]); + + self::assertSame([ + ["count" => 2], + ], iterator_to_array($sqlite->all(["select count(name) as count from user", "group by" => ["amount"], "having" => ["count(name) = 2"]]))); + } }