#!/usr/bin/env tclsh

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

do_execsql_test_on_specific_db ":memory:" pragma-cache-size-default {
  PRAGMA cache_size
} {-2000}

do_execsql_test pragma-set-cache-size {
  PRAGMA cache_size = 100;
  PRAGMA cache_size
} {100}

# Even though the cache size was set to 100 in previous test, a new connection defaults back to -2000.
do_execsql_test pragma-cache-size {
  PRAGMA cache_size
} {-2000}

do_execsql_test pragma-function-cache-size {
  SELECT * FROM pragma_cache_size()
} {-2000}

do_execsql_test pragma-update-journal-mode-wal {
  PRAGMA journal_mode=WAL
} {wal}

do_execsql_test pragma-function-update-journal-mode {
  SELECT * FROM pragma_journal_mode()
} {wal}

do_execsql_test pragma-table-info-equal-syntax {
  PRAGMA table_info=sqlite_schema
} {0|type|TEXT|0||0
1|name|TEXT|0||0
2|tbl_name|TEXT|0||0
3|rootpage|INT|0||0
4|sql|TEXT|0||0
}

do_execsql_test pragma-table-info-call-syntax {
  PRAGMA table_info(sqlite_schema)
} {0|type|TEXT|0||0
1|name|TEXT|0||0
2|tbl_name|TEXT|0||0
3|rootpage|INT|0||0
4|sql|TEXT|0||0
}

do_execsql_test pragma-table-info-alt-name-equal-syntax {
  PRAGMA table_info=sqlite_master
} {0|type|TEXT|0||0
1|name|TEXT|0||0
2|tbl_name|TEXT|0||0
3|rootpage|INT|0||0
4|sql|TEXT|0||0
}

do_execsql_test pragma-table-info-alt-name-call-syntax {
  PRAGMA table_info(sqlite_master)
} {0|type|TEXT|0||0
1|name|TEXT|0||0
2|tbl_name|TEXT|0||0
3|rootpage|INT|0||0
4|sql|TEXT|0||0
}

do_execsql_test pragma-function-table-info-alt-name {
  SELECT * FROM pragma_table_info('sqlite_master')
} {0|type|TEXT|0||0
1|name|TEXT|0||0
2|tbl_name|TEXT|0||0
3|rootpage|INT|0||0
4|sql|TEXT|0||0
}

do_execsql_test pragma-function-table-info {
  SELECT * FROM pragma_table_info('sqlite_schema')
} {0|type|TEXT|0||0
1|name|TEXT|0||0
2|tbl_name|TEXT|0||0
3|rootpage|INT|0||0
4|sql|TEXT|0||0
}

do_execsql_test pragma-vtab-table-info {
  SELECT * FROM pragma_table_info WHERE arg = 'sqlite_schema'
} {0|type|TEXT|0||0
1|name|TEXT|0||0
2|tbl_name|TEXT|0||0
3|rootpage|INT|0||0
4|sql|TEXT|0||0
}

do_execsql_test pragma-table-info-invalid-table {
  PRAGMA table_info=pekka
} {}

do_execsql_test pragma-function-table-info-invalid-table {
  SELECT * FROM pragma_table_info('pekka')
} {}

do_execsql_test pragma-vtab-table-info-invalid-table {
  SELECT * FROM pragma_table_info WHERE arg = 'pekka'
} {}

do_execsql_test_on_specific_db ":memory:" pragma-page-count-empty {
  PRAGMA page_count
} {0}

do_execsql_test_on_specific_db ":memory:" pragma-page-count-empty {
  PRAGMA user_version=1;
  PRAGMA page_count
} {1}

do_execsql_test_on_specific_db ":memory:" pragma-page-count-table {
  CREATE TABLE foo (bar);
  PRAGMA page_count
} {2}

do_execsql_test_on_specific_db "testing/testing_user_version_10.db" pragma-user-version-user-set {
  PRAGMA user_version
} {10}

do_execsql_test_on_specific_db ":memory:" pragma-user-version-default {
  PRAGMA user_version
} {0}

do_execsql_test_on_specific_db ":memory:" pragma-user-version-update {
  PRAGMA user_version = 42;
  PRAGMA user_version;
} {42}

do_execsql_test_on_specific_db ":memory:" pragma-user-version-negative-value {
  PRAGMA user_version = -10;
  PRAGMA user_version;
} {-10}

do_execsql_test_on_specific_db ":memory:" pragma-user-version-float-value {
  PRAGMA user_version = 10.9;
  PRAGMA user_version;
} {10}

do_execsql_test_on_specific_db ":memory:" pragma-application-id-default {
  PRAGMA application_id
} {0}

do_execsql_test_on_specific_db ":memory:" pragma-application-id-update {
  PRAGMA application_id = 12345;
  PRAGMA application_id;
} {12345}

do_execsql_test_on_specific_db ":memory:" pragma-application-id-float-value {
  PRAGMA application_id = 10.9;
  PRAGMA application_id;
} {10}

do_execsql_test_on_specific_db ":memory:" pragma-application-id-large-value {
  PRAGMA application_id = 2147483647;
  PRAGMA application_id;
} {2147483647}

do_execsql_test_on_specific_db ":memory:" pragma-application-id-negative-value {
  PRAGMA application_id = -23;
  PRAGMA application_id;
} {-23}

do_execsql_test_on_specific_db ":memory:" pragma-application-id-zero {
  PRAGMA application_id = 0;
  PRAGMA application_id;
} {0}

do_execsql_test pragma-legacy-file-format {
  PRAGMA legacy_file_format
} {}

