os/persistentdata/persistentstorage/sqlite3api/TEST/TclScript/like.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
# 2005 August 13
sl@0
     2
#
sl@0
     3
# The author disclaims copyright to this source code.  In place of
sl@0
     4
# a legal notice, here is a blessing:
sl@0
     5
#
sl@0
     6
#    May you do good and not evil.
sl@0
     7
#    May you find forgiveness for yourself and forgive others.
sl@0
     8
#    May you share freely, never taking more than you give.
sl@0
     9
#
sl@0
    10
#***********************************************************************
sl@0
    11
# This file implements regression tests for SQLite library.  The
sl@0
    12
# focus of this file is testing the LIKE and GLOB operators and
sl@0
    13
# in particular the optimizations that occur to help those operators
sl@0
    14
# run faster.
sl@0
    15
#
sl@0
    16
# $Id: like.test,v 1.10 2008/09/09 12:31:34 drh Exp $
sl@0
    17
sl@0
    18
set testdir [file dirname $argv0]
sl@0
    19
source $testdir/tester.tcl
sl@0
    20
sl@0
    21
# Create some sample data to work with.
sl@0
    22
#
sl@0
    23
do_test like-1.0 {
sl@0
    24
  execsql {
sl@0
    25
    CREATE TABLE t1(x TEXT);
sl@0
    26
  }
sl@0
    27
  foreach str {
sl@0
    28
    a
sl@0
    29
    ab
sl@0
    30
    abc
sl@0
    31
    abcd
sl@0
    32
sl@0
    33
    acd
sl@0
    34
    abd
sl@0
    35
    bc
sl@0
    36
    bcd
sl@0
    37
sl@0
    38
    xyz
sl@0
    39
    ABC
sl@0
    40
    CDE
sl@0
    41
    {ABC abc xyz}
sl@0
    42
  } {
sl@0
    43
    db eval {INSERT INTO t1 VALUES(:str)}
sl@0
    44
  }
sl@0
    45
  execsql {
sl@0
    46
    SELECT count(*) FROM t1;
sl@0
    47
  }
sl@0
    48
} {12}
sl@0
    49
sl@0
    50
# Test that both case sensitive and insensitive version of LIKE work.
sl@0
    51
#
sl@0
    52
do_test like-1.1 {
sl@0
    53
  execsql {
sl@0
    54
    SELECT x FROM t1 WHERE x LIKE 'abc' ORDER BY 1;
sl@0
    55
  }
sl@0
    56
} {ABC abc}
sl@0
    57
do_test like-1.2 {
sl@0
    58
  execsql {
sl@0
    59
    SELECT x FROM t1 WHERE x GLOB 'abc' ORDER BY 1;
sl@0
    60
  }
sl@0
    61
} {abc}
sl@0
    62
do_test like-1.3 {
sl@0
    63
  execsql {
sl@0
    64
    SELECT x FROM t1 WHERE x LIKE 'ABC' ORDER BY 1;
sl@0
    65
  }
sl@0
    66
} {ABC abc}
sl@0
    67
do_test like-1.4 {
sl@0
    68
  execsql {
sl@0
    69
    SELECT x FROM t1 WHERE x LIKE 'aBc' ORDER BY 1;
sl@0
    70
  }
sl@0
    71
} {ABC abc}
sl@0
    72
do_test like-1.5 {
sl@0
    73
  execsql {
sl@0
    74
    PRAGMA case_sensitive_like=on;
sl@0
    75
    SELECT x FROM t1 WHERE x LIKE 'abc' ORDER BY 1;
sl@0
    76
  }
sl@0
    77
} {abc}
sl@0
    78
do_test like-1.6 {
sl@0
    79
  execsql {
sl@0
    80
    SELECT x FROM t1 WHERE x GLOB 'abc' ORDER BY 1;
sl@0
    81
  }
sl@0
    82
} {abc}
sl@0
    83
do_test like-1.7 {
sl@0
    84
  execsql {
sl@0
    85
    SELECT x FROM t1 WHERE x LIKE 'ABC' ORDER BY 1;
sl@0
    86
  }
sl@0
    87
} {ABC}
sl@0
    88
