modifs.mineures sans commentaires
This commit is contained in:
parent
86cab90788
commit
cf89396467
|
@ -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;
|
||||||
$userorderby[] = "$key $value";
|
if (!is_bool($value)) {
|
||||||
|
$userorderby[] = "$key $value";
|
||||||
|
} elseif ($value) {
|
||||||
|
$userorderby[] = $key;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -297,8 +301,13 @@ class _Query {
|
||||||
if ($key === $index) {
|
if ($key === $index) {
|
||||||
$usergroupby[] = $value;
|
$usergroupby[] = $value;
|
||||||
$index++;
|
$index++;
|
||||||
} elseif ($value !== null) {
|
} else {
|
||||||
$usergroupby[] = $key;
|
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 {
|
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");
|
||||||
|
|
|
@ -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"]])));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue