os/persistentdata/persistentstorage/sqlite3api/TEST/TclScript/schema.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 Jan 24
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 tests the various conditions under which an SQLITE_SCHEMA
sl@0
    14
# error should be returned.
sl@0
    15
#
sl@0
    16
# $Id: schema.test,v 1.8 2007/10/09 08:29:33 danielk1977 Exp $
sl@0
    17
sl@0
    18
#---------------------------------------------------------------------
sl@0
    19
# When any of the following types of SQL statements or actions are 
sl@0
    20
# executed, all pre-compiled statements are invalidated. An attempt
sl@0
    21
# to execute an invalidated statement always returns SQLITE_SCHEMA.
sl@0
    22
#
sl@0
    23
# CREATE/DROP TABLE...................................schema-1.*
sl@0
    24
# CREATE/DROP VIEW....................................schema-2.*
sl@0
    25
# CREATE/DROP TRIGGER.................................schema-3.*
sl@0
    26
# CREATE/DROP INDEX...................................schema-4.*
sl@0
    27
# DETACH..............................................schema-5.*
sl@0
    28
# Deleting a user-function............................schema-6.*
sl@0
    29
# Deleting a collation sequence.......................schema-7.*
sl@0
    30
# Setting or changing the authorization function......schema-8.*
sl@0
    31
# Rollback of a DDL statement.........................schema-12.*
sl@0
    32
#
sl@0
    33
# Test cases schema-9.* and schema-10.* test some specific bugs
sl@0
    34
# that came up during development.
sl@0
    35
#
sl@0
    36
# Test cases schema-11.* test that it is impossible to delete or
sl@0
    37
# change a collation sequence or user-function while SQL statements
sl@0
    38
# are executing. Adding new collations or functions is allowed.
sl@0
    39
#
sl@0
    40
sl@0
    41
set testdir [file dirname $argv0]
sl@0
    42
source $testdir/tester.tcl
sl@0
    43
sl@0
    44
do_test schema-1.1 {
sl@0
    45
  set ::STMT [sqlite3_prepare $::DB {SELECT * FROM sqlite_master} -1 TAIL]
sl@0
    46
  execsql {
sl@0
    47
    CREATE TABLE abc(a, b, c);
sl@0
    48
  }
sl@0
    49
  sqlite3_step $::STMT
sl@0
    50
} {SQLITE_ERROR}
sl@0
    51
do_test schema-1.2 {
sl@0
    52
  sqlite3_finalize $::STMT
sl@0
    53
} {SQLITE_SCHEMA}
sl@0
    54
do_test schema-1.3 {
sl@0
    55
  set ::STMT [sqlite3_prepare $::DB {SELECT * FROM sqlite_master} -1 TAIL]
sl@0
    56
  execsql {
sl@0
    57
    DROP TABLE abc;
sl@0
    58
  }
sl@0
    59
  sqlite3_step $::STMT
sl@0
    60
} {SQLITE_ERROR}
sl@0
    61
do_test schema-1.4 {
sl@0
    62
  sqlite3_finalize $::STMT
sl@0
    63
} {SQLITE_SCHEMA}
sl@0
    64
sl@0
    65
ifcapable view {
sl@0
    66
  do_test schema-2.1 {
sl@0
    67
    set ::STMT [sqlite3_prepare $::DB {SELECT * FROM sqlite_master} -1 TAIL]
sl@0
    68
    execsql {
sl@0
    69
      CREATE VIEW v1 AS SELECT * FROM sqlite_master;
sl@0
    70
    }
sl@0
    71
    sqlite3_step $::STMT
sl@0
    72
  } {SQLITE_ERROR}
sl@0
    73
  do_test schema-2.2 {
sl@0
    74
    sqlite3_finalize $::STMT
sl@0
    75
  } {SQLITE_SCHEMA}
sl@0
    76
  do_test schema-2.3 {
sl@0
    77
    set ::STMT [sqlite3_prepare $::DB {SELECT * FROM sqlite_master} -1 TAIL]
sl@0
    78
    execsql {
sl@0
    79
      DROP VIEW v1;
sl@0
    80
    }
sl@0
    81
    sqlite3_step $::STMT
sl@0
    82
  } {SQLITE_ERROR}
sl@0
    83
  do_test schema-2.4 {
sl@0
    84
    sqlite3_finalize $::STMT
sl@0
    85
  } {SQLITE_SCHEMA}
sl@0
    86
}
sl@0
    87
