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

use nur\sery\ValueException;

class _query_base extends \nur\sery\db\pdo\_query_base {
  protected static function verifix(&$sql, ?array &$bindinds=null, ?array &$meta=null): void {
    if (is_array($sql)) {
      $prefix = $sql[0] ?? null;
      if ($prefix === null) {
        throw new ValueException("requête invalide");
      } elseif (_query_create::isa($prefix)) {
        $sql = _query_create::parse($sql, $bindinds);
        $meta = ["isa" => "create", "type" => "ddl"];
      } elseif (_query_select::isa($prefix)) {
        $sql = _query_select::parse($sql, $bindinds);
        $meta = ["isa" => "select", "type" => "dql"];
      } elseif (_query_insert::isa($prefix)) {
        $sql = _query_insert::parse($sql, $bindinds);
        $meta = ["isa" => "insert", "type" => "dml"];
      } elseif (_query_update::isa($prefix)) {
        $sql = _query_update::parse($sql, $bindinds);
        $meta = ["isa" => "update", "type" => "dml"];
      } elseif (_query_delete::isa($prefix)) {
        $sql = _query_delete::parse($sql, $bindinds);
        $meta = ["isa" => "delete", "type" => "dml"];
      } elseif (_query_generic::isa($prefix)) {
        $sql = _query_generic::parse($sql, $bindinds);
        $meta = ["isa" => "generic", "type" => null];
      } else {
        throw ValueException::invalid_kind($sql, "query");
      }
    } else {
      if (!is_string($sql)) $sql = strval($sql);
      if (_query_create::isa($sql)) {
        $meta = ["isa" => "create", "type" => "ddl"];
      } elseif (_query_select::isa($sql)) {
        $meta = ["isa" => "select", "type" => "dql"];
      } elseif (_query_insert::isa($sql)) {
        $meta = ["isa" => "insert", "type" => "dml"];
      } elseif (_query_update::isa($sql)) {
        $meta = ["isa" => "update", "type" => "dml"];
      } elseif (_query_delete::isa($sql)) {
        $meta = ["isa" => "delete", "type" => "dml"];
      } elseif (_query_generic::isa($sql)) {
        $meta = ["isa" => "generic", "type" => null];
      } else {
        $meta = ["isa" => "generic", "type" => null];
      }
    }
  }
}
