summaryrefslogtreecommitdiff
path: root/test/vtab_shared.test
blob: 34739929001eeeabe5bf236763b6c05aa0c1d2d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# 2007 April 16
#
# 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 tests interactions between the virtual table and
# shared-schema functionality.
#
# $Id: vtab_shared.test,v 1.3 2009/07/24 17:58:53 danielk1977 Exp $

set testdir [file dirname $argv0]
source $testdir/tester.tcl
set testprefix vtab_shared

ifcapable !vtab||!shared_cache {
  finish_test
  return
}

db close
sqlite3_enable_shared_cache 1
sqlite3 db  test.db
sqlite3 db2 test.db

do_test vtab_shared-1.1 {
  register_echo_module [sqlite3_connection_pointer db]
  execsql {
    CREATE TABLE t0(a, b, c);
    INSERT INTO t0 VALUES(1, 2, 3);
    CREATE VIRTUAL TABLE t1 USING echo(t0);
  }
} {}

do_test vtab_shared-1.2 {
  execsql { SELECT * FROM t1 } db
} {1 2 3}

# Fails because the 'echo' module has not been registered with connection db2
do_test vtab_shared-1.3 {
  catchsql { SELECT * FROM t1 } db2
} {1 {no such module: echo}}

do_test vtab_shared-1.4 {
  execsql { SELECT * FROM t0 } db2
} {1 2 3}

do_test vtab_shared-1.5 {
  register_echo_module [sqlite3_connection_pointer db2]
  execsql { SELECT * FROM t1 } db
} {1 2 3}

# Works after the module is registered with db2
do_test vtab_shared-1.6 {
  execsql { SELECT * FROM t1 } db2
} {1 2 3}

# Set a write-lock on table t0 using connection [db]. Then try to read from
# virtual table t1 using [db2]. That this returns an SQLITE_LOCKED error
# shows that the correct sqlite3_vtab is being used.
#
do_test vtab_shared-1.8.1 {
  execsql { 
    BEGIN;
    INSERT INTO t1 VALUES(4, 5, 6);
    SELECT * FROM t1;
  }
} {1 2 3 4 5 6}
do_test vtab_shared-1.8.2 {
  catchsql { SELECT * FROM t1 } db2
} {1 {database table is locked}}
do_test vtab_shared-1.8.3 {
  catchsql { SELECT *  FROM t0 } db2
} {1 {database table is locked: t0}}
do_test vtab_shared-1.8.4 {
  execsql { SELECT * FROM t0 } db
} {1 2 3 4 5 6}
do_test vtab_shared-1.8.5 {
  execsql { COMMIT } db
  execsql { SELECT *  FROM t1 } db2
} {1 2 3 4 5 6}

# While a SELECT is active on virtual table t1 via connection [db], close 
# [db2]. This causes the schema to be reset internally. Verify that this
# does not cause a problem.
#
foreach {iTest dbSelect dbClose} {
  1 db  db2
  2 db  db2
  3 db2 db
} {
  do_test vtab_shared-1.9.$iTest {
    set res [list]
    $dbSelect eval { SELECT * FROM t1 } {
      if {$a == 1} {$dbClose close}
      lappend res $a $b $c
    }
    sqlite3 $dbClose test.db
    register_echo_module [sqlite3_connection_pointer $dbClose]
    set res
  } {1 2 3 4 5 6}
}

# Ensure that it is not possible for one connection to DROP a virtual
# table while a second connection is reading from the database.
#
do_test vtab_shared-1.10 {
  db eval { SELECT * FROM t1 } {
    set error [catchsql { DROP TABLE t1 } db2]
    break
  }
  set error
} {1 {database table is locked: sqlite_master}}

do_test vtab_shared-1.11 {
  execsql {
    CREATE VIRTUAL TABLE t2 USING echo(t0);
    CREATE VIRTUAL TABLE t3 USING echo(t0);
  }
  execsql { SELECT * FROM t3 } db2
} {1 2 3 4 5 6}

ifcapable compound {
  do_test vtab_shared-1.12.1 {
    db close
    execsql { 
      SELECT * FROM t1 UNION ALL
      SELECT * FROM t2 UNION ALL
      SELECT * FROM t3 
    } db2
  } {1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6}
  do_test vtab_shared-1.12.2 {
    sqlite3 db test.db
    register_echo_module [sqlite3_connection_pointer db]
    execsql { 
      SELECT * FROM t1 UNION ALL
      SELECT * FROM t2 UNION ALL
      SELECT * FROM t3 
    } db
  } {1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6}
}