do_execsql_test_error pragma-function-legacy-file-format {
  SELECT * FROM pragma_legacy_file_format()
} {(no such table|Table.*not found)}

do_execsql_test_error_content pragma-function-too-many-arguments {
  SELECT * FROM pragma_table_info('sqlite_schema', 'main', 'arg3')
} {"Too many arguments"}

do_execsql_test_error pragma-function-update {
  SELECT * FROM pragma_wal_checkpoint()
} {(no such table|Table.*not found)}

do_execsql_test pragma-function-nontext-argument {
  SELECT * FROM pragma_table_info('sqlite_schema', NULL);
} {0|type|TEXT|0||0
1|name|TEXT|0||0
2|tbl_name|TEXT|0||0
3|rootpage|INT|0||0
4|sql|TEXT|0||0
}

do_execsql_test pragma-vtab-nontext-argument {
  SELECT * FROM pragma_table_info WHERE arg ='sqlite_schema' AND schema IS NULL;
} {0|type|TEXT|0||0
1|name|TEXT|0||0
2|tbl_name|TEXT|0||0
3|rootpage|INT|0||0
4|sql|TEXT|0||0
}

do_execsql_test pragma-function-no-arguments {
  SELECT * FROM pragma_table_info();
} {}

do_execsql_test pragma-vtab-no-arguments {
  SELECT * FROM pragma_table_info;
} {}

do_execsql_test_on_specific_db ":memory:" pragma-function-argument-with-space {
  CREATE TABLE "foo bar"(c0);
  SELECT * FROM pragma_table_info('foo bar')
} {0|c0||0||0}

# If the argument passed to the first function call were simply concatenated with the underlying PRAGMA statement,
# we would end up with: PRAGMA table_info='sqlite_schema';CREATE TABLE foo (c0);SELECT 'bar'. Depending on how many
# statements are executed at once, at least one of the following would run:
# - PRAGMA table_info='sqlite_schema';
# - CREATE TABLE foo (c0);
# - SELECT 'bar';
# No output means that none of them were executed.
do_execsql_test pragma-function-sql-injection {
  SELECT * FROM pragma_table_info('sqlite_schema'';CREATE TABLE foo (c0);SELECT ''bar');
  SELECT * FROM pragma_table_info('foo');
} {}

do_execsql_test_on_specific_db ":memory:" pragma-page-size-default {
  PRAGMA page_size
} {4096}

do_execsql_test_on_specific_db ":memory:" pragma-page-size-set {
  PRAGMA page_size=1024;
  PRAGMA page_size
} {1024}

# pragma page_size=xxx doesn't change the page size of an initialized database.
do_execsql_test_on_specific_db ":memory:" pragma-page-size-set-initialized-db {
  CREATE TABLE "foo bar"(c0);

  PRAGMA page_size=1024;
  PRAGMA page_size
} {4096}

# pragma page_size=xxx changes the page size of an uninitialized database and persists the change.
set test_pragma_page_size_db "testing/testing_pragma_page_size.db"
catch {file delete -force $test_pragma_page_size_db}
catch {file delete -force "${test_pragma_page_size_db}-wal"}
# set user_version to trigger database initialization.
do_execsql_test_on_specific_db $test_pragma_page_size_db pragma-page-size-set-uninitialized-db-1 {
  PRAGMA page_size=1024;
  PRAGMA user_version=1;
  PRAGMA page_size
} {1024}

do_execsql_test_on_specific_db $test_pragma_page_size_db pragma-page-size-set-uninitialized-db-2 {
  PRAGMA page_size
} {1024}
catch {file delete -force $test_pragma_page_size_db}
catch {file delete -force "${test_pragma_page_size_db}-wal"}

do_execsql_test pragma-vtab-join {
  SELECT s.name, ti.name FROM sqlite_schema AS s, pragma_table_info(s.name) AS ti;
} {users|id
users|first_name
users|last_name
users|email
users|phone_number
users|address
users|city
users|state
users|zipcode
users|age
products|id
products|name
products|price
}

do_execsql_test pragma-vtab-reversed-join-order {
  SELECT s.name, ti.name FROM pragma_table_info(s.name) AS ti, sqlite_schema AS s;
} {users|id
users|first_name
users|last_name
users|email
users|phone_number
users|address
users|city
users|state
users|zipcode
users|age
products|id
products|name
products|price
}

do_execsql_test_on_specific_db ":memory:" pragma-max-page-count-default {
  PRAGMA max_page_count
} {4294967294}

do_execsql_test_on_specific_db ":memory:" pragma-max-page-count-set-large {
  PRAGMA max_page_count = 1000;
  PRAGMA max_page_count
} {1000
1000}

do_execsql_test_on_specific_db ":memory:" pragma-max-page-count-set-zero-ignored {
  PRAGMA max_page_count = 0;
  PRAGMA max_page_count
} {4294967294
4294967294
}

do_execsql_test_on_specific_db ":memory:" pragma-max-page-count-clamping-with-data {
  CREATE TABLE test (id INTEGER);
  PRAGMA page_count;
  PRAGMA max_page_count = 1;
} {2
2}

do_execsql_test_in_memory_any_error pragma-max-page-count-enforcement-error {
  PRAGMA max_page_count = 1;
  CREATE TABLE test (id INTEGER)
}

do_execsql_test_regex pragma-module-list-nonempty {
  SELECT * FROM pragma_module_list;
} {\ngenerate_series\n|^generate_series\n|\ngenerate_series$|^generate_series$}