do_test like-1.8 {
sl@0
    89
  execsql {
sl@0
    90
    SELECT x FROM t1 WHERE x LIKE 'aBc' ORDER BY 1;
sl@0
    91
  }
sl@0
    92
} {}
sl@0
    93
do_test like-1.9 {
sl@0
    94
  execsql {
sl@0
    95
    PRAGMA case_sensitive_like=off;
sl@0
    96
    SELECT x FROM t1 WHERE x LIKE 'abc' ORDER BY 1;
sl@0
    97
  }
sl@0
    98
} {ABC abc}
sl@0
    99
sl@0
   100
# Tests of the REGEXP operator
sl@0
   101
#
sl@0
   102
do_test like-2.1 {
sl@0
   103
  proc test_regexp {a b} {
sl@0
   104
    return [regexp $a $b]
sl@0
   105
  }
sl@0
   106
  db function regexp test_regexp
sl@0
   107
  execsql {
sl@0
   108
    SELECT x FROM t1 WHERE x REGEXP 'abc' ORDER BY 1;
sl@0
   109
  }
sl@0
   110
} {{ABC abc xyz} abc abcd}
sl@0
   111
do_test like-2.2 {
sl@0
   112
  execsql {
sl@0
   113
    SELECT x FROM t1 WHERE x REGEXP '^abc' ORDER BY 1;
sl@0
   114
  }
sl@0
   115
} {abc abcd}
sl@0
   116
sl@0
   117
# Tests of the MATCH operator
sl@0
   118
#
sl@0
   119
do_test like-2.3 {
sl@0
   120
  proc test_match {a b} {
sl@0
   121
    return [string match $a $b]
sl@0
   122
  }
sl@0
   123
  db function match -argcount 2 test_match
sl@0
   124
  execsql {
sl@0
   125
    SELECT x FROM t1 WHERE x MATCH '*abc*' ORDER BY 1;
sl@0
   126
  }
sl@0
   127
} {{ABC abc xyz} abc abcd}
sl@0
   128
do_test like-2.4 {
sl@0
   129
  execsql {
sl@0
   130
    SELECT x FROM t1 WHERE x MATCH 'abc*' ORDER BY 1;
sl@0
   131
  }
sl@0
   132
} {abc abcd}
sl@0
   133
sl@0
   134
# For the remaining tests, we need to have the like optimizations
sl@0
   135
# enabled.
sl@0
   136
#
sl@0
   137
ifcapable !like_opt {
sl@0
   138
  finish_test
sl@0
   139
  return
sl@0
   140
} 
sl@0
   141
sl@0
   142
# This procedure executes the SQL.  Then it appends to the result the
sl@0
   143
# "sort" or "nosort" keyword (as in the cksort procedure above) then
sl@0
   144
# it appends the ::sqlite_query_plan variable.
sl@0
   145
#
sl@0
   146
proc queryplan {sql} {
sl@0
   147
  set ::sqlite_sort_count 0
sl@0
   148
  set data [execsql $sql]
sl@0
   149
  if {$::sqlite_sort_count} {set x sort} {set x nosort}
sl@0
   150
  lappend data $x
sl@0
   151
  return [concat $data $::sqlite_query_plan]
sl@0
   152
}
sl@0
   153
sl@0
   154
# Perform tests on the like optimization.
sl@0
   155
#
sl@0
   156
# With no index on t1.x and with case sensitivity turned off, no optimization
sl@0
   157
# is performed.
sl@0
   158
#
sl@0
   159
do_test like-3.1 {
sl@0
   160
  set sqlite_like_count 0
sl@0
   161
  queryplan {
sl@0
   162
    SELECT x FROM t1 WHERE x LIKE 'abc%' ORDER BY 1;
sl@0
   163
  }
sl@0
   164
} {ABC {ABC abc xyz} abc abcd sort t1 {}}
sl@0
   165
do_test like-3.2 {
sl@0
   166
  set sqlite_like_count
sl@0
   167
} {12}
sl@0
   168
sl@0
   169
# With an index on t1.x and case sensitivity on, optimize completely.
sl@0
   170
#
sl@0
   171
