000001  # 2014-09-25
000002  #
000003  # The author disclaims copyright to this source code.  In place of
000004  # a legal notice, here is a blessing:
000005  #
000006  #    May you do good and not evil.
000007  #    May you find forgiveness for yourself and forgive others.
000008  #    May you share freely, never taking more than you give.
000009  #
000010  #***********************************************************************
000011  #
000012  # This file contains tests for the "truncate" option in the multiplexor.
000013  #
000014  
000015  set testdir [file dirname $argv0]
000016  source $testdir/tester.tcl
000017  set ::testprefix multiplex4
000018  
000019  db close
000020  sqlite3_shutdown
000021  sqlite3_multiplex_initialize {} 0
000022  
000023  # delete all filesl with the base name of $basename
000024  #
000025  proc multiplex_delete_db {basename} {
000026    foreach file [glob -nocomplain $basename.*] {
000027      forcedelete $file
000028    }
000029  }
000030  
000031  # Return a sorted list of all files with the base name of $basename.
000032  # Except, delete all text from the end of $basename through the NNN
000033  # suffix on the end of the filename.
000034  #
000035  proc multiplex_file_list {basename} {
000036    set x {}
000037    foreach file [glob -nocomplain $basename.*] {
000038      regsub "^$basename\\..*(\\d\\d\\d)\$" $file $basename.\\1 file
000039      lappend x $file
000040    }
000041    return [lsort $x]
000042  }
000043  
000044  do_test multiplex4-1.0 {
000045    multiplex_delete_db mx4test
000046    sqlite3 db {file:mx4test.db?chunksize=10&truncate=1} -uri 1 -vfs multiplex
000047    db eval {
000048      CREATE TABLE t1(x);
000049      INSERT INTO t1(x) VALUES(randomblob(250000));
000050    }
000051    multiplex_file_list mx4test
000052  } {mx4test.001 mx4test.db}
000053  
000054  do_test multiplex4-1.1 {
000055    db eval {
000056      DELETE FROM t1;
000057      VACUUM;
000058    }
000059    multiplex_file_list mx4test
000060  } {mx4test.db}
000061  
000062  # NB:  The PRAGMA multiplex_truncate command is implemented using the
000063  # SQLITE_FCNTL_PRAGMA file-control...
000064  #
000065  # EVIDENCE-OF: R-12238-55120 Whenever a PRAGMA statement is parsed, an
000066  # SQLITE_FCNTL_PRAGMA file control is sent to the open sqlite3_file
000067  # object corresponding to the database file to which the pragma
000068  # statement refers.
000069  #
000070  do_test multiplex4-1.2 {
000071    db eval {PRAGMA multiplex_truncate}
000072  } {on}
000073  do_test multiplex4-1.3 {
000074    db eval {PRAGMA multiplex_truncate=off}
000075  } {off}
000076  do_test multiplex4-1.4 {
000077    db eval {PRAGMA multiplex_truncate}
000078  } {off}
000079  do_test multiplex4-1.5 {
000080    db eval {PRAGMA multiplex_truncate=on}
000081  } {on}
000082  do_test multiplex4-1.6 {
000083    db eval {PRAGMA multiplex_truncate}
000084  } {on}
000085  do_test multiplex4-1.7 {
000086    db eval {PRAGMA multiplex_truncate=0}
000087  } {off}
000088  do_test multiplex4-1.8 {
000089    db eval {PRAGMA multiplex_truncate=1}
000090  } {on}
000091  do_test multiplex4-1.9 {
000092    db eval {PRAGMA multiplex_truncate=0}
000093  } {off}
000094  
000095  # EVIDENCE-OF: R-26188-08449 If the SQLITE_FCNTL_PRAGMA file control
000096  # returns SQLITE_OK, then the parser assumes that the VFS has handled
000097  # the PRAGMA itself and the parser generates a no-op prepared statement
000098  # if result string is NULL, or that returns a copy of the result string
000099  # if the string is non-NULL.
000100  #
000101  do_test multiplex4-1.9-explain {
000102    db eval {EXPLAIN PRAGMA multiplex_truncate=0;}
000103  } {/String8 \d \d \d off/}
000104  
000105  do_test multiplex4-1.10 {
000106    db eval {
000107      INSERT INTO t1(x) VALUES(randomblob(250000));
000108    }
000109    multiplex_file_list mx4test
000110  } {mx4test.001 mx4test.db}
000111  
000112  do_test multiplex4-1.11 {
000113    db eval {
000114      DELETE FROM t1;
000115      VACUUM;
000116    }
000117    multiplex_file_list mx4test
000118  } {mx4test.001 mx4test.db}
000119  
000120  do_test multiplex4-1.12 {
000121    db eval {
000122      PRAGMA multiplex_truncate=ON;
000123      DROP TABLE t1;
000124      VACUUM;
000125    }
000126    multiplex_file_list mx4test
000127  } {mx4test.db}
000128  
000129  catch { db close }
000130  forcedelete mx4test.db
000131  sqlite3_multiplex_shutdown
000132  finish_test