sl@0
    88
ifcapable trigger {
sl@0
    89
  do_test schema-3.1 {
sl@0
    90
    execsql {
sl@0
    91
      CREATE TABLE abc(a, b, c);
sl@0
    92
    }
sl@0
    93
    set ::STMT [sqlite3_prepare $::DB {SELECT * FROM sqlite_master} -1 TAIL]
sl@0
    94
    execsql {
sl@0
    95
      CREATE TRIGGER abc_trig AFTER INSERT ON abc BEGIN
sl@0
    96
        SELECT 1, 2, 3;
sl@0
    97
      END;
sl@0
    98
    }
sl@0
    99
    sqlite3_step $::STMT
sl@0
   100
  } {SQLITE_ERROR}
sl@0
   101
  do_test schema-3.2 {
sl@0
   102
    sqlite3_finalize $::STMT
sl@0
   103
  } {SQLITE_SCHEMA}
sl@0
   104
  do_test schema-3.3 {
sl@0
   105
    set ::STMT [sqlite3_prepare $::DB {SELECT * FROM sqlite_master} -1 TAIL]
sl@0
   106
    execsql {
sl@0
   107
      DROP TRIGGER abc_trig;
sl@0
   108
    }
sl@0
   109
    sqlite3_step $::STMT
sl@0
   110
  } {SQLITE_ERROR}
sl@0
   111
  do_test schema-3.4 {
sl@0
   112
    sqlite3_finalize $::STMT
sl@0
   113
  } {SQLITE_SCHEMA}
sl@0
   114
}
sl@0
   115
sl@0
   116
do_test schema-4.1 {
sl@0
   117
  catchsql {
sl@0
   118
    CREATE TABLE abc(a, b, c);
sl@0
   119
  }
sl@0
   120
  set ::STMT [sqlite3_prepare $::DB {SELECT * FROM sqlite_master} -1 TAIL]
sl@0
   121
  execsql {
sl@0
   122
    CREATE INDEX abc_index ON abc(a);
sl@0
   123
  }
sl@0
   124
  sqlite3_step $::STMT
sl@0
   125
} {SQLITE_ERROR}
sl@0
   126
do_test schema-4.2 {
sl@0
   127
  sqlite3_finalize $::STMT
sl@0
   128
} {SQLITE_SCHEMA}
sl@0
   129
do_test schema-4.3 {
sl@0
   130
  set ::STMT [sqlite3_prepare $::DB {SELECT * FROM sqlite_master} -1 TAIL]
sl@0
   131
  execsql {
sl@0
   132
    DROP INDEX abc_index;
sl@0
   133
  }
sl@0
   134
  sqlite3_step $::STMT
sl@0
   135
} {SQLITE_ERROR}
sl@0
   136
do_test schema-4.4 {
sl@0
   137
  sqlite3_finalize $::STMT
sl@0
   138
} {SQLITE_SCHEMA}
sl@0
   139
sl@0
   140
#---------------------------------------------------------------------
sl@0
   141
# Tests 5.1 to 5.4 check that prepared statements are invalidated when
sl@0
   142
# a database is DETACHed (but not when one is ATTACHed).
sl@0
   143
#
sl@0
   144