do_test like-3.3 {
sl@0
   172
  set sqlite_like_count 0
sl@0
   173
  execsql {
sl@0
   174
    PRAGMA case_sensitive_like=on;
sl@0
   175
    CREATE INDEX i1 ON t1(x);
sl@0
   176
  }
sl@0
   177
  queryplan {
sl@0
   178
    SELECT x FROM t1 WHERE x LIKE 'abc%' ORDER BY 1;
sl@0
   179
  }
sl@0
   180
} {abc abcd nosort {} i1}
sl@0
   181
do_test like-3.4 {
sl@0
   182
  set sqlite_like_count
sl@0
   183
} 0
sl@0
   184
sl@0
   185
# Partial optimization when the pattern does not end in '%'
sl@0
   186
#
sl@0
   187
do_test like-3.5 {
sl@0
   188
  set sqlite_like_count 0
sl@0
   189
  queryplan {
sl@0
   190
    SELECT x FROM t1 WHERE x LIKE 'a_c' ORDER BY 1;
sl@0
   191
  }
sl@0
   192
} {abc nosort {} i1}
sl@0
   193
do_test like-3.6 {
sl@0
   194
  set sqlite_like_count
sl@0
   195
} 6
sl@0
   196
do_test like-3.7 {
sl@0
   197
  set sqlite_like_count 0
sl@0
   198
  queryplan {
sl@0
   199
    SELECT x FROM t1 WHERE x LIKE 'ab%d' ORDER BY 1;
sl@0
   200
  }
sl@0
   201
} {abcd abd nosort {} i1}
sl@0
   202
do_test like-3.8 {
sl@0
   203
  set sqlite_like_count
sl@0
   204
} 4
sl@0
   205
do_test like-3.9 {
sl@0
   206
  set sqlite_like_count 0
sl@0
   207
  queryplan {
sl@0
   208
    SELECT x FROM t1 WHERE x LIKE 'a_c%' ORDER BY 1;
sl@0
   209
  }
sl@0
   210
} {abc abcd nosort {} i1}
sl@0
   211
do_test like-3.10 {
sl@0
   212
  set sqlite_like_count
sl@0
   213
} 6
sl@0
   214
sl@0
   215
# No optimization when the pattern begins with a wildcard.
sl@0
   216
# Note that the index is still used but only for sorting.
sl@0
   217
#
sl@0
   218
do_test like-3.11 {
sl@0
   219
  set sqlite_like_count 0
sl@0
   220
  queryplan {
sl@0
   221
    SELECT x FROM t1 WHERE x LIKE '%bcd' ORDER BY 1;
sl@0
   222
  }
sl@0
   223
} {abcd bcd nosort {} i1}
sl@0
   224
do_test like-3.12 {
sl@0
   225
  set sqlite_like_count
sl@0
   226
} 12
sl@0
   227
sl@0
   228
# No optimization for case insensitive LIKE
sl@0
   229
#
sl@0
   230
do_test like-3.13 {
sl@0
   231
  set sqlite_like_count 0
sl@0
   232
  queryplan {
sl@0
   233
    PRAGMA case_sensitive_like=off;
sl@0
   234
    SELECT x FROM t1 WHERE x LIKE 'abc%' ORDER BY 1;
sl@0
   235
  }
sl@0
   236
} {ABC {ABC abc xyz} abc abcd nosort {} i1}
sl@0
   237
do_test like-3.14 {
sl@0
   238
  set sqlite_like_count
sl@0
   239
} 12
sl@0
   240
sl@0
   241
# No optimization without an index.
sl@0
   242
#
sl@0
   243
do_test like-3.15 {
sl@0
   244
  set sqlite_like_count 0
sl@0
   245
  queryplan {
sl@0
   246
    PRAGMA case_sensitive_like=on;
sl@0
   247
    DROP INDEX i1;
sl@0
   248
    SELECT x FROM t1 WHERE x LIKE 'abc%' ORDER BY 1;
sl@0
   249
  }
sl@0
   250
} {abc abcd sort t1 {}}
sl@0
   251
do_test like-3.16 {
sl@0
   252
  set sqlite_like_count
sl@0
   253
} 12
sl@0
   254
sl@0
   255
# No GLOB optimization without an index.
sl@0
   256
#
sl@0
   257
