os/persistentdata/persistentstorage/sqlite3api/TEST/TclScript/rowid.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
# 2001 September 15
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 magic ROWID column that is
sl@0
    13
# found on all tables.
sl@0
    14
#
sl@0
    15
# $Id: rowid.test,v 1.20 2008/01/19 20:11:26 drh 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
# Basic ROWID functionality tests.
sl@0
    21
#
sl@0
    22
do_test rowid-1.1 {
sl@0
    23
  execsql {
sl@0
    24
    CREATE TABLE t1(x int, y int);
sl@0
    25
    INSERT INTO t1 VALUES(1,2);
sl@0
    26
    INSERT INTO t1 VALUES(3,4);
sl@0
    27
    SELECT x FROM t1 ORDER BY y;
sl@0
    28
  }
sl@0
    29
} {1 3}
sl@0
    30
do_test rowid-1.2 {
sl@0
    31
  set r [execsql {SELECT rowid FROM t1 ORDER BY x}]
sl@0
    32
  global x2rowid rowid2x
sl@0
    33
  set x2rowid(1) [lindex $r 0]
sl@0
    34
  set x2rowid(3) [lindex $r 1]
sl@0
    35
  set rowid2x($x2rowid(1)) 1
sl@0
    36
  set rowid2x($x2rowid(3)) 3
sl@0
    37
  llength $r
sl@0
    38
} {2}
sl@0
    39
do_test rowid-1.3 {
sl@0
    40
  global x2rowid
sl@0
    41
  set sql "SELECT x FROM t1 WHERE rowid==$x2rowid(1)"
sl@0
    42
  execsql $sql
sl@0
    43
} {1}
sl@0
    44
do_test rowid-1.4 {
sl@0
    45
  global x2rowid
sl@0
    46
  set sql "SELECT x FROM t1 WHERE rowid==$x2rowid(3)"
sl@0
    47
  execsql $sql
sl@0
    48
} {3}
sl@0
    49
do_test rowid-1.5 {
sl@0
    50
  global x2rowid
sl@0
    51
  set sql "SELECT x FROM t1 WHERE oid==$x2rowid(1)"
sl@0
    52
  execsql $sql
sl@0
    53
} {1}
sl@0
    54
do_test rowid-1.6 {
sl@0
    55
  global x2rowid
sl@0
    56
  set sql "SELECT x FROM t1 WHERE OID==$x2rowid(3)"
sl@0
    57
  execsql $sql
sl@0
    58
} {3}
sl@0
    59
do_test rowid-1.7 {
sl@0
    60
  global x2rowid
sl@0
    61
  set sql "SELECT x FROM t1 WHERE _rowid_==$x2rowid(1)"
sl@0
    62
  execsql $sql
sl@0
    63
} {1}
sl@0
    64
do_test rowid-1.7.1 {
sl@0
    65
  while 1 {
sl@0
    66
    set norow [expr {int(rand()*1000000)}]
sl@0
    67
    if {$norow!=$x2rowid(1) && $norow!=$x2rowid(3)} break
sl@0
    68
  }
sl@0
    69
  execsql "SELECT x FROM t1 WHERE rowid=$norow"
sl@0
    70
} {}
sl@0
    71
do_test rowid-1.8 {
sl@0
    72
  global x2rowid
sl@0
    73
  set v [execsql {SELECT x, oid FROM t1 order by x}]
sl@0
    74
  set v2 [list 1 $x2rowid(1) 3 $x2rowid(3)]
sl@0
    75
  expr {$v==$v2}
sl@0
    76
} {1}
sl@0
    77
do_test rowid-1.9 {
sl@0
    78
  global x2rowid
sl@0
    79
  set v [execsql {SELECT x, RowID FROM t1 order by x}]
sl@0
    80
  set v2 [list 1 $x2rowid(1) 3 $x2rowid(3)]
sl@0
    81
  expr {$v==$v2}
sl@0
    82
} {1}
sl@0
    83
do_test rowid-1.10 {
sl@0
    84
  global x2rowid
sl@0
    85
  set v [execsql {SELECT x, _rowid_ FROM t1 order by x}]
sl@0
    86
  set v2 [list 1 $x2rowid(1) 3 $x2rowid(3)]
sl@0
    87
  expr {$v==$v2}
sl@0
    88
} {1}
sl@0
    89
sl@0
    90
# We can insert or update the ROWID column.
sl@0
    91
#
sl@0
    92
do_test rowid-2.1 {
sl@0
    93
  catchsql {
sl@0
    94
    INSERT INTO t1(rowid,x,y) VALUES(1234,5,6);
sl@0
    95
    SELECT rowid, * FROM t1;
sl@0
    96
  }
sl@0
    97
} {0 {1 1 2 2 3 4 1234 5 6}}
sl@0
    98
do_test rowid-2.2 {
sl@0
    99
  catchsql {
sl@0
   100
    UPDATE t1 SET rowid=12345 WHERE x==1;
sl@0
   101
    SELECT rowid, * FROM t1
sl@0
   102
  }
sl@0
   103
} {0 {2 3 4 1234 5 6 12345 1 2}}
sl@0
   104
