os/persistentdata/persistentstorage/sqlite3api/TEST/TclScript/misc2.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
# 2003 June 21
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.
sl@0
    12
#
sl@0
    13
# This file implements tests for miscellanous features that were
sl@0
    14
# left out of other test files.
sl@0
    15
#
sl@0
    16
# $Id: misc2.test,v 1.28 2007/09/12 17:01:45 danielk1977 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
ifcapable {trigger} {
sl@0
    22
# Test for ticket #360
sl@0
    23
#
sl@0
    24
do_test misc2-1.1 {
sl@0
    25
  catchsql {
sl@0
    26
    CREATE TABLE FOO(bar integer);
sl@0
    27
    CREATE TRIGGER foo_insert BEFORE INSERT ON foo BEGIN
sl@0
    28
      SELECT CASE WHEN (NOT new.bar BETWEEN 0 AND 20)
sl@0
    29
             THEN raise(rollback, 'aiieee') END;
sl@0
    30
    END;
sl@0
    31
    INSERT INTO foo(bar) VALUES (1);
sl@0
    32
  }
sl@0
    33
} {0 {}}
sl@0
    34
do_test misc2-1.2 {
sl@0
    35
  catchsql {
sl@0
    36
    INSERT INTO foo(bar) VALUES (111);
sl@0
    37
  }
sl@0
    38
} {1 aiieee}
sl@0
    39
} ;# endif trigger
sl@0
    40
sl@0
    41
# Make sure ROWID works on a view and a subquery.  Ticket #364
sl@0
    42
#
sl@0
    43
do_test misc2-2.1 {
sl@0
    44
  execsql {
sl@0
    45
    CREATE TABLE t1(a,b,c);
sl@0
    46
    INSERT INTO t1 VALUES(1,2,3);
sl@0
    47
    CREATE TABLE t2(a,b,c);
sl@0
    48
    INSERT INTO t2 VALUES(7,8,9);
sl@0
    49
  }
sl@0
    50
} {}
sl@0
    51
ifcapable subquery {
sl@0
    52
  do_test misc2-2.2 {
sl@0
    53
    execsql {
sl@0
    54
      SELECT rowid, * FROM (SELECT * FROM t1, t2);
sl@0
    55
    }
sl@0
    56
  } {{} 1 2 3 7 8 9}
sl@0
    57
}
sl@0
    58
ifcapable view {
sl@0
    59
  do_test misc2-2.3 {
sl@0
    60
    execsql {
sl@0
    61
      CREATE VIEW v1 AS SELECT * FROM t1, t2;
sl@0
    62
      SELECT rowid, * FROM v1;
sl@0
    63
    }
sl@0
    64
  } {{} 1 2 3 7 8 9}
sl@0
    65
} ;# ifcapable view
sl@0
    66
sl@0
    67
# Ticket #2002 and #1952.
sl@0
    68
ifcapable subquery {
sl@0
    69
  do_test misc2-2.4 {
sl@0
    70
    execsql2 {
sl@0
    71
      SELECT * FROM (SELECT a, b AS 'a', c AS 'a', 4 AS 'a' FROM t1)
sl@0
    72
    }
sl@0
    73
  } {a 1 a:1 2 a:2 3 a:3 4}
sl@0
    74
}
sl@0
    75
sl@0
    76
# Check name binding precedence.  Ticket #387
sl@0
    77
#
sl@0
    78
do_test misc2-3.1 {
sl@0
    79
  catchsql {
sl@0
    80
    SELECT t1.b+t2.b AS a, t1.a, t2.a FROM t1, t2 WHERE a==10
sl@0
    81
  }
sl@0
    82
} {1 {ambiguous column name: a}}
sl@0
    83
sl@0
    84
# Make sure 32-bit integer overflow is handled properly in queries.
sl@0
    85
# ticket #408
sl@0
    86
#
sl@0
    87
do_test misc2-4.1 {
sl@0
    88
  execsql {
sl@0
    89
    INSERT INTO t1 VALUES(4000000000,'a','b');
sl@0
    90
    SELECT a FROM t1 WHERE a>1;
sl@0
    91
  }
sl@0
    92
} {4000000000}
sl@0
    93
do_test misc2-4.2 {
sl@0
    94
  execsql {
sl@0
    95
    INSERT INTO t1 VALUES(2147483648,'b2','c2');
sl@0
    96
    INSERT INTO t1 VALUES(2147483647,'b3','c3');
sl@0
    97
    SELECT a FROM t1 WHERE a>2147483647;
sl@0
    98
  }
sl@0
    99
} {4000000000 2147483648}
sl@0
   100
