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