do_test like-3.17 {
sl@0
   258
  set sqlite_like_count 0
sl@0
   259
  queryplan {
sl@0
   260
    SELECT x FROM t1 WHERE x GLOB 'abc*' ORDER BY 1;
sl@0
   261
  }
sl@0
   262
} {abc abcd sort t1 {}}
sl@0
   263
do_test like-3.18 {
sl@0
   264
  set sqlite_like_count
sl@0
   265
} 12
sl@0
   266
sl@0
   267
# GLOB is optimized regardless of the case_sensitive_like setting.
sl@0
   268
#
sl@0
   269
do_test like-3.19 {
sl@0
   270
  set sqlite_like_count 0
sl@0
   271
  queryplan {
sl@0
   272
    CREATE INDEX i1 ON t1(x);
sl@0
   273
    SELECT x FROM t1 WHERE x GLOB 'abc*' ORDER BY 1;
sl@0
   274
  }
sl@0
   275
} {abc abcd nosort {} i1}
sl@0
   276
do_test like-3.20 {
sl@0
   277
  set sqlite_like_count
sl@0
   278
} 0
sl@0
   279
do_test like-3.21 {
sl@0
   280
  set sqlite_like_count 0
sl@0
   281
  queryplan {
sl@0
   282
    PRAGMA case_sensitive_like=on;
sl@0
   283
    SELECT x FROM t1 WHERE x GLOB 'abc*' ORDER BY 1;
sl@0
   284
  }
sl@0
   285
} {abc abcd nosort {} i1}
sl@0
   286
do_test like-3.22 {
sl@0
   287
  set sqlite_like_count
sl@0
   288
} 0
sl@0
   289
do_test like-3.23 {
sl@0
   290
  set sqlite_like_count 0
sl@0
   291
  queryplan {
sl@0
   292
    PRAGMA case_sensitive_like=off;
sl@0
   293
    SELECT x FROM t1 WHERE x GLOB 'a[bc]d' ORDER BY 1;
sl@0
   294
  }
sl@0
   295
} {abd acd nosort {} i1}
sl@0
   296
do_test like-3.24 {
sl@0
   297
  set sqlite_like_count
sl@0
   298
} 6
sl@0
   299
sl@0
   300
# No optimization if the LHS of the LIKE is not a column name or
sl@0
   301
# if the RHS is not a string.
sl@0
   302
#
sl@0
   303
do_test like-4.1 {
sl@0
   304
  execsql {PRAGMA case_sensitive_like=on}
sl@0
   305
  set sqlite_like_count 0
sl@0
   306
  queryplan {
sl@0
   307
    SELECT x FROM t1 WHERE x LIKE 'abc%' ORDER BY 1
sl@0
   308
  }
sl@0
   309
} {abc abcd nosort {} i1}
sl@0
   310
do_test like-4.2 {
sl@0
   311
  set sqlite_like_count
sl@0
   312
} 0
sl@0
   313
do_test like-4.3 {
sl@0
   314
  set sqlite_like_count 0
sl@0
   315
  queryplan {
sl@0
   316
    SELECT x FROM t1 WHERE +x LIKE 'abc%' ORDER BY 1
sl@0
   317
  }
sl@0
   318
} {abc abcd nosort {} i1}
sl@0
   319
do_test like-4.4 {
sl@0
   320
  set sqlite_like_count
sl@0
   321
} 12
sl@0
   322
do_test like-4.5 {
sl@0
   323
  set sqlite_like_count 0
sl@0
   324
  queryplan {
sl@0
   325
    SELECT x FROM t1 WHERE x LIKE ('ab' || 'c%') ORDER BY 1
sl@0
   326
  }
sl@0
   327
} {abc abcd nosort {} i1}
sl@0
   328
do_test like-4.6 {
sl@0
   329
  set sqlite_like_count
sl@0
   330
} 12
sl@0
   331
sl@0
   332
# Collating sequences on the index disable the LIKE optimization.
sl@0
   333
# Or if the NOCASE collating sequence is used, the LIKE optimization
sl@0
   334
# is enabled when case_sensitive_like is OFF.
sl@0
   335
#
sl@0
   336
