modifs.mineures sans commentaires
This commit is contained in:
parent
ef781dcf37
commit
58f561a4e2
|
@ -37,6 +37,22 @@ class _Query {
|
|||
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 = [
|
||||
"prefix" => "string",
|
||||
"table" => "string",
|
||||
|
@ -64,6 +80,26 @@ class _Query {
|
|||
return false;
|
||||
}
|
||||
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 = [
|
||||
|
@ -78,23 +114,10 @@ class _Query {
|
|||
}
|
||||
static function parse_insert(array $query, ?array &$params=null): string {
|
||||
# fusionner d'abord toutes les parties séquentielles
|
||||
$index = 0;
|
||||
$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;
|
||||
$usersql = $tmpsql = self::merge_seq($query);
|
||||
# vérifier la présence des parties nécessaires
|
||||
$sql = [];
|
||||
if (($prefix = $query["prefix"] ?? null) !== null) {
|
||||
$sql["prefix"] = $prefix;
|
||||
}
|
||||
if (($prefix = $query["prefix"] ?? null) !== null) $sql[] = $prefix;
|
||||
self::consume('insert\s*', $tmpsql); $sql[] = "insert";
|
||||
self::consume('into\s*', $tmpsql); $sql[] = "into";
|
||||
if (self::consume('([a-z_][a-z0-9_]*)\s*', $tmpsql, $ms)) {
|
||||
|
@ -104,42 +127,42 @@ class _Query {
|
|||
} else {
|
||||
throw new ValueException("expected table name: $usersql");
|
||||
}
|
||||
$xcols = [];
|
||||
$xvalues = [];
|
||||
$usercols = [];
|
||||
$uservalues = [];
|
||||
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);
|
||||
$values = cl::withn($query["values"] ?? null);
|
||||
$schema = $query["schema"] ?? null;
|
||||
if ($cols === null) {
|
||||
if ($xcols) {
|
||||
$cols = $xcols;
|
||||
if ($usercols) {
|
||||
$cols = $usercols;
|
||||
} elseif ($values) {
|
||||
$cols = array_keys($values);
|
||||
$xcols = array_merge($xcols, $cols);
|
||||
$usercols = array_merge($usercols, $cols);
|
||||
} elseif ($schema && is_array($schema)) {
|
||||
#XXX implémenter support AssocSchema
|
||||
$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 ($ms[1]) $xvalues[] = $ms[1];
|
||||
if ($ms[1]) $uservalues[] = $ms[1];
|
||||
}
|
||||
self::consume(';\s*', $tmpsql);
|
||||
if ($tmpsql) {
|
||||
throw new ValueException("unexpected value at end: $usersql");
|
||||
}
|
||||
if ($cols !== null && !$xvalues) {
|
||||
if (!$xcols) $xcols = $cols;
|
||||
if ($cols !== null && !$uservalues) {
|
||||
if (!$usercols) $usercols = $cols;
|
||||
foreach ($cols as $col) {
|
||||
$xvalues[] = ":$col";
|
||||
$uservalues[] = ":$col";
|
||||
$params[$col] = $values[$col] ?? null;
|
||||
}
|
||||
}
|
||||
$sql[] = "(".implode(", ", $xcols).")";
|
||||
$sql[] = "values (".implode(", ", $xvalues).")";
|
||||
$sql[] = "(".implode(", ", $usercols).")";
|
||||
$sql[] = "values (".implode(", ", $uservalues).")";
|
||||
return implode(" ", $sql);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue