os/persistentdata/persistentstorage/sqlite3api/TEST/TclScript/attach3.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 July 1
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 script is testing the ATTACH and DETACH commands
sl@0
    13
# and schema changes to attached databases.
sl@0
    14
#
sl@0
    15
# $Id: attach3.test,v 1.18 2007/10/09 08:29:32 danielk1977 Exp $
sl@0
    16
#
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 !attach {
sl@0
    22
  finish_test
sl@0
    23
  return
sl@0
    24
}
sl@0
    25
sl@0
    26
# Create tables t1 and t2 in the main database
sl@0
    27
execsql {
sl@0
    28
  CREATE TABLE t1(a, b);
sl@0
    29
  CREATE TABLE t2(c, d);
sl@0
    30
}
sl@0
    31
sl@0
    32
# Create tables t1 and t2 in database file test2.db
sl@0
    33
file delete -force test2.db
sl@0
    34
file delete -force test2.db-journal
sl@0
    35
sqlite3 db2 test2.db
sl@0
    36
execsql {
sl@0
    37
  CREATE TABLE t1(a, b);
sl@0
    38
  CREATE TABLE t2(c, d);
sl@0
    39
} db2
sl@0
    40
db2 close
sl@0
    41
sl@0
    42
# Create a table in the auxilary database.
sl@0
    43
do_test attach3-1.1 {
sl@0
    44
  execsql {
sl@0
    45
    ATTACH 'test2.db' AS aux;
sl@0
    46
  }
sl@0
    47
} {}
sl@0
    48
do_test attach3-1.2 {
sl@0
    49
  execsql {
sl@0
    50
    CREATE TABLE aux.t3(e, f);
sl@0
    51
  }
sl@0
    52
} {}
sl@0
    53
do_test attach3-1.3 {
sl@0
    54
  execsql {
sl@0
    55
    SELECT * FROM sqlite_master WHERE name = 't3';
sl@0
    56
  }
sl@0
    57
} {}
sl@0
    58
do_test attach3-1.4 {
sl@0
    59
  execsql {
sl@0
    60
    SELECT * FROM aux.sqlite_master WHERE name = 't3';
sl@0
    61
  }
sl@0
    62
} "table t3 t3 [expr $AUTOVACUUM?5:4] {CREATE TABLE t3(e, f)}"
sl@0
    63
do_test attach3-1.5 {
sl@0
    64
  execsql {
sl@0
    65
    INSERT INTO t3 VALUES(1, 2);
sl@0
    66
    SELECT * FROM t3;
sl@0
    67
  }
sl@0
    68
} {1 2}
sl@0
    69
sl@0
    70
# Create an index on the auxilary database table.
sl@0
    71
do_test attach3-2.1 {
sl@0
    72
  execsql {
sl@0
    73
    CREATE INDEX aux.i1 on t3(e);
sl@0
    74
  }
sl@0
    75
} {}
sl@0
    76
do_test attach3-2.2 {
sl@0
    77
  execsql {
sl@0
    78
    SELECT * FROM sqlite_master WHERE name = 'i1';
sl@0
    79
  }
sl@0
    80
} {}
sl@0
    81
do_test attach3-2.3 {
sl@0
    82
  execsql {
sl@0
    83
    SELECT * FROM aux.sqlite_master WHERE name = 'i1';
sl@0
    84
  }
sl@0
    85
} "index i1 t3 [expr $AUTOVACUUM?6:5] {CREATE INDEX i1 on t3(e)}"
sl@0
    86
sl@0
    87
# Drop the index on the aux database table.
sl@0
    88
do_test attach3-3.1 {
sl@0
    89
  execsql {
sl@0
    90
    DROP INDEX aux.i1;
sl@0
    91
    SELECT * FROM aux.sqlite_master WHERE name = 'i1';
sl@0
    92
  }
sl@0
    93
} {}
sl@0
    94
do_test attach3-3.2 {
sl@0
    95
  execsql {
sl@0
    96
    CREATE INDEX aux.i1 on t3(e);
sl@0
    97
    SELECT * FROM aux.sqlite_master WHERE name = 'i1';
sl@0
    98
  }
sl@0
    99
} "index i1 t3 [expr $AUTOVACUUM?6:5] {CREATE INDEX i1 on t3(e)}"
sl@0
   100
do_test attach3-3.3 {
sl@0
   101
  execsql {
sl@0
   102
    DROP INDEX i1;
sl@0
   103
    SELECT * FROM aux.sqlite_master WHERE name = 'i1';
sl@0
   104
  }
sl@0
   105
} {}
sl@0
   106
sl@0
   107
# Drop tables t1 and t2 in the auxilary database.
sl@0
   108
do_test attach3-4.1 {
sl@0
   109
  execsql {
sl@0
   110
    DROP TABLE aux.t1;
sl@0
   111
    SELECT name FROM aux.sqlite_master;
sl@0
   112
  }
sl@0
   113
} {t2 t3}
sl@0
   114
do_test attach3-4.2 {
sl@0
   115
  # This will drop main.t2
sl@0
   116
  execsql {
sl@0
   117
    DROP TABLE t2;
sl@0
   118
    SELECT name FROM aux.sqlite_master;
sl@0
   119
  }
sl@0
   120
} {t2 t3}
sl@0
   121
do_test attach3-4.3 {
sl@0
   122
  execsql {
sl@0
   123
    DROP TABLE t2;
sl@0
   124
    SELECT name FROM aux.sqlite_master;
sl@0
   125
  }
sl@0
   126
} {t3}
sl@0
   127
sl@0
   128
# Create a view in the auxilary database.
sl@0
   129
ifcapable view {
sl@0
   130
do_test attach3-5.1 {
sl@0
   131
  execsql {
sl@0
   132
    CREATE VIEW aux.v1 AS SELECT * FROM t3;
sl@0
   133
  }
sl@0
   134
} {}
sl@0
   135
do_test attach3-5.2 {
sl@0
   136
  execsql {
sl@0
   137
    SELECT * FROM aux.sqlite_master WHERE name = 'v1';
sl@0
   138
  }
sl@0
   139
} {view v1 v1 0 {CREATE VIEW v1 AS SELECT * FROM t3}}
sl@0
   140
do_test attach3-5.3 {
sl@0
   141
  execsql {
sl@0
   142
    INSERT INTO aux.t3 VALUES('hello', 'world');
sl@0
   143
    SELECT * FROM v1;
sl@0
   144
  }
sl@0
   145
} {1 2 hello world}
sl@0
   146
sl@0
   147
# Drop the view 
sl@0
   148
do_test attach3-6.1 {
sl@0
   149
  execsql {
sl@0
   150
    DROP VIEW aux.v1;
sl@0
   151
  }
sl@0
   152
} {}
sl@0
   153
do_test attach3-6.2 {
sl@0
   154
  execsql {
sl@0
   155
    SELECT * FROM aux.sqlite_master WHERE name = 'v1';
sl@0
   156
  }
sl@0
   157
} {}
sl@0
   158
} ;# ifcapable view
sl@0
   159
sl@0
   160