do_test like-5.1 {
sl@0
   337
  execsql {PRAGMA case_sensitive_like=off}
sl@0
   338
  set sqlite_like_count 0
sl@0
   339
  queryplan {
sl@0
   340
    SELECT x FROM t1 WHERE x LIKE 'abc%' ORDER BY 1
sl@0
   341
  }
sl@0
   342
} {ABC {ABC abc xyz} abc abcd nosort {} i1}
sl@0
   343
do_test like-5.2 {
sl@0
   344
  set sqlite_like_count
sl@0
   345
} 12
sl@0
   346
do_test like-5.3 {
sl@0
   347
  execsql {
sl@0
   348
    CREATE TABLE t2(x COLLATE NOCASE);
sl@0
   349
    INSERT INTO t2 SELECT * FROM t1;
sl@0
   350
    CREATE INDEX i2 ON t2(x COLLATE NOCASE);
sl@0
   351
  }
sl@0
   352
  set sqlite_like_count 0
sl@0
   353
  queryplan {
sl@0
   354
    SELECT x FROM t2 WHERE x LIKE 'abc%' ORDER BY 1
sl@0
   355
  }
sl@0
   356
} {abc ABC {ABC abc xyz} abcd nosort {} i2}
sl@0
   357
do_test like-5.4 {
sl@0
   358
  set sqlite_like_count
sl@0
   359
} 0
sl@0
   360
do_test like-5.5 {
sl@0
   361
  execsql {
sl@0
   362
    PRAGMA case_sensitive_like=on;
sl@0
   363
  }
sl@0
   364
  set sqlite_like_count 0
sl@0
   365
  queryplan {
sl@0
   366
    SELECT x FROM t2 WHERE x LIKE 'abc%' ORDER BY 1
sl@0
   367
  }
sl@0
   368
} {abc abcd nosort {} i2}
sl@0
   369
do_test like-5.6 {
sl@0
   370
  set sqlite_like_count
sl@0
   371
} 12
sl@0
   372
do_test like-5.7 {
sl@0
   373
  execsql {
sl@0
   374
    PRAGMA case_sensitive_like=off;
sl@0
   375
  }
sl@0
   376
  set sqlite_like_count 0
sl@0
   377
  queryplan {
sl@0
   378
    SELECT x FROM t2 WHERE x GLOB 'abc*' ORDER BY 1
sl@0
   379
  }
sl@0
   380
} {abc abcd nosort {} i2}
sl@0
   381
do_test like-5.8 {
sl@0
   382
  set sqlite_like_count
sl@0
   383
} 12
sl@0
   384
do_test like-5.11 {
sl@0
   385
  execsql {PRAGMA case_sensitive_like=off}
sl@0
   386
  set sqlite_like_count 0
sl@0
   387
  queryplan {
sl@0
   388
    SELECT x FROM t1 WHERE x LIKE 'ABC%' ORDER BY 1
sl@0
   389
  }
sl@0
   390
} {ABC {ABC abc xyz} abc abcd nosort {} i1}
sl@0
   391
do_test like-5.12 {
sl@0
   392
  set sqlite_like_count
sl@0
   393
} 12
sl@0
   394
do_test like-5.13 {
sl@0
   395
  set sqlite_like_count 0
sl@0
   396
  queryplan {
sl@0
   397
    SELECT x FROM t2 WHERE x LIKE 'ABC%' ORDER BY 1
sl@0
   398
  }
sl@0
   399
} {abc ABC {ABC abc xyz} abcd nosort {} i2}
sl@0
   400
do_test like-5.14 {
sl@0
   401
  set sqlite_like_count
sl@0
   402
} 0
sl@0
   403
do_test like-5.15 {
sl@0
   404
  execsql {
sl@0
   405
    PRAGMA case_sensitive_like=on;
sl@0
   406
  }
sl@0
   407
  set sqlite_like_count 0
sl@0
   408
  queryplan {
sl@0
   409
    SELECT x FROM t2 WHERE x LIKE 'ABC%' ORDER BY 1
sl@0
   410
  }
sl@0
   411
} {ABC {ABC abc xyz} nosort {} i2}
sl@0
   412
do_test like-5.16 {
sl@0
   413
  set sqlite_like_count
sl@0
   414
} 12
sl@0
   415
