1
0
mirror of https://github.com/sqlite/sqlite.git synced 2025-11-12 13:01:09 +03:00

Add the sqlite3_serialize() and sqlite3_deserialize() interfaces, enabled

when the -DSQLITE_ENABLE_DESERIALIZE compile-time option is used.

FossilOrigin-Name: fc42d31d6fca21abc5377a463ed800e3066b2f3fbd2d85cb486f402d2ae7cb15
This commit is contained in:
drh
2018-03-07 13:01:54 +00:00
17 changed files with 1173 additions and 150 deletions

162
test/memdb1.test Normal file
View File

@@ -0,0 +1,162 @@
# 2018-01-02
#
# 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 the "memdb" VFS
#
set testdir [file dirname $argv0]
source $testdir/tester.tcl
set testprefix memdb1
do_not_use_codec
ifcapable !deserialize {
finish_test
return
}
# Create a MEMDB and populate it with some dummy data.
# Then extract the database into the $::db1 variable.
# Verify that the size of $::db1 is the same as the size of
# the database.
#
unset -nocomplain db1
unset -nocomplain sz1
unset -nocomplain pgsz
do_test 100 {
db eval {
CREATE TABLE t1(a,b);
INSERT INTO t1 VALUES(1,2);
}
set ::pgsz [db one {PRAGMA page_size}]
set ::sz1 [expr {$::pgsz*[db one {PRAGMA page_count}]}]
set ::db1 [db serialize]
expr {[string length $::db1]==$::sz1}
} 1
set fd [open db1.db wb]
puts -nonewline $fd $db1
close $fd
# Create a new MEMDB and initialize it to the content of $::db1
# Verify that the content is the same.
#
db close
sqlite3 db
db deserialize $db1
do_execsql_test 110 {
SELECT * FROM t1;
} {1 2}
# What happens when we try to VACUUM a MEMDB database?
#
do_execsql_test 120 {
VACUUM;
} {}
do_execsql_test 130 {
CREATE TABLE t2(x, y);
WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<100)
INSERT INTO t2(x, y) SELECT x, randomblob(1000) FROM c;
DROP TABLE t2;
PRAGMA page_count;
} {116}
do_execsql_test 140 {
VACUUM;
PRAGMA page_count;
} {2}
# Build a largish on-disk database and serialize it. Verify that the
# serialization works.
#
db close
forcedelete test.db
sqlite3 db test.db
do_execsql_test 200 {
CREATE TABLE t3(x, y);
WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<400)
INSERT INTO t3(x, y) SELECT x, randomblob(1000) FROM c;
PRAGMA quick_check;
} {ok}
set fd [open test.db rb]
unset -nocomplain direct
set direct [read $fd]
close $fd
do_test 210 {
string length [db serialize]
} [string length $direct]
do_test 220 {
db eval {ATTACH ':memory:' AS aux1}
db deserialize aux1 $::direct
db eval {
SELECT x, y FROM main.t3 EXCEPT SELECT x, y FROM aux1.t3;
}
} {}
unset -nocomplain direct
# Do the same with a :memory: database.
#
db close
sqlite3 db :memory:
do_execsql_test 300 {
CREATE TABLE t3(x, y);
WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<400)
INSERT INTO t3(x, y) SELECT x, randomblob(1000) FROM c;
PRAGMA quick_check;
} {ok}
do_test 310 {
db eval {ATTACH ':memory:' AS aux1}
db deserialize aux1 [db serialize main]
db eval {
SELECT x, y FROM main.t3 EXCEPT SELECT x, y FROM aux1.t3;
}
} {}
# Deserialize an empty database
#
db close
sqlite3 db
db deserialize {}
do_execsql_test 400 {
PRAGMA integrity_check;
} {ok}
do_execsql_test 410 {
CREATE TABLE t4(a,b);
INSERT INTO t4 VALUES('hello','world!');
PRAGMA integrity_check;
SELECT * FROM t4;
} {ok hello world!}
# Deserialize something that is not a database.
#
db close
sqlite3 db
do_test 500 {
set rc [catch {db deserialize not-a-database} msg]
lappend rc $msg
} {0 {}}
do_catchsql_test 510 {
PRAGMA integrity_check;
} {1 {file is not a database}}
# Abuse the serialize and deserialize commands. Make sure errors are caught.
#
do_test 600 {
set rc [catch {db deserialize} msg]
lappend rc $msg
} {1 {wrong # args: should be "db deserialize ?DATABASE? VALUE"}}
do_test 610 {
set rc [catch {db deserialize a b c} msg]
lappend rc $msg
} {1 {wrong # args: should be "db deserialize ?DATABASE? VALUE"}}
do_test 620 {
set rc [catch {db serialize a b} msg]
lappend rc $msg
} {1 {wrong # args: should be "db serialize ?DATABASE?"}}
finish_test

View File

@@ -22,19 +22,19 @@ source $testdir/tester.tcl
# Check the error messages generated by tclsqlite
#
set r "sqlite_orig HANDLE FILENAME ?-vfs VFSNAME? ?-readonly BOOLEAN? ?-create BOOLEAN? ?-nomutex BOOLEAN? ?-fullmutex BOOLEAN? ?-uri BOOLEAN?"
set r "sqlite_orig HANDLE ?FILENAME? ?-vfs VFSNAME? ?-readonly BOOLEAN? ?-create BOOLEAN? ?-nomutex BOOLEAN? ?-fullmutex BOOLEAN? ?-uri BOOLEAN?"
if {[sqlite3 -has-codec]} {
append r " ?-key CODECKEY?"
}
do_test tcl-1.1 {
set v [catch {sqlite3 bogus} msg]
set v [catch {sqlite3 -bogus} msg]
regsub {really_sqlite3} $msg {sqlite3} msg
lappend v $msg
} [list 1 "wrong # args: should be \"$r\""]
do_test tcl-1.2 {
set v [catch {db bogus} msg]
lappend v $msg
} {1 {bad option "bogus": must be authorizer, backup, busy, cache, changes, close, collate, collation_needed, commit_hook, complete, copy, enable_load_extension, errorcode, eval, exists, function, incrblob, interrupt, last_insert_rowid, nullvalue, onecolumn, preupdate, profile, progress, rekey, restore, rollback_hook, status, timeout, total_changes, trace, trace_v2, transaction, unlock_notify, update_hook, version, or wal_hook}}
} {1 {bad option "bogus": must be authorizer, backup, busy, cache, changes, close, collate, collation_needed, commit_hook, complete, copy, deserialize, enable_load_extension, errorcode, eval, exists, function, incrblob, interrupt, last_insert_rowid, nullvalue, onecolumn, preupdate, profile, progress, rekey, restore, rollback_hook, serialize, status, timeout, total_changes, trace, trace_v2, transaction, unlock_notify, update_hook, version, or wal_hook}}
do_test tcl-1.2.1 {
set v [catch {db cache bogus} msg]
lappend v $msg