do_test rowid-2.3 {
sl@0
   105
  catchsql {
sl@0
   106
    INSERT INTO t1(y,x,oid) VALUES(8,7,1235);
sl@0
   107
    SELECT rowid, * FROM t1 WHERE rowid>1000;
sl@0
   108
  }
sl@0
   109
} {0 {1234 5 6 1235 7 8 12345 1 2}}
sl@0
   110
do_test rowid-2.4 {
sl@0
   111
  catchsql {
sl@0
   112
    UPDATE t1 SET oid=12346 WHERE x==1;
sl@0
   113
    SELECT rowid, * FROM t1;
sl@0
   114
  }
sl@0
   115
} {0 {2 3 4 1234 5 6 1235 7 8 12346 1 2}}
sl@0
   116
do_test rowid-2.5 {
sl@0
   117
  catchsql {
sl@0
   118
    INSERT INTO t1(x,_rowid_,y) VALUES(9,1236,10);
sl@0
   119
    SELECT rowid, * FROM t1 WHERE rowid>1000;
sl@0
   120
  }
sl@0
   121
} {0 {1234 5 6 1235 7 8 1236 9 10 12346 1 2}}
sl@0
   122
do_test rowid-2.6 {
sl@0
   123
  catchsql {
sl@0
   124
    UPDATE t1 SET _rowid_=12347 WHERE x==1;
sl@0
   125
    SELECT rowid, * FROM t1 WHERE rowid>1000;
sl@0
   126
  }
sl@0
   127
} {0 {1234 5 6 1235 7 8 1236 9 10 12347 1 2}}
sl@0
   128
sl@0
   129
# But we can use ROWID in the WHERE clause of an UPDATE that does not
sl@0
   130
# change the ROWID.
sl@0
   131
#
sl@0
   132
do_test rowid-2.7 {
sl@0
   133
  global x2rowid
sl@0
   134
  set sql "UPDATE t1 SET x=2 WHERE OID==$x2rowid(3)"
sl@0
   135
  execsql $sql
sl@0
   136
  execsql {SELECT x FROM t1 ORDER BY x}
sl@0
   137
} {1 2 5 7 9}
sl@0
   138
do_test rowid-2.8 {
sl@0
   139
  global x2rowid
sl@0
   140
  set sql "UPDATE t1 SET x=3 WHERE _rowid_==$x2rowid(3)"
sl@0
   141
  execsql $sql
sl@0
   142
  execsql {SELECT x FROM t1 ORDER BY x}
sl@0
   143
} {1 3 5 7 9}
sl@0
   144
sl@0
   145
# We cannot index by ROWID
sl@0
   146
#
sl@0
   147
do_test rowid-2.9 {
sl@0
   148
  set v [catch {execsql {CREATE INDEX idxt1 ON t1(rowid)}} msg]
sl@0
   149
  lappend v $msg
sl@0
   150
} {1 {table t1 has no column named rowid}}
sl@0
   151
do_test rowid-2.10 {
sl@0
   152
  set v [catch {execsql {CREATE INDEX idxt1 ON t1(_rowid_)}} msg]
sl@0
   153
  lappend v $msg
sl@0
   154
} {1 {table t1 has no column named _rowid_}}
sl@0
   155
do_test rowid-2.11 {
sl@0
   156
  set v [catch {execsql {CREATE INDEX idxt1 ON t1(oid)}} msg]
sl@0
   157
  lappend v $msg
sl@0
   158
} {1 {table t1 has no column named oid}}
sl@0
   159
do_test rowid-2.12 {
sl@0
   160
  set v [catch {execsql {CREATE INDEX idxt1 ON t1(x, rowid)}} msg]
sl@0
   161
  lappend v $msg
sl@0
   162
} {1 {table t1 has no column named rowid}}
sl@0
   163
sl@0
   164
# Columns defined in the CREATE statement override the buildin ROWID
sl@0
   165
# column names.
sl@0
   166
#
sl@0
   167
do_test rowid-3.1 {
sl@0
   168
  execsql {
sl@0
   169
    CREATE TABLE t2(rowid int, x int, y int);
sl@0
   170
    INSERT INTO t2 VALUES(0,2,3);
sl@0
   171
    INSERT INTO t2 VALUES(4,5,6);
sl@0
   172
    INSERT INTO t2 VALUES(7,8,9);
sl@0
   173
    SELECT * FROM t2 ORDER BY x;
sl@0
   174
  }
sl@0
   175
} {0 2 3 4 5 6 7 8 9}
sl@0
   176
do_test rowid-3.2 {
sl@0
   177
  execsql {SELECT * FROM t2 ORDER BY rowid}
sl@0
   178
} {0 2 3 4 5 6 7 8 9}
sl@0
   179
do_test rowid-3.3 {
sl@0
   180
  execsql {SELECT rowid, x, y FROM t2 ORDER BY rowid}
sl@0
   181
} {0 2 3 4 5 6 7 8 9}
sl@0
   182
do_test rowid-3.4 {
sl@0
   183
  set r1 [execsql {SELECT _rowid_, rowid FROM t2 ORDER BY rowid}]
sl@0
   184
  foreach {a b c d e f} $r1 {}
sl@0
   185
  set r2 [execsql {SELECT _rowid_, rowid FROM t2 ORDER BY x DESC}]
sl@0
   186
  foreach {u v w x y z} $r2 {}
sl@0
   187
  expr {$u==$e && $w==$c && $y==$a}
sl@0
   188
} {1}
sl@0
   189