ifcapable attach {
sl@0
   145
  do_test schema-5.1 {
sl@0
   146
    set sql {SELECT * FROM abc;}
sl@0
   147
    set ::STMT [sqlite3_prepare $::DB $sql -1 TAIL]
sl@0
   148
    execsql {
sl@0
   149
      ATTACH 'test2.db' AS aux;
sl@0
   150
    }
sl@0
   151
    sqlite3_step $::STMT
sl@0
   152
  } {SQLITE_DONE}
sl@0
   153
  do_test schema-5.2 {
sl@0
   154
    sqlite3_reset $::STMT
sl@0
   155
  } {SQLITE_OK}
sl@0
   156
  do_test schema-5.3 {
sl@0
   157
    execsql {
sl@0
   158
      DETACH aux;
sl@0
   159
    }
sl@0
   160
    sqlite3_step $::STMT
sl@0
   161
  } {SQLITE_ERROR}
sl@0
   162
  do_test schema-5.4 {
sl@0
   163
    sqlite3_finalize $::STMT
sl@0
   164
  } {SQLITE_SCHEMA}
sl@0
   165
}
sl@0
   166
sl@0
   167
#---------------------------------------------------------------------
sl@0
   168
# Tests 6.* check that prepared statements are invalidated when
sl@0
   169
# a user-function is deleted (but not when one is added).
sl@0
   170
do_test schema-6.1 {
sl@0
   171
  set sql {SELECT * FROM abc;}
sl@0
   172
  set ::STMT [sqlite3_prepare $::DB $sql -1 TAIL]
sl@0
   173
  db function hello_function {}
sl@0
   174
  sqlite3_step $::STMT
sl@0
   175
} {SQLITE_DONE}
sl@0
   176
do_test schema-6.2 {
sl@0
   177
  sqlite3_reset $::STMT
sl@0
   178
} {SQLITE_OK}
sl@0
   179
do_test schema-6.3 {
sl@0
   180
  sqlite_delete_function $::DB hello_function
sl@0
   181
  sqlite3_step $::STMT
sl@0
   182
} {SQLITE_ERROR}
sl@0
   183
do_test schema-6.4 {
sl@0
   184
  sqlite3_finalize $::STMT
sl@0
   185
} {SQLITE_SCHEMA}
sl@0
   186
sl@0
   187
#---------------------------------------------------------------------
sl@0
   188
# Tests 7.* check that prepared statements are invalidated when
sl@0
   189
# a collation sequence is deleted (but not when one is added).
sl@0
   190
#
sl@0
   191
ifcapable utf16 {
sl@0
   192
  do_test schema-7.1 {
sl@0
   193
    set sql {SELECT * FROM abc;}
sl@0
   194
    set ::STMT [sqlite3_prepare $::DB $sql -1 TAIL]
sl@0
   195
    add_test_collate $::DB 1 1 1
sl@0
   196
    sqlite3_step $::STMT
sl@0
   197
  } {SQLITE_DONE}
sl@0
   198
  do_test schema-7.2 {
sl@0
   199
    sqlite3_reset $::STMT
sl@0
   200
  } {SQLITE_OK}
sl@0
   201
  do_test schema-7.3 {
sl@0
   202
    add_test_collate $::DB 0 0 0 
sl@0
   203
    sqlite3_step $::STMT
sl@0
   204
  } {SQLITE_ERROR}
sl@0
   205
  do_test schema-7.4 {
sl@0
   206
    sqlite3_finalize $::STMT
sl@0
   207
  } {SQLITE_SCHEMA}
sl@0
   208
}
sl@0
   209
sl@0
   210
#---------------------------------------------------------------------
sl@0
   211
# Tests 8.1 and 8.2 check that prepared statements are invalidated when
sl@0
   212
# the authorization function is set.
sl@0
   213
#
sl@0
   214
ifcapable auth {
sl@0
   215
  do_test schema-8.1 {
sl@0
   216
    set ::STMT [sqlite3_prepare $::DB {SELECT * FROM sqlite_master} -1 TAIL]
sl@0
   217
    db auth {}
sl@0
   218
    sqlite3_step $::STMT
sl@0
   219
  } {SQLITE_ERROR}
sl@0
   220
  do_test schema-8.3 {
sl@0
   221
    sqlite3_finalize $::STMT
sl@0
   222
  } {SQLITE_SCHEMA}
sl@0
   223
}
sl@0
   224
