-C In\ssqlite3_declare_vtab(),\savoid\saccessing\sthe\sdatabase\sstructure\suntil\safter\sthe\s"api-armour"\ssafety-check\shas\scompleted\sand\sthe\sdb\smutex\shas\sbeen\sobtained.
-D 2015-04-10T16:05:33.033
+C Work\stoward\sadding\sthe\s--changeset\soption\sto\sthe\ssqldiff\sutility\sprogram.\nChanges\sare\sincomplete.\s\sThis\sis\san\sincremental\scheck-in.
+D 2015-04-10T19:41:18.818
F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f
F Makefile.in 5f78b1ab81b64e7c57a75d170832443e66c0880a
F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23
F tool/speedtest8.c 2902c46588c40b55661e471d7a86e4dd71a18224
F tool/speedtest8inst1.c 293327bc76823f473684d589a8160bde1f52c14e
F tool/split-sqlite3c.tcl d9be87f1c340285a3e081eb19b4a247981ed290c
-F tool/sqldiff.c 050763654cb28d23c4d9516deb348c8632e432cd
+F tool/sqldiff.c 3e6f54359a070089ed0d11456e8868dcd3f20e94
F tool/stack_usage.tcl f8e71b92cdb099a147dad572375595eae55eca43
F tool/symbols-mingw.sh 4dbcea7e74768305384c9fd2ed2b41bbf9f0414d
F tool/symbols.sh fec58532668296d7c7dc48be9c87f75ccdb5814f
F tool/warnings-clang.sh f6aa929dc20ef1f856af04a730772f59283631d4
F tool/warnings.sh 0abfd78ceb09b7f7c27c688c8e3fe93268a13b32
F tool/win/sqlite.vsix deb315d026cc8400325c5863eef847784a219a2f
-P ed3cbaab6ad49b0cb5b17e44def26c866919387a
-R f962720dd8c90d63a1a200cd065fa312
-U dan
-Z 666766483214acd3a759f5cb43f45fc3
+P 860e4f8a94901d451fac3954960c1d2f589e8882
+R 6c8da3b8e133d42c3c0048f2d2a47a98
+T *branch * sqldiff-changeset
+T *sym-sqldiff-changeset *
+T -sym-trunk *
+U drh
+Z b58ac33ed90e0dd7edcdd56b18b68a1d
return;
}
+/*
+** Generate a CHANGESET for all differences from main.zTab to aux.zTab.
+*/
+static void changeset_one_table(const char *zTab, FILE *out){
+ sqlite3_stmt *pStmt; /* SQL statment */
+ char *zId = safeId(zTab); /* Escaped name of the table */
+ char **azCol = 0; /* List of escaped column names */
+ int nCol = 0; /* Number of columns */
+ int *aiFlg = 0; /* 0 if column is not part of PK */
+ int *aiPk = 0; /* Column numbers for each PK column */
+ int nPk = 0; /* Number of PRIMARY KEY columns */
+ Str sql; /* SQL for the diff query */
+ int i; /* Loop counter */
+ const char *zSep; /* List separator */
+
+ pStmt = db_prepare(
+ "SELECT A.sql=B.sql FROM main.sqlite_master A, aux.sqlite_master B"
+ " WHERE A.name=%Q AND B.name=%Q", zTab, zTab
+ );
+ if( SQLITE_ROW==sqlite3_step(pStmt) ){
+ if( sqlite3_column_int(pStmt,0)==0 ){
+ runtimeError("schema changes for table %s", safeId(zTab));
+ }
+ }else{
+ runtimeError("table %s missing from one or both databases", safeId(zTab));
+ }
+ sqlite3_finalize(pStmt);
+ pStmt = db_prepare("PRAGMA main.table_info=%Q", zTab);
+ while( SQLITE_ROW==sqlite3_step(pStmt) ){
+ nCol++;
+ azCol = sqlite3_realloc(azCol, sizeof(char*)*nCol);
+ if( azCol==0 ) runtimeError("out of memory");
+ aiFlg = sqlite3_realloc(aiFlg, sizeof(int)*nCol);
+ if( aiFlg==0 ) runtimeError("out of memory");
+ azCol[nCol-1] = safeId((const char*)sqlite3_column_text(pStmt,1));
+ aiFlg[nCol-1] = i = sqlite3_column_int(pStmt,5);
+ if( i>0 ){
+ if( i>nPk ){
+ nPk = i;
+ aiPk = sqlite3_realloc(aiPk, sizeof(int)*nPk);
+ if( aiPk==0 ) runtimeError("out of memory");
+ }
+ aiPk[i-1] = nCol-1;
+ }
+ }
+ sqlite3_finalize(pStmt);
+ if( nPk==0 ) goto end_changeset_one_table;
+ strInit(&sql);
+ if( nCol>nPk ){
+ strPrintf(&sql, "SELECT 1"); /* Changes to non-PK columns */
+ for(i=0; i<nCol; i++) strPrintf(&sql, ", A.%s", azCol[i]);
+ for(i=0; i<nCol; i++) strPrintf(&sql, ", B.%s", azCol[i]);
+ strPrintf(&sql,"\n FROM main.%s A, aux.%s B\n", zId, zId);
+ zSep = " WHERE";
+ for(i=0; i<nPk; i++){
+ strPrintf(&sql, "%s A.%s=B.%s", zSep, azCol[aiPk[i]], azCol[aiPk[i]]);
+ zSep = " AND";
+ }
+ zSep = "\n AND (";
+ for(i=0; i<nCol; i++){
+ if( aiFlg[i] ) continue;
+ strPrintf(&sql, "%sA.%s IS NOT B.%s", zSep, azCol[i], azCol[i]);
+ zSep = " OR\n ";
+ }
+ strPrintf(&sql,")\n UNION ALL\n");
+ }
+ strPrintf(&sql, "SELECT 2"); /* Deleted rows */
+ for(i=0; i<nCol; i++) strPrintf(&sql, ", A.%s", azCol[i]);
+ for(i=0; i<nCol; i++) strPrintf(&sql, ", 0");
+ strPrintf(&sql, " FROM main.%s A\n", zId);
+ strPrintf(&sql, " WHERE NOT EXISTS(SELECT 1 FROM aux.%s B\n", zId);
+ zSep = " WHERE";
+ for(i=0; i<nPk; i++){
+ strPrintf(&sql, "%s A.%s=B.%s", zSep, azCol[aiPk[i]], azCol[aiPk[i]]);
+ zSep = " AND";
+ }
+ strPrintf(&sql, ")\n UNION ALL\n");
+ strPrintf(&sql, "SELECT 3"); /* Inserted rows */
+ for(i=0; i<nCol; i++) strPrintf(&sql, ", 0");
+ for(i=0; i<nCol; i++) strPrintf(&sql, ", B.%s", azCol[i]);
+ strPrintf(&sql, " FROM aux.%s B\n", zId);
+ strPrintf(&sql, " WHERE NOT EXISTS(SELECT 1 FROM main.%s A\n", zId);
+ zSep = " WHERE";
+ for(i=0; i<nPk; i++){
+ strPrintf(&sql, "%s A.%s=B.%s", zSep, azCol[aiPk[i]], azCol[aiPk[i]]);
+ zSep = " AND";
+ }
+ strPrintf(&sql, ")\n");
+ strPrintf(&sql, " ORDER BY");
+ zSep = " ";
+ for(i=0; i<nPk; i++){
+ strPrintf(&sql, "%s %d", zSep, aiPk[i]+1);
+ zSep = ",";
+ }
+ strPrintf(&sql, ";\n");
+
+printf("for table %s:\n%s\n", zId, sql.z);
+strFree(&sql);
+
+
+end_changeset_one_table:
+ while( nCol>0 ) sqlite3_free(azCol[--nCol]);
+ sqlite3_free(azCol);
+ sqlite3_free(aiPk);
+ sqlite3_free(zId);
+}
+
/*
** Print sketchy documentation for this utility program
*/
printf(
"Output SQL text that would transform DB1 into DB2.\n"
"Options:\n"
+" --changeset FILE Write a CHANGESET into FILE\n"
" --primarykey Use schema-defined PRIMARY KEYs\n"
" --schema Show only differences in the schema\n"
" --table TAB Show only differences in table TAB\n"
char *zSql;
sqlite3_stmt *pStmt;
char *zTab = 0;
+ FILE *out = 0;
g.zArgv0 = argv[0];
for(i=1; i<argc; i++){
if( z[0]=='-' ){
z++;
if( z[0]=='-' ) z++;
+ if( strcmp(z,"changeset")==0 ){
+ out = fopen(argv[++i], "wb");
+ if( out==0 ) cmdlineError("cannot open: %s", argv[i]);
+ }else
if( strcmp(z,"debug")==0 ){
g.fDebug = strtol(argv[++i], 0, 0);
}else
}
if( zTab ){
- diff_one_table(zTab);
+ if( out ){
+ changeset_one_table(zTab, out);
+ }else{
+ diff_one_table(zTab);
+ }
}else{
/* Handle tables one by one */
pStmt = db_prepare(
" ORDER BY name"
);
while( SQLITE_ROW==sqlite3_step(pStmt) ){
- diff_one_table((const char*)sqlite3_column_text(pStmt, 0));
+ const char *zTab = (const char*)sqlite3_column_text(pStmt,0);
+ if( out ){
+ changeset_one_table(zTab, out);
+ }else{
+ diff_one_table(zTab);
+ }
}
sqlite3_finalize(pStmt);
}