# sqlite3 v3 - do_probtest doesn't exist anymore?
sl@0
   190
if 0 {
sl@0
   191
do_probtest rowid-3.5 {
sl@0
   192
  set r1 [execsql {SELECT _rowid_, rowid FROM t2 ORDER BY rowid}]
sl@0
   193
  foreach {a b c d e f} $r1 {}
sl@0
   194
  expr {$a!=$b && $c!=$d && $e!=$f}
sl@0
   195
} {1}
sl@0
   196
}
sl@0
   197
sl@0
   198
# Let's try some more complex examples, including some joins.
sl@0
   199
#
sl@0
   200
do_test rowid-4.1 {
sl@0
   201
  execsql {
sl@0
   202
    DELETE FROM t1;
sl@0
   203
    DELETE FROM t2;
sl@0
   204
  }
sl@0
   205
  for {set i 1} {$i<=50} {incr i} {
sl@0
   206
    execsql "INSERT INTO t1(x,y) VALUES($i,[expr {$i*$i}])"
sl@0
   207
  }
sl@0
   208
  execsql {INSERT INTO t2 SELECT _rowid_, x*y, y*y FROM t1}
sl@0
   209
  execsql {SELECT t2.y FROM t1, t2 WHERE t1.x==4 AND t1.rowid==t2.rowid}
sl@0
   210
} {256}
sl@0
   211
do_test rowid-4.2 {
sl@0
   212
  execsql {SELECT t2.y FROM t2, t1 WHERE t1.x==4 AND t1.rowid==t2.rowid}
sl@0
   213
} {256}
sl@0
   214
do_test rowid-4.2.1 {
sl@0
   215
  execsql {SELECT t2.y FROM t2, t1 WHERE t1.x==4 AND t1.oid==t2.rowid}
sl@0
   216
} {256}
sl@0
   217
do_test rowid-4.2.2 {
sl@0
   218
  execsql {SELECT t2.y FROM t2, t1 WHERE t1.x==4 AND t1._rowid_==t2.rowid}
sl@0
   219
} {256}
sl@0
   220
do_test rowid-4.2.3 {
sl@0
   221
  execsql {SELECT t2.y FROM t2, t1 WHERE t1.x==4 AND t2.rowid==t1.rowid}
sl@0
   222
} {256}
sl@0
   223
do_test rowid-4.2.4 {
sl@0
   224
  execsql {SELECT t2.y FROM t2, t1 WHERE t2.rowid==t1.oid AND t1.x==4}
sl@0
   225
} {256}
sl@0
   226
do_test rowid-4.2.5 {
sl@0
   227
  execsql {SELECT t2.y FROM t1, t2 WHERE t1.x==4 AND t1._rowid_==t2.rowid}
sl@0
   228
} {256}
sl@0
   229
do_test rowid-4.2.6 {
sl@0
   230
  execsql {SELECT t2.y FROM t1, t2 WHERE t1.x==4 AND t2.rowid==t1.rowid}
sl@0
   231
} {256}
sl@0
   232
do_test rowid-4.2.7 {
sl@0
   233
  execsql {SELECT t2.y FROM t1, t2 WHERE t2.rowid==t1.oid AND t1.x==4}
sl@0
   234
} {256}
sl@0
   235
do_test rowid-4.3 {
sl@0
   236
  execsql {CREATE INDEX idxt1 ON t1(x)}
sl@0
   237
  execsql {SELECT t2.y FROM t1, t2 WHERE t1.x==4 AND t1.rowid==t2.rowid}
sl@0
   238
} {256}
sl@0
   239
do_test rowid-4.3.1 {
sl@0
   240
  execsql {SELECT t2.y FROM t1, t2 WHERE t1.x==4 AND t1._rowid_==t2.rowid}
sl@0
   241
} {256}
sl@0
   242
do_test rowid-4.3.2 {
sl@0
   243
  execsql {SELECT t2.y FROM t1, t2 WHERE t2.rowid==t1.oid AND 4==t1.x}
sl@0
   244
} {256}
sl@0
   245
do_test rowid-4.4 {
sl@0
   246
  execsql {SELECT t2.y FROM t2, t1 WHERE t1.x==4 AND t1.rowid==t2.rowid}
sl@0
   247
} {256}
sl@0
   248
do_test rowid-4.4.1 {
sl@0
   249
  execsql {SELECT t2.y FROM t2, t1 WHERE t1.x==4 AND t1._rowid_==t2.rowid}
sl@0
   250
} {256}
sl@0
   251
do_test rowid-4.4.2 {
sl@0
   252
  execsql {SELECT t2.y FROM t2, t1 WHERE t2.rowid==t1.oid AND 4==t1.x}
sl@0
   253
} {256}
sl@0
   254
do_test rowid-4.5 {
sl@0
   255
  execsql {CREATE INDEX idxt2 ON t2(y)}
sl@0
   256
  set sqlite_search_count 0
sl@0
   257
  concat [execsql {
sl@0
   258
    SELECT t1.x FROM t2, t1 
sl@0
   259
    WHERE t2.y==256 AND t1.rowid==t2.rowid
sl@0
   260
  }] $sqlite_search_count
sl@0
   261
} {4 3}
sl@0
   262
