#!/usr/bin/env tclsh

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

do_execsql_test_on_specific_db {:memory:} changes-on-basic-insert {
    create table temp (t1 integer, primary key (t1));
    insert into temp values (1);
    select changes();
} {1}

do_execsql_test_on_specific_db {:memory:} changes-on-multiple-row-insert {
    create table temp (t1 integer, primary key (t1));
    insert into temp values (1), (2), (3);
    select changes();
} {3}

do_execsql_test_on_specific_db {:memory:} changes-shows-most-recent {
    create table temp (t1 integer, primary key (t1));
    insert into temp values (1), (2), (3);
    insert into temp values (4), (5), (6), (7);
    select changes();
} {4}


# github.com/tursodatabase/turso/issues/3259
do_execsql_test_on_specific_db {:memory:} changes-doesnt-track-indexes {
    create table users (id integer primary key, name text, age integer);
	create index idx_name on users(name);
	create unique index idx_name_age on users(name, age);
	insert into users (name, age) values ('Alice', 30),('Mike', 33),('Jim', 22),('Sarah', 44),('Rosa', 11),('Cindy', 36),('Bob', 35),('Charlie', 50);
	UPDATE users SET name = 'young' where age < 40;
	select changes();
} {6}

# https://github.com/tursodatabase/turso/issues/3688
do_execsql_test_on_specific_db {:memory:} changes-1.69 {
    create table t(id integer primary key, value text);
    insert into t values (1, 'a');
     select changes();
     update t set id = id+10 where id = 1;
     select changes();
} {1
1}

do_execsql_test_on_specific_db {:memory:} changes-on-delete {
    create table temp (t1 integer, primary key (t1));
    insert into temp values (1), (2), (3), (4), (5);
    delete from temp where t1 > 2;
    select changes();
} {3}

do_execsql_test_on_specific_db {:memory:} changes-on-update {
    create table temp (t1 integer, t2 text, primary key (t1));
    insert into temp values (1, 'a'), (2, 'b'), (3, 'c'), (4, 'd');
    update temp set t2 = 'updated' where t1 <= 3;
    select changes();
} {3}

do_execsql_test_on_specific_db {:memory:} changes-on-update-rowid {
    create table temp (t1 integer primary key, t2 text);
    insert into temp values (1, 'a'), (2, 'b'), (3, 'c');
    update temp set t1 = t1 + 10 where t1 = 2;
    select changes();
} {1}


do_execsql_test_on_specific_db {:memory:} changes-resets-after-select {
    create table temp (t1 integer, primary key (t1));
    insert into temp values (1), (2), (3);
    select * from temp;
    select changes();
} {1
2
3
3}

do_execsql_test_on_specific_db {:memory:} changes-on-delete-no-match {
    create table temp (t1 integer, primary key (t1));
    insert into temp values (1), (2), (3);
    delete from temp where t1 > 100;
    select changes();
} {0}

do_execsql_test_on_specific_db {:memory:} changes-on-update-no-match {
    create table temp (t1 integer, t2 text, primary key (t1));
    insert into temp values (1, 'a'), (2, 'b');
    update temp set t2 = 'updated' where t1 > 100;
    select changes();
} {0}

do_execsql_test_on_specific_db {:memory:} changes-on-delete-all {
    create table temp (t1 integer, primary key (t1));
    insert into temp values (1), (2), (3), (4), (5), (6);
    delete from temp;
    select changes();
} {6}

do_execsql_test_on_specific_db {:memory:} changes-mixed-operations {
    create table temp (t1 integer, t2 text, primary key (t1));
    insert into temp values (1, 'a'), (2, 'b'), (3, 'c');
    update temp set t2 = 'updated' where t1 <= 2;
    delete from temp where t1 = 1;
    select changes();
} {1}
