SQLite

Check-in [6e97186880]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Update the sqlite_sequence table when a table is renamed with ALTER_TABLE. (CVS 2114)
Downloads: Tarball | ZIP archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 6e971868808e3c3f77fa521de626f1510ba9644a
User & Date: danielk1977 2004-11-19 08:02:14.000
Context
2004-11-19
08:41
Ensure ALTER TABLE respects the system table convention - "sqlite_*". (CVS 2115) (check-in: f635b6aae6 user: danielk1977 tags: trunk)
08:02
Update the sqlite_sequence table when a table is renamed with ALTER_TABLE. (CVS 2114) (check-in: 6e97186880 user: danielk1977 tags: trunk)
07:07
When dropping a table, update the sqlite_sequence table first, as auto-vacuum mode may need to move sqlite_sequence when the btree table is dropped. (CVS 2113) (check-in: 0514107bff user: danielk1977 tags: trunk)
Changes
Unified Diff Ignore Whitespace Patch
Changes to src/build.c.
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
**     CREATE INDEX
**     DROP INDEX
**     creating ID lists
**     BEGIN TRANSACTION
**     COMMIT
**     ROLLBACK
**
** $Id: build.c,v 1.282 2004/11/19 07:07:31 danielk1977 Exp $
*/
#include "sqliteInt.h"
#include <ctype.h>

/*
** This routine is called when a new SQL statement is beginning to
** be parsed.  Check to see if the schema for the database needs







|







18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
**     CREATE INDEX
**     DROP INDEX
**     creating ID lists
**     BEGIN TRANSACTION
**     COMMIT
**     ROLLBACK
**
** $Id: build.c,v 1.283 2004/11/19 08:02:14 danielk1977 Exp $
*/
#include "sqliteInt.h"
#include <ctype.h>

/*
** This routine is called when a new SQL statement is beginning to
** be parsed.  Check to see if the schema for the database needs
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001











3002
3003
3004
3005
3006
3007
3008
          "tbl_name = %Q, "
          "name = CASE "
            "WHEN type='table' THEN %Q "
            "WHEN name LIKE 'sqlite_autoindex%%' AND type='index' THEN "
              "'sqlite_autoindex_' || %Q || substr(name, %d+18,10) "
            "ELSE name END "
      "WHERE tbl_name=%Q AND type IN ('table', 'index', 'trigger');", 
      db->aDb[iDb].zName, SCHEMA_TABLE(iDb), zName, zName, zName, zName, 
      zName, strlen(pTab->zName), pTab->zName
  );












#ifndef SQLITE_OMIT_TRIGGER
  /* If there are TEMP triggers on this table, modify the sqlite_temp_master
  ** table. Don't do this if the table being ALTERed is itself located in
  ** the temp database.
  */
  if( iDb!=1 ){







|


>
>
>
>
>
>
>
>
>
>
>







2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
          "tbl_name = %Q, "
          "name = CASE "
            "WHEN type='table' THEN %Q "
            "WHEN name LIKE 'sqlite_autoindex%%' AND type='index' THEN "
              "'sqlite_autoindex_' || %Q || substr(name, %d+18,10) "
            "ELSE name END "
      "WHERE tbl_name=%Q AND type IN ('table', 'index', 'trigger');", 
      zDb, SCHEMA_TABLE(iDb), zName, zName, zName, zName, 
      zName, strlen(pTab->zName), pTab->zName
  );

#ifndef SQLITE_OMIT_AUTOINCREMENT
  /* If the sqlite_sequence table exists in this database, then update 
  ** it with the new table name.
  */
  if( sqlite3FindTable(db, "sqlite_sequence", zDb) ){
    sqlite3NestedParse(pParse,
        "UPDATE %Q.sqlite_sequence set name = %Q WHERE name = %Q",
        zDb, zName, pTab->zName);
  }
#endif

#ifndef SQLITE_OMIT_TRIGGER
  /* If there are TEMP triggers on this table, modify the sqlite_temp_master
  ** table. Don't do this if the table being ALTERed is itself located in
  ** the temp database.
  */
  if( iDb!=1 ){
Changes to test/alter.test.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#
# The author or author's hereby grant to the public domain a non-exclusive,
# fully paid-up, perpetual, license in the software and all related
# intellectual property to make, have made, use, have used, reproduce,
# prepare derivative works, distribute, perform and display the work.  
#
#*************************************************************************
# This file implements regression tests for SQLite library.  The
# focus of this script is testing the ALTER TABLE statement.
#
# $Id: alter.test,v 1.4 2004/11/19 05:14:56 danielk1977 Exp $
#

set testdir [file dirname $argv0]
source $testdir/tester.tcl

# If SQLITE_OMIT_ALTERTABLE is defined, omit this file.
ifcapable !altertable {










|







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#
# The author or author's hereby grant to the public domain a non-exclusive,
# fully paid-up, perpetual, license in the software and all related
# intellectual property to make, have made, use, have used, reproduce,
# prepare derivative works, distribute, perform and display the work.  
#
#*************************************************************************
# This file implements regression tests for SQLite library.  The
# focus of this script is testing the ALTER TABLE statement.
#
# $Id: alter.test,v 1.5 2004/11/19 08:02:14 danielk1977 Exp $
#

set testdir [file dirname $argv0]
source $testdir/tester.tcl

# If SQLITE_OMIT_ALTERTABLE is defined, omit this file.
ifcapable !altertable {
29
30
31
32
33
34
35

36
37
38
39
40
41
42
# alter-1.8.*: Tests for ALTER TABLE when the table resides in an 
#     attached database.
# alter-1.9.*: Tests for ALTER TABLE when their is whitespace between the
#     table name and left parenthesis token. i.e: 
#     "CREATE TABLE abc       (a, b, c);"
# alter-2.*: Test error conditions and messages.
# alter-3.*: Test ALTER TABLE on tables that have TRIGGERs attached to them.

#

# Create some tables to rename.  Be sure to include some TEMP tables
# and some tables with odd names.
#
do_test alter-1.1 {
  execsql {







>







29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# alter-1.8.*: Tests for ALTER TABLE when the table resides in an 
#     attached database.
# alter-1.9.*: Tests for ALTER TABLE when their is whitespace between the
#     table name and left parenthesis token. i.e: 
#     "CREATE TABLE abc       (a, b, c);"
# alter-2.*: Test error conditions and messages.
# alter-3.*: Test ALTER TABLE on tables that have TRIGGERs attached to them.
# alter-4.*: Test ALTER TABLE on tables that have AUTOINCREMENT fields.
#

# Create some tables to rename.  Be sure to include some TEMP tables
# and some tables with odd names.
#
do_test alter-1.1 {
  execsql {
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
} {1 {there is already another table or index with this name: t3}}
do_test alter-2.3 {
  catchsql {
    ALTER TABLE [<t2>] RENAME TO i3;
  }
} {1 {there is already another table or index with this name: i3}}

# If this compilation does not include triggers, omit the remainder
# of this file.
ifcapable !trigger {
  finish_test
  return
}

#-----------------------------------------------------------------------
# Tests alter-3.* test ALTER TABLE on tables that have triggers.
#
# alter-3.1.*: ALTER TABLE with triggers.
# alter-3.2.*: Test that the ON keyword cannot be used as a database,
#     table or column name unquoted. This is done because part of the







|
<
|
<
<
<







252
253
254
255
256
257
258
259

260



261
262
263
264
265
266
267
} {1 {there is already another table or index with this name: t3}}
do_test alter-2.3 {
  catchsql {
    ALTER TABLE [<t2>] RENAME TO i3;
  }
} {1 {there is already another table or index with this name: i3}}

# If this compilation does not include triggers, omit the alter-3.* tests.

ifcapable trigger {




#-----------------------------------------------------------------------
# Tests alter-3.* test ALTER TABLE on tables that have triggers.
#
# alter-3.1.*: ALTER TABLE with triggers.
# alter-3.2.*: Test that the ON keyword cannot be used as a database,
#     table or column name unquoted. This is done because part of the
447
448
449
450
451
452
453
454




























455
} {trig2 G h i}
do_test alter-3.3.7 {
  execsql {
    DROP TABLE tbl3;
    SELECT * FROM sqlite_temp_master WHERE type = 'trigger';
  }
} {}





























finish_test








>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>

444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
} {trig2 G h i}
do_test alter-3.3.7 {
  execsql {
    DROP TABLE tbl3;
    SELECT * FROM sqlite_temp_master WHERE type = 'trigger';
  }
} {}

} ;# ifcapable trigger

# If the build does not include AUTOINCREMENT fields, omit alter-4.*.
ifcapable autoinc {

do_test alter-4.1 {
  execsql {
    CREATE TABLE tbl1(a INTEGER PRIMARY KEY AUTOINCREMENT);
    INSERT INTO tbl1 VALUES(10);
  }
} {}
do_test alter-4.2 {
  execsql {
    INSERT INTO tbl1 VALUES(NULL);
    SELECT a FROM tbl1;
  }
} {10 11}
do_test alter-4.3 {
  execsql {
    ALTER TABLE tbl1 RENAME TO tbl2;
    DELETE FROM tbl2;
    INSERT INTO tbl2 VALUES(NULL);
    SELECT a FROM tbl2;
  }
} {12}

} ;# ifcapable autoinc

finish_test