do_test rowid-4.5.1 {
sl@0
   263
  set sqlite_search_count 0
sl@0
   264
  concat [execsql {
sl@0
   265
    SELECT t1.x FROM t2, t1 
sl@0
   266
    WHERE t1.OID==t2.rowid AND t2.y==81
sl@0
   267
  }] $sqlite_search_count
sl@0
   268
} {3 3}
sl@0
   269
do_test rowid-4.6 {
sl@0
   270
  execsql {
sl@0
   271
    SELECT t1.x FROM t1, t2
sl@0
   272
    WHERE t2.y==256 AND t1.rowid==t2.rowid
sl@0
   273
  }
sl@0
   274
} {4}
sl@0
   275
sl@0
   276
do_test rowid-5.1.1 {
sl@0
   277
  ifcapable subquery {
sl@0
   278
    execsql {DELETE FROM t1 WHERE _rowid_ IN (SELECT oid FROM t1 WHERE x>8)}
sl@0
   279
  } else {
sl@0
   280
    set oids [execsql {SELECT oid FROM t1 WHERE x>8}]
sl@0
   281
    set where "_rowid_ = [join $oids { OR _rowid_ = }]"
sl@0
   282
    execsql "DELETE FROM t1 WHERE $where"
sl@0
   283
  }
sl@0
   284
} {}
sl@0
   285
do_test rowid-5.1.2 {
sl@0
   286
  execsql {SELECT max(x) FROM t1}
sl@0
   287
} {8}
sl@0
   288
sl@0
   289
# Make sure a "WHERE rowid=X" clause works when there is no ROWID of X.
sl@0
   290
#
sl@0
   291
do_test rowid-6.1 {
sl@0
   292
  execsql {
sl@0
   293
    SELECT x FROM t1
sl@0
   294
  }
sl@0
   295
} {1 2 3 4 5 6 7 8}
sl@0
   296
do_test rowid-6.2 {
sl@0
   297
  for {set ::norow 1} {1} {incr ::norow} {
sl@0
   298
    if {[execsql "SELECT x FROM t1 WHERE rowid=$::norow"]==""}  break
sl@0
   299
  }
sl@0
   300
  execsql [subst {
sl@0
   301
    DELETE FROM t1 WHERE rowid=$::norow
sl@0
   302
  }]
sl@0
   303
} {}
sl@0
   304
do_test rowid-6.3 {
sl@0
   305
  execsql {
sl@0
   306
    SELECT x FROM t1
sl@0
   307
  }
sl@0
   308
} {1 2 3 4 5 6 7 8}
sl@0
   309
sl@0
   310
# Beginning with version 2.3.4, SQLite computes rowids of new rows by
sl@0
   311
# finding the maximum current rowid and adding one.  It falls back to
sl@0
   312
# the old random algorithm if the maximum rowid is the largest integer.
sl@0
   313
# The following tests are for this new behavior.
sl@0
   314
#
sl@0
   315
do_test rowid-7.0 {
sl@0
   316
  execsql {
sl@0
   317
    DELETE FROM t1;
sl@0
   318
    DROP TABLE t2;
sl@0
   319
    DROP INDEX idxt1;
sl@0
   320
    INSERT INTO t1 VALUES(1,2);
sl@0
   321
    SELECT rowid, * FROM t1;
sl@0
   322
  }
sl@0
   323
} {1 1 2}
sl@0
   324
do_test rowid-7.1 {
sl@0
   325
  execsql {
sl@0
   326
    INSERT INTO t1 VALUES(99,100);
sl@0
   327
    SELECT rowid,* FROM t1
sl@0
   328
  }
sl@0
   329
} {1 1 2 2 99 100}
sl@0
   330
do_test rowid-7.2 {
sl@0
   331
  execsql {
sl@0
   332
    CREATE TABLE t2(a INTEGER PRIMARY KEY, b);
sl@0
   333
    INSERT INTO t2(b) VALUES(55);
sl@0
   334
    SELECT * FROM t2;
sl@0
   335
  }
sl@0
   336
} {1 55}
sl@0
   337
do_test rowid-7.3 {
sl@0
   338
  execsql {
sl@0
   339
    INSERT INTO t2(b) VALUES(66);
sl@0
   340
    SELECT * FROM t2;
sl@0
   341
  }
sl@0
   342
} {1 55 2 66}
sl@0
   343
do_test rowid-7.4 {
sl@0
   344
  execsql {
sl@0
   345
    INSERT INTO t2(a,b) VALUES(1000000,77);
sl@0
   346
    INSERT INTO t2(b) VALUES(88);
sl@0
   347
    SELECT * FROM t2;
sl@0
   348
  }
sl@0
   349
} {1 55 2 66 1000000 77 1000001 88}
sl@0
   350
do_test rowid-7.5 {
sl@0
   351
  execsql {
sl@0
   352
    INSERT INTO t2(a,b) VALUES(2147483647,99);
sl@0
   353
    INSERT INTO t2(b) VALUES(11);
sl@0
   354
    SELECT b FROM t2 ORDER BY b;
sl@0
   355
  }
sl@0
   356
} {11 55 66 77 88 99}
sl@0
   357
