modifs.mineures sans commentaires

This commit is contained in:
Jephté Clain 2024-05-09 12:14:49 +04:00
parent 86cab90788
commit cf89396467
2 changed files with 39 additions and 6 deletions

View File

@ -275,9 +275,13 @@ class _Query {
if ($key === $index) { if ($key === $index) {
$userorderby[] = $value; $userorderby[] = $value;
$index++; $index++;
} elseif ($value !== null) { } else {
if (is_bool($value)) $value = $value? "asc": "desc"; if ($value === null) $value = false;
if (!is_bool($value)) {
$userorderby[] = "$key $value"; $userorderby[] = "$key $value";
} elseif ($value) {
$userorderby[] = $key;
}
} }
} }
} }
@ -297,11 +301,16 @@ class _Query {
if ($key === $index) { if ($key === $index) {
$usergroupby[] = $value; $usergroupby[] = $value;
$index++; $index++;
} elseif ($value !== null) { } else {
if ($value === null) $value = false;
if (!is_bool($value)) {
$usergroupby[] = "$key $value";
} elseif ($value) {
$usergroupby[] = $key; $usergroupby[] = $key;
} }
} }
} }
}
if ($usergroupby) { if ($usergroupby) {
$sql[] = "group by"; $sql[] = "group by";
$sql[] = implode(", ", $usergroupby); $sql[] = implode(", ", $usergroupby);
@ -345,10 +354,14 @@ class _Query {
static function parse_insert(array $query, ?array &$params=null): string { static function parse_insert(array $query, ?array &$params=null): string {
# fusionner d'abord toutes les parties séquentielles # fusionner d'abord toutes les parties séquentielles
$usersql = $tmpsql = self::merge_seq($query); $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 = []; $sql = [];
if (($prefix = $query["prefix"] ?? null) !== null) $sql[] = $prefix; if (($prefix = $query["prefix"] ?? null) !== null) $sql[] = $prefix;
## insert
self::consume('insert\s*', $tmpsql); $sql[] = "insert"; self::consume('insert\s*', $tmpsql); $sql[] = "insert";
## into
self::consume('into\s*', $tmpsql); $sql[] = "into"; self::consume('into\s*', $tmpsql); $sql[] = "into";
$into = $query["into"] ?? null; $into = $query["into"] ?? null;
if (self::consume('([a-z_][a-z0-9_]*)\s*', $tmpsql, $ms)) { if (self::consume('([a-z_][a-z0-9_]*)\s*', $tmpsql, $ms)) {
@ -359,6 +372,8 @@ class _Query {
} else { } else {
throw new ValueException("expected table name: $usersql"); throw new ValueException("expected table name: $usersql");
} }
## cols & values
$usercols = []; $usercols = [];
$uservalues = []; $uservalues = [];
if (self::consume('\(([^)]*)\)\s*', $tmpsql, $ms)) { if (self::consume('\(([^)]*)\)\s*', $tmpsql, $ms)) {
@ -382,6 +397,8 @@ class _Query {
if (self::consume('values\s+\(\s*(.*)\s*\)\s*', $tmpsql, $ms)) { if (self::consume('values\s+\(\s*(.*)\s*\)\s*', $tmpsql, $ms)) {
if ($ms[1]) $uservalues[] = $ms[1]; if ($ms[1]) $uservalues[] = $ms[1];
} }
## fin de la requête
self::consume(';\s*', $tmpsql); self::consume(';\s*', $tmpsql);
if ($tmpsql) { if ($tmpsql) {
throw new ValueException("unexpected value at end: $usersql"); throw new ValueException("unexpected value at end: $usersql");

View File

@ -121,4 +121,20 @@ class SqliteTest extends TestCase {
"plouf" => "jclain1", "plouf" => "jclain1",
], $sqlite->one(["select", "cols" => ["plouf" => "name"], "from" => "user", "where" => ["name" => "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"]])));
}
} }