modifs.mineures sans commentaires

This commit is contained in:
Jephté Clain 2024-05-02 23:08:23 +04:00
parent ef781dcf37
commit 58f561a4e2
1 changed files with 51 additions and 28 deletions

View File

@ -37,6 +37,22 @@ class _Query {
return true; return true;
} }
/** fusionner toutes les parties séquentielles d'une requête */
static function merge_seq(array $query): string {
$index = 0;
$sql = "";
foreach ($query as $key => $value) {
if ($key === $index) {
$index++;
if ($sql && !str::ends_with(" ", $sql) && !str::starts_with(" ", $value)) {
$sql .= " ";
}
$sql .= $value;
}
}
return $sql;
}
const create_SCHEMA = [ const create_SCHEMA = [
"prefix" => "string", "prefix" => "string",
"table" => "string", "table" => "string",
@ -64,6 +80,26 @@ class _Query {
return false; return false;
} }
static function parse_select(array $query, ?array &$params=null): string { static function parse_select(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
$sql = [];
if (($prefix = $query["prefix"] ?? null) !== null) $sql[] = $prefix;
self::consume('select\s*', $tmpsql); $sql[] = "select";
if (self::consume('(.*)\s*(?:\s+(from)\s*(?:\s+([a-z_][a-z0-9_]*)\s*(?:(\S+.*)\s*(?:\s+(where)\s*)?)?)?)?', $tmpsql, $ms)) {
$usercols = $ms[1];
$userfrom = $ms[2];
$usertable = $ms[3];
$userjoins = $ms[4];
$userwhere = $ms[5];
} else {
throw new ValueException("expected select query: $usersql");
}
self::consume(';\s*', $tmpsql);
if ($tmpsql) {
throw new ValueException("unexpected value at end: $usersql");
}
return implode(" ", $sql);
} }
const insert_SCHEMA = [ const insert_SCHEMA = [
@ -78,23 +114,10 @@ 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
$index = 0; $usersql = $tmpsql = self::merge_seq($query);
$tmpsql = "";
foreach ($query as $key => $value) {
if ($key === $index) {
$index++;
if ($tmpsql && !str::ends_with(" ", $tmpsql) && !str::starts_with(" ", $value)) {
$tmpsql .= " ";
}
$tmpsql .= $value;
}
}
$usersql = $tmpsql;
# 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) { if (($prefix = $query["prefix"] ?? null) !== null) $sql[] = $prefix;
$sql["prefix"] = $prefix;
}
self::consume('insert\s*', $tmpsql); $sql[] = "insert"; self::consume('insert\s*', $tmpsql); $sql[] = "insert";
self::consume('into\s*', $tmpsql); $sql[] = "into"; self::consume('into\s*', $tmpsql); $sql[] = "into";
if (self::consume('([a-z_][a-z0-9_]*)\s*', $tmpsql, $ms)) { if (self::consume('([a-z_][a-z0-9_]*)\s*', $tmpsql, $ms)) {
@ -104,42 +127,42 @@ class _Query {
} else { } else {
throw new ValueException("expected table name: $usersql"); throw new ValueException("expected table name: $usersql");
} }
$xcols = []; $usercols = [];
$xvalues = []; $uservalues = [];
if (self::consume('\(([^)]*)\)\s*', $tmpsql, $ms)) { if (self::consume('\(([^)]*)\)\s*', $tmpsql, $ms)) {
$xcols = array_merge($xcols, preg_split("/\s*,\s*/", $ms[1])); $usercols = array_merge($usercols, preg_split("/\s*,\s*/", $ms[1]));
} }
$cols = cl::withn($query["cols"] ?? null); $cols = cl::withn($query["cols"] ?? null);
$values = cl::withn($query["values"] ?? null); $values = cl::withn($query["values"] ?? null);
$schema = $query["schema"] ?? null; $schema = $query["schema"] ?? null;
if ($cols === null) { if ($cols === null) {
if ($xcols) { if ($usercols) {
$cols = $xcols; $cols = $usercols;
} elseif ($values) { } elseif ($values) {
$cols = array_keys($values); $cols = array_keys($values);
$xcols = array_merge($xcols, $cols); $usercols = array_merge($usercols, $cols);
} elseif ($schema && is_array($schema)) { } elseif ($schema && is_array($schema)) {
#XXX implémenter support AssocSchema #XXX implémenter support AssocSchema
$cols = array_keys($schema); $cols = array_keys($schema);
$xcols = array_merge($xcols, $cols); $usercols = array_merge($usercols, $cols);
} }
} }
if (self::consume('values\s+\(\s*(.*)\s*\)\s*', $tmpsql, $ms)) { if (self::consume('values\s+\(\s*(.*)\s*\)\s*', $tmpsql, $ms)) {
if ($ms[1]) $xvalues[] = $ms[1]; if ($ms[1]) $uservalues[] = $ms[1];
} }
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");
} }
if ($cols !== null && !$xvalues) { if ($cols !== null && !$uservalues) {
if (!$xcols) $xcols = $cols; if (!$usercols) $usercols = $cols;
foreach ($cols as $col) { foreach ($cols as $col) {
$xvalues[] = ":$col"; $uservalues[] = ":$col";
$params[$col] = $values[$col] ?? null; $params[$col] = $values[$col] ?? null;
} }
} }
$sql[] = "(".implode(", ", $xcols).")"; $sql[] = "(".implode(", ", $usercols).")";
$sql[] = "values (".implode(", ", $xvalues).")"; $sql[] = "values (".implode(", ", $uservalues).")";
return implode(" ", $sql); return implode(" ", $sql);
} }