os/persistentdata/persistentstorage/sqlite3api/TEST/TclScript/collate4.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
#
sl@0
     2
# 2001 September 15
sl@0
     3
#
sl@0
     4
# The author disclaims copyright to this source code.  In place of
sl@0
     5
# a legal notice, here is a blessing:
sl@0
     6
#
sl@0
     7
#    May you do good and not evil.
sl@0
     8
#    May you find forgiveness for yourself and forgive others.
sl@0
     9
#    May you share freely, never taking more than you give.
sl@0
    10
#
sl@0
    11
#***********************************************************************
sl@0
    12
# This file implements regression tests for SQLite library.  The
sl@0
    13
# focus of this script is page cache subsystem.
sl@0
    14
#
sl@0
    15
# $Id: collate4.test,v 1.9 2008/01/05 17:39:30 danielk1977 Exp $
sl@0
    16
sl@0
    17
set testdir [file dirname $argv0]
sl@0
    18
source $testdir/tester.tcl
sl@0
    19
sl@0
    20
db collate TEXT text_collate
sl@0
    21
proc text_collate {a b} {
sl@0
    22
  return [string compare $a $b]
sl@0
    23
}
sl@0
    24
sl@0
    25
# Do an SQL statement.  Append the search count to the end of the result.
sl@0
    26
#
sl@0
    27
proc count sql {
sl@0
    28
  set ::sqlite_search_count 0
sl@0
    29
  return [concat [execsql $sql] $::sqlite_search_count]
sl@0
    30
}
sl@0
    31
sl@0
    32
# This procedure executes the SQL.  Then it checks the generated program
sl@0
    33
# for the SQL and appends a "nosort" to the result if the program contains the
sl@0
    34
# SortCallback opcode.  If the program does not contain the SortCallback
sl@0
    35
# opcode it appends "sort"
sl@0
    36
#
sl@0
    37
proc cksort {sql} {
sl@0
    38
  set ::sqlite_sort_count 0
sl@0
    39
  set data [execsql $sql]
sl@0
    40
  if {$::sqlite_sort_count} {set x sort} {set x nosort}
sl@0
    41
  lappend data $x
sl@0
    42
  return $data
sl@0
    43
}
sl@0
    44
sl@0
    45
# 
sl@0
    46
# Test cases are organized roughly as follows:
sl@0
    47
#
sl@0
    48
# collate4-1.*      ORDER BY.
sl@0
    49
# collate4-2.*      WHERE clauses.
sl@0
    50
# collate4-3.*      constraints (primary key, unique).
sl@0
    51
# collate4-4.*      simple min() or max() queries.
sl@0
    52
# collate4-5.*      REINDEX command
sl@0
    53
# collate4-6.*      INTEGER PRIMARY KEY indices.
sl@0
    54
#
sl@0
    55
sl@0
    56
#
sl@0
    57
# These tests - collate4-1.* - check that indices are correctly
sl@0
    58
# selected or not selected to implement ORDER BY clauses when 
sl@0
    59
# user defined collation sequences are involved. 
sl@0
    60
#
sl@0
    61
# Because these tests also exercise all the different ways indices 
sl@0
    62
# can be created, they also serve to verify that indices are correctly 
sl@0
    63
# initialised with user-defined collation sequences when they are
sl@0
    64
# created.
sl@0
    65
#
sl@0
    66
# Tests named collate4-1.1.* use indices with a single column. Tests
sl@0
    67
# collate4-1.2.* use indices with two columns.
sl@0
    68
#
sl@0
    69
do_test collate4-1.1.0 {
sl@0
    70
  execsql {
sl@0
    71
    CREATE TABLE collate4t1(a COLLATE NOCASE, b COLLATE TEXT);
sl@0
    72
    INSERT INTO collate4t1 VALUES( 'a', 'a' );
sl@0
    73
    INSERT INTO collate4t1 VALUES( 'b', 'b' );
sl@0
    74
    INSERT INTO collate4t1 VALUES( NULL, NULL );
sl@0
    75
    INSERT INTO collate4t1 VALUES( 'B', 'B' );
sl@0
    76
    INSERT INTO collate4t1 VALUES( 'A', 'A' );
sl@0
    77
    CREATE INDEX collate4i1 ON collate4t1(a);
sl@0
    78
    CREATE INDEX collate4i2 ON collate4t1(b);
sl@0
    79
  }
sl@0
    80
} {}
sl@0
    81
do_test collate4-1.1.1 {
sl@0
    82
  cksort {SELECT a FROM collate4t1 ORDER BY a}
sl@0
    83
} {{} a A b B nosort}
sl@0
    84
do_test collate4-1.1.2 {
sl@0
    85
  cksort {SELECT a FROM collate4t1 ORDER BY a COLLATE NOCASE}
sl@0
    86
} {{} a A b B nosort}
sl@0
    87
do_test collate4-1.1.3 {
sl@0
    88
  cksort {SELECT a FROM collate4t1 ORDER BY a COLLATE TEXT}
sl@0
    89
} {{} A B a b sort}
sl@0
    90
