3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
6 # May you do good and not evil.
7 # May you find forgiveness for yourself and forgive others.
8 # May you share freely, never taking more than you give.
10 #***********************************************************************
11 # This file implements regression tests for SQLite library. The
12 # focus of this script is page cache subsystem.
14 # $Id: collate3.test,v 1.13 2008/08/20 16:35:10 drh Exp $
16 set testdir [file dirname $argv0]
17 source $testdir/tester.tcl
20 # Tests are organised as follows:
22 # collate3.1.* - Errors related to unknown collation sequences.
23 # collate3.2.* - Errors related to undefined collation sequences.
24 # collate3.3.* - Writing to a table that has an index with an undefined c.s.
25 # collate3.4.* - Misc errors.
26 # collate3.5.* - Collation factory.
30 # These tests ensure that when a user executes a statement with an
31 # unknown collation sequence an error is returned.
33 do_test collate3-1.0 {
35 CREATE TABLE collate3t1(c1 UNIQUE);
38 do_test collate3-1.1 {
40 SELECT * FROM collate3t1 ORDER BY 1 collate garbage;
42 } {1 {no such collation sequence: garbage}}
43 do_test collate3-1.1.2 {
45 SELECT DISTINCT c1 COLLATE garbage FROM collate3t1;
47 } {1 {no such collation sequence: garbage}}
48 do_test collate3-1.2 {
50 CREATE TABLE collate3t2(c1 collate garbage);
52 } {1 {no such collation sequence: garbage}}
53 do_test collate3-1.3 {
55 CREATE INDEX collate3i1 ON collate3t1(c1 COLLATE garbage);
57 } {1 {no such collation sequence: garbage}}
60 DROP TABLE collate3t1;
63 proc caseless {a b} { string compare -nocase $a $b }
64 do_test collate3-1.4 {
65 db collate caseless caseless
67 CREATE TABLE t1(a COLLATE caseless);
68 INSERT INTO t1 VALUES('Abc2');
69 INSERT INTO t1 VALUES('abc1');
70 INSERT INTO t1 VALUES('aBc3');
72 execsql { SELECT * FROM t1 ORDER BY a }
75 do_test collate3-1.5 {
78 catchsql { SELECT * FROM t1 ORDER BY a }
79 } {1 {no such collation sequence: caseless}}
81 do_test collate3-1.6.1 {
82 db collate caseless caseless
83 execsql { CREATE INDEX i1 ON t1(a) }
84 execsql { SELECT * FROM t1 ORDER BY a }
87 do_test collate3-1.6.2 {
90 catchsql { SELECT * FROM t1 ORDER BY a }
91 } {1 {no such collation sequence: caseless}}
93 do_test collate3-1.6.3 {
96 catchsql { PRAGMA integrity_check }
97 } {1 {no such collation sequence: caseless}}
99 do_test collate3-1.6.4 {
103 } {1 {no such collation sequence: caseless}}
105 do_test collate3-1.7.1 {
106 db collate caseless caseless
110 CREATE INDEX i1 ON t1(a COLLATE caseless);
111 INSERT INTO t1 VALUES('Abc2');
112 INSERT INTO t1 VALUES('abc1');
113 INSERT INTO t1 VALUES('aBc3');
114 SELECT * FROM t1 ORDER BY a COLLATE caseless;
118 do_test collate3-1.7.2 {
121 catchsql { SELECT * FROM t1 ORDER BY a COLLATE caseless}
122 } {1 {no such collation sequence: caseless}}
124 do_test collate3-1.7.4 {
128 } {1 {no such collation sequence: caseless}}
130 do_test collate3-1.7.3 {
133 catchsql { PRAGMA integrity_check }
134 } {1 {no such collation sequence: caseless}}
136 do_test collate3-1.7.4 {
140 } {1 {no such collation sequence: caseless}}
142 do_test collate3-1.7.5 {
145 db collate caseless caseless
146 catchsql { PRAGMA integrity_check }
149 proc needed {nm} { db collate caseless caseless }
150 do_test collate3-1.7.6 {
153 db collation_needed needed
154 catchsql { PRAGMA integrity_check }
157 do_test collate3-1.8 {
158 execsql { DROP TABLE t1 }
162 # Create a table with a default collation sequence, then close
163 # and re-open the database without re-registering the collation
164 # sequence. Then make sure the library stops us from using
165 # the collation sequence in:
166 # * an explicitly collated ORDER BY
167 # * an ORDER BY that uses the default collation sequence
168 # * an expression (=)
169 # * a CREATE TABLE statement
170 # * a CREATE INDEX statement that uses a default collation sequence
171 # * a GROUP BY that uses the default collation sequence
172 # * a SELECT DISTINCT that uses the default collation sequence
173 # * Compound SELECTs that uses the default collation sequence
174 # * An ORDER BY on a compound SELECT with an explicit ORDER BY.
176 do_test collate3-2.0 {
177 db collate string_compare {string compare}
179 CREATE TABLE collate3t1(c1 COLLATE string_compare, c2);
185 do_test collate3-2.1 {
187 SELECT * FROM collate3t1 ORDER BY 1 COLLATE string_compare;
189 } {1 {no such collation sequence: string_compare}}
190 do_test collate3-2.2 {
192 SELECT * FROM collate3t1 ORDER BY c1;
194 } {1 {no such collation sequence: string_compare}}
195 do_test collate3-2.3 {
197 SELECT * FROM collate3t1 WHERE c1 = 'xxx';
199 } {1 {no such collation sequence: string_compare}}
200 do_test collate3-2.4 {
202 CREATE TABLE collate3t2(c1 COLLATE string_compare);
204 } {1 {no such collation sequence: string_compare}}
205 do_test collate3-2.5 {
207 CREATE INDEX collate3t1_i1 ON collate3t1(c1);
209 } {1 {no such collation sequence: string_compare}}
210 do_test collate3-2.6 {
212 SELECT * FROM collate3t1;
215 do_test collate3-2.7.1 {
217 SELECT count(*) FROM collate3t1 GROUP BY c1;
219 } {1 {no such collation sequence: string_compare}}
220 # do_test collate3-2.7.2 {
222 # SELECT * FROM collate3t1 GROUP BY c1;
224 # } {1 {GROUP BY may only be used on aggregate queries}}
225 do_test collate3-2.7.2 {
227 SELECT * FROM collate3t1 GROUP BY c1;
229 } {1 {no such collation sequence: string_compare}}
230 do_test collate3-2.8 {
232 SELECT DISTINCT c1 FROM collate3t1;
234 } {1 {no such collation sequence: string_compare}}
237 do_test collate3-2.9 {
239 SELECT c1 FROM collate3t1 UNION SELECT c1 FROM collate3t1;
241 } {1 {no such collation sequence: string_compare}}
242 do_test collate3-2.10 {
244 SELECT c1 FROM collate3t1 EXCEPT SELECT c1 FROM collate3t1;
246 } {1 {no such collation sequence: string_compare}}
247 do_test collate3-2.11 {
249 SELECT c1 FROM collate3t1 INTERSECT SELECT c1 FROM collate3t1;
251 } {1 {no such collation sequence: string_compare}}
252 do_test collate3-2.12 {
254 SELECT c1 FROM collate3t1 UNION ALL SELECT c1 FROM collate3t1;
257 do_test collate3-2.13 {
259 SELECT 10 UNION ALL SELECT 20 ORDER BY 1 COLLATE string_compare;
261 } {1 {no such collation sequence: string_compare}}
262 do_test collate3-2.14 {
264 SELECT 10 INTERSECT SELECT 20 ORDER BY 1 COLLATE string_compare;
266 } {1 {no such collation sequence: string_compare}}
267 do_test collate3-2.15 {
269 SELECT 10 EXCEPT SELECT 20 ORDER BY 1 COLLATE string_compare;
271 } {1 {no such collation sequence: string_compare}}
272 do_test collate3-2.16 {
274 SELECT 10 UNION SELECT 20 ORDER BY 1 COLLATE string_compare;
276 } {1 {no such collation sequence: string_compare}}
277 do_test collate3-2.17 {
279 SELECT c1 FROM collate3t1 UNION ALL SELECT c1 FROM collate3t1 ORDER BY 1;
281 } {1 {no such collation sequence: string_compare}}
282 } ;# ifcapable compound
285 # Create an index that uses a collation sequence then close and
286 # re-open the database without re-registering the collation
287 # sequence. Then check that for the table with the index
289 # * An UPDATE on the column with the index fails,
290 # * An UPDATE on a different column succeeds.
291 # * A DELETE with a WHERE clause fails
292 # * A DELETE without a WHERE clause succeeds
294 # Also, ensure that the restrictions tested by collate3-2.* still
295 # apply after the index has been created.
297 do_test collate3-3.0 {
298 db collate string_compare {string compare}
300 CREATE INDEX collate3t1_i1 ON collate3t1(c1);
301 INSERT INTO collate3t1 VALUES('xxx', 'yyy');
307 db eval {select * from collate3t1}
308 do_test collate3-3.1 {
310 INSERT INTO collate3t1 VALUES('xxx', 0);
312 } {1 {no such collation sequence: string_compare}}
313 do_test collate3-3.2 {
315 UPDATE collate3t1 SET c1 = 'xxx';
317 } {1 {no such collation sequence: string_compare}}
318 do_test collate3-3.3 {
320 UPDATE collate3t1 SET c2 = 'xxx';
323 do_test collate3-3.4 {
325 DELETE FROM collate3t1 WHERE 1;
327 } {1 {no such collation sequence: string_compare}}
328 do_test collate3-3.5 {
330 SELECT * FROM collate3t1;
333 do_test collate3-3.6 {
335 DELETE FROM collate3t1;
338 ifcapable {integrityck} {
339 do_test collate3-3.8 {
341 PRAGMA integrity_check
343 } {1 {no such collation sequence: string_compare}}
345 do_test collate3-3.9 {
347 SELECT * FROM collate3t1;
350 do_test collate3-3.10 {
352 SELECT * FROM collate3t1 ORDER BY 1 COLLATE string_compare;
354 } {1 {no such collation sequence: string_compare}}
355 do_test collate3-3.11 {
357 SELECT * FROM collate3t1 ORDER BY c1;
359 } {1 {no such collation sequence: string_compare}}
360 do_test collate3-3.12 {
362 SELECT * FROM collate3t1 WHERE c1 = 'xxx';
364 } {1 {no such collation sequence: string_compare}}
365 do_test collate3-3.13 {
367 CREATE TABLE collate3t2(c1 COLLATE string_compare);
369 } {1 {no such collation sequence: string_compare}}
370 do_test collate3-3.14 {
372 CREATE INDEX collate3t1_i2 ON collate3t1(c1);
374 } {1 {no such collation sequence: string_compare}}
375 do_test collate3-3.15 {
377 DROP TABLE collate3t1;
381 # Check we can create an index that uses an explicit collation
382 # sequence and then close and re-open the database.
383 do_test collate3-4.6 {
384 db collate user_defined "string compare"
386 CREATE TABLE collate3t1(a, b);
387 INSERT INTO collate3t1 VALUES('hello', NULL);
388 CREATE INDEX collate3i1 ON collate3t1(a COLLATE user_defined);
391 do_test collate3-4.7 {
395 SELECT * FROM collate3t1 ORDER BY a COLLATE user_defined;
397 } {1 {no such collation sequence: user_defined}}
398 do_test collate3-4.8.1 {
399 db collate user_defined "string compare"
401 SELECT * FROM collate3t1 ORDER BY a COLLATE user_defined;
404 do_test collate3-4.8.2 {
410 do_test collate3-4.8.3 {
412 DROP TABLE collate3t1;
416 # Compare strings as numbers.
417 proc numeric_compare {lhs rhs} {
421 set res [expr ($lhs > $rhs)?1:0]
426 # Check we can create a view that uses an explicit collation
427 # sequence and then close and re-open the database.
429 do_test collate3-4.9 {
430 db collate user_defined numeric_compare
432 CREATE TABLE collate3t1(a, b);
433 INSERT INTO collate3t1 VALUES('2', NULL);
434 INSERT INTO collate3t1 VALUES('101', NULL);
435 INSERT INTO collate3t1 VALUES('12', NULL);
436 CREATE VIEW collate3v1 AS SELECT * FROM collate3t1
437 ORDER BY 1 COLLATE user_defined;
438 SELECT * FROM collate3v1;
440 } {2 {} 12 {} 101 {}}
441 do_test collate3-4.10 {
445 SELECT * FROM collate3v1;
447 } {1 {no such collation sequence: user_defined}}
448 do_test collate3-4.11 {
449 db collate user_defined numeric_compare
451 SELECT * FROM collate3v1;
453 } {0 {2 {} 12 {} 101 {}}}
454 do_test collate3-4.12 {
456 DROP TABLE collate3t1;
462 # Test the collation factory. In the code, the "no such collation sequence"
463 # message is only generated in two places. So these tests just test that
464 # the collation factory can be called once from each of those points.
466 do_test collate3-5.0 {
468 CREATE TABLE collate3t1(a);
469 INSERT INTO collate3t1 VALUES(10);
470 SELECT a FROM collate3t1 ORDER BY 1 COLLATE unk;
472 } {1 {no such collation sequence: unk}}
473 do_test collate3-5.1 {
476 db collate $nm {string compare}
479 db collation_needed cfact
481 do_test collate3-5.2 {
483 SELECT a FROM collate3t1 ORDER BY 1 COLLATE unk;
486 do_test collate3-5.3 {
489 do_test collate3-5.4 {
491 SELECT a FROM collate3t1 ORDER BY 1 COLLATE unk;
494 do_test collate3-5.5 {
497 do_test collate3-5.6 {
499 SELECT a FROM collate3t1 ORDER BY 1 COLLATE unk;
502 do_test collate3-5.7 {
504 DROP TABLE collate3t1;
505 CREATE TABLE collate3t1(a COLLATE unk);
510 SELECT a FROM collate3t1 ORDER BY 1;
512 } {1 {no such collation sequence: unk}}
513 do_test collate3-5.8 {
516 db collate $nm {string compare}
519 db collation_needed cfact
521 SELECT a FROM collate3t1 ORDER BY 1;
525 do_test collate3-5.9 {
527 DROP TABLE collate3t1;