ifcapable {trigger} {
sl@0
   161
# Create a trigger in the auxilary database.
sl@0
   162
do_test attach3-7.1 {
sl@0
   163
  execsql {
sl@0
   164
    CREATE TRIGGER aux.tr1 AFTER INSERT ON t3 BEGIN
sl@0
   165
      INSERT INTO t3 VALUES(new.e*2, new.f*2);
sl@0
   166
    END;
sl@0
   167
  }
sl@0
   168
} {}
sl@0
   169
do_test attach3-7.2 {
sl@0
   170
  execsql {
sl@0
   171
    DELETE FROM t3;
sl@0
   172
    INSERT INTO t3 VALUES(10, 20);
sl@0
   173
    SELECT * FROM t3;
sl@0
   174
  }
sl@0
   175
} {10 20 20 40}
sl@0
   176
do_test attach3-5.3 {
sl@0
   177
  execsql {
sl@0
   178
    SELECT * FROM aux.sqlite_master WHERE name = 'tr1';
sl@0
   179
  }
sl@0
   180
} {trigger tr1 t3 0 {CREATE TRIGGER tr1 AFTER INSERT ON t3 BEGIN
sl@0
   181
      INSERT INTO t3 VALUES(new.e*2, new.f*2);
sl@0
   182
    END}}
sl@0
   183
sl@0
   184
# Drop the trigger 
sl@0
   185
do_test attach3-8.1 {
sl@0
   186
  execsql {
sl@0
   187
    DROP TRIGGER aux.tr1;
sl@0
   188
  }
sl@0
   189
} {}
sl@0
   190
do_test attach3-8.2 {
sl@0
   191
  execsql {
sl@0
   192
    SELECT * FROM aux.sqlite_master WHERE name = 'tr1';
sl@0
   193
  }
sl@0
   194
} {}
sl@0
   195
sl@0
   196
ifcapable tempdb {
sl@0
   197
  # Try to trick SQLite into dropping the wrong temp trigger.
sl@0
   198
  do_test attach3-9.0 {
sl@0
   199
    execsql {
sl@0
   200
      CREATE TABLE main.t4(a, b, c);
sl@0
   201
      CREATE TABLE aux.t4(a, b, c);
sl@0
   202
      CREATE TEMP TRIGGER tst_trigger BEFORE INSERT ON aux.t4 BEGIN 
sl@0
   203
        SELECT 'hello world';
sl@0
   204
      END;
sl@0
   205
      SELECT count(*) FROM sqlite_temp_master;
sl@0
   206
    }
sl@0
   207
  } {1}
sl@0
   208
  do_test attach3-9.1 {
sl@0
   209
    execsql {
sl@0
   210
      DROP TABLE main.t4;
sl@0
   211
      SELECT count(*) FROM sqlite_temp_master;
sl@0
   212
    }
sl@0
   213
  } {1}
sl@0
   214
  do_test attach3-9.2 {
sl@0
   215
    execsql {
sl@0
   216
      DROP TABLE aux.t4;
sl@0
   217
      SELECT count(*) FROM sqlite_temp_master;
sl@0
   218
    }
sl@0
   219
  } {0}
sl@0
   220
}
sl@0
   221
} ;# endif trigger
sl@0
   222
sl@0
   223
# Make sure the aux.sqlite_master table is read-only
sl@0
   224
do_test attach3-10.0 {
sl@0
   225
  catchsql {
sl@0
   226
    INSERT INTO aux.sqlite_master VALUES(1, 2, 3, 4, 5);
sl@0
   227
  }
sl@0
   228
} {1 {table sqlite_master may not be modified}}
sl@0
   229
sl@0
   230
# Failure to attach leaves us in a workable state.
sl@0
   231
# Ticket #811
sl@0
   232
# Symbian OS: '/' in the file name replaced with '\'
sl@0
   233
do_test attach3-11.0 {
sl@0
   234
  catchsql {
sl@0
   235
    ATTACH DATABASE '\nodir\nofile.x' AS notadb;
sl@0
   236
  }
sl@0
   237
} {1 {unable to open database: \nodir\nofile.x}}
sl@0
   238
do_test attach3-11.1 {
sl@0
   239
  catchsql {
sl@0
   240
    ATTACH DATABASE ':memory:' AS notadb;
sl@0
   241
  }
sl@0
   242
} {0 {}}
sl@0
   243
do_test attach3-11.2 {
sl@0
   244
  catchsql {
sl@0
   245
    DETACH DATABASE notadb;
sl@0
   246
  }
sl@0
   247
} {0 {}}
sl@0
   248
sl@0
   249
# Return a list of attached databases
sl@0
   250