do_test collate4-1.1.4 {
sl@0
    91
  cksort {SELECT b FROM collate4t1 ORDER BY b}
sl@0
    92
} {{} A B a b nosort}
sl@0
    93
do_test collate4-1.1.5 {
sl@0
    94
  cksort {SELECT b FROM collate4t1 ORDER BY b COLLATE TEXT}
sl@0
    95
} {{} A B a b nosort}
sl@0
    96
do_test collate4-1.1.6 {
sl@0
    97
  cksort {SELECT b FROM collate4t1 ORDER BY b COLLATE NOCASE}
sl@0
    98
} {{} a A b B sort}
sl@0
    99
sl@0
   100
do_test collate4-1.1.7 {
sl@0
   101
  execsql {
sl@0
   102
    CREATE TABLE collate4t2(
sl@0
   103
      a PRIMARY KEY COLLATE NOCASE, 
sl@0
   104
      b UNIQUE COLLATE TEXT
sl@0
   105
    );
sl@0
   106
    INSERT INTO collate4t2 VALUES( 'a', 'a' );
sl@0
   107
    INSERT INTO collate4t2 VALUES( NULL, NULL );
sl@0
   108
    INSERT INTO collate4t2 VALUES( 'B', 'B' );
sl@0
   109
  }
sl@0
   110
} {}
sl@0
   111
do_test collate4-1.1.8 {
sl@0
   112
  cksort {SELECT a FROM collate4t2 ORDER BY a}
sl@0
   113
} {{} a B nosort}
sl@0
   114
do_test collate4-1.1.9 {
sl@0
   115
  cksort {SELECT a FROM collate4t2 ORDER BY a COLLATE NOCASE}
sl@0
   116
} {{} a B nosort}
sl@0
   117
do_test collate4-1.1.10 {
sl@0
   118
  cksort {SELECT a FROM collate4t2 ORDER BY a COLLATE TEXT}
sl@0
   119
} {{} B a sort}
sl@0
   120
do_test collate4-1.1.11 {
sl@0
   121
  cksort {SELECT b FROM collate4t2 ORDER BY b}
sl@0
   122
} {{} B a nosort}
sl@0
   123
do_test collate4-1.1.12 {
sl@0
   124
  cksort {SELECT b FROM collate4t2 ORDER BY b COLLATE TEXT}
sl@0
   125
} {{} B a nosort}
sl@0
   126
do_test collate4-1.1.13 {
sl@0
   127
  cksort {SELECT b FROM collate4t2 ORDER BY b COLLATE NOCASE}
sl@0
   128
} {{} a B sort}
sl@0
   129
sl@0
   130
do_test collate4-1.1.14 {
sl@0
   131
  execsql {
sl@0
   132
    CREATE TABLE collate4t3(
sl@0
   133
      b COLLATE TEXT,  
sl@0
   134
      a COLLATE NOCASE, 
sl@0
   135
      UNIQUE(a), PRIMARY KEY(b)
sl@0
   136
    );
sl@0
   137
    INSERT INTO collate4t3 VALUES( 'a', 'a' );
sl@0
   138
    INSERT INTO collate4t3 VALUES( NULL, NULL );
sl@0
   139
    INSERT INTO collate4t3 VALUES( 'B', 'B' );
sl@0
   140
  }
sl@0
   141
} {}
sl@0
   142
do_test collate4-1.1.15 {
sl@0
   143
  cksort {SELECT a FROM collate4t3 ORDER BY a}
sl@0
   144
} {{} a B nosort}
sl@0
   145
do_test collate4-1.1.16 {
sl@0
   146
  cksort {SELECT a FROM collate4t3 ORDER BY a COLLATE NOCASE}
sl@0
   147
} {{} a B nosort}
sl@0
   148
do_test collate4-1.1.17 {
sl@0
   149
  cksort {SELECT a FROM collate4t3 ORDER BY a COLLATE TEXT}
sl@0
   150
} {{} B a sort}
sl@0
   151
do_test collate4-1.1.18 {
sl@0
   152
  cksort {SELECT b FROM collate4t3 ORDER BY b}
sl@0
   153
} {{} B a nosort}
sl@0
   154
do_test collate4-1.1.19 {
sl@0
   155
  cksort {SELECT b FROM collate4t3 ORDER BY b COLLATE TEXT}
sl@0
   156
} {{} B a nosort}
sl@0
   157
do_test collate4-1.1.20 {
sl@0
   158
  cksort {SELECT b FROM collate4t3 ORDER BY b COLLATE NOCASE}
sl@0
   159
} {{} a B sort}
sl@0
   160
sl@0
   161