ifcapable subquery {
sl@0
   358
  do_test rowid-7.6 {
sl@0
   359
    execsql {
sl@0
   360
      SELECT b FROM t2 WHERE a NOT IN(1,2,1000000,1000001,2147483647);
sl@0
   361
    }
sl@0
   362
  } {11}
sl@0
   363
  do_test rowid-7.7 {
sl@0
   364
    execsql {
sl@0
   365
      INSERT INTO t2(b) VALUES(22);
sl@0
   366
      INSERT INTO t2(b) VALUES(33);
sl@0
   367
      INSERT INTO t2(b) VALUES(44);
sl@0
   368
      INSERT INTO t2(b) VALUES(55);
sl@0
   369
      SELECT b FROM t2 WHERE a NOT IN(1,2,1000000,1000001,2147483647) 
sl@0
   370
          ORDER BY b;
sl@0
   371
    }
sl@0
   372
  } {11 22 33 44 55}
sl@0
   373
}
sl@0
   374
do_test rowid-7.8 {
sl@0
   375
  execsql {
sl@0
   376
    DELETE FROM t2 WHERE a!=2;
sl@0
   377
    INSERT INTO t2(b) VALUES(111);
sl@0
   378
    SELECT * FROM t2;
sl@0
   379
  }
sl@0
   380
} {2 66 3 111}
sl@0
   381
sl@0
   382
ifcapable {trigger} {
sl@0
   383
# Make sure AFTER triggers that do INSERTs do not change the last_insert_rowid.
sl@0
   384
# Ticket #290
sl@0
   385
#
sl@0
   386
do_test rowid-8.1 {
sl@0
   387
  execsql {
sl@0
   388
    CREATE TABLE t3(a integer primary key);
sl@0
   389
    CREATE TABLE t4(x);
sl@0
   390
    INSERT INTO t4 VALUES(1);
sl@0
   391
    CREATE TRIGGER r3 AFTER INSERT on t3 FOR EACH ROW BEGIN
sl@0
   392
      INSERT INTO t4 VALUES(NEW.a+10);
sl@0
   393
    END;
sl@0
   394
    SELECT * FROM t3;
sl@0
   395
  }
sl@0
   396
} {}
sl@0
   397
do_test rowid-8.2 {
sl@0
   398
  execsql {
sl@0
   399
    SELECT rowid, * FROM t4;
sl@0
   400
  }
sl@0
   401
} {1 1}
sl@0
   402
do_test rowid-8.3 {
sl@0
   403
  execsql {
sl@0
   404
    INSERT INTO t3 VALUES(123);
sl@0
   405
    SELECT last_insert_rowid();
sl@0
   406
  }
sl@0
   407
} {123}
sl@0
   408
do_test rowid-8.4 {
sl@0
   409
  execsql {
sl@0
   410
    SELECT * FROM t3;
sl@0
   411
  }
sl@0
   412
} {123}
sl@0
   413
do_test rowid-8.5 {
sl@0
   414
  execsql {
sl@0
   415
    SELECT rowid, * FROM t4;
sl@0
   416
  }
sl@0
   417
} {1 1 2 133}
sl@0
   418
do_test rowid-8.6 {
sl@0
   419
  execsql {
sl@0
   420
    INSERT INTO t3 VALUES(NULL);
sl@0
   421
    SELECT last_insert_rowid();
sl@0
   422
  }
sl@0
   423
} {124}
sl@0
   424
do_test rowid-8.7 {
sl@0
   425
  execsql {
sl@0
   426
    SELECT * FROM t3;
sl@0
   427
  }
sl@0
   428
} {123 124}
sl@0
   429
do_test rowid-8.8 {
sl@0
   430
  execsql {
sl@0
   431
    SELECT rowid, * FROM t4;
sl@0
   432
  }
sl@0
   433
} {1 1 2 133 3 134}
sl@0
   434
} ;# endif trigger
sl@0
   435
sl@0
   436
# If triggers are not enable, simulate their effect for the tests that
sl@0
   437
# follow.
sl@0
   438
ifcapable {!trigger} {
sl@0
   439
  execsql {
sl@0
   440
    CREATE TABLE t3(a integer primary key);
sl@0
   441
    INSERT INTO t3 VALUES(123);
sl@0
   442
    INSERT INTO t3 VALUES(124);
sl@0
   443
  }
sl@0
   444
}
sl@0
   445
sl@0
   446
# ticket #377: Comparison between integer primiary key and floating point
sl@0
   447
# values.
sl@0
   448
#
sl@0
   449
do_test rowid-9.1 {
sl@0
   450
  execsql {
sl@0
   451
    SELECT * FROM t3 WHERE a<123.5
sl@0
   452
  }
sl@0
   453
} {123}
sl@0
   454
do_test rowid-9.2 {
sl@0
   455
  execsql {
sl@0
   456
    SELECT * FROM t3 WHERE a<124.5
sl@0
   457
  }
sl@0
   458
} {123 124}
sl@0
   459
do_test rowid-9.3 {
sl@0
   460
  execsql {
sl@0
   461
    SELECT * FROM t3 WHERE a>123.5
sl@0
   462
  }
sl@0
   463
} {124}
sl@0
   464
do_test rowid-9.4 {
sl@0
   465
  execsql {
sl@0
   466
    SELECT * FROM t3 WHERE a>122.5
sl@0
   467
  }
sl@0
   468
} {123 124}
sl@0
   469
