2 ** This program is used to generate and verify databases with hot journals.
3 ** Use this program to generate a hot journal on one machine and verify
4 ** that it rolls back correctly on another machine with a different
9 ** rollback-test new [-utf8] [-utf16le] [-utf16be] [-pagesize=N] DATABASE
10 ** rollback-test check DATABASE
11 ** rollback-test crash [-wal] [-rollback] DATABASE
18 static void usage(char *argv0){
20 "Usage: %s new [-utf8] [-utf16le] [-utf16be] [-pagesize=N] DATABASE\n"
21 " %s check DATABASE\n"
22 " %s crash [-wal] DATABASE\n",
28 static sqlite3 *openDb(const char *zFilename){
31 rc = sqlite3_open(zFilename, &db);
33 fprintf(stderr, "Cannot open \"%s\": %s\n",
34 zFilename, sqlite3_errmsg(db));
41 static int nReply = 0;
42 static char zReply[1000];
44 static int execCallback(void *NotUsed, int nArg, char **azArg, char **azCol){
47 for(i=0; i<nArg; i++){
49 if( z==0 ) z = "NULL";
50 if( nReply>0 && nReply<sizeof(zReply)-1 ) zReply[nReply++] = ' ';
52 if( nReply+n>=sizeof(zReply)-1 ) n = sizeof(zReply) - nReply - 1;
53 memcpy(&zReply[nReply], z, n);
60 static void runSql(sqlite3 *db, const char *zSql){
64 rc = sqlite3_exec(db, zSql, execCallback, 0, &zErr);
66 fprintf(stderr, "SQL error: %s\n", zErr);
70 fprintf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
75 int main(int argc, char **argv){
79 if( argc<3 ) usage(argv[0]);
80 if( strcmp(argv[1], "new")==0 ){
81 db = openDb(argv[argc-1]);
82 for(i=2; i<argc-1; i++){
83 if( strcmp(argv[i],"-utf8")==0 ){
84 runSql(db, "PRAGMA encoding=UTF8");
85 }else if( strcmp(argv[i], "-utf16le")==0 ){
86 runSql(db, "PRAGMA encoding=UTF16LE");
87 }else if( strcmp(argv[i], "-utf16be")==0 ){
88 runSql(db, "PRAGMA encoding=UTF16BE");
89 }else if( strncmp(argv[i], "-pagesize=", 10)==0 ){
90 int szPg = atoi(&argv[i][10]);
92 sprintf(zBuf, "PRAGMA pagesize=%d", szPg);
95 fprintf(stderr, "unknown option %s\n", argv[i]);
101 "CREATE TABLE t1(x INTEGER PRIMARY KEY, y);"
102 "INSERT INTO t1(y) VALUES('abcdefghijklmnopqrstuvwxyz');"
103 "INSERT INTO t1(y) VALUES('abcdefghijklmnopqrstuvwxyz');"
104 "INSERT INTO t1(y) SELECT y FROM t1;" /* 4 */
105 "INSERT INTO t1(y) SELECT y FROM t1;" /* 8 */
106 "INSERT INTO t1(y) SELECT y FROM t1;" /* 16 */
107 "INSERT INTO t1(y) SELECT y FROM t1;" /* 32 */
108 "INSERT INTO t1(y) SELECT y FROM t1;" /* 64 */
109 "INSERT INTO t1(y) SELECT y FROM t1;" /* 128 */
110 "INSERT INTO t1(y) SELECT y FROM t1;" /* 256 */
111 "INSERT INTO t1(y) SELECT y FROM t1;" /* 512 */
112 "INSERT INTO t1(y) SELECT y FROM t1;" /* 1024 */
113 "UPDATE t1 SET y=(y || x);"
114 "CREATE INDEX t1y ON t1(y);"
118 }else if( strcmp(argv[1], "check")==0 ){
119 db = openDb(argv[argc-1]);
120 runSql(db, "PRAGMA integrity_check");
121 if( strcmp(zReply, "ok")!=0 ){
122 fprintf(stderr, "Integrity check: %s\n", zReply);
126 "SELECT count(*) FROM t1 WHERE y<>('abcdefghijklmnopqrstuvwxyz' || x)"
128 if( strcmp(zReply, "0")!=0 ){
129 fprintf(stderr, "Wrong content\n");
133 }else if( strcmp(argv[1], "crash")==0 ){
134 db = openDb(argv[argc-1]);
135 for(i=2; i<argc-1; i++){
136 if( strcmp(argv[i],"-wal")==0 ){
137 runSql(db, "PRAGMA journal_mode=WAL");
138 }else if( strcmp(argv[i], "-rollback")==0 ){
139 runSql(db, "PRAGMA journal_mode=DELETE");
141 fprintf(stderr, "unknown option %s\n", argv[i]);
146 "PRAGMA cache_size=10;"
148 "UPDATE t1 SET y=(y || -x)"