do_test collate4-1.1.21 {
sl@0
   162
  execsql {
sl@0
   163
    CREATE TABLE collate4t4(a COLLATE NOCASE, b COLLATE TEXT);
sl@0
   164
    INSERT INTO collate4t4 VALUES( 'a', 'a' );
sl@0
   165
    INSERT INTO collate4t4 VALUES( 'b', 'b' );
sl@0
   166
    INSERT INTO collate4t4 VALUES( NULL, NULL );
sl@0
   167
    INSERT INTO collate4t4 VALUES( 'B', 'B' );
sl@0
   168
    INSERT INTO collate4t4 VALUES( 'A', 'A' );
sl@0
   169
    CREATE INDEX collate4i3 ON collate4t4(a COLLATE TEXT);
sl@0
   170
    CREATE INDEX collate4i4 ON collate4t4(b COLLATE NOCASE);
sl@0
   171
  }
sl@0
   172
} {}
sl@0
   173
do_test collate4-1.1.22 {
sl@0
   174
  cksort {SELECT a FROM collate4t4 ORDER BY a}
sl@0
   175
} {{} a A b B sort}
sl@0
   176
do_test collate4-1.1.23 {
sl@0
   177
  cksort {SELECT a FROM collate4t4 ORDER BY a COLLATE NOCASE}
sl@0
   178
} {{} a A b B sort}
sl@0
   179
do_test collate4-1.1.24 {
sl@0
   180
  cksort {SELECT a FROM collate4t4 ORDER BY a COLLATE TEXT}
sl@0
   181
} {{} A B a b nosort}
sl@0
   182
do_test collate4-1.1.25 {
sl@0
   183
  cksort {SELECT b FROM collate4t4 ORDER BY b}
sl@0
   184
} {{} A B a b sort}
sl@0
   185
do_test collate4-1.1.26 {
sl@0
   186
  cksort {SELECT b FROM collate4t4 ORDER BY b COLLATE TEXT}
sl@0
   187
} {{} A B a b sort}
sl@0
   188
do_test collate4-1.1.27 {
sl@0
   189
  cksort {SELECT b FROM collate4t4 ORDER BY b COLLATE NOCASE}
sl@0
   190
} {{} a A b B nosort}
sl@0
   191
sl@0
   192
do_test collate4-1.1.30 {
sl@0
   193
  execsql {
sl@0
   194
    DROP TABLE collate4t1;
sl@0
   195
    DROP TABLE collate4t2;
sl@0
   196
    DROP TABLE collate4t3;
sl@0
   197
    DROP TABLE collate4t4;
sl@0
   198
  }
sl@0
   199
} {}
sl@0
   200
sl@0
   201
do_test collate4-1.2.0 {
sl@0
   202
  execsql {
sl@0
   203
    CREATE TABLE collate4t1(a COLLATE NOCASE, b COLLATE TEXT);
sl@0
   204
    INSERT INTO collate4t1 VALUES( 'a', 'a' );
sl@0
   205
    INSERT INTO collate4t1 VALUES( 'b', 'b' );
sl@0
   206
    INSERT INTO collate4t1 VALUES( NULL, NULL );
sl@0
   207
    INSERT INTO collate4t1 VALUES( 'B', 'B' );
sl@0
   208
    INSERT INTO collate4t1 VALUES( 'A', 'A' );
sl@0
   209
    CREATE INDEX collate4i1 ON collate4t1(a, b);
sl@0
   210
  }
sl@0
   211
} {}
sl@0
   212
do_test collate4-1.2.1 {
sl@0
   213
  cksort {SELECT a FROM collate4t1 ORDER BY a}
sl@0
   214
} {{} A a B b nosort}
sl@0
   215
do_test collate4-1.2.2 {
sl@0
   216
  cksort {SELECT a FROM collate4t1 ORDER BY a COLLATE nocase}
sl@0
   217
} {{} A a B b nosort}
sl@0
   218
do_test collate4-1.2.3 {
sl@0
   219
  cksort {SELECT a FROM collate4t1 ORDER BY a COLLATE text}
sl@0
   220
} {{} A B a b sort}
sl@0
   221
do_test collate4-1.2.4 {
sl@0
   222
  cksort {SELECT a FROM collate4t1 ORDER BY a, b}
sl@0
   223
} {{} A a B b nosort}
sl@0
   224
do_test collate4-1.2.5 {
sl@0
   225
  cksort {SELECT a FROM collate4t1 ORDER BY a, b COLLATE nocase}
sl@0
   226
} {{} a A b B sort}
sl@0
   227
do_test collate4-1.2.6 {
sl@0
   228
  cksort {SELECT a FROM collate4t1 ORDER BY a, b COLLATE text}
sl@0
   229
} {{} A a B b nosort}
sl@0
   230
sl@0
   231
do_test collate4-1.2.7 {
sl@0
   232
  execsql {
sl@0
   233
    CREATE TABLE collate4t2(
sl@0
   234
      a COLLATE NOCASE, 
sl@0
   235
      b COLLATE TEXT, 
sl@0
   236
      PRIMARY KEY(a, b)
sl@0
   237
    );
sl@0
   238
    INSERT INTO collate4t2 VALUES( 'a', 'a' );
sl@0
   239
    INSERT INTO collate4t2 VALUES( NULL, NULL );
sl@0
   240
    INSERT INTO collate4t2 VALUES( 'B', 'B' );
sl@0
   241
  }
sl@0
   242
} {}
sl@0
   243