do_test rowid-9.5 {
sl@0
   470
  execsql {
sl@0
   471
    SELECT * FROM t3 WHERE a==123.5
sl@0
   472
  }
sl@0
   473
} {}
sl@0
   474
do_test rowid-9.6 {
sl@0
   475
  execsql {
sl@0
   476
    SELECT * FROM t3 WHERE a==123.000
sl@0
   477
  }
sl@0
   478
} {123}
sl@0
   479
do_test rowid-9.7 {
sl@0
   480
  execsql {
sl@0
   481
    SELECT * FROM t3 WHERE a>100.5 AND a<200.5
sl@0
   482
  }
sl@0
   483
} {123 124}
sl@0
   484
do_test rowid-9.8 {
sl@0
   485
  execsql {
sl@0
   486
    SELECT * FROM t3 WHERE a>'xyz';
sl@0
   487
  }
sl@0
   488
} {}
sl@0
   489
do_test rowid-9.9 {
sl@0
   490
  execsql {
sl@0
   491
    SELECT * FROM t3 WHERE a<'xyz';
sl@0
   492
  }
sl@0
   493
} {123 124}
sl@0
   494
do_test rowid-9.10 {
sl@0
   495
  execsql {
sl@0
   496
    SELECT * FROM t3 WHERE a>=122.9 AND a<=123.1
sl@0
   497
  }
sl@0
   498
} {123}
sl@0
   499
sl@0
   500
# Ticket #567.  Comparisons of ROWID or integery primary key against
sl@0
   501
# floating point numbers still do not always work.
sl@0
   502
#
sl@0
   503
do_test rowid-10.1 {
sl@0
   504
  execsql {
sl@0
   505
    CREATE TABLE t5(a);
sl@0
   506
    INSERT INTO t5 VALUES(1);
sl@0
   507
    INSERT INTO t5 VALUES(2);
sl@0
   508
    INSERT INTO t5 SELECT a+2 FROM t5;
sl@0
   509
    INSERT INTO t5 SELECT a+4 FROM t5;
sl@0
   510
    SELECT rowid, * FROM t5;
sl@0
   511
  }
sl@0
   512
} {1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8}
sl@0
   513
do_test rowid-10.2 {
sl@0
   514
  execsql {SELECT rowid, a FROM t5 WHERE rowid>=5.5}
sl@0
   515
} {6 6 7 7 8 8}
sl@0
   516
do_test rowid-10.3 {
sl@0
   517
  execsql {SELECT rowid, a FROM t5 WHERE rowid>=5.0}
sl@0
   518
} {5 5 6 6 7 7 8 8}
sl@0
   519
do_test rowid-10.4 {
sl@0
   520
  execsql {SELECT rowid, a FROM t5 WHERE rowid>5.5}
sl@0
   521
} {6 6 7 7 8 8}
sl@0
   522
do_test rowid-10.3.2 {
sl@0
   523
  execsql {SELECT rowid, a FROM t5 WHERE rowid>5.0}
sl@0
   524
} {6 6 7 7 8 8}
sl@0
   525
do_test rowid-10.5 {
sl@0
   526
  execsql {SELECT rowid, a FROM t5 WHERE 5.5<=rowid}
sl@0
   527
} {6 6 7 7 8 8}
sl@0
   528
do_test rowid-10.6 {
sl@0
   529
  execsql {SELECT rowid, a FROM t5 WHERE 5.5<rowid}
sl@0
   530
} {6 6 7 7 8 8}
sl@0
   531
do_test rowid-10.7 {
sl@0
   532
  execsql {SELECT rowid, a FROM t5 WHERE rowid<=5.5}
sl@0
   533
} {1 1 2 2 3 3 4 4 5 5}
sl@0
   534
do_test rowid-10.8 {
sl@0
   535
  execsql {SELECT rowid, a FROM t5 WHERE rowid<5.5}
sl@0
   536
} {1 1 2 2 3 3 4 4 5 5}
sl@0
   537
do_test rowid-10.9 {
sl@0
   538
  execsql {SELECT rowid, a FROM t5 WHERE 5.5>=rowid}
sl@0
   539
} {1 1 2 2 3 3 4 4 5 5}
sl@0
   540
do_test rowid-10.10 {
sl@0
   541
  execsql {SELECT rowid, a FROM t5 WHERE 5.5>rowid}
sl@0
   542
} {1 1 2 2 3 3 4 4 5 5}
sl@0
   543
do_test rowid-10.11 {
sl@0
   544
  execsql {SELECT rowid, a FROM t5 WHERE rowid>=5.5 ORDER BY rowid DESC}
sl@0
   545
} {8 8 7 7 6 6}
sl@0
   546
do_test rowid-10.11.2 {
sl@0
   547
  execsql {SELECT rowid, a FROM t5 WHERE rowid>=5.0 ORDER BY rowid DESC}
sl@0
   548
} {8 8 7 7 6 6 5 5}
sl@0
   549
do_test rowid-10.12 {
sl@0
   550
  execsql {SELECT rowid, a FROM t5 WHERE rowid>5.5 ORDER BY rowid DESC}
sl@0
   551
} {8 8 7 7 6 6}
sl@0
   552
do_test rowid-10.12.2 {
sl@0
   553
  execsql {SELECT rowid, a FROM t5 WHERE rowid>5.0 ORDER BY rowid DESC}
sl@0
   554
} {8 8 7 7 6 6}
sl@0
   555