do_test misc2-4.3 {
sl@0
   101
  execsql {
sl@0
   102
    SELECT a FROM t1 WHERE a<2147483648;
sl@0
   103
  }
sl@0
   104
} {1 2147483647}
sl@0
   105
do_test misc2-4.4 {
sl@0
   106
  execsql {
sl@0
   107
    SELECT a FROM t1 WHERE a<=2147483648;
sl@0
   108
  }
sl@0
   109
} {1 2147483648 2147483647}
sl@0
   110
do_test misc2-4.5 {
sl@0
   111
  execsql {
sl@0
   112
    SELECT a FROM t1 WHERE a<10000000000;
sl@0
   113
  }
sl@0
   114
} {1 4000000000 2147483648 2147483647}
sl@0
   115
do_test misc2-4.6 {
sl@0
   116
  execsql {
sl@0
   117
    SELECT a FROM t1 WHERE a<1000000000000 ORDER BY 1;
sl@0
   118
  }
sl@0
   119
} {1 2147483647 2147483648 4000000000}
sl@0
   120
sl@0
   121
# There were some issues with expanding a SrcList object using a call
sl@0
   122
# to sqliteSrcListAppend() if the SrcList had previously been duplicated
sl@0
   123
# using a call to sqliteSrcListDup().  Ticket #416.  The following test
sl@0
   124
# makes sure the problem has been fixed.
sl@0
   125
#
sl@0
   126
ifcapable view {
sl@0
   127
do_test misc2-5.1 {
sl@0
   128
  execsql {
sl@0
   129
    CREATE TABLE x(a,b);
sl@0
   130
    CREATE VIEW y AS 
sl@0
   131
      SELECT x1.b AS p, x2.b AS q FROM x AS x1, x AS x2 WHERE x1.a=x2.a;
sl@0
   132
    CREATE VIEW z AS
sl@0
   133
      SELECT y1.p, y2.p FROM y AS y1, y AS y2 WHERE y1.q=y2.q;
sl@0
   134
    SELECT * from z;
sl@0
   135
  }
sl@0
   136
} {}
sl@0
   137
}
sl@0
   138
sl@0
   139
# Make sure we can open a database with an empty filename.  What this
sl@0
   140
# does is store the database in a temporary file that is deleted when
sl@0
   141
# the database is closed.  Ticket #432.
sl@0
   142
#
sl@0
   143
do_test misc2-6.1 {
sl@0
   144
  db close
sl@0
   145
  sqlite3 db {}
sl@0
   146
  execsql {
sl@0
   147
    CREATE TABLE t1(a,b);
sl@0
   148
    INSERT INTO t1 VALUES(1,2);
sl@0
   149
    SELECT * FROM t1;
sl@0
   150
  }
sl@0
   151
} {1 2}
sl@0
   152
sl@0
   153
# Make sure we get an error message (not a segfault) on an attempt to
sl@0
   154
# update a table from within the callback of a select on that same
sl@0
   155
# table.
sl@0
   156
#
sl@0
   157
# 2006-08-16:  This has changed.  It is now permitted to update
sl@0
   158
# the table being SELECTed from within the callback of the query.
sl@0
   159
#
sl@0
   160