do_test collate4-1.2.8 {
sl@0
   244
  cksort {SELECT a FROM collate4t2 ORDER BY a}
sl@0
   245
} {{} a B nosort}
sl@0
   246
do_test collate4-1.2.9 {
sl@0
   247
  cksort {SELECT a FROM collate4t2 ORDER BY a COLLATE nocase}
sl@0
   248
} {{} a B nosort}
sl@0
   249
do_test collate4-1.2.10 {
sl@0
   250
  cksort {SELECT a FROM collate4t2 ORDER BY a COLLATE text}
sl@0
   251
} {{} B a sort}
sl@0
   252
do_test collate4-1.2.11 {
sl@0
   253
  cksort {SELECT a FROM collate4t2 ORDER BY a, b}
sl@0
   254
} {{} a B nosort}
sl@0
   255
do_test collate4-1.2.12 {
sl@0
   256
  cksort {SELECT a FROM collate4t2 ORDER BY a, b COLLATE nocase}
sl@0
   257
} {{} a B sort}
sl@0
   258
do_test collate4-1.2.13 {
sl@0
   259
  cksort {SELECT a FROM collate4t2 ORDER BY a, b COLLATE text}
sl@0
   260
} {{} a B nosort}
sl@0
   261
sl@0
   262
do_test collate4-1.2.14 {
sl@0
   263
  execsql {
sl@0
   264
    CREATE TABLE collate4t3(a COLLATE NOCASE, b COLLATE TEXT);
sl@0
   265
    INSERT INTO collate4t3 VALUES( 'a', 'a' );
sl@0
   266
    INSERT INTO collate4t3 VALUES( 'b', 'b' );
sl@0
   267
    INSERT INTO collate4t3 VALUES( NULL, NULL );
sl@0
   268
    INSERT INTO collate4t3 VALUES( 'B', 'B' );
sl@0
   269
    INSERT INTO collate4t3 VALUES( 'A', 'A' );
sl@0
   270
    CREATE INDEX collate4i2 ON collate4t3(a COLLATE TEXT, b COLLATE NOCASE);
sl@0
   271
  }
sl@0
   272
} {}
sl@0
   273
do_test collate4-1.2.15 {
sl@0
   274
  cksort {SELECT a FROM collate4t3 ORDER BY a}
sl@0
   275
} {{} a A b B sort}
sl@0
   276
do_test collate4-1.2.16 {
sl@0
   277
  cksort {SELECT a FROM collate4t3 ORDER BY a COLLATE nocase}
sl@0
   278
} {{} a A b B sort}
sl@0
   279
do_test collate4-1.2.17 {
sl@0
   280
  cksort {SELECT a FROM collate4t3 ORDER BY a COLLATE text}
sl@0
   281
} {{} A B a b nosort}
sl@0
   282
do_test collate4-1.2.18 {
sl@0
   283
  cksort {SELECT a FROM collate4t3 ORDER BY a COLLATE text, b}
sl@0
   284
} {{} A B a b sort}
sl@0
   285
do_test collate4-1.2.19 {
sl@0
   286
  cksort {SELECT a FROM collate4t3 ORDER BY a COLLATE text, b COLLATE nocase}
sl@0
   287
} {{} A B a b nosort}
sl@0
   288
do_test collate4-1.2.20 {
sl@0
   289
  cksort {SELECT a FROM collate4t3 ORDER BY a COLLATE text, b COLLATE text}
sl@0
   290
} {{} A B a b sort}
sl@0
   291
do_test collate4-1.2.21 {
sl@0
   292
  cksort {SELECT a FROM collate4t3 ORDER BY a COLLATE text DESC}
sl@0
   293
} {b a B A {} nosort}
sl@0
   294
do_test collate4-1.2.22 {
sl@0
   295
  cksort {SELECT a FROM collate4t3 ORDER BY a COLLATE text DESC, b}
sl@0
   296
} {b a B A {} sort}
sl@0
   297
do_test collate4-1.2.23 {
sl@0
   298
  cksort {SELECT a FROM collate4t3 
sl@0
   299
            ORDER BY a COLLATE text DESC, b COLLATE nocase}
sl@0
   300
} {b a B A {} sort}
sl@0
   301
do_test collate4-1.2.24 {
sl@0
   302
  cksort {SELECT a FROM collate4t3 
sl@0
   303
            ORDER BY a COLLATE text DESC, b COLLATE nocase DESC}
sl@0
   304
} {b a B A {} nosort}
sl@0
   305
sl@0
   306
do_test collate4-1.2.25 {
sl@0
   307
  execsql {
sl@0
   308
    DROP TABLE collate4t1;
sl@0
   309
    DROP TABLE collate4t2;
sl@0
   310
    DROP TABLE collate4t3;
sl@0
   311
  }
sl@0
   312
} {}
sl@0
   313
sl@0
   314
#
sl@0
   315
# These tests - collate4-2.* - check that indices are correctly
sl@0
   316
# selected or not selected to implement WHERE clauses when user 
sl@0
   317