do_test like-5.17 {
sl@0
   416
  execsql {
sl@0
   417
    PRAGMA case_sensitive_like=off;
sl@0
   418
  }
sl@0
   419
  set sqlite_like_count 0
sl@0
   420
  queryplan {
sl@0
   421
    SELECT x FROM t2 WHERE x GLOB 'ABC*' ORDER BY 1
sl@0
   422
  }
sl@0
   423
} {ABC {ABC abc xyz} nosort {} i2}
sl@0
   424
do_test like-5.18 {
sl@0
   425
  set sqlite_like_count
sl@0
   426
} 12
sl@0
   427
sl@0
   428
# Boundary case.  The prefix for a LIKE comparison is rounded up
sl@0
   429
# when constructing the comparison.  Example:  "ab" becomes "ac".
sl@0
   430
# In other words, the last character is increased by one.
sl@0
   431
#
sl@0
   432
# Make sure this happens correctly when the last character is a 
sl@0
   433
# "z" and we are doing case-insensitive comparisons.
sl@0
   434
#
sl@0
   435
# Ticket #2959
sl@0
   436
#
sl@0
   437
do_test like-5.21 {
sl@0
   438
  execsql {
sl@0
   439
    PRAGMA case_sensitive_like=off;
sl@0
   440
    INSERT INTO t2 VALUES('ZZ-upper-upper');
sl@0
   441
    INSERT INTO t2 VALUES('zZ-lower-upper');
sl@0
   442
    INSERT INTO t2 VALUES('Zz-upper-lower');
sl@0
   443
    INSERT INTO t2 VALUES('zz-lower-lower');
sl@0
   444
  }
sl@0
   445
  queryplan {
sl@0
   446
    SELECT x FROM t2 WHERE x LIKE 'zz%';
sl@0
   447
  }
sl@0
   448
} {zz-lower-lower zZ-lower-upper Zz-upper-lower ZZ-upper-upper nosort {} i2}
sl@0
   449
do_test like-5.22 {
sl@0
   450
  queryplan {
sl@0
   451
    SELECT x FROM t2 WHERE x LIKE 'zZ%';
sl@0
   452
  }
sl@0
   453
} {zz-lower-lower zZ-lower-upper Zz-upper-lower ZZ-upper-upper nosort {} i2}
sl@0
   454
do_test like-5.23 {
sl@0
   455
  queryplan {
sl@0
   456
    SELECT x FROM t2 WHERE x LIKE 'Zz%';
sl@0
   457
  }
sl@0
   458
} {zz-lower-lower zZ-lower-upper Zz-upper-lower ZZ-upper-upper nosort {} i2}
sl@0
   459
do_test like-5.24 {
sl@0
   460
  queryplan {
sl@0
   461
    SELECT x FROM t2 WHERE x LIKE 'ZZ%';
sl@0
   462
  }
sl@0
   463
} {zz-lower-lower zZ-lower-upper Zz-upper-lower ZZ-upper-upper nosort {} i2}
sl@0
   464
do_test like-5.25 {
sl@0
   465
  queryplan {
sl@0
   466
    PRAGMA case_sensitive_like=on;
sl@0
   467
    CREATE TABLE t3(x);
sl@0
   468
    CREATE INDEX i3 ON t3(x);
sl@0
   469
    INSERT INTO t3 VALUES('ZZ-upper-upper');
sl@0
   470
    INSERT INTO t3 VALUES('zZ-lower-upper');
sl@0
   471
    INSERT INTO t3 VALUES('Zz-upper-lower');
sl@0
   472
    INSERT INTO t3 VALUES('zz-lower-lower');
sl@0
   473
    SELECT x FROM t3 WHERE x LIKE 'zz%';
sl@0
   474
  }
sl@0
   475
} {zz-lower-lower nosort {} i3}
sl@0
   476
do_test like-5.26 {
sl@0
   477
  queryplan {
sl@0
   478
    SELECT x FROM t3 WHERE x LIKE 'zZ%';
sl@0
   479
  }
sl@0
   480
} {zZ-lower-upper nosort {} i3}
sl@0
   481
