os/persistentdata/persistentstorage/sqlite3api/TEST/TclScript/alter.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
# 2004 November 10
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 ALTER TABLE statement.
sl@0
    13
#
sl@0
    14
# $Id: alter.test,v 1.30 2008/05/09 14:17:52 drh Exp $
sl@0
    15
#
sl@0
    16
sl@0
    17
set testdir [file dirname $argv0]
sl@0
    18
source $testdir/tester.tcl
sl@0
    19
sl@0
    20
# If SQLITE_OMIT_ALTERTABLE is defined, omit this file.
sl@0
    21
ifcapable !altertable {
sl@0
    22
  finish_test
sl@0
    23
  return
sl@0
    24
}
sl@0
    25
sl@0
    26
#----------------------------------------------------------------------
sl@0
    27
# Test organization:
sl@0
    28
#
sl@0
    29
# alter-1.1.* - alter-1.7.*: Basic tests of ALTER TABLE, including tables
sl@0
    30
#     with implicit and explicit indices. These tests came from an earlier
sl@0
    31
#     fork of SQLite that also supported ALTER TABLE.
sl@0
    32
# alter-1.8.*: Tests for ALTER TABLE when the table resides in an 
sl@0
    33
#     attached database.
sl@0
    34
# alter-1.9.*: Tests for ALTER TABLE when their is whitespace between the
sl@0
    35
#     table name and left parenthesis token. i.e: 
sl@0
    36
#     "CREATE TABLE abc       (a, b, c);"
sl@0
    37
# alter-2.*: Test error conditions and messages.
sl@0
    38
# alter-3.*: Test ALTER TABLE on tables that have TRIGGERs attached to them.
sl@0
    39
# alter-4.*: Test ALTER TABLE on tables that have AUTOINCREMENT fields.
sl@0
    40
# ...
sl@0
    41
# alter-12.*: Test ALTER TABLE on views.
sl@0
    42
#
sl@0
    43
sl@0
    44
# Create some tables to rename.  Be sure to include some TEMP tables
sl@0
    45
# and some tables with odd names.
sl@0
    46
#
sl@0
    47
