rollback-test.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  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
  5. ** architecture.
  6. **
  7. ** Usage:
  8. **
  9. ** rollback-test new [-utf8] [-utf16le] [-utf16be] [-pagesize=N] DATABASE
  10. ** rollback-test check DATABASE
  11. ** rollback-test crash [-wal] [-rollback] DATABASE
  12. */
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include "sqlite3.h"
  17. static void usage(char *argv0){
  18. fprintf(stderr,
  19. "Usage: %s new [-utf8] [-utf16le] [-utf16be] [-pagesize=N] DATABASE\n"
  20. " %s check DATABASE\n"
  21. " %s crash [-wal] DATABASE\n",
  22. argv0, argv0, argv0
  23. );
  24. exit(1);
  25. }
  26. static sqlite3 *openDb(const char *zFilename){
  27. int rc;
  28. sqlite3 *db;
  29. rc = sqlite3_open(zFilename, &db);
  30. if( rc ){
  31. fprintf(stderr, "Cannot open \"%s\": %s\n",
  32. zFilename, sqlite3_errmsg(db));
  33. sqlite3_close(db);
  34. exit(1);
  35. }
  36. return db;
  37. }
  38. static int nReply = 0;
  39. static char zReply[1000];
  40. static int execCallback(void *NotUsed, int nArg, char **azArg, char **azCol){
  41. int i, n;
  42. char *z;
  43. for(i=0; i<nArg; i++){
  44. z = azArg[i];
  45. if( z==0 ) z = "NULL";
  46. if( nReply>0 && nReply<sizeof(zReply)-1 ) zReply[nReply++] = ' ';
  47. n = strlen(z);
  48. if( nReply+n>=sizeof(zReply)-1 ) n = sizeof(zReply) - nReply - 1;
  49. memcpy(&zReply[nReply], z, n);
  50. nReply += n;
  51. zReply[nReply] = 0;
  52. }
  53. return 0;
  54. }
  55. static void runSql(sqlite3 *db, const char *zSql){
  56. char *zErr = 0;
  57. int rc;
  58. nReply = 0;
  59. rc = sqlite3_exec(db, zSql, execCallback, 0, &zErr);
  60. if( zErr ){
  61. fprintf(stderr, "SQL error: %s\n", zErr);
  62. exit(1);
  63. }
  64. if( rc ){
  65. fprintf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
  66. exit(1);
  67. }
  68. }
  69. int main(int argc, char **argv){
  70. sqlite3 *db;
  71. int i;
  72. if( argc<3 ) usage(argv[0]);
  73. if( strcmp(argv[1], "new")==0 ){
  74. db = openDb(argv[argc-1]);
  75. for(i=2; i<argc-1; i++){
  76. if( strcmp(argv[i],"-utf8")==0 ){
  77. runSql(db, "PRAGMA encoding=UTF8");
  78. }else if( strcmp(argv[i], "-utf16le")==0 ){
  79. runSql(db, "PRAGMA encoding=UTF16LE");
  80. }else if( strcmp(argv[i], "-utf16be")==0 ){
  81. runSql(db, "PRAGMA encoding=UTF16BE");
  82. }else if( strncmp(argv[i], "-pagesize=", 10)==0 ){
  83. int szPg = atoi(&argv[i][10]);
  84. char zBuf[100];
  85. sprintf(zBuf, "PRAGMA pagesize=%d", szPg);
  86. runSql(db, zBuf);
  87. }else{
  88. fprintf(stderr, "unknown option %s\n", argv[i]);
  89. usage(argv[0]);
  90. }
  91. }
  92. runSql(db,
  93. "BEGIN;"
  94. "CREATE TABLE t1(x INTEGER PRIMARY KEY, y);"
  95. "INSERT INTO t1(y) VALUES('abcdefghijklmnopqrstuvwxyz');"
  96. "INSERT INTO t1(y) VALUES('abcdefghijklmnopqrstuvwxyz');"
  97. "INSERT INTO t1(y) SELECT y FROM t1;" /* 4 */
  98. "INSERT INTO t1(y) SELECT y FROM t1;" /* 8 */
  99. "INSERT INTO t1(y) SELECT y FROM t1;" /* 16 */
  100. "INSERT INTO t1(y) SELECT y FROM t1;" /* 32 */
  101. "INSERT INTO t1(y) SELECT y FROM t1;" /* 64 */
  102. "INSERT INTO t1(y) SELECT y FROM t1;" /* 128 */
  103. "INSERT INTO t1(y) SELECT y FROM t1;" /* 256 */
  104. "INSERT INTO t1(y) SELECT y FROM t1;" /* 512 */
  105. "INSERT INTO t1(y) SELECT y FROM t1;" /* 1024 */
  106. "UPDATE t1 SET y=(y || x);"
  107. "CREATE INDEX t1y ON t1(y);"
  108. "COMMIT;"
  109. );
  110. sqlite3_close(db);
  111. }else if( strcmp(argv[1], "check")==0 ){
  112. db = openDb(argv[argc-1]);
  113. runSql(db, "PRAGMA integrity_check");
  114. if( strcmp(zReply, "ok")!=0 ){
  115. fprintf(stderr, "Integrity check: %s\n", zReply);
  116. exit(1);
  117. }
  118. runSql(db,
  119. "SELECT count(*) FROM t1 WHERE y<>('abcdefghijklmnopqrstuvwxyz' || x)"
  120. );
  121. if( strcmp(zReply, "0")!=0 ){
  122. fprintf(stderr, "Wrong content\n");
  123. exit(1);
  124. }
  125. printf("Ok\n");
  126. }else if( strcmp(argv[1], "crash")==0 ){
  127. db = openDb(argv[argc-1]);
  128. for(i=2; i<argc-1; i++){
  129. if( strcmp(argv[i],"-wal")==0 ){
  130. runSql(db, "PRAGMA journal_mode=WAL");
  131. }else if( strcmp(argv[i], "-rollback")==0 ){
  132. runSql(db, "PRAGMA journal_mode=DELETE");
  133. }else{
  134. fprintf(stderr, "unknown option %s\n", argv[i]);
  135. usage(argv[0]);
  136. }
  137. }
  138. runSql(db,
  139. "PRAGMA cache_size=10;"
  140. "BEGIN;"
  141. "UPDATE t1 SET y=(y || -x)"
  142. );
  143. exit(0);
  144. }else{
  145. usage(argv[0]);
  146. }
  147. return 0;
  148. }