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

use PHPUnit\Framework\TestCase;

class _queryTest extends TestCase {
  function testParseConds(): void {
    $sql = $params = null;
    _query_base::parse_conds(null, $sql, $params);
    self::assertNull($sql);
    self::assertNull($params);

    $sql = $params = null;
    _query_base::parse_conds([], $sql, $params);
    self::assertNull($sql);
    self::assertNull($params);

    $sql = $params = null;
    _query_base::parse_conds(["col" => null], $sql, $params);
    self::assertSame(["col is null"], $sql);
    self::assertNull($params);

    $sql = $params = null;
    _query_base::parse_conds(["col = 'value'"], $sql, $params);
    self::assertSame(["col = 'value'"], $sql);
    self::assertNull($params);

    $sql = $params = null;
    _query_base::parse_conds([["col = 'value'"]], $sql, $params);
    self::assertSame(["col = 'value'"], $sql);
    self::assertNull($params);

    $sql = $params = null;
    _query_base::parse_conds(["int" => 42, "string" => "value"], $sql, $params);
    self::assertSame(["(int = :int and string = :string)"], $sql);
    self::assertSame(["int" => 42, "string" => "value"], $params);

    $sql = $params = null;
    _query_base::parse_conds(["or", "int" => 42, "string" => "value"], $sql, $params);
    self::assertSame(["(int = :int or string = :string)"], $sql);
    self::assertSame(["int" => 42, "string" => "value"], $params);

    $sql = $params = null;
    _query_base::parse_conds([["int" => 42, "string" => "value"], ["int" => 24, "string" => "eulav"]], $sql, $params);
    self::assertSame(["((int = :int and string = :string) and (int = :int2 and string = :string2))"], $sql);
    self::assertSame(["int" => 42, "string" => "value", "int2" => 24, "string2" => "eulav"], $params);

    $sql = $params = null;
    _query_base::parse_conds(["int" => ["is null"], "string" => ["<>", "value"]], $sql, $params);
    self::assertSame(["(int is null and string <> :string)"], $sql);
    self::assertSame(["string" => "value"], $params);

    $sql = $params = null;
    _query_base::parse_conds(["col" => ["between", "lower", "upper"]], $sql, $params);
    self::assertSame(["col between :col and :col2"], $sql);
    self::assertSame(["col" => "lower", "col2" => "upper"], $params);

    $sql = $params = null;
    _query_base::parse_conds(["col" => ["in", "one"]], $sql, $params);
    self::assertSame(["col in (:col)"], $sql);
    self::assertSame(["col" => "one"], $params);

    $sql = $params = null;
    _query_base::parse_conds(["col" => ["in", ["one", "two"]]], $sql, $params);
    self::assertSame(["col in (:col, :col2)"], $sql);
    self::assertSame(["col" => "one", "col2" => "two"], $params);

    $sql = $params = null;
    _query_base::parse_conds(["col" => ["=", ["one", "two"]]], $sql, $params);
    self::assertSame(["col = :col and col = :col2"], $sql);
    self::assertSame(["col" => "one", "col2" => "two"], $params);

    $sql = $params = null;
    _query_base::parse_conds(["or", "col" => ["=", ["one", "two"]]], $sql, $params);
    self::assertSame(["col = :col or col = :col2"], $sql);
    self::assertSame(["col" => "one", "col2" => "two"], $params);

    $sql = $params = null;
    _query_base::parse_conds(["col" => ["<>", ["one", "two"]]], $sql, $params);
    self::assertSame(["col <> :col and col <> :col2"], $sql);
    self::assertSame(["col" => "one", "col2" => "two"], $params);

    $sql = $params = null;
    _query_base::parse_conds(["or", "col" => ["<>", ["one", "two"]]], $sql, $params);
    self::assertSame(["col <> :col or col <> :col2"], $sql);
    self::assertSame(["col" => "one", "col2" => "two"], $params);
  }

  function testParseValues(): void {
    $sql = $params = null;
    _query_base::parse_set_values(null, $sql, $params);
    self::assertNull($sql);
    self::assertNull($params);

    $sql = $params = null;
    _query_base::parse_set_values([], $sql, $params);
    self::assertNull($sql);
    self::assertNull($params);

    $sql = $params = null;
    _query_base::parse_set_values(["col = 'value'"], $sql, $params);
    self::assertSame(["col = 'value'"], $sql);
    self::assertNull($params);

    $sql = $params = null;
    _query_base::parse_set_values([["col = 'value'"]], $sql, $params);
    self::assertSame(["col = 'value'"], $sql);
    self::assertNull($params);

    $sql = $params = null;
    _query_base::parse_set_values(["int" => 42, "string" => "value"], $sql, $params);
    self::assertSame(["int = :int", "string = :string"], $sql);
    self::assertSame(["int" => 42, "string" => "value"], $params);

    $sql = $params = null;
    _query_base::parse_set_values(["int" => 42, "string" => "value"], $sql, $params);
    self::assertSame(["int = :int", "string = :string"], $sql);
    self::assertSame(["int" => 42, "string" => "value"], $params);

    $sql = $params = null;
    _query_base::parse_set_values([["int" => 42, "string" => "value"], ["int" => 24, "string" => "eulav"]], $sql, $params);
    self::assertSame(["int = :int", "string = :string", "int = :int2", "string = :string2"], $sql);
    self::assertSame(["int" => 42, "string" => "value", "int2" => 24, "string2" => "eulav"], $params);
  }
}