#
sl@0
   251
proc db_list {} {
sl@0
   252
  set x [execsql {
sl@0
   253
    PRAGMA database_list;
sl@0
   254
  }]
sl@0
   255
  set y {}
sl@0
   256
  foreach {n id file} $x {lappend y $id}
sl@0
   257
  return $y
sl@0
   258
}
sl@0
   259
sl@0
   260
ifcapable schema_pragmas&&tempdb {
sl@0
   261
sl@0
   262
ifcapable !trigger {
sl@0
   263
  execsql {create temp table dummy(dummy)}
sl@0
   264
}
sl@0
   265
sl@0
   266
# Ticket #1825
sl@0
   267
#
sl@0
   268
do_test attach3-12.1 {
sl@0
   269
  db_list
sl@0
   270
} {main temp aux}
sl@0
   271
do_test attach3-12.2 {
sl@0
   272
  execsql {
sl@0
   273
    ATTACH DATABASE ? AS ?
sl@0
   274
  }
sl@0
   275
  db_list
sl@0
   276
} {main temp aux {}}
sl@0
   277
do_test attach3-12.3 {
sl@0
   278
  execsql {
sl@0
   279
    DETACH aux
sl@0
   280
  }
sl@0
   281
  db_list
sl@0
   282
} {main temp {}}
sl@0
   283
do_test attach3-12.4 {
sl@0
   284
  execsql {
sl@0
   285
    DETACH ?
sl@0
   286
  }
sl@0
   287
  db_list
sl@0
   288
} {main temp}
sl@0
   289
do_test attach3-12.5 {
sl@0
   290
  execsql {
sl@0
   291
    ATTACH DATABASE '' AS ''
sl@0
   292
  }
sl@0
   293
  db_list
sl@0
   294
} {main temp {}}
sl@0
   295
do_test attach3-12.6 {
sl@0
   296
  execsql {
sl@0
   297
    DETACH ''
sl@0
   298
  }
sl@0
   299
  db_list
sl@0
   300
} {main temp}
sl@0
   301
do_test attach3-12.7 {
sl@0
   302
  execsql {
sl@0
   303
    ATTACH DATABASE '' AS ?
sl@0
   304
  }
sl@0
   305
  db_list
sl@0
   306
} {main temp {}}
sl@0
   307
do_test attach3-12.8 {
sl@0
   308
  execsql {
sl@0
   309
    DETACH ''
sl@0
   310
  }
sl@0
   311
  db_list
sl@0
   312
} {main temp}
sl@0
   313
do_test attach3-12.9 {
sl@0
   314
  execsql {
sl@0
   315
    ATTACH DATABASE '' AS NULL
sl@0
   316
  }
sl@0
   317
  db_list
sl@0
   318
} {main temp {}}
sl@0
   319
do_test attach3-12.10 {
sl@0
   320
  execsql {
sl@0
   321
    DETACH ?
sl@0
   322
  }
sl@0
   323
  db_list
sl@0
   324
} {main temp}
sl@0
   325
do_test attach3-12.11 {
sl@0
   326
  catchsql {
sl@0
   327
    DETACH NULL
sl@0
   328
  }
sl@0
   329
} {1 {no such database: }}
sl@0
   330
do_test attach3-12.12 {
sl@0
   331
  catchsql {
sl@0
   332
    ATTACH null AS null;
sl@0
   333
    ATTACH '' AS '';
sl@0
   334
  }
sl@0
   335
} {1 {database  is already in use}}
sl@0
   336
do_test attach3-12.13 {
sl@0
   337
  db_list
sl@0
   338
} {main temp {}}
sl@0
   339
do_test attach3-12.14 {
sl@0
   340
  execsql {
sl@0
   341
    DETACH '';
sl@0
   342
  }
sl@0
   343
  db_list
sl@0
   344
} {main temp}
sl@0
   345
sl@0
   346
} ;# ifcapable pragma
sl@0
   347
sl@0
   348
finish_test