sl@0
   225
#---------------------------------------------------------------------
sl@0
   226
# schema-9.1: Test that if a table is dropped by one database connection, 
sl@0
   227
#             other database connections are aware of the schema change.
sl@0
   228
# schema-9.2: Test that if a view is dropped by one database connection,
sl@0
   229
#             other database connections are aware of the schema change.
sl@0
   230
#
sl@0
   231
do_test schema-9.1 {
sl@0
   232
  sqlite3 db2 test.db
sl@0
   233
  execsql {
sl@0
   234
    DROP TABLE abc;
sl@0
   235
  } db2
sl@0
   236
  db2 close
sl@0
   237
  catchsql {
sl@0
   238
    SELECT * FROM abc;
sl@0
   239
  }
sl@0
   240
} {1 {no such table: abc}}
sl@0
   241
execsql {
sl@0
   242
  CREATE TABLE abc(a, b, c);
sl@0
   243
}
sl@0
   244
ifcapable view {
sl@0
   245
  do_test schema-9.2 {
sl@0
   246
    execsql {
sl@0
   247
      CREATE VIEW abcview AS SELECT * FROM abc;
sl@0
   248
    }
sl@0
   249
    sqlite3 db2 test.db
sl@0
   250
    execsql {
sl@0
   251
      DROP VIEW abcview;
sl@0
   252
    } db2
sl@0
   253
    db2 close
sl@0
   254
    catchsql {
sl@0
   255
      SELECT * FROM abcview;
sl@0
   256
    }
sl@0
   257
  } {1 {no such table: abcview}}
sl@0
   258
}
sl@0
   259
sl@0
   260
#---------------------------------------------------------------------
sl@0
   261
# Test that if a CREATE TABLE statement fails because there are other
sl@0
   262
# btree cursors open on the same database file it does not corrupt
sl@0
   263
# the sqlite_master table.
sl@0
   264
#
sl@0
   265
# 2007-05-02: These tests have been overcome by events.  Open btree
sl@0
   266
# cursors no longer block CREATE TABLE.  But there is no reason not
sl@0
   267
# to keep the tests in the test suite.
sl@0
   268
#
sl@0
   269
do_test schema-10.1 {
sl@0
   270
  execsql {
sl@0
   271
    INSERT INTO abc VALUES(1, 2, 3);
sl@0
   272
  }
sl@0
   273
  set sql {SELECT * FROM abc}
sl@0
   274
  set ::STMT [sqlite3_prepare $::DB $sql -1 TAIL]
sl@0
   275
  sqlite3_step $::STMT
sl@0
   276
} {SQLITE_ROW}
sl@0
   277
do_test schema-10.2 {
sl@0
   278
  catchsql {
sl@0
   279
    CREATE TABLE t2(a, b, c);
sl@0
   280
  }
sl@0
   281
} {0 {}}
sl@0
   282
do_test schema-10.3 {
sl@0
   283
  sqlite3_finalize $::STMT
sl@0
   284
} {SQLITE_OK}
sl@0
   285
do_test schema-10.4 {
sl@0
   286
  sqlite3 db2 test.db
sl@0
   287
  execsql {
sl@0
   288
    SELECT * FROM abc
sl@0
   289
  } db2
sl@0
   290
} {1 2 3}
sl@0
   291
do_test schema-10.5 {
sl@0
   292
  db2 close
sl@0
   293
} {}
sl@0
   294
sl@0
   295
#---------------------------------------------------------------------
sl@0
   296
# Attempting to delete or replace a user-function or collation sequence 
sl@0
   297
# while there are active statements returns an SQLITE_BUSY error.
sl@0
   298