do_test rowid-10.13 {
sl@0
   556
  execsql {SELECT rowid, a FROM t5 WHERE 5.5<=rowid ORDER BY rowid DESC}
sl@0
   557
} {8 8 7 7 6 6}
sl@0
   558
do_test rowid-10.14 {
sl@0
   559
  execsql {SELECT rowid, a FROM t5 WHERE 5.5<rowid ORDER BY rowid DESC}
sl@0
   560
} {8 8 7 7 6 6}
sl@0
   561
do_test rowid-10.15 {
sl@0
   562
  execsql {SELECT rowid, a FROM t5 WHERE rowid<=5.5 ORDER BY rowid DESC}
sl@0
   563
} {5 5 4 4 3 3 2 2 1 1}
sl@0
   564
do_test rowid-10.16 {
sl@0
   565
  execsql {SELECT rowid, a FROM t5 WHERE rowid<5.5 ORDER BY rowid DESC}
sl@0
   566
} {5 5 4 4 3 3 2 2 1 1}
sl@0
   567
do_test rowid-10.17 {
sl@0
   568
  execsql {SELECT rowid, a FROM t5 WHERE 5.5>=rowid ORDER BY rowid DESC}
sl@0
   569
} {5 5 4 4 3 3 2 2 1 1}
sl@0
   570
do_test rowid-10.18 {
sl@0
   571
  execsql {SELECT rowid, a FROM t5 WHERE 5.5>rowid ORDER BY rowid DESC}
sl@0
   572
} {5 5 4 4 3 3 2 2 1 1}
sl@0
   573
sl@0
   574
do_test rowid-10.30 {
sl@0
   575
  execsql {
sl@0
   576
    CREATE TABLE t6(a);
sl@0
   577
    INSERT INTO t6(rowid,a) SELECT -a,a FROM t5;
sl@0
   578
    SELECT rowid, * FROM t6;
sl@0
   579
  }
sl@0
   580
} {-8 8 -7 7 -6 6 -5 5 -4 4 -3 3 -2 2 -1 1}
sl@0
   581
do_test rowid-10.31.1 {
sl@0
   582
  execsql {SELECT rowid, a FROM t6 WHERE rowid>=-5.5}
sl@0
   583
} {-5 5 -4 4 -3 3 -2 2 -1 1}
sl@0
   584
do_test rowid-10.31.2 {
sl@0
   585
  execsql {SELECT rowid, a FROM t6 WHERE rowid>=-5.0}
sl@0
   586
} {-5 5 -4 4 -3 3 -2 2 -1 1}
sl@0
   587
do_test rowid-10.32.1 {
sl@0
   588
  execsql {SELECT rowid, a FROM t6 WHERE rowid>=-5.5 ORDER BY rowid DESC}
sl@0
   589
} {-1 1 -2 2 -3 3 -4 4 -5 5}
sl@0
   590
do_test rowid-10.32.1 {
sl@0
   591
  execsql {SELECT rowid, a FROM t6 WHERE rowid>=-5.0 ORDER BY rowid DESC}
sl@0
   592
} {-1 1 -2 2 -3 3 -4 4 -5 5}
sl@0
   593
do_test rowid-10.33 {
sl@0
   594
  execsql {SELECT rowid, a FROM t6 WHERE -5.5<=rowid}
sl@0
   595
} {-5 5 -4 4 -3 3 -2 2 -1 1}
sl@0
   596
do_test rowid-10.34 {
sl@0
   597
  execsql {SELECT rowid, a FROM t6 WHERE -5.5<=rowid ORDER BY rowid DESC}
sl@0
   598
} {-1 1 -2 2 -3 3 -4 4 -5 5}
sl@0
   599
do_test rowid-10.35.1 {
sl@0
   600
  execsql {SELECT rowid, a FROM t6 WHERE rowid>-5.5}
sl@0
   601
} {-5 5 -4 4 -3 3 -2 2 -1 1}
sl@0
   602
do_test rowid-10.35.2 {
sl@0
   603
  execsql {SELECT rowid, a FROM t6 WHERE rowid>-5.0}
sl@0
   604
} {-4 4 -3 3 -2 2 -1 1}
sl@0
   605
do_test rowid-10.36.1 {
sl@0
   606
  execsql {SELECT rowid, a FROM t6 WHERE rowid>-5.5 ORDER BY rowid DESC}
sl@0
   607
} {-1 1 -2 2 -3 3 -4 4 -5 5}
sl@0
   608
do_test rowid-10.36.2 {
sl@0
   609
  execsql {SELECT rowid, a FROM t6 WHERE rowid>-5.0 ORDER BY rowid DESC}
sl@0
   610
} {-1 1 -2 2 -3 3 -4 4}
sl@0
   611
do_test rowid-10.37 {
sl@0
   612
  execsql {SELECT rowid, a FROM t6 WHERE -5.5<rowid}
sl@0
   613
} {-5 5 -4 4 -3 3 -2 2 -1 1}
sl@0
   614
do_test rowid-10.38 {
sl@0
   615
  execsql {SELECT rowid, a FROM t6 WHERE -5.5<rowid ORDER BY rowid DESC}
sl@0
   616
} {-1 1 -2 2 -3 3 -4 4 -5 5}
sl@0
   617
