#!/usr/bin/env tclsh

set testdir [file dirname $argv0]
source $testdir/tester.tcl

do_execsql_test_in_memory_any_error create_table_one_unique_set {
    CREATE TABLE t4 (a, unique(b));
}

do_execsql_test_on_specific_db {:memory:} create_table_same_uniques_and_primary_keys {
    CREATE TABLE t2 (a,b, unique(a,b), primary key(a,b));
} {}

do_execsql_test_on_specific_db {:memory:} create_table_unique_contained_in_primary_keys {
    CREATE TABLE t4 (a,b, primary key(a,b), unique(a));
} {}

# https://github.com/tursodatabase/turso/issues/3187
do_execsql_test_on_specific_db {:memory:} create_table_unique_constraint_and_autoinc_backticks { 
	CREATE TABLE `databases` (`id` integer PRIMARY KEY AUTOINCREMENT,`created_at` datetime,`updated_at` datetime,`deleted_at` datetime,`hostname` text NOT NULL,`namespace` text,`address` text,`primary_address` text,`local` numeric,`allowed_ips` text, CONSTRAINT `uni_databases_hostname` UNIQUE (`hostname`));
} {}

# https://github.com/tursodatabase/turso/issues/2686
do_execsql_test_on_specific_db {:memory:} create_table_rowid_unique_regression_test {
  create table u(x integer unique primary key);
  insert into u values (1),(2),(3);
  select * from u where x > 2;
} {3}

# https://github.com/tursodatabase/turso/issues/2886#issuecomment-3244885481
do_execsql_test_on_specific_db {:memory:} create_table_with_empty_string_name {
  create table ''('' INTEGER) strict;
  insert into '' values(9);
  select * from '';
} {9}

do_execsql_test_in_memory_any_error create_table_multiple_column_primary_keys {
    CREATE TABLE t(a primary key, b primary key);
}

do_execsql_test_in_memory_any_error create_table_column_and_table_primary_keys {
    CREATE TABLE t(a primary key, b,c, primary key(b,c));
}

do_execsql_test_in_memory_any_error create_table_multiple_table_primary_keys {
    CREATE TABLE t(a,b,c,d,primary key(a,b), primary key(c,d));
}

# https://github.com/tursodatabase/turso/issues/3282
do_execsql_test_on_specific_db {:memory:} col-named-rowid {
  create table t(rowid, a);
  insert into t values (1,2), (2,3), (3,4);
  update t set rowid = 1; -- should allow regular update and not throw unique constraint
  select count(*) from t where rowid = 1;
} {3}

# https://github.com/tursodatabase/turso/issues/3637
do_execsql_test_in_memory_any_error create_table_duplicate_column_names {
    CREATE TABLE t(a, a);
}

do_execsql_test_in_memory_any_error create_table_duplicate_column_names_case_insensitive {
    CREATE TABLE t(A, a);
}

do_execsql_test_in_memory_any_error create_table_duplicate_column_names_quoted {
    CREATE TABLE t("a", a);
}

# https://github.com/tursodatabase/turso/issues/3675
do_execsql_test_in_memory_any_error create_table_view_collision-1 {
    CREATE VIEW v_same AS SELECT 1;
    CREATE TABLE v_same(x INT);
}

do_execsql_test_in_memory_any_error create_view_table_collision-1 {
    CREATE TABLE t_same(x INT);
    CREATE VIEW t_same AS SELECT 1;
}

do_execsql_test_in_memory_any_error create_index_view_collision-1 {
    CREATE VIEW i_same AS SELECT 1;
    CREATE TABLE t1(x);
    CREATE INDEX i_same ON t1(x);
}

do_execsql_test_in_memory_any_error create_index_table_collision-1 {
    CREATE TABLE i_same(x INT);
    CREATE TABLE t2(y);
    CREATE INDEX i_same ON t2(y);
}

do_execsql_test_in_memory_any_error create_table_index_collision-1 {
    CREATE TABLE t3(z);
    CREATE INDEX ix_same ON t3(z);
    CREATE TABLE ix_same(x INT);
}

do_execsql_test_in_memory_any_error create_view_index_collision-1 {
    CREATE TABLE t4(w);
    CREATE INDEX ix_same ON t4(w);
    CREATE VIEW ix_same AS SELECT 1;
}

# https://github.com/tursodatabase/turso/issues/3796
do_execsql_test_on_specific_db {:memory:} col-default-true {
  create table t(id integer primary key, a default true);
  insert into t (id) values (1);
  SELECT a from t;
} {1}

# https://github.com/tursodatabase/turso/issues/3796
do_execsql_test_on_specific_db {:memory:} col-default-false {
  create table t(id integer primary key, a default false);
  insert into t (id) values (1);
  SELECT a from t;
} {0}