do_test like-5.27 {
sl@0
   482
  queryplan {
sl@0
   483
    SELECT x FROM t3 WHERE x LIKE 'Zz%';
sl@0
   484
  }
sl@0
   485
} {Zz-upper-lower nosort {} i3}
sl@0
   486
do_test like-5.28 {
sl@0
   487
  queryplan {
sl@0
   488
    SELECT x FROM t3 WHERE x LIKE 'ZZ%';
sl@0
   489
  }
sl@0
   490
} {ZZ-upper-upper nosort {} i3}
sl@0
   491
sl@0
   492
sl@0
   493
# ticket #2407
sl@0
   494
#
sl@0
   495
# Make sure the LIKE prefix optimization does not strip off leading
sl@0
   496
# characters of the like pattern that happen to be quote characters.
sl@0
   497
#
sl@0
   498
do_test like-6.1 {
sl@0
   499
  foreach x { 'abc 'bcd 'def 'ax } {
sl@0
   500
    set x2 '[string map {' ''} $x]'
sl@0
   501
    db eval "INSERT INTO t2 VALUES($x2)"
sl@0
   502
  }
sl@0
   503
  execsql {
sl@0
   504
    SELECT * FROM t2 WHERE x LIKE '''a%'
sl@0
   505
  }
sl@0
   506
} {'abc 'ax}
sl@0
   507
sl@0
   508
do_test like-7.1 {
sl@0
   509
  execsql {
sl@0
   510
    SELECT * FROM t1 WHERE rowid GLOB '1*';
sl@0
   511
  }
sl@0
   512
} {a}
sl@0
   513
sl@0
   514
# ticket #3345.
sl@0
   515
#
sl@0
   516
# Overloading the LIKE function with -1 for the number of arguments
sl@0
   517
# will overload both the 2-argument and the 3-argument LIKE.
sl@0
   518
#
sl@0
   519
do_test like-8.1 {
sl@0
   520
  db eval {
sl@0
   521
    CREATE TABLE t8(x);
sl@0
   522
    INSERT INTO t8 VALUES('abcdef');
sl@0
   523
    INSERT INTO t8 VALUES('ghijkl');
sl@0
   524
    INSERT INTO t8 VALUES('mnopqr');
sl@0
   525
    SELECT 1, x FROM t8 WHERE x LIKE '%h%';
sl@0
   526
    SELECT 2, x FROM t8 WHERE x LIKE '%h%' ESCAPE 'x';
sl@0
   527
  }
sl@0
   528
} {1 ghijkl 2 ghijkl}
sl@0
   529
do_test like-8.2 {
sl@0
   530
  proc newlike {args} {return 1} ;# Alternative LIKE is always return TRUE
sl@0
   531
  db function like newlike       ;# Uses -1 for nArg in sqlite3_create_function
sl@0
   532
  db cache flush
sl@0
   533
  db eval {
sl@0
   534
    SELECT 1, x FROM t8 WHERE x LIKE '%h%';
sl@0
   535
    SELECT 2, x FROM t8 WHERE x LIKE '%h%' ESCAPE 'x';
sl@0
   536
  }
sl@0
   537
} {1 ghijkl 2 ghijkl}
sl@0
   538
do_test like-8.3 {
sl@0
   539
  db function like -argcount 2 newlike
sl@0
   540
  db eval {
sl@0
   541
    SELECT 1, x FROM t8 WHERE x LIKE '%h%';
sl@0
   542
    SELECT 2, x FROM t8 WHERE x LIKE '%h%' ESCAPE 'x';
sl@0
   543
  }
sl@0
   544
} {1 abcdef 1 ghijkl 1 mnopqr 2 ghijkl}
sl@0
   545
do_test like-8.4 {
sl@0
   546
  db function like -argcount 3 newlike
sl@0
   547
  db eval {
sl@0
   548
    SELECT 1, x FROM t8 WHERE x LIKE '%h%';
sl@0
   549
    SELECT 2, x FROM t8 WHERE x LIKE '%h%' ESCAPE 'x';
sl@0
   550
  }
sl@0
   551
} {1 abcdef 1 ghijkl 1 mnopqr 2 abcdef 2 ghijkl 2 mnopqr}
sl@0
   552
sl@0
   553
sl@0
   554
finish_test