do_test rowid-10.39 {
sl@0
   618
  execsql {SELECT rowid, a FROM t6 WHERE rowid<=-5.5}
sl@0
   619
} {-8 8 -7 7 -6 6}
sl@0
   620
do_test rowid-10.40 {
sl@0
   621
  execsql {SELECT rowid, a FROM t6 WHERE rowid<=-5.5 ORDER BY rowid DESC}
sl@0
   622
} {-6 6 -7 7 -8 8}
sl@0
   623
do_test rowid-10.41 {
sl@0
   624
  execsql {SELECT rowid, a FROM t6 WHERE -5.5>=rowid}
sl@0
   625
} {-8 8 -7 7 -6 6}
sl@0
   626
do_test rowid-10.42 {
sl@0
   627
  execsql {SELECT rowid, a FROM t6 WHERE -5.5>=rowid ORDER BY rowid DESC}
sl@0
   628
} {-6 6 -7 7 -8 8}
sl@0
   629
do_test rowid-10.43 {
sl@0
   630
  execsql {SELECT rowid, a FROM t6 WHERE rowid<-5.5}
sl@0
   631
} {-8 8 -7 7 -6 6}
sl@0
   632
do_test rowid-10.44 {
sl@0
   633
  execsql {SELECT rowid, a FROM t6 WHERE rowid<-5.5 ORDER BY rowid DESC}
sl@0
   634
} {-6 6 -7 7 -8 8}
sl@0
   635
do_test rowid-10.44 {
sl@0
   636
  execsql {SELECT rowid, a FROM t6 WHERE -5.5>rowid}
sl@0
   637
} {-8 8 -7 7 -6 6}
sl@0
   638
do_test rowid-10.46 {
sl@0
   639
  execsql {SELECT rowid, a FROM t6 WHERE -5.5>rowid ORDER BY rowid DESC}
sl@0
   640
} {-6 6 -7 7 -8 8}
sl@0
   641
sl@0
   642
# Comparison of rowid against string values.
sl@0
   643
#
sl@0
   644
do_test rowid-11.1 {
sl@0
   645
  execsql {SELECT rowid, a FROM t5 WHERE rowid>'abc'}
sl@0
   646
} {}
sl@0
   647
do_test rowid-11.2 {
sl@0
   648
  execsql {SELECT rowid, a FROM t5 WHERE rowid>='abc'}
sl@0
   649
} {}
sl@0
   650
do_test rowid-11.3 {
sl@0
   651
  execsql {SELECT rowid, a FROM t5 WHERE rowid<'abc'}
sl@0
   652
} {1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8}
sl@0
   653
do_test rowid-11.4 {
sl@0
   654
  execsql {SELECT rowid, a FROM t5 WHERE rowid<='abc'}
sl@0
   655
} {1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8}
sl@0
   656
sl@0
   657
# Test the automatic generation of rowids when the table already contains
sl@0
   658
# a rowid with the maximum value.
sl@0
   659
#
sl@0
   660
# Once the the maximum rowid is taken, rowids are normally chosen at
sl@0
   661
# random.  By by reseting the random number generator, we can cause
sl@0
   662
# the rowid guessing loop to collide with prior rowids, and test the
sl@0
   663
# loop out to its limit of 100 iterations.  After 100 collisions, the
sl@0
   664
# rowid guesser gives up and reports SQLITE_FULL.
sl@0
   665
#
sl@0
   666
do_test rowid-12.1 {
sl@0
   667
  execsql {
sl@0
   668
    CREATE TABLE t7(x INTEGER PRIMARY KEY, y);
sl@0
   669
    INSERT INTO t7 VALUES(9223372036854775807,'a');
sl@0
   670
    SELECT y FROM t7;
sl@0
   671
  }
sl@0
   672
} {a}
sl@0
   673
do_test rowid-12.2 {
sl@0
   674
  db close
sl@0
   675
  sqlite3 db test.db
sl@0
   676
  save_prng_state
sl@0
   677
  execsql {
sl@0
   678
    INSERT INTO t7 VALUES(NULL,'b');
sl@0
   679
    SELECT x, y FROM t7;
sl@0
   680
  }
sl@0
   681
} {1 b 9223372036854775807 a}
sl@0
   682
execsql {INSERT INTO t7 VALUES(2,'y');}
sl@0
   683
for {set i 1} {$i<=101} {incr i} {
sl@0
   684
  do_test rowid-12.3.$i {
sl@0
   685
    restore_prng_state
sl@0
   686
    execsql {
sl@0
   687
      INSERT INTO t7 VALUES(NULL,'x');
sl@0
   688
      INSERT OR IGNORE INTO t7 VALUES(last_insert_rowid()+1,'y');
sl@0
   689
      SELECT count(*) FROM t7 WHERE y=='x';
sl@0
   690
    }
sl@0
   691
  } $i
sl@0
   692
}
sl@0
   693
do_test rowid-12.4 {
sl@0
   694
  restore_prng_state
sl@0
   695
  catchsql {
sl@0
   696
    INSERT INTO t7 VALUES(NULL,'x');
sl@0
   697
  }
sl@0
   698
} {1 {database or disk is full}}
sl@0
   699
sl@0
   700
sl@0
   701
finish_test