|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "sqlite3.h" |
|
|
#include <stdio.h> |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int rename_all_columns_of_table( |
|
|
sqlite3 *db, |
|
|
const char *zTab, |
|
|
const char *zColPrefix, |
|
|
sqlite3_str *pConvert, |
|
|
sqlite3_str *pUndo |
|
|
){ |
|
|
sqlite3_stmt *pStmt; |
|
|
int rc; |
|
|
int cnt = 0; |
|
|
|
|
|
rc = sqlite3_prepare_v2(db, |
|
|
"SELECT name FROM pragma_table_info(?1);", |
|
|
-1, &pStmt, 0); |
|
|
if( rc ) return rc; |
|
|
sqlite3_bind_text(pStmt, 1, zTab, -1, SQLITE_STATIC); |
|
|
while( sqlite3_step(pStmt)==SQLITE_ROW ){ |
|
|
const char *zCol = (const char*)sqlite3_column_text(pStmt, 0); |
|
|
cnt++; |
|
|
sqlite3_str_appendf(pConvert, |
|
|
"ALTER TABLE \"%w\" RENAME COLUMN \"%w\" TO \"%w%d\";\n", |
|
|
zTab, zCol, zColPrefix, cnt |
|
|
); |
|
|
if( pUndo ){ |
|
|
sqlite3_str_appendf(pUndo, |
|
|
"ALTER TABLE \"%w\" RENAME COLUMN \"%w%d\" TO \"%w\";\n", |
|
|
zTab, zColPrefix, cnt, zCol |
|
|
); |
|
|
} |
|
|
} |
|
|
sqlite3_finalize(pStmt); |
|
|
return SQLITE_OK; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
int rename_all_tables( |
|
|
sqlite3 *db, |
|
|
sqlite3_str *pConvert, |
|
|
sqlite3_str *pUndo |
|
|
){ |
|
|
sqlite3_stmt *pStmt; |
|
|
int rc; |
|
|
int cnt = 0; |
|
|
|
|
|
rc = sqlite3_prepare_v2(db, |
|
|
"SELECT name FROM sqlite_schema WHERE type='table'" |
|
|
" AND name NOT LIKE 'sqlite_%';", |
|
|
-1, &pStmt, 0); |
|
|
if( rc ) return rc; |
|
|
while( sqlite3_step(pStmt)==SQLITE_ROW ){ |
|
|
const char *zTab = (const char*)sqlite3_column_text(pStmt, 0); |
|
|
char *zNewTab; |
|
|
char zPrefix[2]; |
|
|
|
|
|
zPrefix[0] = (cnt%26) + 'a'; |
|
|
zPrefix[1] = 0; |
|
|
zNewTab = sqlite3_mprintf("tx%d", ++cnt); |
|
|
if( pUndo ){ |
|
|
sqlite3_str_appendf(pUndo, |
|
|
"ALTER TABLE \"%s\" RENAME TO \"%w\";\n", |
|
|
zNewTab, zTab |
|
|
); |
|
|
} |
|
|
rename_all_columns_of_table(db, zTab, zPrefix, pConvert, pUndo); |
|
|
sqlite3_str_appendf(pConvert, |
|
|
"ALTER TABLE \"%w\" RENAME TO \"%s\";\n", |
|
|
zTab, zNewTab |
|
|
); |
|
|
sqlite3_free(zNewTab); |
|
|
} |
|
|
sqlite3_finalize(pStmt); |
|
|
return SQLITE_OK; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char **argv){ |
|
|
sqlite3 *db; |
|
|
int rc; |
|
|
sqlite3_str *pConvert; |
|
|
sqlite3_str *pUndo; |
|
|
char *zDbName; |
|
|
char *zSql1, *zSql2; |
|
|
if( argc!=2 ){ |
|
|
fprintf(stderr, "Usage: %s DATABASE\n", argv[0]); |
|
|
} |
|
|
zDbName = argv[1]; |
|
|
rc = sqlite3_open(zDbName, &db); |
|
|
if( rc ){ |
|
|
fprintf(stderr, "sqlite3_open() returns %d\n", rc); |
|
|
return 1; |
|
|
} |
|
|
pConvert = sqlite3_str_new(db); |
|
|
pUndo = sqlite3_str_new(db); |
|
|
rename_all_tables(db, pConvert, pUndo); |
|
|
zSql1 = sqlite3_str_finish(pConvert); |
|
|
zSql2 = sqlite3_str_finish(pUndo); |
|
|
sqlite3_close(db); |
|
|
printf("BEGIN;\n"); |
|
|
printf("%s", zSql1); |
|
|
sqlite3_free(zSql1); |
|
|
printf(".schema --indent\n"); |
|
|
printf("PRAGMA integrity_check;\n"); |
|
|
printf("%s", zSql2); |
|
|
sqlite3_free(zSql2); |
|
|
printf("PRAGMA integrity_check;\n"); |
|
|
printf("ROLLBACK;\n"); |
|
|
return 0; |
|
|
} |
|
|
|