2 ** This program generates a script that stresses the ALTER TABLE statement.
6 ** gcc -g -o atrc atrc.c sqlite3.o -ldl -lpthread
8 ** Run the program this way:
10 ** ./atrc DATABASE | ./sqlite3 DATABASE
12 ** This program "atrc" generates a script that can be fed into an ordinary
13 ** command-line shell. The script performs many ALTER TABLE statements,
14 ** runs ".schema --indent" and "PRAGMA integrity_check;", does more
15 ** ALTER TABLE statements to restore the original schema, and then
16 ** runs "PRAGMA integrity_check" again. Every table and column has its
17 ** name changed. The entire script is contained within BEGIN...ROLLBACK
18 ** so that no changes are ever actually made to the database.
24 ** Generate the text of ALTER TABLE statements that will rename
25 ** every column in table zTable to a generic name composed from
26 ** zColPrefix and a sequential number. The generated text is
27 ** appended pConvert. If pUndo is not NULL, then SQL text that
28 ** will undo the change is appended to pUndo.
30 ** The table to be converted must be in the "main" schema.
32 int rename_all_columns_of_table(
33 sqlite3
*db
, /* Database connection */
34 const char *zTab
, /* Table whose columns should all be renamed */
35 const char *zColPrefix
, /* Prefix for new column names */
36 sqlite3_str
*pConvert
, /* Append ALTER TABLE statements here */
37 sqlite3_str
*pUndo
/* SQL to undo the change, if not NULL */
43 rc
= sqlite3_prepare_v2(db
,
44 "SELECT name FROM pragma_table_info(?1);",
47 sqlite3_bind_text(pStmt
, 1, zTab
, -1, SQLITE_STATIC
);
48 while( sqlite3_step(pStmt
)==SQLITE_ROW
){
49 const char *zCol
= (const char*)sqlite3_column_text(pStmt
, 0);
51 sqlite3_str_appendf(pConvert
,
52 "ALTER TABLE \"%w\" RENAME COLUMN \"%w\" TO \"%w%d\";\n",
53 zTab
, zCol
, zColPrefix
, cnt
56 sqlite3_str_appendf(pUndo
,
57 "ALTER TABLE \"%w\" RENAME COLUMN \"%w%d\" TO \"%w\";\n",
58 zTab
, zColPrefix
, cnt
, zCol
62 sqlite3_finalize(pStmt
);
66 /* Rename all tables and their columns in the main database
68 int rename_all_tables(
69 sqlite3
*db
, /* Database connection */
70 sqlite3_str
*pConvert
, /* Append SQL to do the rename here */
71 sqlite3_str
*pUndo
/* Append SQL to undo the rename here */
77 rc
= sqlite3_prepare_v2(db
,
78 "SELECT name FROM sqlite_schema WHERE type='table'"
79 " AND name NOT LIKE 'sqlite_%';",
82 while( sqlite3_step(pStmt
)==SQLITE_ROW
){
83 const char *zTab
= (const char*)sqlite3_column_text(pStmt
, 0);
87 zPrefix
[0] = (cnt
%26) + 'a';
89 zNewTab
= sqlite3_mprintf("tx%d", ++cnt
);
91 sqlite3_str_appendf(pUndo
,
92 "ALTER TABLE \"%s\" RENAME TO \"%w\";\n",
96 rename_all_columns_of_table(db
, zTab
, zPrefix
, pConvert
, pUndo
);
97 sqlite3_str_appendf(pConvert
,
98 "ALTER TABLE \"%w\" RENAME TO \"%s\";\n",
101 sqlite3_free(zNewTab
);
103 sqlite3_finalize(pStmt
);
108 ** Generate a script that does this:
110 ** (1) Start a transaction
111 ** (2) Rename all tables and columns to use generic names.
112 ** (3) Print the schema after this rename
113 ** (4) Run pragma integrity_check
114 ** (5) Do more ALTER TABLE statements to change the names back
115 ** (6) Run pragma integrity_check again
116 ** (7) Rollback the transaction
118 int main(int argc
, char **argv
){
121 sqlite3_str
*pConvert
;
126 fprintf(stderr
, "Usage: %s DATABASE\n", argv
[0]);
129 rc
= sqlite3_open(zDbName
, &db
);
131 fprintf(stderr
, "sqlite3_open() returns %d\n", rc
);
134 pConvert
= sqlite3_str_new(db
);
135 pUndo
= sqlite3_str_new(db
);
136 rename_all_tables(db
, pConvert
, pUndo
);
137 zSql1
= sqlite3_str_finish(pConvert
);
138 zSql2
= sqlite3_str_finish(pUndo
);
143 printf(".schema --indent\n");
144 printf("PRAGMA integrity_check;\n");
147 printf("PRAGMA integrity_check;\n");
148 printf("ROLLBACK;\n");