2015-12-07 17:33:07 +03:00
|
|
|
# 2015 December 7
|
|
|
|
#
|
|
|
|
# The author disclaims copyright to this source code. In place of
|
|
|
|
# a legal notice, here is a blessing:
|
|
|
|
#
|
|
|
|
# May you do good and not evil.
|
|
|
|
# May you find forgiveness for yourself and forgive others.
|
|
|
|
# May you share freely, never taking more than you give.
|
|
|
|
#
|
|
|
|
#***********************************************************************
|
|
|
|
# This file implements regression tests for SQLite library. The
|
|
|
|
# focus of this file is testing the SELECT statement.
|
|
|
|
#
|
|
|
|
# $Id: select1.test,v 1.70 2009/05/28 01:00:56 drh Exp $
|
|
|
|
|
|
|
|
set testdir [file dirname $argv0]
|
|
|
|
source $testdir/tester.tcl
|
|
|
|
set testprefix snapshot
|
|
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
# Check some error conditions in snapshot_get(). It is an error if:
|
|
|
|
#
|
|
|
|
# 1) snapshot_get() is called on a non-WAL database.
|
|
|
|
# 2) there is no open read transaction on the database, or
|
|
|
|
# 3) there is an open write transaction on the database.
|
|
|
|
#
|
|
|
|
do_execsql_test 1.0 {
|
|
|
|
CREATE TABLE t1(a, b);
|
|
|
|
INSERT INTO t1 VALUES(1, 2);
|
|
|
|
INSERT INTO t1 VALUES(3, 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
do_test 1.1.1 {
|
|
|
|
execsql { BEGIN; SELECT * FROM t1; }
|
|
|
|
list [catch { sqlite3_snapshot_get db main } msg] $msg
|
|
|
|
} {1 SQLITE_ERROR}
|
|
|
|
do_execsql_test 1.1.2 COMMIT
|
|
|
|
|
|
|
|
do_test 1.2.1 {
|
|
|
|
execsql {
|
|
|
|
PRAGMA journal_mode = wal;
|
|
|
|
INSERT INTO t1 VALUES(5, 6);
|
|
|
|
}
|
|
|
|
list [catch { sqlite3_snapshot_get db main } msg] $msg
|
|
|
|
} {1 SQLITE_ERROR}
|
|
|
|
|
|
|
|
do_test 1.3.1 {
|
|
|
|
execsql {
|
|
|
|
BEGIN;
|
|
|
|
INSERT INTO t1 VALUES(7, 8);
|
|
|
|
}
|
|
|
|
list [catch { sqlite3_snapshot_get db main } msg] $msg
|
|
|
|
} {1 SQLITE_ERROR}
|
|
|
|
do_execsql_test 1.3.2 COMMIT
|
|
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
# Check that a simple case works. Reuse the database created by the
|
|
|
|
# block of tests above.
|
|
|
|
#
|
2015-12-09 23:05:27 +03:00
|
|
|
# UPDATE: This case (2.1) no longer works. 2.2 does.
|
|
|
|
#
|
|
|
|
do_execsql_test 2.1.0 {
|
2015-12-07 17:33:07 +03:00
|
|
|
BEGIN;
|
|
|
|
SELECT * FROM t1;
|
|
|
|
} {1 2 3 4 5 6 7 8}
|
|
|
|
|
2015-12-09 23:05:27 +03:00
|
|
|
do_test 2.1.1 {
|
2015-12-07 17:33:07 +03:00
|
|
|
set snapshot [sqlite3_snapshot_get db main]
|
|
|
|
execsql {
|
|
|
|
COMMIT;
|
|
|
|
INSERT INTO t1 VALUES(9, 10);
|
|
|
|
SELECT * FROM t1;
|
|
|
|
}
|
|
|
|
} {1 2 3 4 5 6 7 8 9 10}
|
|
|
|
|
2015-12-09 23:05:27 +03:00
|
|
|
do_test 2.1.2 {
|
|
|
|
execsql BEGIN
|
|
|
|
list [catch { sqlite3_snapshot_open db main $snapshot } msg] $msg
|
|
|
|
} {1 SQLITE_BUSY_SNAPSHOT}
|
|
|
|
|
|
|
|
do_test 2.1.3 {
|
|
|
|
sqlite3_snapshot_free $snapshot
|
|
|
|
execsql COMMIT
|
|
|
|
} {}
|
|
|
|
|
|
|
|
do_test 2.2.0 {
|
|
|
|
sqlite3 db2 test.db
|
|
|
|
execsql {
|
|
|
|
BEGIN;
|
|
|
|
SELECT * FROM t1;
|
|
|
|
} db2
|
|
|
|
} {1 2 3 4 5 6 7 8 9 10}
|
|
|
|
|
|
|
|
do_test 2.2.1 {
|
|
|
|
set snapshot [sqlite3_snapshot_get db2 main]
|
|
|
|
execsql {
|
|
|
|
INSERT INTO t1 VALUES(11, 12);
|
|
|
|
SELECT * FROM t1;
|
|
|
|
}
|
|
|
|
} {1 2 3 4 5 6 7 8 9 10 11 12}
|
|
|
|
|
|
|
|
do_test 2.1.2 {
|
2015-12-07 17:33:07 +03:00
|
|
|
execsql BEGIN
|
|
|
|
sqlite3_snapshot_open db main $snapshot
|
2015-12-09 23:05:27 +03:00
|
|
|
execsql {
|
|
|
|
SELECT * FROM t1;
|
|
|
|
}
|
|
|
|
} {1 2 3 4 5 6 7 8 9 10}
|
2015-12-07 17:33:07 +03:00
|
|
|
|
2015-12-09 23:05:27 +03:00
|
|
|
do_test 2.1.3 {
|
2015-12-07 17:33:07 +03:00
|
|
|
sqlite3_snapshot_free $snapshot
|
|
|
|
execsql COMMIT
|
2015-12-09 23:05:27 +03:00
|
|
|
execsql COMMIT db2
|
|
|
|
db2 close
|
2015-12-07 17:33:07 +03:00
|
|
|
} {}
|
|
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
# Check some errors in sqlite3_snapshot_open(). It is an error if:
|
|
|
|
#
|
|
|
|
# 1) the db is in auto-commit mode,
|
|
|
|
# 2) the db has an open (read or write) transaction,
|
|
|
|
# 3) the db is not a wal database,
|
|
|
|
#
|
|
|
|
# Reuse the database created by earlier tests.
|
|
|
|
#
|
|
|
|
do_execsql_test 3.0.0 {
|
|
|
|
CREATE TABLE t2(x, y);
|
|
|
|
INSERT INTO t2 VALUES('a', 'b');
|
|
|
|
INSERT INTO t2 VALUES('c', 'd');
|
|
|
|
BEGIN;
|
|
|
|
SELECT * FROM t2;
|
|
|
|
} {a b c d}
|
|
|
|
do_test 3.0.1 {
|
|
|
|
set snapshot [sqlite3_snapshot_get db main]
|
|
|
|
execsql { COMMIT }
|
|
|
|
execsql { INSERT INTO t2 VALUES('e', 'f'); }
|
|
|
|
} {}
|
|
|
|
|
|
|
|
do_test 3.1 {
|
|
|
|
list [catch {sqlite3_snapshot_open db main $snapshot } msg] $msg
|
|
|
|
} {1 SQLITE_ERROR}
|
|
|
|
|
|
|
|
do_test 3.2.1 {
|
|
|
|
execsql {
|
|
|
|
BEGIN;
|
|
|
|
SELECT * FROM t2;
|
|
|
|
}
|
|
|
|
} {a b c d e f}
|
|
|
|
do_test 3.2.2 {
|
|
|
|
list [catch {sqlite3_snapshot_open db main $snapshot } msg] $msg
|
|
|
|
} {1 SQLITE_ERROR}
|
|
|
|
|
|
|
|
do_test 3.2.3 {
|
|
|
|
execsql {
|
|
|
|
COMMIT;
|
|
|
|
BEGIN;
|
|
|
|
INSERT INTO t2 VALUES('g', 'h');
|
|
|
|
}
|
|
|
|
list [catch {sqlite3_snapshot_open db main $snapshot } msg] $msg
|
|
|
|
} {1 SQLITE_ERROR}
|
|
|
|
do_execsql_test 3.2.4 COMMIT
|
|
|
|
|
|
|
|
do_test 3.3.1 {
|
|
|
|
execsql { PRAGMA journal_mode = DELETE }
|
|
|
|
execsql { BEGIN }
|
|
|
|
list [catch {sqlite3_snapshot_open db main $snapshot } msg] $msg
|
|
|
|
} {1 SQLITE_ERROR}
|
|
|
|
|
|
|
|
do_test 3.3.2 {
|
|
|
|
sqlite3_snapshot_free $snapshot
|
|
|
|
execsql COMMIT
|
|
|
|
} {}
|
|
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
# Check that SQLITE_BUSY_SNAPSHOT is returned if the specified snapshot
|
|
|
|
# no longer exists because the wal file has been checkpointed.
|
|
|
|
#
|
|
|
|
# 1. Reading a snapshot from the middle of a wal file is not possible
|
|
|
|
# after the wal file has been checkpointed.
|
|
|
|
#
|
|
|
|
# 2. That a snapshot from the end of a wal file can not be read once
|
|
|
|
# the wal file has been wrapped.
|
|
|
|
#
|
|
|
|
do_execsql_test 4.1.0 {
|
|
|
|
PRAGMA journal_mode = wal;
|
|
|
|
CREATE TABLE t3(i, j);
|
|
|
|
INSERT INTO t3 VALUES('o', 't');
|
|
|
|
INSERT INTO t3 VALUES('t', 'f');
|
|
|
|
BEGIN;
|
|
|
|
SELECT * FROM t3;
|
|
|
|
} {wal o t t f}
|
|
|
|
|
|
|
|
do_test 4.1.1 {
|
|
|
|
set snapshot [sqlite3_snapshot_get db main]
|
|
|
|
execsql COMMIT
|
|
|
|
} {}
|
|
|
|
do_test 4.1.2 {
|
|
|
|
execsql {
|
|
|
|
INSERT INTO t3 VALUES('f', 's');
|
|
|
|
BEGIN;
|
|
|
|
}
|
|
|
|
sqlite3_snapshot_open db main $snapshot
|
|
|
|
execsql { SELECT * FROM t3 }
|
|
|
|
} {o t t f}
|
|
|
|
|
|
|
|
do_test 4.1.3 {
|
|
|
|
execsql {
|
|
|
|
COMMIT;
|
|
|
|
PRAGMA wal_checkpoint;
|
|
|
|
BEGIN;
|
|
|
|
}
|
|
|
|
list [catch {sqlite3_snapshot_open db main $snapshot} msg] $msg
|
|
|
|
} {1 SQLITE_BUSY_SNAPSHOT}
|
|
|
|
do_test 4.1.4 {
|
|
|
|
sqlite3_snapshot_free $snapshot
|
|
|
|
execsql COMMIT
|
|
|
|
} {}
|
|
|
|
|
|
|
|
do_test 4.2.1 {
|
|
|
|
execsql {
|
|
|
|
INSERT INTO t3 VALUES('s', 'e');
|
|
|
|
INSERT INTO t3 VALUES('n', 't');
|
|
|
|
BEGIN;
|
|
|
|
SELECT * FROM t3;
|
|
|
|
}
|
|
|
|
} {o t t f f s s e n t}
|
|
|
|
do_test 4.2.2 {
|
|
|
|
set snapshot [sqlite3_snapshot_get db main]
|
|
|
|
execsql {
|
|
|
|
COMMIT;
|
|
|
|
PRAGMA wal_checkpoint;
|
|
|
|
BEGIN;
|
|
|
|
}
|
|
|
|
sqlite3_snapshot_open db main $snapshot
|
|
|
|
execsql { SELECT * FROM t3 }
|
|
|
|
} {o t t f f s s e n t}
|
|
|
|
do_test 4.2.3 {
|
|
|
|
execsql {
|
|
|
|
COMMIT;
|
|
|
|
INSERT INTO t3 VALUES('e', 't');
|
|
|
|
BEGIN;
|
|
|
|
}
|
|
|
|
list [catch {sqlite3_snapshot_open db main $snapshot} msg] $msg
|
|
|
|
} {1 SQLITE_BUSY_SNAPSHOT}
|
|
|
|
do_test 4.2.4 {
|
|
|
|
sqlite3_snapshot_free $snapshot
|
|
|
|
} {}
|
|
|
|
|
|
|
|
finish_test
|
|
|
|
|