do_test alter-1.1 {
sl@0
    48
  ifcapable tempdb {
sl@0
    49
    set ::temp TEMP
sl@0
    50
  } else {
sl@0
    51
    set ::temp {}
sl@0
    52
  }
sl@0
    53
  execsql [subst -nocommands {
sl@0
    54
    CREATE TABLE t1(a,b);
sl@0
    55
    INSERT INTO t1 VALUES(1,2);
sl@0
    56
    CREATE TABLE [t1'x1](c UNIQUE, b PRIMARY KEY);
sl@0
    57
    INSERT INTO [t1'x1] VALUES(3,4);
sl@0
    58
    CREATE INDEX t1i1 ON T1(B);
sl@0
    59
    CREATE INDEX t1i2 ON t1(a,b);
sl@0
    60
    CREATE INDEX i3 ON [t1'x1](b,c);
sl@0
    61
    CREATE $::temp TABLE "temp table"(e,f,g UNIQUE);
sl@0
    62
    CREATE INDEX i2 ON [temp table](f);
sl@0
    63
    INSERT INTO [temp table] VALUES(5,6,7);
sl@0
    64
  }]
sl@0
    65
  execsql {
sl@0
    66
    SELECT 't1', * FROM t1;
sl@0
    67
    SELECT 't1''x1', * FROM "t1'x1";
sl@0
    68
    SELECT * FROM [temp table];
sl@0
    69
  }
sl@0
    70
} {t1 1 2 t1'x1 3 4 5 6 7}
sl@0
    71
do_test alter-1.2 {
sl@0
    72
  execsql [subst {
sl@0
    73
    CREATE $::temp TABLE objlist(type, name, tbl_name);
sl@0
    74
    INSERT INTO objlist SELECT type, name, tbl_name 
sl@0
    75
        FROM sqlite_master WHERE NAME!='objlist';
sl@0
    76
  }]
sl@0
    77
  ifcapable tempdb {
sl@0
    78
    execsql {
sl@0
    79
      INSERT INTO objlist SELECT type, name, tbl_name 
sl@0
    80
          FROM sqlite_temp_master WHERE NAME!='objlist';
sl@0
    81
    }
sl@0
    82
  }
sl@0
    83
sl@0
    84
  execsql {
sl@0
    85
    SELECT type, name, tbl_name FROM objlist ORDER BY tbl_name, type desc, name;
sl@0
    86
  }
sl@0
    87
} [list \
sl@0
    88
     table t1                              t1             \
sl@0
    89
     index t1i1                            t1             \
sl@0
    90
     index t1i2                            t1             \
sl@0
    91
     table t1'x1                           t1'x1          \
sl@0
    92
     index i3                              t1'x1          \
sl@0
    93
     index {sqlite_autoindex_t1'x1_1}      t1'x1          \
sl@0
    94
     index {sqlite_autoindex_t1'x1_2}      t1'x1          \
sl@0
    95
     table {temp table}                    {temp table}   \
sl@0
    96
     index i2                              {temp table}   \
sl@0
    97
     index {sqlite_autoindex_temp table_1} {temp table}   \
sl@0
    98
  ]
sl@0
    99
sl@0
   100
# Make some changes
sl@0
   101
#
sl@0
   102
integrity_check alter-1.3.0
sl@0
   103
do_test alter-1.3 {
sl@0
   104
  execsql {
sl@0
   105
    ALTER TABLE [T1] RENAME to [-t1-];
sl@0
   106
    ALTER TABLE "t1'x1" RENAME TO T2;
sl@0
   107
    ALTER TABLE [temp table] RENAME to TempTab;
sl@0
   108
  }
sl@0
   109
} {}
sl@0
   110
integrity_check alter-1.3.1
sl@0
   111
do_test alter-1.4 {
sl@0
   112
  execsql {
sl@0
   113
    SELECT 't1', * FROM [-t1-];
sl@0
   114
    SELECT 't2', * FROM t2;
sl@0
   115
    SELECT * FROM temptab;
sl@0
   116
  }
sl@0
   117
} {t1 1 2 t2 3 4 5 6 7}
sl@0
   118
do_test alter-1.5 {
sl@0
   119
  execsql {
sl@0
   120
    DELETE FROM objlist;
sl@0
   121
    INSERT INTO objlist SELECT type, name, tbl_name
sl@0
   122
        FROM sqlite_master WHERE NAME!='objlist';
sl@0
   123
  }
sl@0
   124
  catchsql {
sl@0
   125
    INSERT INTO objlist SELECT type, name, tbl_name 
sl@0
   126
        FROM sqlite_temp_master WHERE NAME!='objlist';
sl@0
   127
  }
sl@0
   128
  execsql {
sl@0
   129
    SELECT type, name, tbl_name FROM objlist ORDER BY tbl_name, type desc, name;
sl@0
   130
  }
sl@0
   131
} [list \
sl@0
   132
     table -t1-                         -t1-        \
sl@0
   133
     index t1i1                         -t1-        \
sl@0
   134
     index t1i2                         -t1-        \
sl@0
   135
     table T2                           T2          \
sl@0
   136
     index i3                           T2          \
sl@0
   137
     index {sqlite_autoindex_T2_1}      T2          \
sl@0
   138
     index {sqlite_autoindex_T2_2}      T2          \
sl@0
   139
     table {TempTab}                    {TempTab}   \
sl@0
   140
     index i2                           {TempTab}   \
sl@0
   141
     index {sqlite_autoindex_TempTab_1} {TempTab}   \
sl@0
   142
  ]
sl@0
   143
sl@0
   144
# Make sure the changes persist after restarting the database.
sl@0
   145
# (The TEMP table will not persist, of course.)
sl@0
   146
#
sl@0
   147
ifcapable tempdb {
sl@0
   148
  do_test alter-1.6 {
sl@0
   149
    db close
sl@0
   150
    sqlite3 db test.db
sl@0
   151
    set DB [sqlite3_connection_pointer db]
sl@0
   152
    execsql {
sl@0
   153
      CREATE TEMP TABLE objlist(type, name, tbl_name);
sl@0
   154
      INSERT INTO objlist SELECT type, name, tbl_name FROM sqlite_master;
sl@0
   155
      INSERT INTO objlist 
sl@0
   156
          SELECT type, name, tbl_name FROM sqlite_temp_master 
sl@0
   157
          WHERE NAME!='objlist';
sl@0
   158
      SELECT type, name, tbl_name FROM objlist 
sl@0
   159
          ORDER BY tbl_name, type desc, name;
sl@0
   160
    }
sl@0
   161
  } [list \
sl@0
   162
       table -t1-                         -t1-           \
sl@0
   163
       index t1i1                         -t1-           \
sl@0
   164
       index t1i2                         -t1-           \
sl@0
   165
       table T2                           T2          \
sl@0
   166
       index i3                           T2          \
sl@0
   167
       index {sqlite_autoindex_T2_1}      T2          \
sl@0
   168
       index {sqlite_autoindex_T2_2}      T2          \
sl@0
   169
    ]
sl@0
   170
} else {
sl@0
   171
  execsql {
sl@0
   172
    DROP TABLE TempTab;
sl@0
   173
  }
sl@0
   174
}
sl@0
   175
sl@0
   176
# Make sure the ALTER TABLE statements work with the
sl@0
   177
# non-callback API
sl@0
   178
#
sl@0
   179
do_test alter-1.7 {
sl@0
   180
  stepsql $DB {
sl@0
   181
    ALTER TABLE [-t1-] RENAME to [*t1*];
sl@0
   182
    ALTER TABLE T2 RENAME TO [<t2>];
sl@0
   183
  }
sl@0
   184
  execsql {
sl@0
   185
    DELETE FROM objlist;
sl@0
   186
    INSERT INTO objlist SELECT type, name, tbl_name
sl@0
   187
        FROM sqlite_master WHERE NAME!='objlist';
sl@0
   188
  }
sl@0
   189
  catchsql {
sl@0
   190
    INSERT INTO objlist SELECT type, name, tbl_name 
sl@0
   191
        FROM sqlite_temp_master WHERE NAME!='objlist';
sl@0
   192
  }
sl@0
   193
  execsql {
sl@0
   194
    SELECT type, name, tbl_name FROM objlist ORDER BY tbl_name, type desc, name;
sl@0
   195
  }
sl@0
   196
} [list \
sl@0
   197
     table *t1*                         *t1*           \
sl@0
   198
     index t1i1                         *t1*           \
sl@0
   199
     index t1i2                         *t1*           \
sl@0
   200
     table <t2>                         <t2>          \
sl@0
   201
     index i3                           <t2>          \
sl@0
   202
     index {sqlite_autoindex_<t2>_1}    <t2>          \
sl@0
   203
     index {sqlite_autoindex_<t2>_2}    <t2>          \
sl@0
   204
  ]
sl@0
   205
sl@0
   206
# Check that ALTER TABLE works on attached databases.
sl@0
   207
#
sl@0
   208
ifcapable attach {
sl@0
   209
  do_test alter-1.8.1 {
sl@0
   210
    file delete -force test2.db
sl@0
   211
    file delete -force test2.db-journal
sl@0
   212
    execsql {
sl@0
   213
      ATTACH 'test2.db' AS aux;
sl@0
   214
    }
sl@0
   215
  } {}
sl@0
   216
  do_test alter-1.8.2 {
sl@0
   217
    execsql {
sl@0
   218
      CREATE TABLE t4(a PRIMARY KEY, b, c);
sl@0
   219
      CREATE TABLE aux.t4(a PRIMARY KEY, b, c);
sl@0
   220
      CREATE INDEX i4 ON t4(b);
sl@0
   221
      CREATE INDEX aux.i4 ON t4(b);
sl@0
   222
    }
sl@0
   223
  } {}
sl@0
   224
  do_test alter-1.8.3 {
sl@0
   225
    execsql {
sl@0
   226
      INSERT INTO t4 VALUES('main', 'main', 'main');
sl@0
   227
      INSERT INTO aux.t4 VALUES('aux', 'aux', 'aux');
sl@0
   228
      SELECT * FROM t4 WHERE a = 'main';
sl@0
   229
    }
sl@0
   230
  } {main main main}
sl@0
   231
  do_test alter-1.8.4 {
sl@0
   232
    execsql {
sl@0
   233
      ALTER TABLE t4 RENAME TO t5;
sl@0
   234
      SELECT * FROM t4 WHERE a = 'aux';
sl@0
   235
    }
sl@0
   236
  } {aux aux aux}
sl@0
   237
  do_test alter-1.8.5 {
sl@0
   238
    execsql {
sl@0
   239
      SELECT * FROM t5;
sl@0
   240
    }
sl@0
   241
  } {main main main}
sl@0
   242
  do_test alter-1.8.6 {
sl@0
   243
    execsql {
sl@0
   244
      SELECT * FROM t5 WHERE b = 'main';
sl@0
   245
    }
sl@0
   246
  } {main main main}
sl@0
   247
  do_test alter-1.8.7 {
sl@0
   248
    execsql {
sl@0
   249
      ALTER TABLE aux.t4 RENAME TO t5;
sl@0
   250
      SELECT * FROM aux.t5 WHERE b = 'aux';
sl@0
   251
    }
sl@0
   252
  } {aux aux aux}
sl@0
   253
}
sl@0
   254
sl@0
   255
do_test alter-1.9.1 {
sl@0
   256
  execsql {
sl@0
   257
    CREATE TABLE tbl1   (a, b, c);
sl@0
   258
    INSERT INTO tbl1 VALUES(1, 2, 3);
sl@0
   259
  }
sl@0
   260
} {}
sl@0
   261
do_test alter-1.9.2 {
sl@0
   262
  execsql {
sl@0
   263
    SELECT * FROM tbl1;
sl@0
   264
  }
sl@0
   265
} {1 2 3}
sl@0
   266
do_test alter-1.9.3 {
sl@0
   267
  execsql {
sl@0
   268
    ALTER TABLE tbl1 RENAME TO tbl2;
sl@0
   269
    SELECT * FROM tbl2;
sl@0
   270
  }
sl@0
   271
} {1 2 3}
sl@0
   272
do_test alter-1.9.4 {
sl@0
   273
  execsql {
sl@0
   274
    DROP TABLE tbl2;
sl@0
   275
  }
sl@0
   276
} {}
sl@0
   277
sl@0
   278
# Test error messages
sl@0
   279
#
sl@0
   280
do_test alter-2.1 {
sl@0
   281
  catchsql {
sl@0
   282
    ALTER TABLE none RENAME TO hi;
sl@0
   283
  }
sl@0
   284
} {1 {no such table: none}}
sl@0
   285
do_test alter-2.2 {
sl@0
   286
  execsql {
sl@0
   287
    CREATE TABLE t3(p,q,r);
sl@0
   288
  }
sl@0
   289
  catchsql {
sl@0
   290
    ALTER TABLE [<t2>] RENAME TO t3;
sl@0
   291
  }
sl@0
   292
} {1 {there is already another table or index with this name: t3}}
sl@0
   293
do_test alter-2.3 {
sl@0
   294
  catchsql {
sl@0
   295
    ALTER TABLE [<t2>] RENAME TO i3;
sl@0
   296
  }
sl@0
   297
} {1 {there is already another table or index with this name: i3}}
sl@0
   298
do_test alter-2.4 {
sl@0
   299
  catchsql {
sl@0
   300
    ALTER TABLE SqLiTe_master RENAME TO master;
sl@0
   301
  }
sl@0
   302
} {1 {table sqlite_master may not be altered}}
sl@0
   303
do_test alter-2.5 {
sl@0
   304
  catchsql {
sl@0
   305
    ALTER TABLE t3 RENAME TO sqlite_t3;
sl@0
   306
  }
sl@0
   307
} {1 {object name reserved for internal use: sqlite_t3}}
sl@0
   308
do_test alter-2.6 {
sl@0
   309
  catchsql {
sl@0
   310
    ALTER TABLE t3 ADD COLUMN (ALTER TABLE t3 ADD COLUMN);
sl@0
   311
  }
sl@0
   312
} {1 {near "(": syntax error}}
sl@0
   313
sl@0
   314
# If this compilation does not include triggers, omit the alter-3.* tests.
sl@0
   315
ifcapable trigger {
sl@0
   316
sl@0
   317
#-----------------------------------------------------------------------
sl@0
   318
# Tests alter-3.* test ALTER TABLE on tables that have triggers.
sl@0
   319
#
sl@0
   320
# alter-3.1.*: ALTER TABLE with triggers.
sl@0
   321
# alter-3.2.*: Test that the ON keyword cannot be used as a database,
sl@0
   322
#     table or column name unquoted. This is done because part of the
sl@0
   323
#     ALTER TABLE code (specifically the implementation of SQL function
sl@0
   324
#     "sqlite_alter_trigger") will break in this case.
sl@0
   325
# alter-3.3.*: ALTER TABLE with TEMP triggers (todo).
sl@0
   326
#
sl@0
   327
sl@0
   328
# An SQL user-function for triggers to fire, so that we know they
sl@0
   329
# are working.
sl@0
   330
proc trigfunc {args} {
sl@0
   331
  set ::TRIGGER $args
sl@0
   332
}
sl@0
   333
db func trigfunc trigfunc
sl@0
   334
sl@0
   335
do_test alter-3.1.0 {
sl@0
   336
  execsql {
sl@0
   337
    CREATE TABLE t6(a, b, c);
sl@0
   338
    CREATE TRIGGER trig1 AFTER INSERT ON t6 BEGIN
sl@0
   339
      SELECT trigfunc('trig1', new.a, new.b, new.c);
sl@0
   340
    END;
sl@0
   341
  }
sl@0
   342
} {}
sl@0
   343
do_test alter-3.1.1 {
sl@0
   344
  execsql {
sl@0
   345
    INSERT INTO t6 VALUES(1, 2, 3);
sl@0
   346
  }
sl@0
   347
  set ::TRIGGER
sl@0
   348
} {trig1 1 2 3}
sl@0
   349
do_test alter-3.1.2 {
sl@0
   350
  execsql {
sl@0
   351
    ALTER TABLE t6 RENAME TO t7;
sl@0
   352
    INSERT INTO t7 VALUES(4, 5, 6);
sl@0
   353
  }
sl@0
   354
  set ::TRIGGER
sl@0
   355
} {trig1 4 5 6}
sl@0
   356
do_test alter-3.1.3 {
sl@0
   357
  execsql {
sl@0
   358
    DROP TRIGGER trig1;
sl@0
   359
  }
sl@0
   360
} {}
sl@0
   361
do_test alter-3.1.4 {
sl@0
   362
  execsql {
sl@0
   363
    CREATE TRIGGER trig2 AFTER INSERT ON main.t7 BEGIN
sl@0
   364
      SELECT trigfunc('trig2', new.a, new.b, new.c);
sl@0
   365
    END;
sl@0
   366
    INSERT INTO t7 VALUES(1, 2, 3);
sl@0
   367
  }
sl@0
   368
  set ::TRIGGER
sl@0
   369
} {trig2 1 2 3}
sl@0
   370
do_test alter-3.1.5 {
sl@0
   371
  execsql {
sl@0
   372
    ALTER TABLE t7 RENAME TO t8;
sl@0
   373
    INSERT INTO t8 VALUES(4, 5, 6);
sl@0
   374
  }
sl@0
   375
  set ::TRIGGER
sl@0
   376
} {trig2 4 5 6}
sl@0
   377
do_test alter-3.1.6 {
sl@0
   378
  execsql {
sl@0
   379
    DROP TRIGGER trig2;
sl@0
   380
  }
sl@0
   381
} {}
sl@0
   382
do_test alter-3.1.7 {
sl@0
   383
  execsql {
sl@0
   384
    CREATE TRIGGER trig3 AFTER INSERT ON main.'t8'BEGIN
sl@0
   385
      SELECT trigfunc('trig3', new.a, new.b, new.c);
sl@0
   386
    END;
sl@0
   387
    INSERT INTO t8 VALUES(1, 2, 3);
sl@0
   388
  }
sl@0
   389
  set ::TRIGGER
sl@0
   390
} {trig3 1 2 3}
sl@0
   391
do_test alter-3.1.8 {
sl@0
   392
  execsql {
sl@0
   393
    ALTER TABLE t8 RENAME TO t9;
sl@0
   394
    INSERT INTO t9 VALUES(4, 5, 6);
sl@0
   395
  }
sl@0
   396
  set ::TRIGGER
sl@0
   397
} {trig3 4 5 6}
sl@0
   398
sl@0
   399
# Make sure "ON" cannot be used as a database, table or column name without
sl@0
   400
# quoting. Otherwise the sqlite_alter_trigger() function might not work.
sl@0
   401
file delete -force test3.db
sl@0
   402
file delete -force test3.db-journal
sl@0
   403
ifcapable attach {
sl@0
   404
  do_test alter-3.2.1 {
sl@0
   405
    catchsql {
sl@0
   406
      ATTACH 'test3.db' AS ON;
sl@0
   407
    }
sl@0
   408
  } {1 {near "ON": syntax error}}
sl@0
   409
  do_test alter-3.2.2 {
sl@0
   410
    catchsql {
sl@0
   411
      ATTACH 'test3.db' AS 'ON';
sl@0
   412
    }
sl@0
   413
  } {0 {}}
sl@0
   414
  do_test alter-3.2.3 {
sl@0
   415
    catchsql {
sl@0
   416
      CREATE TABLE ON.t1(a, b, c); 
sl@0
   417
    }
sl@0
   418
  } {1 {near "ON": syntax error}}
sl@0
   419
  do_test alter-3.2.4 {
sl@0
   420
    catchsql {
sl@0
   421
      CREATE TABLE 'ON'.t1(a, b, c); 
sl@0
   422
    }
sl@0
   423
  } {0 {}}
sl@0
   424
  do_test alter-3.2.4 {
sl@0
   425
    catchsql {
sl@0
   426
      CREATE TABLE 'ON'.ON(a, b, c); 
sl@0
   427
    }
sl@0
   428
  } {1 {near "ON": syntax error}}
sl@0
   429
  do_test alter-3.2.5 {
sl@0
   430
    catchsql {
sl@0
   431
      CREATE TABLE 'ON'.'ON'(a, b, c); 
sl@0
   432
    }
sl@0
   433
  } {0 {}}
sl@0
   434
}
sl@0
   435
do_test alter-3.2.6 {
sl@0
   436
  catchsql {
sl@0
   437
    CREATE TABLE t10(a, ON, c);
sl@0
   438
  }
sl@0
   439
} {1 {near "ON": syntax error}}
sl@0
   440
do_test alter-3.2.7 {
sl@0
   441
  catchsql {
sl@0
   442
    CREATE TABLE t10(a, 'ON', c);
sl@0
   443
  }
sl@0
   444
} {0 {}}
sl@0
   445
do_test alter-3.2.8 {
sl@0
   446
  catchsql {
sl@0
   447
    CREATE TRIGGER trig4 AFTER INSERT ON ON BEGIN SELECT 1; END;
sl@0
   448
  }
sl@0
   449
} {1 {near "ON": syntax error}}
sl@0
   450
ifcapable attach {
sl@0
   451
  do_test alter-3.2.9 {
sl@0
   452
    catchsql {
sl@0
   453
      CREATE TRIGGER 'on'.trig4 AFTER INSERT ON 'ON' BEGIN SELECT 1; END;
sl@0
   454
    }
sl@0
   455
  } {0 {}}
sl@0
   456
}
sl@0
   457
do_test alter-3.2.10 {
sl@0
   458
  execsql {
sl@0
   459
    DROP TABLE t10;
sl@0
   460
  }
sl@0
   461
} {}
sl@0
   462
sl@0
   463
do_test alter-3.3.1 {
sl@0
   464
  execsql [subst {
sl@0
   465
    CREATE TABLE tbl1(a, b, c);
sl@0
   466
    CREATE $::temp TRIGGER trig1 AFTER INSERT ON tbl1 BEGIN
sl@0
   467
      SELECT trigfunc('trig1', new.a, new.b, new.c);
sl@0
   468
    END;
sl@0
   469
  }]
sl@0
   470
} {}
sl@0
   471
do_test alter-3.3.2 {
sl@0
   472
  execsql {
sl@0
   473
    INSERT INTO tbl1 VALUES('a', 'b', 'c');
sl@0
   474
  }
sl@0
   475
  set ::TRIGGER
sl@0
   476
} {trig1 a b c}
sl@0
   477
do_test alter-3.3.3 {
sl@0
   478
  execsql {
sl@0
   479
    ALTER TABLE tbl1 RENAME TO tbl2;
sl@0
   480
    INSERT INTO tbl2 VALUES('d', 'e', 'f');
sl@0
   481
  } 
sl@0
   482
  set ::TRIGGER
sl@0
   483
} {trig1 d e f}
sl@0
   484
do_test alter-3.3.4 {
sl@0
   485
  execsql [subst {
sl@0
   486
    CREATE $::temp TRIGGER trig2 AFTER UPDATE ON tbl2 BEGIN
sl@0
   487
      SELECT trigfunc('trig2', new.a, new.b, new.c);
sl@0
   488
    END;
sl@0
   489
  }] 
sl@0
   490
} {}
sl@0
   491
do_test alter-3.3.5 {
sl@0
   492
  execsql {
sl@0
   493
    ALTER TABLE tbl2 RENAME TO tbl3;
sl@0
   494
    INSERT INTO tbl3 VALUES('g', 'h', 'i');
sl@0
   495
  } 
sl@0
   496
  set ::TRIGGER
sl@0
   497
} {trig1 g h i}
sl@0
   498
do_test alter-3.3.6 {
sl@0
   499
  execsql {
sl@0
   500
    UPDATE tbl3 SET a = 'G' where a = 'g';
sl@0
   501
  } 
sl@0
   502
  set ::TRIGGER
sl@0
   503
} {trig2 G h i}
sl@0
   504
do_test alter-3.3.7 {
sl@0
   505
  execsql {
sl@0
   506
    DROP TABLE tbl3;
sl@0
   507
  }
sl@0
   508
} {}
sl@0
   509
ifcapable tempdb {
sl@0
   510
  do_test alter-3.3.8 {
sl@0
   511
    execsql {
sl@0
   512
      SELECT * FROM sqlite_temp_master WHERE type = 'trigger';
sl@0
   513
    }
sl@0
   514
  } {}
sl@0
   515
}
sl@0
   516
sl@0
   517
} ;# ifcapable trigger
sl@0
   518
sl@0
   519
# If the build does not include AUTOINCREMENT fields, omit alter-4.*.
sl@0
   520
ifcapable autoinc {
sl@0
   521
sl@0
   522
do_test alter-4.1 {
sl@0
   523
  execsql {
sl@0
   524
    CREATE TABLE tbl1(a INTEGER PRIMARY KEY AUTOINCREMENT);
sl@0
   525
    INSERT INTO tbl1 VALUES(10);
sl@0
   526
  }
sl@0
   527
} {}
sl@0
   528
do_test alter-4.2 {
sl@0
   529
  execsql {
sl@0
   530
    INSERT INTO tbl1 VALUES(NULL);
sl@0
   531
    SELECT a FROM tbl1;
sl@0
   532
  }
sl@0
   533
} {10 11}
sl@0
   534
do_test alter-4.3 {
sl@0
   535
  execsql {
sl@0
   536
    ALTER TABLE tbl1 RENAME TO tbl2;
sl@0
   537
    DELETE FROM tbl2;
sl@0
   538
    INSERT INTO tbl2 VALUES(NULL);
sl@0
   539
    SELECT a FROM tbl2;
sl@0
   540
  }
sl@0
   541
} {12}
sl@0
   542
do_test alter-4.4 {
sl@0
   543
  execsql {
sl@0
   544
    DROP TABLE tbl2;
sl@0
   545
  }
sl@0
   546
} {}
sl@0
   547
sl@0
   548
} ;# ifcapable autoinc
sl@0
   549
sl@0
   550
# Test that it is Ok to execute an ALTER TABLE immediately after
sl@0
   551
# opening a database.
sl@0
   552
do_test alter-5.1 {
sl@0
   553
  execsql {
sl@0
   554
    CREATE TABLE tbl1(a, b, c);
sl@0
   555
    INSERT INTO tbl1 VALUES('x', 'y', 'z');
sl@0
   556
  }
sl@0
   557
} {}
sl@0
   558
do_test alter-5.2 {
sl@0
   559
  sqlite3 db2 test.db
sl@0
   560
  execsql {
sl@0
   561
    ALTER TABLE tbl1 RENAME TO tbl2;
sl@0
   562
    SELECT * FROM tbl2;
sl@0
   563
  } db2
sl@0
   564
} {x y z}
sl@0
   565
do_test alter-5.3 {
sl@0
   566
  db2 close
sl@0
   567
} {}
sl@0
   568
sl@0
   569
foreach tblname [execsql {
sl@0
   570
  SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite%'
sl@0
   571
}] {
sl@0
   572
  execsql "DROP TABLE \"$tblname\""
sl@0
   573
}
sl@0
   574
sl@0
   575
set ::tbl_name "abc\uABCDdef"
sl@0
   576
do_test alter-6.1 {
sl@0
   577
  string length $::tbl_name
sl@0
   578
} {7}
sl@0
   579
do_test alter-6.2 {
sl@0
   580
  execsql "
sl@0
   581
    CREATE TABLE ${tbl_name}(a, b, c);
sl@0
   582
  "
sl@0
   583
  set ::oid [execsql {SELECT max(oid) FROM sqlite_master}]
sl@0
   584
  execsql "
sl@0
   585
    SELECT sql FROM sqlite_master WHERE oid = $::oid;
sl@0
   586
  "
sl@0
   587
} "{CREATE TABLE ${::tbl_name}(a, b, c)}"
sl@0
   588
execsql "
sl@0
   589
  SELECT * FROM ${::tbl_name}
sl@0
   590
"
sl@0
   591
set ::tbl_name2 "abcXdef"
sl@0
   592
do_test alter-6.3 {
sl@0
   593
  execsql "
sl@0
   594
    ALTER TABLE $::tbl_name RENAME TO $::tbl_name2 
sl@0
   595
  "
sl@0
   596
  execsql "
sl@0
   597
    SELECT sql FROM sqlite_master WHERE oid = $::oid
sl@0
   598
  "
sl@0
   599
} "{CREATE TABLE \"${::tbl_name2}\"(a, b, c)}"
sl@0
   600
do_test alter-6.4 {
sl@0
   601
  execsql "
sl@0
   602
    ALTER TABLE $::tbl_name2 RENAME TO $::tbl_name
sl@0
   603
  "
sl@0
   604
  execsql "
sl@0
   605
    SELECT sql FROM sqlite_master WHERE oid = $::oid
sl@0
   606
  "
sl@0
   607
} "{CREATE TABLE \"${::tbl_name}\"(a, b, c)}"
sl@0
   608
set ::col_name ghi\1234\jkl
sl@0
   609
do_test alter-6.5 {
sl@0
   610
  execsql "
sl@0
   611
    ALTER TABLE $::tbl_name ADD COLUMN $::col_name VARCHAR
sl@0
   612
  "
sl@0
   613
  execsql "
sl@0
   614
    SELECT sql FROM sqlite_master WHERE oid = $::oid
sl@0
   615
  "
sl@0
   616
} "{CREATE TABLE \"${::tbl_name}\"(a, b, c, $::col_name VARCHAR)}"
sl@0
   617
set ::col_name2 B\3421\A
sl@0
   618
do_test alter-6.6 {
sl@0
   619
  db close
sl@0
   620
  sqlite3 db test.db
sl@0
   621
  execsql "
sl@0
   622
    ALTER TABLE $::tbl_name ADD COLUMN $::col_name2
sl@0
   623
  "
sl@0
   624
  execsql "
sl@0
   625
    SELECT sql FROM sqlite_master WHERE oid = $::oid
sl@0
   626
  "
sl@0
   627
} "{CREATE TABLE \"${::tbl_name}\"(a, b, c, $::col_name VARCHAR, $::col_name2)}"
sl@0
   628
do_test alter-6.7 {
sl@0
   629
  execsql "
sl@0
   630
    INSERT INTO ${::tbl_name} VALUES(1, 2, 3, 4, 5);
sl@0
   631
    SELECT $::col_name, $::col_name2 FROM $::tbl_name;
sl@0
   632
  "
sl@0
   633
} {4 5}
sl@0
   634
sl@0
   635
# Ticket #1665:  Make sure ALTER TABLE ADD COLUMN works on a table
sl@0
   636
# that includes a COLLATE clause.
sl@0
   637
#
sl@0
   638
do_test alter-7.1 {
sl@0
   639
  execsql {
sl@0
   640
    CREATE TABLE t1(a TEXT COLLATE BINARY);
sl@0
   641
    ALTER TABLE t1 ADD COLUMN b INTEGER COLLATE NOCASE;
sl@0
   642
    INSERT INTO t1 VALUES(1,'-2');
sl@0
   643
    INSERT INTO t1 VALUES(5.4e-08,'5.4e-08');
sl@0
   644
    SELECT typeof(a), a, typeof(b), b FROM t1;
sl@0
   645
  }
sl@0
   646
} {text 1 integer -2 text 5.4e-08 real 5.4e-08}
sl@0
   647
sl@0
   648
# Make sure that when a column is added by ALTER TABLE ADD COLUMN and has
sl@0
   649
# a default value that the default value is used by aggregate functions.
sl@0
   650
#
sl@0
   651
do_test alter-8.1 {
sl@0
   652
  execsql {
sl@0
   653
    CREATE TABLE t2(a INTEGER);
sl@0
   654
    INSERT INTO t2 VALUES(1);
sl@0
   655
    INSERT INTO t2 VALUES(1);
sl@0
   656
    INSERT INTO t2 VALUES(2);
sl@0
   657
    ALTER TABLE t2 ADD COLUMN b INTEGER DEFAULT 9;
sl@0
   658
    SELECT sum(b) FROM t2;
sl@0
   659
  }
sl@0
   660
} {27}
sl@0
   661
do_test alter-8.2 {
sl@0
   662
  execsql {
sl@0
   663
    SELECT a, sum(b) FROM t2 GROUP BY a;
sl@0
   664
  }
sl@0
   665
} {1 18 2 9}
sl@0
   666
sl@0
   667
#--------------------------------------------------------------------------
sl@0
   668
# alter-9.X - Special test: Make sure the sqlite_rename_trigger() and
sl@0
   669
# rename_table() functions do not crash when handed bad input.
sl@0
   670
#
sl@0
   671
ifcapable trigger {
sl@0
   672
  do_test alter-9.1 {
sl@0
   673
    execsql {SELECT SQLITE_RENAME_TRIGGER(0,0)}
sl@0
   674
  } {{}}
sl@0
   675
}
sl@0
   676
do_test alter-9.2 {
sl@0
   677
  execsql {
sl@0
   678
    SELECT SQLITE_RENAME_TABLE(0,0);
sl@0
   679
    SELECT SQLITE_RENAME_TABLE(10,20);
sl@0
   680
    SELECT SQLITE_RENAME_TABLE("foo", "foo");
sl@0
   681
  }
sl@0
   682
} {{} {} {}}
sl@0
   683
sl@0
   684
#------------------------------------------------------------------------
sl@0
   685
# alter-10.X - Make sure ALTER TABLE works with multi-byte UTF-8 characters 
sl@0
   686
# in the names.
sl@0
   687
#
sl@0
   688
do_test alter-10.1 {
sl@0
   689
  execsql "CREATE TABLE xyz(x UNIQUE)"
sl@0
   690
  execsql "ALTER TABLE xyz RENAME TO xyz\u1234abc"
sl@0
   691
  execsql {SELECT name FROM sqlite_master WHERE name LIKE 'xyz%'}
sl@0
   692
} [list xyz\u1234abc]
sl@0
   693
do_test alter-10.2 {
sl@0
   694
  execsql {SELECT name FROM sqlite_master WHERE name LIKE 'sqlite_autoindex%'}
sl@0
   695
} [list sqlite_autoindex_xyz\u1234abc_1]
sl@0
   696
do_test alter-10.3 {
sl@0
   697
  execsql "ALTER TABLE xyz\u1234abc RENAME TO xyzabc"
sl@0
   698
  execsql {SELECT name FROM sqlite_master WHERE name LIKE 'xyz%'}
sl@0
   699
} [list xyzabc]
sl@0
   700
do_test alter-10.4 {
sl@0
   701
  execsql {SELECT name FROM sqlite_master WHERE name LIKE 'sqlite_autoindex%'}
sl@0
   702
} [list sqlite_autoindex_xyzabc_1]
sl@0
   703
sl@0
   704
do_test alter-11.1 {
sl@0
   705
  sqlite3_exec db {CREATE TABLE t11(%c6%c6)}
sl@0
   706
  execsql {
sl@0
   707
    ALTER TABLE t11 ADD COLUMN abc;
sl@0
   708
  }
sl@0
   709
  catchsql {
sl@0
   710
    ALTER TABLE t11 ADD COLUMN abc;
sl@0
   711
  }
sl@0
   712
} {1 {duplicate column name: abc}}
sl@0
   713
set isutf16 [regexp 16 [db one {PRAGMA encoding}]]
sl@0
   714
if {!$isutf16} {
sl@0
   715
  do_test alter-11.2 {
sl@0
   716
    execsql {INSERT INTO t11 VALUES(1,2)}
sl@0
   717
    sqlite3_exec db {SELECT %c6%c6 AS xyz, abc FROM t11}
sl@0
   718
  } {0 {xyz abc 1 2}}
sl@0
   719
}
sl@0
   720
do_test alter-11.3 {
sl@0
   721
  sqlite3_exec db {CREATE TABLE t11b("%81%82%83" text)}
sl@0
   722
  execsql {
sl@0
   723
    ALTER TABLE t11b ADD COLUMN abc;
sl@0
   724
  }
sl@0
   725
  catchsql {
sl@0
   726
    ALTER TABLE t11b ADD COLUMN abc;
sl@0
   727
  }
sl@0
   728
} {1 {duplicate column name: abc}}
sl@0
   729
if {!$isutf16} {
sl@0
   730
  do_test alter-11.4 {
sl@0
   731
    execsql {INSERT INTO t11b VALUES(3,4)}
sl@0
   732
    sqlite3_exec db {SELECT %81%82%83 AS xyz, abc FROM t11b}
sl@0
   733
  } {0 {xyz abc 3 4}}
sl@0
   734
  do_test alter-11.5 {
sl@0
   735
    sqlite3_exec db {SELECT [%81%82%83] AS xyz, abc FROM t11b}
sl@0
   736
  } {0 {xyz abc 3 4}}
sl@0
   737
  do_test alter-11.6 {
sl@0
   738
    sqlite3_exec db {SELECT "%81%82%83" AS xyz, abc FROM t11b}
sl@0
   739
  } {0 {xyz abc 3 4}}
sl@0
   740
}
sl@0
   741
do_test alter-11.7 {
sl@0
   742
  sqlite3_exec db {CREATE TABLE t11c(%81%82%83 text)}
sl@0
   743
  execsql {
sl@0
   744
    ALTER TABLE t11c ADD COLUMN abc;
sl@0
   745
  }
sl@0
   746
  catchsql {
sl@0
   747
    ALTER TABLE t11c ADD COLUMN abc;
sl@0
   748
  }
sl@0
   749
} {1 {duplicate column name: abc}}
sl@0
   750
if {!$isutf16} {
sl@0
   751
  do_test alter-11.8 {
sl@0
   752
    execsql {INSERT INTO t11c VALUES(5,6)}
sl@0
   753
    sqlite3_exec db {SELECT %81%82%83 AS xyz, abc FROM t11c}
sl@0
   754
  } {0 {xyz abc 5 6}}
sl@0
   755
  do_test alter-11.9 {
sl@0
   756
    sqlite3_exec db {SELECT [%81%82%83] AS xyz, abc FROM t11c}
sl@0
   757
  } {0 {xyz abc 5 6}}
sl@0
   758
  do_test alter-11.10 {
sl@0
   759
    sqlite3_exec db {SELECT "%81%82%83" AS xyz, abc FROM t11c}
sl@0
   760
  } {0 {xyz abc 5 6}}
sl@0
   761
}
sl@0
   762
sl@0
   763
do_test alter-12.1 {
sl@0
   764
  execsql {
sl@0
   765
    CREATE TABLE t12(a, b, c);
sl@0
   766
    CREATE VIEW v1 AS SELECT * FROM t12;
sl@0
   767
  }
sl@0
   768
} {}
sl@0
   769
do_test alter-12.2 {
sl@0
   770
  catchsql {
sl@0
   771
    ALTER TABLE v1 RENAME TO v2;
sl@0
   772
  }
sl@0
   773
} {1 {view v1 may not be altered}}
sl@0
   774
do_test alter-12.3 {
sl@0
   775
  execsql { SELECT * FROM v1; }
sl@0
   776
} {}
sl@0
   777
do_test alter-12.4 {
sl@0
   778
  db close
sl@0
   779
  sqlite3 db test.db
sl@0
   780
  execsql { SELECT * FROM v1; }
sl@0
   781
} {}
sl@0
   782
do_test alter-12.5 {
sl@0
   783
  catchsql { 
sl@0
   784
    ALTER TABLE v1 ADD COLUMN new_column;
sl@0
   785
  }
sl@0
   786
} {1 {Cannot add a column to a view}}
sl@0
   787
sl@0
   788
# Ticket #3102:
sl@0
   789
# Verify that comments do not interfere with the table rename
sl@0
   790
# algorithm.
sl@0
   791
#
sl@0
   792
do_test alter-13.1 {
sl@0
   793
  execsql {
sl@0
   794
    CREATE TABLE /* hi */ t3102a(x);
sl@0
   795
    CREATE TABLE t3102b -- comment
sl@0
   796
    (y);
sl@0
   797
    CREATE INDEX t3102c ON t3102a(x);
sl@0
   798
    SELECT name FROM sqlite_master WHERE name LIKE 't3102%' ORDER BY 1;
sl@0
   799
  }
sl@0
   800
} {t3102a t3102b t3102c}
sl@0
   801
do_test alter-13.2 {
sl@0
   802
  execsql {
sl@0
   803
    ALTER TABLE t3102a RENAME TO t3102a_rename;
sl@0
   804
    SELECT name FROM sqlite_master WHERE name LIKE 't3102%' ORDER BY 1;
sl@0
   805
  }
sl@0
   806
} {t3102a_rename t3102b t3102c}
sl@0
   807
do_test alter-13.3 {
sl@0
   808
  execsql {
sl@0
   809
    ALTER TABLE t3102b RENAME TO t3102b_rename;
sl@0
   810
    SELECT name FROM sqlite_master WHERE name LIKE 't3102%' ORDER BY 1;
sl@0
   811
  }
sl@0
   812
} {t3102a_rename t3102b_rename t3102c}
sl@0
   813
sl@0
   814
finish_test