4 # The author disclaims copyright to this source code. In place of
5 # a legal notice, here is a blessing:
7 # May you do good and not evil.
8 # May you find forgiveness for yourself and forgive others.
9 # May you share freely, never taking more than you give.
11 #***********************************************************************
12 # This file implements regression tests for SQLite library. The
13 # focus of this script is testing collation sequences.
16 set testdir [file dirname $argv0]
17 source $testdir/tester.tcl
18 set testprefix collate1
21 # Tests are roughly organised as follows:
23 # collate1-1.* - Single-field ORDER BY with an explicit COLLATE clause.
24 # collate1-2.* - Multi-field ORDER BY with an explicit COLLATE clause.
25 # collate1-3.* - ORDER BY using a default collation type. Also that an
26 # explict collate type overrides a default collate type.
27 # collate1-4.* - ORDER BY using a data type.
31 # Collation type 'HEX'. If an argument can be interpreted as a hexadecimal
32 # number, then it is converted to one before the comparison is performed.
33 # Numbers are less than other strings. If neither argument is a number,
34 # [string compare] is used.
36 db collate HEX hex_collate
37 proc hex_collate {lhs rhs} {
38 set lhs_ishex [regexp {^(0x|)[1234567890abcdefABCDEF]+$} $lhs]
39 set rhs_ishex [regexp {^(0x|)[1234567890abcdefABCDEF]+$} $rhs]
40 if {$lhs_ishex && $rhs_ishex} {
41 set lhsx [scan $lhs %x]
42 set rhsx [scan $rhs %x]
43 if {$lhs < $rhs} {return -1}
44 if {$lhs == $rhs} {return 0}
45 if {$lhs > $rhs} {return 1}
53 return [string compare $lhs $rhs]
55 db function hex {format 0x%X}
57 # Mimic the SQLite 2 collation type NUMERIC.
58 db collate numeric numeric_collate
59 proc numeric_collate {lhs rhs} {
60 if {$lhs == $rhs} {return 0}
61 return [expr ($lhs>$rhs)?1:-1]
64 do_test collate1-1.0 {
66 CREATE TABLE collate1t1(c1, c2);
67 INSERT INTO collate1t1 VALUES(45, hex(45));
68 INSERT INTO collate1t1 VALUES(NULL, NULL);
69 INSERT INTO collate1t1 VALUES(281, hex(281));
72 do_test collate1-1.1 {
74 SELECT c2 FROM collate1t1 ORDER BY 1;
77 do_test collate1-1.2 {
79 SELECT c2 FROM collate1t1 ORDER BY 1 COLLATE hex;
82 do_test collate1-1.3 {
84 SELECT c2 FROM collate1t1 ORDER BY 1 COLLATE hex DESC;
87 do_test collate1-1.4 {
89 SELECT c2 FROM collate1t1 ORDER BY 1 COLLATE hex ASC;
92 do_test collate1-1.5 {
94 SELECT c2 COLLATE hex FROM collate1t1 ORDER BY 1
97 do_test collate1-1.6 {
99 SELECT c2 COLLATE hex FROM collate1t1 ORDER BY 1 ASC
102 do_test collate1-1.7 {
104 SELECT c2 COLLATE hex FROM collate1t1 ORDER BY 1 DESC
107 do_test collate1-1.99 {
109 DROP TABLE collate1t1;
113 do_test collate1-2.0 {
115 CREATE TABLE collate1t1(c1, c2);
116 INSERT INTO collate1t1 VALUES('5', '0x11');
117 INSERT INTO collate1t1 VALUES('5', '0xA');
118 INSERT INTO collate1t1 VALUES(NULL, NULL);
119 INSERT INTO collate1t1 VALUES('7', '0xA');
120 INSERT INTO collate1t1 VALUES('11', '0x11');
121 INSERT INTO collate1t1 VALUES('11', '0x101');
124 do_test collate1-2.2 {
126 SELECT c1, c2 FROM collate1t1 ORDER BY 1 COLLATE numeric, 2 COLLATE hex;
128 } {{} {} 5 0xA 5 0x11 7 0xA 11 0x11 11 0x101}
129 do_test collate1-2.3 {
131 SELECT c1, c2 FROM collate1t1 ORDER BY 1 COLLATE binary, 2 COLLATE hex;
133 } {{} {} 11 0x11 11 0x101 5 0xA 5 0x11 7 0xA}
134 do_test collate1-2.4 {
136 SELECT c1, c2 FROM collate1t1 ORDER BY 1 COLLATE binary DESC, 2 COLLATE hex;
138 } {7 0xA 5 0xA 5 0x11 11 0x11 11 0x101 {} {}}
139 do_test collate1-2.5 {
141 SELECT c1, c2 FROM collate1t1
142 ORDER BY 1 COLLATE binary DESC, 2 COLLATE hex DESC;
144 } {7 0xA 5 0x11 5 0xA 11 0x101 11 0x11 {} {}}
145 do_test collate1-2.6 {
147 SELECT c1, c2 FROM collate1t1
148 ORDER BY 1 COLLATE binary ASC, 2 COLLATE hex ASC;
150 } {{} {} 11 0x11 11 0x101 5 0xA 5 0x11 7 0xA}
151 do_test collate1-2.12.1 {
153 SELECT c1 COLLATE numeric, c2 FROM collate1t1
154 ORDER BY 1, 2 COLLATE hex;
156 } {{} {} 5 0xA 5 0x11 7 0xA 11 0x11 11 0x101}
157 do_test collate1-2.12.2 {
159 SELECT c1 COLLATE hex, c2 FROM collate1t1
160 ORDER BY 1 COLLATE numeric, 2 COLLATE hex;
162 } {{} {} 5 0xA 5 0x11 7 0xA 11 0x11 11 0x101}
163 do_test collate1-2.12.3 {
165 SELECT c1, c2 COLLATE hex FROM collate1t1
166 ORDER BY 1 COLLATE numeric, 2;
168 } {{} {} 5 0xA 5 0x11 7 0xA 11 0x11 11 0x101}
169 do_test collate1-2.12.4 {
171 SELECT c1 COLLATE numeric, c2 COLLATE hex
175 } {{} {} 5 0xA 5 0x11 7 0xA 11 0x11 11 0x101}
176 do_test collate1-2.13 {
178 SELECT c1 COLLATE binary, c2 COLLATE hex
182 } {{} {} 11 0x11 11 0x101 5 0xA 5 0x11 7 0xA}
183 do_test collate1-2.14 {
186 FROM collate1t1 ORDER BY 1 COLLATE binary DESC, 2 COLLATE hex;
188 } {7 0xA 5 0xA 5 0x11 11 0x11 11 0x101 {} {}}
189 do_test collate1-2.15 {
191 SELECT c1 COLLATE binary, c2 COLLATE hex
193 ORDER BY 1 DESC, 2 DESC;
195 } {7 0xA 5 0x11 5 0xA 11 0x101 11 0x11 {} {}}
196 do_test collate1-2.16 {
198 SELECT c1 COLLATE hex, c2 COLLATE binary
200 ORDER BY 1 COLLATE binary ASC, 2 COLLATE hex ASC;
202 } {{} {} 11 0x11 11 0x101 5 0xA 5 0x11 7 0xA}
203 do_test collate1-2.99 {
205 DROP TABLE collate1t1;
210 # These tests ensure that the default collation type for a column is used
211 # by an ORDER BY clause correctly. The focus is all the different ways
212 # the column can be referenced. i.e. a, collate2t1.a, main.collate2t1.a etc.
214 do_test collate1-3.0 {
216 CREATE TABLE collate1t1(a COLLATE hex, b);
217 INSERT INTO collate1t1 VALUES( '0x5', 5 );
218 INSERT INTO collate1t1 VALUES( '1', 1 );
219 INSERT INTO collate1t1 VALUES( '0x45', 69 );
220 INSERT INTO collate1t1 VALUES( NULL, NULL );
221 SELECT * FROM collate1t1 ORDER BY a;
223 } {{} {} 1 1 0x5 5 0x45 69}
225 do_test collate1-3.1 {
227 SELECT * FROM collate1t1 ORDER BY 1;
229 } {{} {} 1 1 0x5 5 0x45 69}
230 do_test collate1-3.2 {
232 SELECT * FROM collate1t1 ORDER BY collate1t1.a;
234 } {{} {} 1 1 0x5 5 0x45 69}
235 do_test collate1-3.3 {
237 SELECT * FROM collate1t1 ORDER BY main.collate1t1.a;
239 } {{} {} 1 1 0x5 5 0x45 69}
240 do_test collate1-3.4 {
242 SELECT a as c1, b as c2 FROM collate1t1 ORDER BY c1;
244 } {{} {} 1 1 0x5 5 0x45 69}
245 do_test collate1-3.5 {
247 SELECT a as c1, b as c2 FROM collate1t1 ORDER BY c1 COLLATE binary;
249 } {{} {} 0x45 69 0x5 5 1 1}
250 do_test collate1-3.5.1 {
252 SELECT a COLLATE binary as c1, b as c2
253 FROM collate1t1 ORDER BY c1;
255 } {{} {} 0x45 69 0x5 5 1 1}
256 do_test collate1-3.6 {
258 DROP TABLE collate1t1;
262 # Update for SQLite version 3. The collate1-4.* test cases were written
263 # before manifest types were introduced. The following test cases still
264 # work, due to the 'affinity' mechanism, but they don't prove anything
265 # about collation sequences.
267 do_test collate1-4.0 {
269 CREATE TABLE collate1t1(c1 numeric, c2 text);
270 INSERT INTO collate1t1 VALUES(1, 1);
271 INSERT INTO collate1t1 VALUES(12, 12);
272 INSERT INTO collate1t1 VALUES(NULL, NULL);
273 INSERT INTO collate1t1 VALUES(101, 101);
276 do_test collate1-4.1 {
278 SELECT c1 FROM collate1t1 ORDER BY 1;
281 do_test collate1-4.2 {
283 SELECT c2 FROM collate1t1 ORDER BY 1;
286 do_test collate1-4.3 {
288 SELECT c2+0 FROM collate1t1 ORDER BY 1;
291 do_test collate1-4.4 {
293 SELECT c1||'' FROM collate1t1 ORDER BY 1;
296 do_test collate1-4.4.1 {
298 SELECT (c1||'') COLLATE numeric FROM collate1t1 ORDER BY 1;
301 do_test collate1-4.5 {
303 DROP TABLE collate1t1;
307 # A problem reported on the mailing list: A CREATE TABLE statement
308 # is allowed to have two or more COLLATE clauses on the same column.
309 # That probably ought to be an error, but we allow it for backwards
310 # compatibility. Just make sure it works and doesn't leak memory.
312 do_test collate1-5.1 {
315 id INTEGER PRIMARY KEY,
316 a TEXT COLLATE binary COLLATE nocase COLLATE rtrim,
317 b TEXT COLLATE nocase COLLATE binary,
318 c TEXT COLLATE rtrim COLLATE binary COLLATE rtrim COLLATE nocase
320 INSERT INTO c5 VALUES(1, 'abc','abc','abc');
321 INSERT INTO c5 VALUES(2, 'abc ','ABC','ABC');
322 SELECT id FROM c5 WHERE a='abc' ORDER BY id;
325 do_test collate1-5.2 {
327 SELECT id FROM c5 WHERE b='abc' ORDER BY id;
330 do_test collate1-5.3 {
332 SELECT id FROM c5 WHERE c='abc' ORDER BY id;
338 #-------------------------------------------------------------------------
339 # Fix problems with handling collation sequences named '"""'.
341 do_execsql_test 6.1 {
345 do_catchsql_test 6.2 {
347 SELECT a FROM x1 ORDER BY a COLLATE """""""";
348 } {1 {no such collation sequence: """}}
350 do_catchsql_test 6.3 {
351 SELECT a FROM x1 ORDER BY 1 COLLATE """""""";
352 } {1 {no such collation sequence: """}}
354 do_catchsql_test 6.4 {
355 SELECT 0 UNION SELECT 0 ORDER BY 1 COLLATE """""""";
356 } {1 {no such collation sequence: """}}
358 db collate {"""} [list string compare -nocase]
360 do_execsql_test 6.5 {
361 PRAGMA foreign_keys = ON;
362 CREATE TABLE p1(a PRIMARY KEY COLLATE '"""');
363 CREATE TABLE c1(x, y REFERENCES p1);
366 do_execsql_test 6.6 {
367 INSERT INTO p1 VALUES('abc');
368 INSERT INTO c1 VALUES(1, 'ABC');
371 ifcapable foreignkey {
372 do_catchsql_test 6.7 {
373 DELETE FROM p1 WHERE rowid = 1
374 } {1 {FOREIGN KEY constraint failed}}
377 do_execsql_test 6.8 {
378 INSERT INTO p1 VALUES('abb');
379 INSERT INTO p1 VALUES('wxz');
380 INSERT INTO p1 VALUES('wxy');
382 INSERT INTO c1 VALUES(2, 'abb');
383 INSERT INTO c1 VALUES(3, 'wxz');
384 INSERT INTO c1 VALUES(4, 'WXY');
385 SELECT x, y FROM c1 ORDER BY y COLLATE """""""";
386 } {2 abb 1 ABC 4 WXY 3 wxz}
388 # 2015-04-15: Nested COLLATE operators
390 do_execsql_test 7.0 {
391 SELECT 'abc' UNION ALL SELECT 'DEF'
392 ORDER BY 1 COLLATE nocase COLLATE nocase COLLATE nocase COLLATE nocase;
394 do_execsql_test 7.1 {
395 SELECT 'abc' UNION ALL SELECT 'DEF'
396 ORDER BY 1 COLLATE nocase COLLATE nocase COLLATE nocase COLLATE binary;
398 do_execsql_test 7.2 {
399 SELECT 'abc' UNION ALL SELECT 'DEF'
400 ORDER BY 1 COLLATE binary COLLATE binary COLLATE binary COLLATE nocase;