# defined collation sequences are involved. 
sl@0
   318
#
sl@0
   319
# Indices may optimise WHERE clauses using <, >, <=, >=, = or IN
sl@0
   320
# operators.
sl@0
   321
#
sl@0
   322
do_test collate4-2.1.0 {
sl@0
   323
  execsql {
sl@0
   324
    CREATE TABLE collate4t1(a COLLATE NOCASE);
sl@0
   325
    CREATE TABLE collate4t2(b COLLATE TEXT);
sl@0
   326
sl@0
   327
    INSERT INTO collate4t1 VALUES('a');
sl@0
   328
    INSERT INTO collate4t1 VALUES('A');
sl@0
   329
    INSERT INTO collate4t1 VALUES('b');
sl@0
   330
    INSERT INTO collate4t1 VALUES('B');
sl@0
   331
    INSERT INTO collate4t1 VALUES('c');
sl@0
   332
    INSERT INTO collate4t1 VALUES('C');
sl@0
   333
    INSERT INTO collate4t1 VALUES('d');
sl@0
   334
    INSERT INTO collate4t1 VALUES('D');
sl@0
   335
    INSERT INTO collate4t1 VALUES('e');
sl@0
   336
    INSERT INTO collate4t1 VALUES('D');
sl@0
   337
sl@0
   338
    INSERT INTO collate4t2 VALUES('A');
sl@0
   339
    INSERT INTO collate4t2 VALUES('Z');
sl@0
   340
  }
sl@0
   341
} {}
sl@0
   342
do_test collate4-2.1.1 {
sl@0
   343
  count {
sl@0
   344
    SELECT * FROM collate4t2, collate4t1 WHERE a = b;
sl@0
   345
  }
sl@0
   346
} {A a A A 19}
sl@0
   347
do_test collate4-2.1.2 {
sl@0
   348
  execsql {
sl@0
   349
    CREATE INDEX collate4i1 ON collate4t1(a);
sl@0
   350
  }
sl@0
   351
  count {
sl@0
   352
    SELECT * FROM collate4t2, collate4t1 WHERE a = b;
sl@0
   353
  }
sl@0
   354
} {A a A A 5}
sl@0
   355
do_test collate4-2.1.3 {
sl@0
   356
  count {
sl@0
   357
    SELECT * FROM collate4t2, collate4t1 WHERE b = a;
sl@0
   358
  }
sl@0
   359
} {A A 19}
sl@0
   360
do_test collate4-2.1.4 {
sl@0
   361
  execsql {
sl@0
   362
    DROP INDEX collate4i1;
sl@0
   363
    CREATE INDEX collate4i1 ON collate4t1(a COLLATE TEXT);
sl@0
   364
  }
sl@0
   365
  count {
sl@0
   366
    SELECT * FROM collate4t2, collate4t1 WHERE a = b;
sl@0
   367
  }
sl@0
   368
} {A a A A 19}
sl@0
   369
do_test collate4-2.1.5 {
sl@0
   370
  count {
sl@0
   371
    SELECT * FROM collate4t2, collate4t1 WHERE b = a;
sl@0
   372
  }
sl@0
   373
} {A A 4}
sl@0
   374
ifcapable subquery {
sl@0
   375
  do_test collate4-2.1.6 {
sl@0
   376
    count {
sl@0
   377
      SELECT a FROM collate4t1 WHERE a IN (SELECT * FROM collate4t2);
sl@0
   378
    }
sl@0
   379
  } {a A 10}
sl@0
   380
  do_test collate4-2.1.7 {
sl@0
   381
    execsql {
sl@0
   382
      DROP INDEX collate4i1;
sl@0
   383
      CREATE INDEX collate4i1 ON collate4t1(a);
sl@0
   384
    }
sl@0
   385
    count {
sl@0
   386
      SELECT a FROM collate4t1 WHERE a IN (SELECT * FROM collate4t2);
sl@0
   387
    }
sl@0
   388
  } {a A 6}
sl@0
   389
  do_test collate4-2.1.8 {
sl@0
   390
    count {
sl@0
   391
      SELECT a FROM collate4t1 WHERE a IN ('z', 'a');
sl@0
   392
    }
sl@0
   393
  } {a A 5}
sl@0
   394
  do_test collate4-2.1.9 {
sl@0
   395
    execsql {
sl@0
   396
      DROP INDEX collate4i1;
sl@0
   397
      CREATE INDEX collate4i1 ON collate4t1(a COLLATE TEXT);
sl@0
   398
    }
sl@0
   399
    count {
sl@0
   400
      SELECT a FROM collate4t1 WHERE a IN ('z', 'a');
sl@0
   401
    }
sl@0
   402
  } {a A 9}
sl@0
   403
}
sl@0
   404
do_test collate4-2.1.10 {
sl@0
   405
  execsql {
sl@0
   406
    DROP TABLE collate4t1;
sl@0
   407
    DROP TABLE collate4t2;
sl@0
   408
  }
sl@0
   409
} {}
sl@0
   410
sl@0
   411