# Try a rename or two.
#
ifcapable altertable {
  do_test vtab_shared-1.13.1 {
    execsql { ALTER TABLE t1 RENAME TO t4 }
    execsql { SELECT * FROM t4 } db
  } {1 2 3 4 5 6}
  do_test vtab_shared-1.13.2 {
    execsql { SELECT * FROM t4 } db2
  } {1 2 3 4 5 6}
  do_test vtab_shared-1.13.3 {
    execsql { ALTER TABLE t2 RENAME TO t5 }
    execsql { SELECT * FROM t4 } db2
  } {1 2 3 4 5 6}
}

# Try an UPDATE/INSERT/DELETE on a shared vtab as the first statement after a
# schema is loaded.
do_test vtab_shared_1.14.1 {
  db2 close
  sqlite3 db2 test.db
  register_echo_module [sqlite3_connection_pointer db2]
  execsql { SELECT * FROM t3 }
} {1 2 3 4 5 6}
do_test vtab_shared_1.14.2 {
  execsql { 
    UPDATE t3 SET c = 'six' WHERE c = 6;
    SELECT * FROM t3;
  } db2
} {1 2 3 4 5 six}
do_test vtab_shared_1.14.3 {
  db2 close
  sqlite3 db2 test.db
  register_echo_module [sqlite3_connection_pointer db2]
  execsql { SELECT * FROM t3 }
} {1 2 3 4 5 six}
do_test vtab_shared_1.14.4 {
  execsql { 
    DELETE FROM t3 WHERE c = 'six';
    SELECT * FROM t3;
  } db2
} {1 2 3}
do_test vtab_shared_1.14.5 {
  db2 close
  sqlite3 db2 test.db
  register_echo_module [sqlite3_connection_pointer db2]
  execsql { SELECT * FROM t3 }
} {1 2 3}
do_test vtab_shared_1.14.6 {
  execsql { 
    INSERT INTO t3 VALUES(4, 5, 6);
    SELECT * FROM t3;
  } db2
} {1 2 3 4 5 6}

do_test vtab_shared_1.15.1 {
  db2 close
  sqlite3 db2 test.db
  register_echo_module [sqlite3_connection_pointer db2]
  execsql { 
    UPDATE t3 SET c = 'six' WHERE c = 6;
    SELECT * FROM t3;
  } db2
} {1 2 3 4 5 six}
do_test vtab_shared_1.15.2 {
  db2 close
  sqlite3 db2 test.db
  register_echo_module [sqlite3_connection_pointer db2]
  execsql { 
    DELETE FROM t3 WHERE c = 'six';
    SELECT * FROM t3;
  } db2
} {1 2 3}
do_test vtab_shared_1.15.3 {
  db2 close
  sqlite3 db2 test.db
  register_echo_module [sqlite3_connection_pointer db2]
  execsql { 
    INSERT INTO t3 VALUES(4, 5, 6);
    SELECT * FROM t3;
  }
} {1 2 3 4 5 6}

db close
db2 close

#---------------------------------------------------------------
# Test calling sqlite3_close() with vtabs on the disconnect list.
#
ifcapable rtree {
  reset_db
  do_test 2.1.1 {
    sqlite3 db  test.db
    sqlite3 db2 test.db
  
    # Create a virtual table using [db]. 
    execsql {
      CREATE VIRTUAL TABLE rt USING rtree(id, x1, x2);
      INSERT INTO rt VALUES(1, 2 ,3);
      SELECT * FROM rt;
    }
  
    # Drop the virtual table using [db2]. The sqlite3_vtab object belonging
    # to [db] is moved to the sqlite3.pDisconnect list.
    execsql { DROP TABLE rt } db2
  
    # Immediately close [db]. At one point this would fail due to the 
    # unfinalized statements held by the un-xDisconnect()ed sqlite3_vtab.
    db close
  } {}
  db2 close
}

ifcapable fts3 {
  # Same test as above, except using fts3 instead of rtree.
  reset_db
  do_test 2.2.1 {
    sqlite3 db  test.db
    sqlite3 db2 test.db
    execsql {
      CREATE VIRTUAL TABLE ft USING fts3;
      INSERT INTO ft VALUES('hello world');
      SELECT * FROM ft;
    } 
    execsql { DROP TABLE ft } db2
    db close
  } {}
  db2 close
}

sqlite3_enable_shared_cache 0
finish_test