ifcapable tclvar {
sl@0
   161
  do_test misc2-7.1 {
sl@0
   162
    db close
sl@0
   163
    file delete -force test.db
sl@0
   164
    sqlite3 db test.db
sl@0
   165
    execsql {
sl@0
   166
      CREATE TABLE t1(x);
sl@0
   167
      INSERT INTO t1 VALUES(1);
sl@0
   168
      INSERT INTO t1 VALUES(2);
sl@0
   169
      INSERT INTO t1 VALUES(3);
sl@0
   170
      SELECT * FROM t1;
sl@0
   171
    }
sl@0
   172
  } {1 2 3}
sl@0
   173
  do_test misc2-7.2 {
sl@0
   174
    set rc [catch {
sl@0
   175
      db eval {SELECT rowid FROM t1} {} {
sl@0
   176
        db eval "DELETE FROM t1 WHERE rowid=$rowid"
sl@0
   177
      }
sl@0
   178
    } msg]
sl@0
   179
    lappend rc $msg
sl@0
   180
  } {0 {}}
sl@0
   181
  do_test misc2-7.3 {
sl@0
   182
    execsql {SELECT * FROM t1}
sl@0
   183
  } {}
sl@0
   184
  do_test misc2-7.4 {
sl@0
   185
    execsql {
sl@0
   186
      DELETE FROM t1;
sl@0
   187
      INSERT INTO t1 VALUES(1);
sl@0
   188
      INSERT INTO t1 VALUES(2);
sl@0
   189
      INSERT INTO t1 VALUES(3);
sl@0
   190
      INSERT INTO t1 VALUES(4);
sl@0
   191
    }
sl@0
   192
    db eval {SELECT rowid, x FROM t1} {
sl@0
   193
      if {$x & 1} {
sl@0
   194
        db eval {DELETE FROM t1 WHERE rowid=$rowid}
sl@0
   195
      }
sl@0
   196
    }
sl@0
   197
    execsql {SELECT * FROM t1}
sl@0
   198
  } {2 4}
sl@0
   199
  do_test misc2-7.5 {
sl@0
   200
    execsql {
sl@0
   201
      DELETE FROM t1;
sl@0
   202
      INSERT INTO t1 VALUES(1);
sl@0
   203
      INSERT INTO t1 VALUES(2);
sl@0
   204
      INSERT INTO t1 VALUES(3);
sl@0
   205
      INSERT INTO t1 VALUES(4);
sl@0
   206
    }
sl@0
   207
    db eval {SELECT rowid, x FROM t1} {
sl@0
   208
      if {$x & 1} {
sl@0
   209
        db eval {DELETE FROM t1 WHERE rowid=$rowid+1}
sl@0
   210
      }
sl@0
   211
    }
sl@0
   212
    execsql {SELECT * FROM t1}
sl@0
   213
  } {1 3}
sl@0
   214
  do_test misc2-7.6 {
sl@0
   215
    execsql {
sl@0
   216
      DELETE FROM t1;
sl@0
   217
      INSERT INTO t1 VALUES(1);
sl@0
   218
      INSERT INTO t1 VALUES(2);
sl@0
   219
      INSERT INTO t1 VALUES(3);
sl@0
   220
      INSERT INTO t1 VALUES(4);
sl@0
   221
    }
sl@0
   222
    db eval {SELECT rowid, x FROM t1} {
sl@0
   223
      if {$x & 1} {
sl@0
   224
        db eval {DELETE FROM t1}
sl@0
   225
      }
sl@0
   226
    }
sl@0
   227
    execsql {SELECT * FROM t1}
sl@0
   228
  } {}
sl@0
   229
  do_test misc2-7.7 {
sl@0
   230
    execsql {
sl@0
   231
      DELETE FROM t1;
sl@0
   232
      INSERT INTO t1 VALUES(1);
sl@0
   233
      INSERT INTO t1 VALUES(2);
sl@0
   234
      INSERT INTO t1 VALUES(3);
sl@0
   235
      INSERT INTO t1 VALUES(4);
sl@0
   236
    }
sl@0
   237
    db eval {SELECT rowid, x FROM t1} {
sl@0
   238
      if {$x & 1} {
sl@0
   239
        db eval {UPDATE t1 SET x=x+100 WHERE rowid=$rowid}
sl@0
   240
      }
sl@0
   241
    }
sl@0
   242
    execsql {SELECT * FROM t1}
sl@0
   243
  } {101 2 103 4}
sl@0
   244
  do_test misc2-7.8 {
sl@0
   245
    execsql {
sl@0
   246
      DELETE FROM t1;
sl@0
   247
      INSERT INTO t1 VALUES(1);
sl@0
   248
    }
sl@0
   249
    db eval {SELECT rowid, x FROM t1} {
sl@0
   250
      if {$x<10} {
sl@0
   251
        db eval {INSERT INTO t1 VALUES($x+1)}
sl@0
   252
      }
sl@0
   253
    }
sl@0
   254
    execsql {SELECT * FROM t1}
sl@0
   255
  } {1 2 3 4 5 6 7 8 9 10}
sl@0
   256
  
sl@0
   257
  # Repeat the tests 7.1 through 7.8 about but this time do the SELECTs
sl@0
   258
  # in reverse order so that we exercise the sqlite3BtreePrev() routine
sl@0
   259
  # instead of sqlite3BtreeNext()
sl@0
   260
  #
sl@0
   261
  do_test misc2-7.11 {
sl@0
   262
    db close
sl@0
   263
    file delete -force test.db
sl@0
   264
    sqlite3 db test.db
sl@0
   265
    execsql {
sl@0
   266
      CREATE TABLE t1(x);
sl@0
   267
      INSERT INTO t1 VALUES(1);
sl@0
   268
      INSERT INTO t1 VALUES(2);
sl@0
   269
      INSERT INTO t1 VALUES(3);
sl@0
   270
      SELECT * FROM t1;
sl@0
   271
    }
sl@0
   272
  } {1 2 3}
sl@0
   273
  do_test misc2-7.12 {
sl@0
   274
    set rc [catch {
sl@0
   275
      db eval {SELECT rowid FROM t1 ORDER BY rowid DESC} {} {
sl@0
   276
        db eval "DELETE FROM t1 WHERE rowid=$rowid"
sl@0
   277
      }
sl@0
   278
    } msg]
sl@0
   279
    lappend rc $msg
sl@0
   280
  } {0 {}}
sl@0
   281
  do_test misc2-7.13 {
sl@0
   282
    execsql {SELECT * FROM t1}
sl@0
   283
  } {}
sl@0
   284
  do_test misc2-7.14 {
sl@0
   285
    execsql {
sl@0
   286
      DELETE FROM t1;
sl@0
   287
      INSERT INTO t1 VALUES(1);
sl@0
   288
      INSERT INTO t1 VALUES(2);
sl@0
   289
      INSERT INTO t1 VALUES(3);
sl@0
   290
      INSERT INTO t1 VALUES(4);
sl@0
   291
    }
sl@0
   292
    db eval {SELECT rowid, x FROM t1 ORDER BY rowid DESC} {
sl@0
   293
      if {$x & 1} {
sl@0
   294
        db eval {DELETE FROM t1 WHERE rowid=$rowid}
sl@0
   295
      }
sl@0
   296
    }
sl@0
   297
    execsql {SELECT * FROM t1}
sl@0
   298
  } {2 4}
sl@0
   299
  do_test misc2-7.15 {
sl@0
   300
    execsql {
sl@0
   301
      DELETE FROM t1;
sl@0
   302
      INSERT INTO t1 VALUES(1);
sl@0
   303
      INSERT INTO t1 VALUES(2);
sl@0
   304
      INSERT INTO t1 VALUES(3);
sl@0
   305
      INSERT INTO t1 VALUES(4);
sl@0
   306
    }
sl@0
   307
    db eval {SELECT rowid, x FROM t1} {
sl@0
   308
      if {$x & 1} {
sl@0
   309
        db eval {DELETE FROM t1 WHERE rowid=$rowid+1}
sl@0
   310
      }
sl@0
   311
    }
sl@0
   312
    execsql {SELECT * FROM t1}
sl@0
   313
  } {1 3}
sl@0
   314
  do_test misc2-7.16 {
sl@0
   315
    execsql {
sl@0
   316
      DELETE FROM t1;
sl@0
   317
      INSERT INTO t1 VALUES(1);
sl@0
   318
      INSERT INTO t1 VALUES(2);
sl@0
   319
      INSERT INTO t1 VALUES(3);
sl@0
   320
      INSERT INTO t1 VALUES(4);
sl@0
   321
    }
sl@0
   322
    db eval {SELECT rowid, x FROM t1 ORDER BY rowid DESC} {
sl@0
   323
      if {$x & 1} {
sl@0
   324
        db eval {DELETE FROM t1}
sl@0
   325
      }
sl@0
   326
    }
sl@0
   327
    execsql {SELECT * FROM t1}
sl@0
   328
  } {}
sl@0
   329
  do_test misc2-7.17 {
sl@0
   330
    execsql {
sl@0
   331
      DELETE FROM t1;
sl@0
   332
      INSERT INTO t1 VALUES(1);
sl@0
   333
      INSERT INTO t1 VALUES(2);
sl@0
   334
      INSERT INTO t1 VALUES(3);
sl@0
   335
      INSERT INTO t1 VALUES(4);
sl@0
   336
    }
sl@0
   337
    db eval {SELECT rowid, x FROM t1 ORDER BY rowid DESC} {
sl@0
   338
      if {$x & 1} {
sl@0
   339
        db eval {UPDATE t1 SET x=x+100 WHERE rowid=$rowid}
sl@0
   340
      }
sl@0
   341
    }
sl@0
   342
    execsql {SELECT * FROM t1}
sl@0
   343
  } {101 2 103 4}
sl@0
   344
  do_test misc2-7.18 {
sl@0
   345
    execsql {
sl@0
   346
      DELETE FROM t1;
sl@0
   347
      INSERT INTO t1(rowid,x) VALUES(10,10);
sl@0
   348
    }
sl@0
   349
    db eval {SELECT rowid, x FROM t1 ORDER BY rowid DESC} {
sl@0
   350
      if {$x>1} {
sl@0
   351
        db eval {INSERT INTO t1(rowid,x) VALUES($x-1,$x-1)}
sl@0
   352
      }
sl@0
   353
    }
sl@0
   354
    execsql {SELECT * FROM t1}
sl@0
   355
  } {1 2 3 4 5 6 7 8 9 10}
sl@0
   356
}
sl@0
   357