do_test collate4-2.2.0 {
sl@0
   412
  execsql {
sl@0
   413
    CREATE TABLE collate4t1(a COLLATE nocase, b COLLATE text, c);
sl@0
   414
    CREATE TABLE collate4t2(a COLLATE nocase, b COLLATE text, c COLLATE TEXT);
sl@0
   415
sl@0
   416
    INSERT INTO collate4t1 VALUES('0', '0', '0');
sl@0
   417
    INSERT INTO collate4t1 VALUES('0', '0', '1');
sl@0
   418
    INSERT INTO collate4t1 VALUES('0', '1', '0');
sl@0
   419
    INSERT INTO collate4t1 VALUES('0', '1', '1');
sl@0
   420
    INSERT INTO collate4t1 VALUES('1', '0', '0');
sl@0
   421
    INSERT INTO collate4t1 VALUES('1', '0', '1');
sl@0
   422
    INSERT INTO collate4t1 VALUES('1', '1', '0');
sl@0
   423
    INSERT INTO collate4t1 VALUES('1', '1', '1');
sl@0
   424
    insert into collate4t2 SELECT * FROM collate4t1;
sl@0
   425
  }
sl@0
   426
} {}
sl@0
   427
do_test collate4-2.2.1 {
sl@0
   428
  count {
sl@0
   429
    SELECT * FROM collate4t2 NATURAL JOIN collate4t1;
sl@0
   430
  }
sl@0
   431
} {0 0 0 0 0 1 0 1 0 0 1 1 1 0 0 1 0 1 1 1 0 1 1 1 63}
sl@0
   432
do_test collate4-2.2.1b {
sl@0
   433
  execsql {
sl@0
   434
    CREATE INDEX collate4i1 ON collate4t1(a, b, c);
sl@0
   435
  }
sl@0
   436
  count {
sl@0
   437
    SELECT * FROM collate4t2 NATURAL JOIN collate4t1;
sl@0
   438
  }
sl@0
   439
} {0 0 0 0 0 1 0 1 0 0 1 1 1 0 0 1 0 1 1 1 0 1 1 1 29}
sl@0
   440
do_test collate4-2.2.2 {
sl@0
   441
  execsql {
sl@0
   442
    DROP INDEX collate4i1;
sl@0
   443
    CREATE INDEX collate4i1 ON collate4t1(a, b, c COLLATE text);
sl@0
   444
  }
sl@0
   445
  count {
sl@0
   446
    SELECT * FROM collate4t2 NATURAL JOIN collate4t1;
sl@0
   447
  }
sl@0
   448
} {0 0 0 0 0 1 0 1 0 0 1 1 1 0 0 1 0 1 1 1 0 1 1 1 22}
sl@0
   449
sl@0
   450
do_test collate4-2.2.10 {
sl@0
   451
  execsql {
sl@0
   452
    DROP TABLE collate4t1;
sl@0
   453
    DROP TABLE collate4t2;
sl@0
   454
  }
sl@0
   455
} {}
sl@0
   456
sl@0
   457
#
sl@0
   458
# These tests - collate4-3.* verify that indices that implement
sl@0
   459
# UNIQUE and PRIMARY KEY constraints operate correctly with user
sl@0
   460
# defined collation sequences.
sl@0
   461
#
sl@0
   462
do_test collate4-3.0 {
sl@0
   463
  execsql {
sl@0
   464
    CREATE TABLE collate4t1(a PRIMARY KEY COLLATE NOCASE);
sl@0
   465
  }
sl@0
   466
} {}
sl@0
   467
do_test collate4-3.1 {
sl@0
   468
  catchsql {
sl@0
   469
    INSERT INTO collate4t1 VALUES('abc');
sl@0
   470
    INSERT INTO collate4t1 VALUES('ABC');
sl@0
   471
  }
sl@0
   472
} {1 {column a is not unique}}
sl@0
   473
do_test collate4-3.2 {
sl@0
   474
  execsql {
sl@0
   475
    SELECT * FROM collate4t1;
sl@0
   476
  }
sl@0
   477
} {abc}
sl@0
   478
do_test collate4-3.3 {
sl@0
   479
  catchsql {
sl@0
   480
    INSERT INTO collate4t1 SELECT upper(a) FROM collate4t1;
sl@0
   481
  }
sl@0
   482
} {1 {column a is not unique}}
sl@0
   483
do_test collate4-3.4 {
sl@0
   484
  catchsql {
sl@0
   485
    INSERT INTO collate4t1 VALUES(1);
sl@0
   486
    UPDATE collate4t1 SET a = 'abc';
sl@0
   487
  }
sl@0
   488
} {1 {column a is not unique}}
sl@0
   489
do_test collate4-3.5 {
sl@0
   490
  execsql {
sl@0
   491
    DROP TABLE collate4t1;
sl@0
   492
    CREATE TABLE collate4t1(a COLLATE NOCASE UNIQUE);
sl@0
   493
  }
sl@0
   494
} {}
sl@0
   495