#
sl@0
   299
# schema-11.1 - 11.4: User function.
sl@0
   300
# schema-11.5 - 11.8: Collation sequence.
sl@0
   301
#
sl@0
   302
do_test schema-11.1 {
sl@0
   303
  db function tstfunc {}
sl@0
   304
  set sql {SELECT * FROM abc}
sl@0
   305
  set ::STMT [sqlite3_prepare $::DB $sql -1 TAIL]
sl@0
   306
  sqlite3_step $::STMT
sl@0
   307
} {SQLITE_ROW}
sl@0
   308
do_test schema-11.2 {
sl@0
   309
  sqlite_delete_function $::DB tstfunc
sl@0
   310
} {SQLITE_BUSY}
sl@0
   311
do_test schema-11.3 {
sl@0
   312
  set rc [catch {
sl@0
   313
    db function tstfunc {}
sl@0
   314
  } msg]
sl@0
   315
  list $rc $msg
sl@0
   316
} {1 {Unable to delete/modify user-function due to active statements}}
sl@0
   317
do_test schema-11.4 {
sl@0
   318
  sqlite3_finalize $::STMT
sl@0
   319
} {SQLITE_OK}
sl@0
   320
do_test schema-11.5 {
sl@0
   321
  db collate tstcollate {}
sl@0
   322
  set sql {SELECT * FROM abc}
sl@0
   323
  set ::STMT [sqlite3_prepare $::DB $sql -1 TAIL]
sl@0
   324
  sqlite3_step $::STMT
sl@0
   325
} {SQLITE_ROW}
sl@0
   326
do_test schema-11.6 {
sl@0
   327
  sqlite_delete_collation $::DB tstcollate
sl@0
   328
} {SQLITE_BUSY}
sl@0
   329
do_test schema-11.7 {
sl@0
   330
  set rc [catch {
sl@0
   331
    db collate tstcollate {}
sl@0
   332
  } msg]
sl@0
   333
  list $rc $msg
sl@0
   334
} {1 {Unable to delete/modify collation sequence due to active statements}}
sl@0
   335
do_test schema-11.8 {
sl@0
   336
  sqlite3_finalize $::STMT
sl@0
   337
} {SQLITE_OK}
sl@0
   338
sl@0
   339
# The following demonstrates why statements need to be expired whenever
sl@0
   340
# there is a rollback (explicit or otherwise).
sl@0
   341
#
sl@0
   342
do_test schema-12.1 {
sl@0
   343
  # Begin a transaction and create a table. This increments 
sl@0
   344
  # the schema cookie. Then compile an SQL statement, using
sl@0
   345
  # the current (incremented) value of the cookie.
sl@0
   346
  execsql {
sl@0
   347
    BEGIN;
sl@0
   348
    CREATE TABLE t3(a, b, c);
sl@0
   349
  }
sl@0
   350
  set ::STMT [sqlite3_prepare $::DB "CREATE TABLE t4(a,b,c)" -1 TAIL]
sl@0
   351
sl@0
   352
  # Rollback the transaction, resetting the schema cookie to the value
sl@0
   353
  # it had at the start of this test case. Then create a table, 
sl@0
   354
  # incrementing the schema cookie.
sl@0
   355
  execsql {
sl@0
   356
    ROLLBACK;
sl@0
   357
    CREATE TABLE t4(a, b, c);
sl@0
   358
  }
sl@0
   359
sl@0
   360
  # The schema cookie now has the same value as it did when SQL statement
sl@0
   361
  # $::STMT was prepared. So unless it has been expired, it would be
sl@0
   362
  # possible to run the "CREATE TABLE t4" statement and create a
sl@0
   363
  # duplicate table.
sl@0
   364
  list [sqlite3_step $::STMT] [sqlite3_finalize $::STMT]
sl@0
   365
} {SQLITE_ERROR SQLITE_SCHEMA}
sl@0
   366
sl@0
   367
finish_test