nulib/php/src/db/_private/_delete.php

49 lines
1.1 KiB
PHP

<?php
namespace nulib\db\_private;
class _delete extends _common {
const SCHEMA = [
"prefix" => "?string",
"from" => "?string",
"where" => "?array",
"suffix" => "?string",
];
static function isa(string $sql): bool {
return preg_match("/^delete(?:\s+from)?\b/i", $sql);
}
static function parse(array $query, ?array &$bindings=null): string {
#XXX implémentation minimale
$tmpsql = self::merge_seq($query);
self::consume('delete(?:\s+from)?\b', $tmpsql);
$sql = ["delete from"];
if ($tmpsql) $sql[] = $tmpsql;
## préfixe
$prefix = $query["prefix"] ?? null;
if ($prefix !== null) $sql[] = $prefix;
## table
$from = $query["from"] ?? null;
if ($from !== null) $sql[] = $from;
## where
$where = $query["where"] ?? null;
if ($where !== null) {
self::parse_conds($where, $wheresql, $bindings);
if ($wheresql) {
$sql[] = "where";
$sql[] = implode(" and ", $wheresql);
}
}
## suffixe
$suffix = $query["suffix"] ?? null;
if ($suffix !== null) $sql[] = $suffix;
## fin de la requête
return implode(" ", $sql);
}
}