do_test collate4-3.6 {
sl@0
   496
  catchsql {
sl@0
   497
    INSERT INTO collate4t1 VALUES('abc');
sl@0
   498
    INSERT INTO collate4t1 VALUES('ABC');
sl@0
   499
  }
sl@0
   500
} {1 {column a is not unique}}
sl@0
   501
do_test collate4-3.7 {
sl@0
   502
  execsql {
sl@0
   503
    SELECT * FROM collate4t1;
sl@0
   504
  }
sl@0
   505
} {abc}
sl@0
   506
do_test collate4-3.8 {
sl@0
   507
  catchsql {
sl@0
   508
    INSERT INTO collate4t1 SELECT upper(a) FROM collate4t1;
sl@0
   509
  }
sl@0
   510
} {1 {column a is not unique}}
sl@0
   511
do_test collate4-3.9 {
sl@0
   512
  catchsql {
sl@0
   513
    INSERT INTO collate4t1 VALUES(1);
sl@0
   514
    UPDATE collate4t1 SET a = 'abc';
sl@0
   515
  }
sl@0
   516
} {1 {column a is not unique}}
sl@0
   517
do_test collate4-3.10 {
sl@0
   518
  execsql {
sl@0
   519
    DROP TABLE collate4t1;
sl@0
   520
    CREATE TABLE collate4t1(a);
sl@0
   521
    CREATE UNIQUE INDEX collate4i1 ON collate4t1(a COLLATE NOCASE);
sl@0
   522
  }
sl@0
   523
} {}
sl@0
   524
do_test collate4-3.11 {
sl@0
   525
  catchsql {
sl@0
   526
    INSERT INTO collate4t1 VALUES('abc');
sl@0
   527
    INSERT INTO collate4t1 VALUES('ABC');
sl@0
   528
  }
sl@0
   529
} {1 {column a is not unique}}
sl@0
   530
do_test collate4-3.12 {
sl@0
   531
  execsql {
sl@0
   532
    SELECT * FROM collate4t1;
sl@0
   533
  }
sl@0
   534
} {abc}
sl@0
   535
do_test collate4-3.13 {
sl@0
   536
  catchsql {
sl@0
   537
    INSERT INTO collate4t1 SELECT upper(a) FROM collate4t1;
sl@0
   538
  }
sl@0
   539
} {1 {column a is not unique}}
sl@0
   540
do_test collate4-3.14 {
sl@0
   541
  catchsql {
sl@0
   542
    INSERT INTO collate4t1 VALUES(1);
sl@0
   543
    UPDATE collate4t1 SET a = 'abc';
sl@0
   544
  }
sl@0
   545
} {1 {column a is not unique}}
sl@0
   546
sl@0
   547
do_test collate4-3.15 {
sl@0
   548
  execsql {
sl@0
   549
    DROP TABLE collate4t1;
sl@0
   550
  }
sl@0
   551
} {}
sl@0
   552
sl@0
   553
# Mimic the SQLite 2 collation type NUMERIC.
sl@0
   554
db collate numeric numeric_collate
sl@0
   555
proc numeric_collate {lhs rhs} {
sl@0
   556
  if {$lhs == $rhs} {return 0} 
sl@0
   557
  return [expr ($lhs>$rhs)?1:-1]
sl@0
   558
}
sl@0
   559
sl@0
   560
#
sl@0
   561
# These tests - collate4-4.* check that min() and max() only ever 
sl@0
   562
# use indices constructed with built-in collation type numeric.
sl@0
   563
#
sl@0
   564
# CHANGED:  min() and max() now use the collation type. If there
sl@0
   565
# is an indice that can be used, it is used.
sl@0
   566
#
sl@0
   567
do_test collate4-4.0 {
sl@0
   568
  execsql {
sl@0
   569
    CREATE TABLE collate4t1(a COLLATE TEXT);
sl@0
   570
    INSERT INTO collate4t1 VALUES('2');
sl@0
   571
    INSERT INTO collate4t1 VALUES('10');
sl@0
   572
    INSERT INTO collate4t1 VALUES('20');
sl@0
   573
    INSERT INTO collate4t1 VALUES('104');
sl@0
   574
  }
sl@0
   575
} {}
sl@0
   576
do_test collate4-4.1 {
sl@0
   577
  count {
sl@0
   578
    SELECT max(a) FROM collate4t1
sl@0
   579
  }
sl@0
   580
} {20 3}
sl@0
   581
do_test collate4-4.2 {
sl@0
   582
  count {
sl@0
   583
    SELECT min(a) FROM collate4t1
sl@0
   584
  }
sl@0
   585
} {10 3}
sl@0
   586
do_test collate4-4.3 {
sl@0
   587
  # Test that the index with collation type TEXT is used.
sl@0
   588
  execsql {
sl@0
   589
    CREATE INDEX collate4i1 ON collate4t1(a);
sl@0
   590
  }
sl@0
   591
  count {
sl@0
   592
    SELECT min(a) FROM collate4t1;
sl@0
   593
  }
sl@0
   594
} {10 1}
sl@0
   595
