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

trait Tupdate {
  static function isa(string $sql): bool {
    return preg_match("/^update\b/i", $sql);
  }

  static function parse(array $query, ?array &$bindings=null): string {
    #XXX implémentation minimale
    $sql = [self::merge_seq($query)];

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

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

    ## set
    self::parse_set_values($query["values"], $setsql, $bindings);
    $sql[] = "set";
    $sql[] = implode(", ", $setsql);

    ## 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);
  }
}
