os/persistentdata/persistentstorage/sqlite3api/TEST/TclScript/vtabC.test
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
# 2008 April 10
sl@0
     2
#
sl@0
     3
# The author disclaims copyright to this source code.  In place of
sl@0
     4
# a legal notice, here is a blessing:
sl@0
     5
#
sl@0
     6
#    May you do good and not evil.
sl@0
     7
#    May you find forgiveness for yourself and forgive others.
sl@0
     8
#    May you share freely, never taking more than you give.
sl@0
     9
#
sl@0
    10
#***********************************************************************
sl@0
    11
# This file implements regression tests for SQLite library.  The
sl@0
    12
# focus of this file is is verifying that the xUpdate, xSync, xCommit
sl@0
    13
# and xRollback methods are only invoked after an xBegin or xCreate.
sl@0
    14
# Ticket #3083.
sl@0
    15
#
sl@0
    16
# $Id: vtabC.test,v 1.1 2008/04/28 20:27:54 drh Exp $
sl@0
    17
sl@0
    18
set testdir [file dirname $argv0]
sl@0
    19
source $testdir/tester.tcl
sl@0
    20
sl@0
    21
ifcapable !vtab {
sl@0
    22
  finish_test
sl@0
    23
  return
sl@0
    24
}
sl@0
    25
sl@0
    26
sl@0
    27
# N will be the number of virtual tables we have defined.
sl@0
    28
#
sl@0
    29
unset -nocomplain N
sl@0
    30
for {set N 1} {$N<=20} {incr N} {
sl@0
    31
  db close
sl@0
    32
  file delete -force test.db test.db-journal
sl@0
    33
  sqlite3 db test.db
sl@0
    34
  register_echo_module [sqlite3_connection_pointer db]
sl@0
    35
sl@0
    36
  # Create $N tables and $N virtual tables to echo them.
sl@0
    37
  #
sl@0
    38
  unset -nocomplain tablist
sl@0
    39
  set tablist {}
sl@0
    40
  do_test vtabC-1.$N.1 {
sl@0
    41
    for {set i 1} {$i<=$::N} {incr i} {
sl@0
    42
      execsql "CREATE TABLE t${i}(x)"
sl@0
    43
      execsql "CREATE VIRTUAL TABLE vt$i USING echo(t$i)"
sl@0
    44
      lappend ::tablist t$i vt$i
sl@0
    45
    }
sl@0
    46
    execsql {SELECT count(*) FROM sqlite_master}
sl@0
    47
  } [expr {$N*2}]
sl@0
    48
  do_test vtabC-1.$N.2 {
sl@0
    49
    execsql {SELECT name FROM sqlite_master}
sl@0
    50
  } $tablist
sl@0
    51
sl@0
    52
  # Create a table m and add triggers to make changes on all
sl@0
    53
  # of the virtual tables when m is changed.
sl@0
    54
  #
sl@0
    55
  do_test vtabC-1.$N.3 {
sl@0
    56
    execsql {CREATE TABLE m(a)}
sl@0
    57
    set sql "CREATE TRIGGER rins AFTER INSERT ON m BEGIN\n"
sl@0
    58
    for {set i 1} {$i<=$::N} {incr i} {
sl@0
    59
      append sql "  INSERT INTO vt$i VALUES(NEW.a+$i);\n"
sl@0
    60
    }
sl@0
    61
    append sql "END;"
sl@0
    62
    execsql $sql
sl@0
    63
    execsql {SELECT count(*) FROM sqlite_master}
sl@0
    64
  } [expr {$N*2+2}]
sl@0
    65
  do_test vtabC-1.$N.4 {
sl@0
    66
    execsql {
sl@0
    67
      INSERT INTO m VALUES(1000);
sl@0
    68
      SELECT * FROM m;
sl@0
    69
    }
sl@0
    70
  } {1000}
sl@0
    71
  for {set j 1} {$j<=$::N} {incr j} {
sl@0
    72
    do_test vtabC-1.$N.5.$j {
sl@0
    73
      execsql "SELECT * FROM t$::j"
sl@0
    74
    } [expr {$j+1000}]
sl@0
    75
    do_test vtabC-1.$N.6.$j {
sl@0
    76
      execsql "SELECT * FROM vt$::j"
sl@0
    77
    } [expr {$j+1000}]
sl@0
    78
  }
sl@0
    79
  do_test vtabC-1.$N.7 {
sl@0
    80
    set sql "CREATE TRIGGER rins2 BEFORE INSERT ON m BEGIN\n"
sl@0
    81
    for {set i 1} {$i<=$::N} {incr i} {
sl@0
    82
      append sql "  INSERT INTO vt$i VALUES(NEW.a+$i*100);\n"
sl@0
    83
    }
sl@0
    84
    for {set i 1} {$i<=$::N} {incr i} {
sl@0
    85
      append sql "  INSERT INTO vt$i VALUES(NEW.a+$i*10000);\n"
sl@0
    86
    }
sl@0
    87
    append sql "END;"
sl@0
    88
    execsql $sql
sl@0
    89
    execsql {SELECT count(*) FROM sqlite_master}
sl@0
    90
  } [expr {$N*2+3}]
sl@0
    91
  do_test vtabC-1.$N.8 {
sl@0
    92
    execsql {
sl@0
    93
      INSERT INTO m VALUES(9000000);
sl@0
    94
      SELECT * FROM m;
sl@0
    95
    }
sl@0
    96
  } {1000 9000000}
sl@0
    97
  unset -nocomplain res
sl@0
    98
  for {set j 1} {$j<=$::N} {incr j} {
sl@0
    99
    set res [expr {$j+1000}]
sl@0
   100
    lappend res [expr {$j*100+9000000}]
sl@0
   101
    lappend res [expr {$j*10000+9000000}]
sl@0
   102
    lappend res [expr {$j+9000000}]
sl@0
   103
    do_test vtabC-1.$N.9.$j {
sl@0
   104
      execsql "SELECT * FROM t$::j"
sl@0
   105
    } $res
sl@0
   106
    do_test vtabC-1.$N.10.$j {
sl@0
   107
      execsql "SELECT * FROM vt$::j"
sl@0
   108
    } $res
sl@0
   109
  }
sl@0
   110
}
sl@0
   111
unset -nocomplain res N i j
sl@0
   112
sl@0
   113
sl@0
   114
finish_test