do_test collate4-4.4 {
sl@0
   596
  count {
sl@0
   597
    SELECT max(a) FROM collate4t1;
sl@0
   598
  }
sl@0
   599
} {20 0}
sl@0
   600
do_test collate4-4.5 {
sl@0
   601
  # Test that the index with collation type NUMERIC is not used.
sl@0
   602
  execsql {
sl@0
   603
    DROP INDEX collate4i1;
sl@0
   604
    CREATE INDEX collate4i1 ON collate4t1(a COLLATE NUMERIC);
sl@0
   605
  }
sl@0
   606
  count {
sl@0
   607
    SELECT min(a) FROM collate4t1;
sl@0
   608
  }
sl@0
   609
} {10 3}
sl@0
   610
do_test collate4-4.6 {
sl@0
   611
  count {
sl@0
   612
    SELECT max(a) FROM collate4t1;
sl@0
   613
  }
sl@0
   614
} {20 3}
sl@0
   615
do_test collate4-4.7 {
sl@0
   616
  execsql {
sl@0
   617
    DROP TABLE collate4t1;
sl@0
   618
  }
sl@0
   619
} {}
sl@0
   620
sl@0
   621
# Also test the scalar min() and max() functions.
sl@0
   622
#
sl@0
   623
do_test collate4-4.8 {
sl@0
   624
  execsql {
sl@0
   625
    CREATE TABLE collate4t1(a COLLATE TEXT, b COLLATE NUMERIC);
sl@0
   626
    INSERT INTO collate4t1 VALUES('11', '101');
sl@0
   627
    INSERT INTO collate4t1 VALUES('101', '11')
sl@0
   628
  }
sl@0
   629
} {}
sl@0
   630
do_test collate4-4.9 {
sl@0
   631
  execsql {
sl@0
   632
    SELECT max(a, b) FROM collate4t1;
sl@0
   633
  }
sl@0
   634
} {11 11}
sl@0
   635
do_test collate4-4.10 {
sl@0
   636
  execsql {
sl@0
   637
    SELECT max(b, a) FROM collate4t1;
sl@0
   638
  }
sl@0
   639
} {101 101}
sl@0
   640
do_test collate4-4.11 {
sl@0
   641
  execsql {
sl@0
   642
    SELECT max(a, '101') FROM collate4t1;
sl@0
   643
  }
sl@0
   644
} {11 101}
sl@0
   645
do_test collate4-4.12 {
sl@0
   646
  execsql {
sl@0
   647
    SELECT max('101', a) FROM collate4t1;
sl@0
   648
  }
sl@0
   649
} {11 101}
sl@0
   650
do_test collate4-4.13 {
sl@0
   651
  execsql {
sl@0
   652
    SELECT max(b, '101') FROM collate4t1;
sl@0
   653
  }
sl@0
   654
} {101 101}
sl@0
   655
do_test collate4-4.14 {
sl@0
   656
  execsql {
sl@0
   657
    SELECT max('101', b) FROM collate4t1;
sl@0
   658
  }
sl@0
   659
} {101 101}
sl@0
   660
sl@0
   661
do_test collate4-4.15 {
sl@0
   662
  execsql {
sl@0
   663
    DROP TABLE collate4t1;
sl@0
   664
  }
sl@0
   665
} {}
sl@0
   666
sl@0
   667
#
sl@0
   668
# These tests - collate4.6.* - ensure that implict INTEGER PRIMARY KEY 
sl@0
   669
# indices do not confuse collation sequences. 
sl@0
   670
#
sl@0
   671
# These indices are never used for sorting in SQLite. And you can't
sl@0
   672
# create another index on an INTEGER PRIMARY KEY column, so we don't have 
sl@0
   673
# to test that.
sl@0
   674
# (Revised 2004-Nov-22):  The ROWID can be used for sorting now.
sl@0
   675
#
sl@0
   676
do_test collate4-6.0 {
sl@0
   677
  execsql {
sl@0
   678
    CREATE TABLE collate4t1(a INTEGER PRIMARY KEY);
sl@0
   679
    INSERT INTO collate4t1 VALUES(101);
sl@0
   680
    INSERT INTO collate4t1 VALUES(10);
sl@0
   681
    INSERT INTO collate4t1 VALUES(15);
sl@0
   682
  }
sl@0
   683
} {}
sl@0
   684
do_test collate4-6.1 {
sl@0
   685
  cksort {
sl@0
   686
    SELECT * FROM collate4t1 ORDER BY 1;
sl@0
   687
  }
sl@0
   688
} {10 15 101 nosort}
sl@0
   689
do_test collate4-6.2 {
sl@0
   690
  cksort {
sl@0
   691
    SELECT * FROM collate4t1 ORDER BY oid;
sl@0
   692
  }
sl@0
   693
} {10 15 101 nosort}
sl@0
   694
do_test collate4-6.3 {
sl@0
   695
  cksort {
sl@0
   696
    SELECT * FROM collate4t1 ORDER BY oid||'' COLLATE TEXT;
sl@0
   697
  }
sl@0
   698
} {10 101 15 sort}
sl@0
   699
sl@0
   700
finish_test