<?php
namespace nur\sery\db\_private;

trait Tdelete {

  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", $tmpsql];

    ## préfixe
    if (($prefix = $query["prefix"] ?? null) !== null) $sql[] = $prefix;

    ## table
    $sql[] = $query["from"];

    ## where
    $where = $query["where"] ?? null;
    if ($where !== null) {
      self::parse_conds($where, $wheresql, $bindings);
      if ($wheresql) {
        $sql[] = "where";
        $sql[] = implode(" and ", $wheresql);
      }
    }

    ## suffixe
    if (($suffix = $query["suffix"] ?? null) !== null) $sql[] = $suffix;

    ## fin de la requête
    return implode(" ", $sql);
  }
}