sl@0
   358
db close
sl@0
   359
file delete -force test.db
sl@0
   360
sqlite3 db test.db
sl@0
   361
sl@0
   362
# Ticket #453.  If the SQL ended with "-", the tokenizer was calling that
sl@0
   363
# an incomplete token, which caused problem.  The solution was to just call
sl@0
   364
# it a minus sign.
sl@0
   365
#
sl@0
   366
do_test misc2-8.1 {
sl@0
   367
  catchsql {-}
sl@0
   368
} {1 {near "-": syntax error}}
sl@0
   369
sl@0
   370
# Ticket #513.  Make sure the VDBE stack does not grow on a 3-way join.
sl@0
   371
#
sl@0
   372
ifcapable tempdb {
sl@0
   373
  do_test misc2-9.1 {
sl@0
   374
    execsql {
sl@0
   375
      BEGIN;
sl@0
   376
      CREATE TABLE counts(n INTEGER PRIMARY KEY);
sl@0
   377
      INSERT INTO counts VALUES(0);
sl@0
   378
      INSERT INTO counts VALUES(1);
sl@0
   379
      INSERT INTO counts SELECT n+2 FROM counts;
sl@0
   380
      INSERT INTO counts SELECT n+4 FROM counts;
sl@0
   381
      INSERT INTO counts SELECT n+8 FROM counts;
sl@0
   382
      COMMIT;
sl@0
   383
  
sl@0
   384
      CREATE TEMP TABLE x AS
sl@0
   385
      SELECT dim1.n, dim2.n, dim3.n
sl@0
   386
      FROM counts AS dim1, counts AS dim2, counts AS dim3
sl@0
   387
      WHERE dim1.n<10 AND dim2.n<10 AND dim3.n<10;
sl@0
   388
  
sl@0
   389
      SELECT count(*) FROM x;
sl@0
   390
    }
sl@0
   391
  } {1000}
sl@0
   392
  do_test misc2-9.2 {
sl@0
   393
    execsql {
sl@0
   394
      DROP TABLE x;
sl@0
   395
      CREATE TEMP TABLE x AS
sl@0
   396
      SELECT dim1.n, dim2.n, dim3.n
sl@0
   397
      FROM counts AS dim1, counts AS dim2, counts AS dim3
sl@0
   398
      WHERE dim1.n>=6 AND dim2.n>=6 AND dim3.n>=6;
sl@0
   399
  
sl@0
   400
      SELECT count(*) FROM x;
sl@0
   401
    }
sl@0
   402
  } {1000}
sl@0
   403
  do_test misc2-9.3 {
sl@0
   404
    execsql {
sl@0
   405
      DROP TABLE x;
sl@0
   406
      CREATE TEMP TABLE x AS
sl@0
   407
      SELECT dim1.n, dim2.n, dim3.n, dim4.n
sl@0
   408
      FROM counts AS dim1, counts AS dim2, counts AS dim3, counts AS dim4
sl@0
   409
      WHERE dim1.n<5 AND dim2.n<5 AND dim3.n<5 AND dim4.n<5;
sl@0
   410
  
sl@0
   411
      SELECT count(*) FROM x;
sl@0
   412
    }
sl@0
   413
  } [expr 5*5*5*5]
sl@0
   414
}
sl@0
   415
sl@0
   416
# Ticket #1229.  Sometimes when a "NEW.X" appears in a SELECT without
sl@0
   417
# a FROM clause deep within a trigger, the code generator is unable to
sl@0
   418
# trace the NEW.X back to an original table and thus figure out its
sl@0
   419
# declared datatype.
sl@0
   420
#
sl@0
   421
# The SQL code below was causing a segfault.
sl@0
   422
#
sl@0
   423
ifcapable subquery&&trigger {
sl@0
   424
  do_test misc2-10.1 {
sl@0
   425
    execsql {
sl@0
   426
      CREATE TABLE t1229(x);
sl@0
   427
      CREATE TRIGGER r1229 BEFORE INSERT ON t1229 BEGIN
sl@0
   428
        INSERT INTO t1229 SELECT y FROM (SELECT new.x y);
sl@0
   429
      END;
sl@0
   430
      INSERT INTO t1229 VALUES(1);
sl@0
   431
    }
sl@0
   432
  } {}
sl@0
   433
}
sl@0
   434
sl@0
   435
finish_test