summaryrefslogtreecommitdiffstats
path: root/ext/intck/intck1.test
blob: 187132f766c67e88f706ca7a2421fa59a22a0163 (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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# 2008 Feb 19
#
# 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.
#
#***********************************************************************
#
# The focus of this file is testing the incremental integrity check
# (intck) extension.
#

source [file join [file dirname [info script]] intck_common.tcl]
set testprefix intck1
return_if_no_intck

foreach {tn sql} {
  1 "CREATE TABLE t1(a PRIMARY KEY, b)"
  2 "CREATE TABLE t2(a PRIMARY KEY, b) WITHOUT   ROWID  "
  3 "CREATE TABLE t3(a PRIMARY KEY, b) WITHOUT   rowID;"
  4 "CREATE TABLE t4(a PRIMARY KEY, ROWID)"
  5 {CREATE TABLE t5(a PRIMARY KEY, ROWID) WITHOUT ROWID
    }
} {
  do_test 1.1.$tn {
    db eval $sql
    set {} {}
  } {}
}

set space " \n\v\t\r\f"

do_execsql_test 1.2 {
  SELECT name, (rtrim(sql, $space) LIKE '%rowid') 
  FROM sqlite_schema WHERE type='table'
  ORDER BY 1
} {
  t1 0
  t2 1
  t3 1
  t4 0
  t5 1
}

do_execsql_test 1.3 {
  CREATE TABLE x1(a COLLATE nocase, b INTEGER, c BLOB);
  INSERT INTO x1 VALUES('lEtTeRs', 1234, 1234);
}
do_execsql_test 1.3.1 {
  WITH wrapper(c1, c2, c3) AS (
    SELECT a, b, c FROM x1
  )
  SELECT * FROM wrapper WHERE c1='letters';
} {lEtTeRs 1234 1234}
do_execsql_test 1.3.2 {
  WITH wrapper(c1, c2, c3) AS (
    SELECT a, b, c FROM x1
  )
  SELECT * FROM wrapper WHERE c2='1234';
} {lEtTeRs 1234 1234}
do_execsql_test 1.3.2 {
  WITH wrapper(c1, c2, c3) AS (
    SELECT a, b, c FROM x1
  )
  SELECT * FROM wrapper WHERE c3='1234';
} {}

do_execsql_test 1.4 {
  CREATE TABLE z1(a, b);
  CREATE INDEX z1ab ON z1(a+b COLLATE nocase);
}
do_execsql_test 1.4.1 {
  SELECT * FROM z1 INDEXED BY z1ab 
}

do_catchsql_test 1.5.1 {
  CREATE INDEX z1b ON z1(b ASC NULLS LAST);
} {1 {unsupported use of NULLS LAST}}
do_catchsql_test 1.5.2 {
  CREATE INDEX z1b ON z1(b DESC NULLS LAST);
} {1 {unsupported use of NULLS LAST}}
do_catchsql_test 1.5.3 {
  CREATE INDEX z1b ON z1(b ASC NULLS FIRST);
} {1 {unsupported use of NULLS FIRST}}
do_catchsql_test 1.5.4 {
  CREATE INDEX z1b ON z1(b DESC NULLS FIRST);
} {1 {unsupported use of NULLS FIRST}}


reset_db
do_execsql_test 1.6.1 {
  CREATE TABLE t1(i INTEGER PRIMARY KEY, b, c);
  CREATE INDEX i1 ON t1(b);
  ANALYZE;
  INSERT INTO sqlite_stat1 VALUES('t1', 'i1', '10000 10000');
  ANALYZE sqlite_schema;
} {}
do_eqp_test 1.6.2 {
  SELECT 1 FROM t1 INDEXED BY i1 WHERE (b, i) IS (?, ?);
} {SEARCH}



#-------------------------------------------------------------------------
reset_db

do_test 2.0 {
  set ic [sqlite3_intck db main]
  $ic close
} {}

do_execsql_test 2.1 {
  CREATE TABLE t1(a, b);
  INSERT INTO t1 VALUES(1, 2);
  INSERT INTO t1 VALUES(3, 4);

  CREATE INDEX i1 ON t1(a COLLATE nocase);
  CREATE INDEX i2 ON t1(b, a);
  CREATE INDEX i3 ON t1(b + a COLLATE nocase) WHERE a!=1;
}

do_intck_test 2.2 {
}

# Delete a row from each of the i1 and i2 indexes using the imposter
# table interface.
#
do_test 2.3 {
  db eval {SELECT name, rootpage FROM sqlite_schema} {
    set R($name) $rootpage
  }
  sqlite3_test_control SQLITE_TESTCTRL_IMPOSTER db main 1 $R(i1)
  db eval { CREATE TABLE imp1(a PRIMARY KEY, rowid) WITHOUT ROWID; }
  sqlite3_test_control SQLITE_TESTCTRL_IMPOSTER db main 1 $R(i2)
  db eval { CREATE TABLE imp2(b, a, rowid, PRIMARY KEY(b, a)) WITHOUT ROWID; }
  sqlite3_test_control SQLITE_TESTCTRL_IMPOSTER db main 0 0

  db eval {
    DELETE FROM imp1 WHERE rowid=1;
    DELETE FROM imp2 WHERE rowid=2;
  }

  db close
  sqlite3 db test.db
} {}

do_intck_test 2.4 {
  {entry (1,1) missing from index i1} 
  {entry (4,3,2) missing from index i2}
}

#-------------------------------------------------------------------------
reset_db
do_execsql_test 3.0 {
  CREATE TABLE x1(a, b, c, PRIMARY KEY(c, b)) WITHOUT ROWID;
  CREATE INDEX x1a ON x1(a COLLATE nocase);

  INSERT INTO x1 VALUES(1, 2, 'three');
  INSERT INTO x1 VALUES(4, 5, 'six');
  INSERT INTO x1 VALUES(7, 8, 'nine');
}

do_intck_test 3.1 { }

do_test 3.2 {
  db eval {SELECT name, rootpage FROM sqlite_schema} {
    set R($name) $rootpage
  }
  sqlite3_test_control SQLITE_TESTCTRL_IMPOSTER db main 1 $R(x1a)
  db eval { CREATE TABLE imp1(c, b, a, PRIMARY KEY(c, b)) WITHOUT ROWID }
  sqlite3_test_control SQLITE_TESTCTRL_IMPOSTER db main 0 0

  db eval {
    DELETE FROM imp1 WHERE a=5;
  }
  execsql_pp {
  }

  db close
  sqlite3 db test.db
} {}

do_intck_test 3.3 { 
  {entry (4,'six',5) missing from index x1a}
}

#-------------------------------------------------------------------------
reset_db
do_execsql_test 4.0 {
  CREATE TABLE www(x, y, z);
  CREATE INDEX w1 ON www( (x+1), z );
  INSERT INTO www VALUES(1, 1, 1), (2, 2, 2);
}

do_intck_test 4.1 { }

#-------------------------------------------------------------------------
reset_db
do_execsql_test 5.0 {
  CREATE TABLE t1(a, b);             
  CREATE INDEX i1 ON t1(a COLLATE NOCASE);
  INSERT INTO t1 VALUES(1, 1);
  INSERT INTO t1 VALUES(2, 2);
}

do_test 5.1 {
  set ic [sqlite3_intck db nosuchdb]
  $ic step
} {SQLITE_ERROR}

do_test 5.2 {
  $ic close
  set ic [sqlite3_intck db {}]
  while {[$ic step]=="SQLITE_OK"} {}
  set res [$ic error]
  $ic close
  set res
} {SQLITE_OK {}}

do_test 5.3 { test_do_intck db "main" } {}

do_test 5.4 {
  set ret {}
  set ic [sqlite3_intck db main]
  db eval [$ic test_sql t1] {
    if {$error_message!=""} { lappend ret $error_message }
  }
  $ic close
  set ret
} {}

do_test 5.5 {
  set ret {}
  set ic [sqlite3_intck db main]
  db eval [$ic test_sql {}] {
    if {$error_message!=""} { lappend ret $error_message }
  }
  $ic close
  set ret
} {}

db cache flush

do_test 5.6 {
  set ret {}
  set ic [sqlite3_intck db main]
  $ic step
  db eval [$ic test_sql {}] {
    if {$error_message!=""} { lappend ret $error_message }
  }
  $ic close
  set ret
} {}

#-------------------------------------------------------------------------
reset_db

do_execsql_test 6.0 {
  CREATE TABLE t1(x, y, PRIMARY KEY(x)) WITHOUT ROWID;
  CREATE INDEX i1 ON t1(y, x);
  INSERT INTO t1 VALUES(X'0000', X'1111');
}

do_intck_test 6.1 {}

do_execsql_test 6.2.1 {
  PRAGMA writable_schema = 1;
  UPDATE sqlite_schema SET sql = 'CREATE INDEX i1' WHERE name='i1';
} {}
do_intck_test 6.2.2 {}

do_execsql_test 6.3.1 {
  UPDATE sqlite_schema SET sql = 'CREATE INDEX i1(y' WHERE name='i1';
} {}
do_intck_test 6.3.2 {}

do_execsql_test 6.4.1 {
  UPDATE sqlite_schema 
  SET sql = 'CREATE INDEX i1(y) hello world' 
  WHERE name='i1';
} {}
do_intck_test 6.4.2 {}

do_execsql_test 6.5.1 {
  UPDATE sqlite_schema 
  SET sql = 'CREATE INDEX i1(y, x) WHERE 1     ' 
  WHERE name='i1';
} {}
do_intck_test 6.5.2 {}

do_execsql_test 6.6.1 {
  UPDATE sqlite_schema 
  SET sql = 'CREATE INDEX i1(  ,  ) WHERE 1     ' 
  WHERE name='i1';
} {}

do_test 6.7.2 {
  set ic [sqlite3_intck db main]
  $ic step
} {SQLITE_ERROR}
do_test 6.5.3 {
  $ic error
} {SQLITE_ERROR {near "AS": syntax error}}
$ic close

do_execsql_test 6.6.1 {
  UPDATE sqlite_schema 
  SET sql = 'CREATE INDEX i1([y'
  WHERE name='i1';
} {}
do_intck_test 6.6.2 {}

#-------------------------------------------------------------------------
reset_db
do_execsql_test 7.0 {
  CREATE TABLE x1("1", "22", "3333", four);
  CREATE INDEX i1 ON x1(  "1"  , "22", NULL);
  INSERT INTO x1 VALUES(1, 22, 3333, NULL);
  INSERT INTO x1 VALUES(1, 22, 3333, NULL);
}
do_execsql_test 7.1 " CREATE INDEX i2 ON x1(  \"1\"\r\n\t ) "
do_execsql_test 7.2 { CREATE INDEX i3 ON x1( "22" || 'abc''def' || `1` ) }
do_execsql_test 7.3 { CREATE INDEX i4 ON x1( [22] + [1] ) }
do_execsql_test 7.4 { CREATE INDEX i5 ON x1( four||'hello' ) }

do_intck_test 7.5 {}


finish_test