Add extra checks for the validity of a numeric literal to sqlite3DequoteNumber().
[sqlite.git] / tool / sqldiff.c
blobcbdfc35cd0367464eba7760cdb2c3b5b6ea38a61
1 /*
2 ** 2015-04-06
3 **
4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
6 **
7 ** May you do good and not evil.
8 ** May you find forgiveness for yourself and forgive others.
9 ** May you share freely, never taking more than you give.
11 *************************************************************************
13 ** This is a utility program that computes the differences in content
14 ** between two SQLite databases.
16 ** To compile, simply link against SQLite. (Windows builds must also link
17 ** against ext/consio/console_io.c.)
19 ** See the showHelp() routine below for a brief description of how to
20 ** run the utility.
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <stdarg.h>
25 #include <ctype.h>
26 #include <string.h>
27 #include <assert.h>
28 #include "sqlite3.h"
30 /* Output function substitutions that cause UTF8 characters to be rendered
31 ** correctly on Windows:
33 ** fprintf() -> Wfprintf()
34 **
36 #if defined(_WIN32)
37 # include "console_io.h"
38 # define Wfprintf fPrintfUtf8
39 #else
40 # define Wfprintf fprintf
41 #endif
44 ** All global variables are gathered into the "g" singleton.
46 struct GlobalVars {
47 const char *zArgv0; /* Name of program */
48 int bSchemaOnly; /* Only show schema differences */
49 int bSchemaPK; /* Use the schema-defined PK, not the true PK */
50 int bHandleVtab; /* Handle fts3, fts4, fts5 and rtree vtabs */
51 unsigned fDebug; /* Debug flags */
52 int bSchemaCompare; /* Doing single-table sqlite_schema compare */
53 sqlite3 *db; /* The database connection */
54 } g;
57 ** Allowed values for g.fDebug
59 #define DEBUG_COLUMN_NAMES 0x000001
60 #define DEBUG_DIFF_SQL 0x000002
63 ** Clear and free an sqlite3_str object
65 static void strFree(sqlite3_str *pStr){
66 sqlite3_free(sqlite3_str_finish(pStr));
70 ** Print an error resulting from faulting command-line arguments and
71 ** abort the program.
73 static void cmdlineError(const char *zFormat, ...){
74 sqlite3_str *pOut = sqlite3_str_new(0);
75 va_list ap;
76 va_start(ap, zFormat);
77 sqlite3_str_vappendf(pOut, zFormat, ap);
78 va_end(ap);
79 Wfprintf(stderr, "%s: %s\n", g.zArgv0, sqlite3_str_value(pOut));
80 strFree(pOut);
81 Wfprintf(stderr, "\"%s --help\" for more help\n", g.zArgv0);
82 exit(1);
86 ** Print an error message for an error that occurs at runtime, then
87 ** abort the program.
89 static void runtimeError(const char *zFormat, ...){
90 sqlite3_str *pOut = sqlite3_str_new(0);
91 va_list ap;
92 va_start(ap, zFormat);
93 sqlite3_str_vappendf(pOut, zFormat, ap);
94 va_end(ap);
95 Wfprintf(stderr, "%s: %s\n", g.zArgv0, sqlite3_str_value(pOut));
96 strFree(pOut);
97 exit(1);
101 /* Safely quote an SQL identifier. Use the minimum amount of transformation
102 ** necessary to allow the string to be used with %s.
104 ** Space to hold the returned string is obtained from sqlite3_malloc(). The
105 ** caller is responsible for ensuring this space is freed when no longer
106 ** needed.
108 static char *safeId(const char *zId){
109 int i, x;
110 char c;
111 if( zId[0]==0 ) return sqlite3_mprintf("\"\"");
112 for(i=x=0; (c = zId[i])!=0; i++){
113 if( !isalpha(c) && c!='_' ){
114 if( i>0 && isdigit(c) ){
115 x++;
116 }else{
117 return sqlite3_mprintf("\"%w\"", zId);
121 if( x || !sqlite3_keyword_check(zId,i) ){
122 return sqlite3_mprintf("%s", zId);
124 return sqlite3_mprintf("\"%w\"", zId);
128 ** Prepare a new SQL statement. Print an error and abort if anything
129 ** goes wrong.
131 static sqlite3_stmt *db_vprepare(const char *zFormat, va_list ap){
132 char *zSql;
133 int rc;
134 sqlite3_stmt *pStmt;
136 zSql = sqlite3_vmprintf(zFormat, ap);
137 if( zSql==0 ) runtimeError("out of memory");
138 rc = sqlite3_prepare_v2(g.db, zSql, -1, &pStmt, 0);
139 if( rc ){
140 runtimeError("SQL statement error: %s\n\"%s\"", sqlite3_errmsg(g.db),
141 zSql);
143 sqlite3_free(zSql);
144 return pStmt;
146 static sqlite3_stmt *db_prepare(const char *zFormat, ...){
147 va_list ap;
148 sqlite3_stmt *pStmt;
149 va_start(ap, zFormat);
150 pStmt = db_vprepare(zFormat, ap);
151 va_end(ap);
152 return pStmt;
156 ** Free a list of strings
158 static void namelistFree(char **az){
159 if( az ){
160 int i;
161 for(i=0; az[i]; i++) sqlite3_free(az[i]);
162 sqlite3_free(az);
167 ** Return a list of column names [a] for the table zDb.zTab. Space to
168 ** hold the list is obtained from sqlite3_malloc() and should released
169 ** using namelistFree() when no longer needed.
171 ** Primary key columns are listed first, followed by data columns.
172 ** The number of columns in the primary key is returned in *pnPkey.
174 ** Normally [a], the "primary key" in the previous sentence is the true
175 ** primary key - the rowid or INTEGER PRIMARY KEY for ordinary tables
176 ** or the declared PRIMARY KEY for WITHOUT ROWID tables. However, if
177 ** the g.bSchemaPK flag is set, then the schema-defined PRIMARY KEY is
178 ** used in all cases. In that case, entries that have NULL values in
179 ** any of their primary key fields will be excluded from the analysis.
181 ** If the primary key for a table is the rowid but rowid is inaccessible,
182 ** then this routine returns a NULL pointer.
184 ** [a. If the lone, named table is "sqlite_schema", "rootpage" column is
185 ** omitted and the "type" and "name" columns are made to be the PK.]
187 ** Examples:
188 ** CREATE TABLE t1(a INT UNIQUE, b INTEGER, c TEXT, PRIMARY KEY(c));
189 ** *pnPKey = 1;
190 ** az = { "rowid", "a", "b", "c", 0 } // Normal case
191 ** az = { "c", "a", "b", 0 } // g.bSchemaPK==1
193 ** CREATE TABLE t2(a INT UNIQUE, b INTEGER, c TEXT, PRIMARY KEY(b));
194 ** *pnPKey = 1;
195 ** az = { "b", "a", "c", 0 }
197 ** CREATE TABLE t3(x,y,z,PRIMARY KEY(y,z));
198 ** *pnPKey = 1 // Normal case
199 ** az = { "rowid", "x", "y", "z", 0 } // Normal case
200 ** *pnPKey = 2 // g.bSchemaPK==1
201 ** az = { "y", "x", "z", 0 } // g.bSchemaPK==1
203 ** CREATE TABLE t4(x,y,z,PRIMARY KEY(y,z)) WITHOUT ROWID;
204 ** *pnPKey = 2
205 ** az = { "y", "z", "x", 0 }
207 ** CREATE TABLE t5(rowid,_rowid_,oid);
208 ** az = 0 // The rowid is not accessible
210 static char **columnNames(
211 const char *zDb, /* Database ("main" or "aux") to query */
212 const char *zTab, /* Name of table to return details of */
213 int *pnPKey, /* OUT: Number of PK columns */
214 int *pbRowid /* OUT: True if PK is an implicit rowid */
216 char **az = 0; /* List of column names to be returned */
217 int naz = 0; /* Number of entries in az[] */
218 sqlite3_stmt *pStmt; /* SQL statement being run */
219 char *zPkIdxName = 0; /* Name of the PRIMARY KEY index */
220 int truePk = 0; /* PRAGMA table_info indentifies the PK to use */
221 int nPK = 0; /* Number of PRIMARY KEY columns */
222 int i, j; /* Loop counters */
224 if( g.bSchemaPK==0 ){
225 /* Normal case: Figure out what the true primary key is for the table.
226 ** * For WITHOUT ROWID tables, the true primary key is the same as
227 ** the schema PRIMARY KEY, which is guaranteed to be present.
228 ** * For rowid tables with an INTEGER PRIMARY KEY, the true primary
229 ** key is the INTEGER PRIMARY KEY.
230 ** * For all other rowid tables, the rowid is the true primary key.
232 pStmt = db_prepare("PRAGMA %s.index_list=%Q", zDb, zTab);
233 while( SQLITE_ROW==sqlite3_step(pStmt) ){
234 if( sqlite3_stricmp((const char*)sqlite3_column_text(pStmt,3),"pk")==0 ){
235 zPkIdxName = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 1));
236 break;
239 sqlite3_finalize(pStmt);
240 if( zPkIdxName ){
241 int nKey = 0;
242 int nCol = 0;
243 truePk = 0;
244 pStmt = db_prepare("PRAGMA %s.index_xinfo=%Q", zDb, zPkIdxName);
245 while( SQLITE_ROW==sqlite3_step(pStmt) ){
246 nCol++;
247 if( sqlite3_column_int(pStmt,5) ){ nKey++; continue; }
248 if( sqlite3_column_int(pStmt,1)>=0 ) truePk = 1;
250 if( nCol==nKey ) truePk = 1;
251 if( truePk ){
252 nPK = nKey;
253 }else{
254 nPK = 1;
256 sqlite3_finalize(pStmt);
257 sqlite3_free(zPkIdxName);
258 }else{
259 truePk = 1;
260 nPK = 1;
262 pStmt = db_prepare("PRAGMA %s.table_info=%Q", zDb, zTab);
263 }else{
264 /* The g.bSchemaPK==1 case: Use whatever primary key is declared
265 ** in the schema. The "rowid" will still be used as the primary key
266 ** if the table definition does not contain a PRIMARY KEY.
268 nPK = 0;
269 pStmt = db_prepare("PRAGMA %s.table_info=%Q", zDb, zTab);
270 while( SQLITE_ROW==sqlite3_step(pStmt) ){
271 if( sqlite3_column_int(pStmt,5)>0 ) nPK++;
273 sqlite3_reset(pStmt);
274 if( nPK==0 ) nPK = 1;
275 truePk = 1;
277 if( g.bSchemaCompare ){
278 assert( sqlite3_stricmp(zTab,"sqlite_schema")==0
279 || sqlite3_stricmp(zTab,"sqlite_master")==0 );
280 /* For sqlite_schema, will use type and name as the PK. */
281 nPK = 2;
282 truePk = 0;
284 *pnPKey = nPK;
285 naz = nPK;
286 az = sqlite3_malloc( sizeof(char*)*(nPK+1) );
287 if( az==0 ) runtimeError("out of memory");
288 memset(az, 0, sizeof(char*)*(nPK+1));
289 if( g.bSchemaCompare ){
290 az[0] = sqlite3_mprintf("%s", "type");
291 az[1] = sqlite3_mprintf("%s", "name");
293 while( SQLITE_ROW==sqlite3_step(pStmt) ){
294 char * sid = safeId((char*)sqlite3_column_text(pStmt,1));
295 int iPKey;
296 if( truePk && (iPKey = sqlite3_column_int(pStmt,5))>0 ){
297 az[iPKey-1] = sid;
298 }else{
299 if( !g.bSchemaCompare
300 || !(strcmp(sid,"rootpage")==0
301 ||strcmp(sid,"name")==0
302 ||strcmp(sid,"type")==0)){
303 az = sqlite3_realloc(az, sizeof(char*)*(naz+2) );
304 if( az==0 ) runtimeError("out of memory");
305 az[naz++] = sid;
309 sqlite3_finalize(pStmt);
310 if( az ) az[naz] = 0;
312 /* If it is non-NULL, set *pbRowid to indicate whether or not the PK of
313 ** this table is an implicit rowid (*pbRowid==1) or not (*pbRowid==0). */
314 if( pbRowid ) *pbRowid = (az[0]==0);
316 /* If this table has an implicit rowid for a PK, figure out how to refer
317 ** to it. There are usually three options - "rowid", "_rowid_" and "oid".
318 ** Any of these will work, unless the table has an explicit column of the
319 ** same name or the sqlite_schema tables are to be compared. In the latter
320 ** case, pretend that the "true" primary key is the name column, which
321 ** avoids extraneous diffs against the schemas due to rowid variance. */
322 if( az[0]==0 ){
323 const char *azRowid[] = { "rowid", "_rowid_", "oid" };
324 for(i=0; i<sizeof(azRowid)/sizeof(azRowid[0]); i++){
325 for(j=1; j<naz; j++){
326 if( sqlite3_stricmp(az[j], azRowid[i])==0 ) break;
328 if( j>=naz ){
329 az[0] = sqlite3_mprintf("%s", azRowid[i]);
330 break;
333 if( az[0]==0 ){
334 for(i=1; i<naz; i++) sqlite3_free(az[i]);
335 sqlite3_free(az);
336 az = 0;
339 return az;
343 ** Print the sqlite3_value X as an SQL literal.
345 static void printQuoted(FILE *out, sqlite3_value *X){
346 switch( sqlite3_value_type(X) ){
347 case SQLITE_FLOAT: {
348 double r1;
349 char zBuf[50];
350 r1 = sqlite3_value_double(X);
351 sqlite3_snprintf(sizeof(zBuf), zBuf, "%!.15g", r1);
352 fprintf(out, "%s", zBuf);
353 break;
355 case SQLITE_INTEGER: {
356 fprintf(out, "%lld", sqlite3_value_int64(X));
357 break;
359 case SQLITE_BLOB: {
360 const unsigned char *zBlob = sqlite3_value_blob(X);
361 int nBlob = sqlite3_value_bytes(X);
362 if( zBlob ){
363 int i;
364 fprintf(out, "x'");
365 for(i=0; i<nBlob; i++){
366 fprintf(out, "%02x", zBlob[i]);
368 fprintf(out, "'");
369 }else{
370 /* Could be an OOM, could be a zero-byte blob */
371 fprintf(out, "X''");
373 break;
375 case SQLITE_TEXT: {
376 const unsigned char *zArg = sqlite3_value_text(X);
378 if( zArg==0 ){
379 fprintf(out, "NULL");
380 }else{
381 int inctl = 0;
382 int i, j;
383 fprintf(out, "'");
384 for(i=j=0; zArg[i]; i++){
385 char c = zArg[i];
386 int ctl = iscntrl(c);
387 if( ctl>inctl ){
388 inctl = ctl;
389 fprintf(out, "%.*s'||X'%02x", i-j, &zArg[j], c);
390 j = i+1;
391 }else if( ctl ){
392 fprintf(out, "%02x", c);
393 j = i+1;
394 }else{
395 if( inctl ){
396 inctl = 0;
397 fprintf(out, "'\n||'");
399 if( c=='\'' ){
400 fprintf(out, "%.*s'", i-j+1, &zArg[j]);
401 j = i+1;
405 fprintf(out, "%s'", &zArg[j]);
407 break;
409 case SQLITE_NULL: {
410 fprintf(out, "NULL");
411 break;
417 ** Output SQL that will recreate the aux.zTab table.
419 static void dump_table(const char *zTab, FILE *out){
420 char *zId = safeId(zTab); /* Name of the table */
421 char **az = 0; /* List of columns */
422 int nPk; /* Number of true primary key columns */
423 int nCol; /* Number of data columns */
424 int i; /* Loop counter */
425 sqlite3_stmt *pStmt; /* SQL statement */
426 const char *zSep; /* Separator string */
427 sqlite3_str *pIns; /* Beginning of the INSERT statement */
429 pStmt = db_prepare("SELECT sql FROM aux.sqlite_schema WHERE name=%Q", zTab);
430 if( SQLITE_ROW==sqlite3_step(pStmt) ){
431 fprintf(out, "%s;\n", sqlite3_column_text(pStmt,0));
433 sqlite3_finalize(pStmt);
434 if( !g.bSchemaOnly ){
435 az = columnNames("aux", zTab, &nPk, 0);
436 pIns = sqlite3_str_new(0);
437 if( az==0 ){
438 pStmt = db_prepare("SELECT * FROM aux.%s", zId);
439 sqlite3_str_appendf(pIns,"INSERT INTO %s VALUES", zId);
440 }else{
441 sqlite3_str *pSql = sqlite3_str_new(0);
442 zSep = "SELECT";
443 for(i=0; az[i]; i++){
444 sqlite3_str_appendf(pSql, "%s %s", zSep, az[i]);
445 zSep = ",";
447 sqlite3_str_appendf(pSql," FROM aux.%s", zId);
448 zSep = " ORDER BY";
449 for(i=1; i<=nPk; i++){
450 sqlite3_str_appendf(pSql, "%s %d", zSep, i);
451 zSep = ",";
453 pStmt = db_prepare("%s", sqlite3_str_value(pSql));
454 strFree(pSql);
455 sqlite3_str_appendf(pIns, "INSERT INTO %s", zId);
456 zSep = "(";
457 for(i=0; az[i]; i++){
458 sqlite3_str_appendf(pIns, "%s%s", zSep, az[i]);
459 zSep = ",";
461 sqlite3_str_appendf(pIns,") VALUES");
462 namelistFree(az);
464 nCol = sqlite3_column_count(pStmt);
465 while( SQLITE_ROW==sqlite3_step(pStmt) ){
466 Wfprintf(out, "%s",sqlite3_str_value(pIns));
467 zSep = "(";
468 for(i=0; i<nCol; i++){
469 Wfprintf(out, "%s",zSep);
470 printQuoted(out, sqlite3_column_value(pStmt,i));
471 zSep = ",";
473 Wfprintf(out, ");\n");
475 sqlite3_finalize(pStmt);
476 strFree(pIns);
477 } /* endif !g.bSchemaOnly */
478 pStmt = db_prepare("SELECT sql FROM aux.sqlite_schema"
479 " WHERE type='index' AND tbl_name=%Q AND sql IS NOT NULL",
480 zTab);
481 while( SQLITE_ROW==sqlite3_step(pStmt) ){
482 Wfprintf(out, "%s;\n", sqlite3_column_text(pStmt,0));
484 sqlite3_finalize(pStmt);
485 sqlite3_free(zId);
490 ** Compute all differences for a single table, except if the
491 ** table name is sqlite_schema, ignore the rootpage column.
493 static void diff_one_table(const char *zTab, FILE *out){
494 char *zId = safeId(zTab); /* Name of table (translated for us in SQL) */
495 char **az = 0; /* Columns in main */
496 char **az2 = 0; /* Columns in aux */
497 int nPk; /* Primary key columns in main */
498 int nPk2; /* Primary key columns in aux */
499 int n = 0; /* Number of columns in main */
500 int n2; /* Number of columns in aux */
501 int nQ; /* Number of output columns in the diff query */
502 int i; /* Loop counter */
503 const char *zSep; /* Separator string */
504 sqlite3_str *pSql; /* Comparison query */
505 sqlite3_stmt *pStmt; /* Query statement to do the diff */
506 const char *zLead = /* Becomes line-comment for sqlite_schema */
507 (g.bSchemaCompare)? "-- " : "";
509 pSql = sqlite3_str_new(0);
510 if( g.fDebug==DEBUG_COLUMN_NAMES ){
511 /* Simply run columnNames() on all tables of the origin
512 ** database and show the results. This is used for testing
513 ** and debugging of the columnNames() function.
515 az = columnNames("aux",zTab, &nPk, 0);
516 if( az==0 ){
517 Wfprintf(stdout, "Rowid not accessible for %s\n", zId);
518 }else{
519 Wfprintf(stdout, "%s:", zId);
520 for(i=0; az[i]; i++){
521 Wfprintf(stdout, " %s", az[i]);
522 if( i+1==nPk ) Wfprintf(stdout, " *");
524 Wfprintf(stdout, "\n");
526 goto end_diff_one_table;
529 if( sqlite3_table_column_metadata(g.db,"aux",zTab,0,0,0,0,0,0) ){
530 if( !sqlite3_table_column_metadata(g.db,"main",zTab,0,0,0,0,0,0) ){
531 /* Table missing from second database. */
532 if( g.bSchemaCompare )
533 Wfprintf(out, "-- 2nd DB has no %s table\n", zTab);
534 else
535 Wfprintf(out, "DROP TABLE %s;\n", zId);
537 goto end_diff_one_table;
540 if( sqlite3_table_column_metadata(g.db,"main",zTab,0,0,0,0,0,0) ){
541 /* Table missing from source */
542 if( g.bSchemaCompare ){
543 Wfprintf(out, "-- 1st DB has no %s table\n", zTab);
544 }else{
545 dump_table(zTab, out);
547 goto end_diff_one_table;
550 az = columnNames("main", zTab, &nPk, 0);
551 az2 = columnNames("aux", zTab, &nPk2, 0);
552 if( az && az2 ){
553 for(n=0; az[n] && az2[n]; n++){
554 if( sqlite3_stricmp(az[n],az2[n])!=0 ) break;
557 if( az==0
558 || az2==0
559 || nPk!=nPk2
560 || az[n]
562 /* Schema mismatch */
563 Wfprintf(out, "%sDROP TABLE %s; -- due to schema mismatch\n", zLead, zId);
564 dump_table(zTab, out);
565 goto end_diff_one_table;
568 /* Build the comparison query */
569 for(n2=n; az2[n2]; n2++){
570 char *zNTab = safeId(az2[n2]);
571 Wfprintf(out, "ALTER TABLE %s ADD COLUMN %s;\n", zId, zNTab);
572 sqlite3_free(zNTab);
574 nQ = nPk2+1+2*(n2-nPk2);
575 if( n2>nPk2 ){
576 zSep = "SELECT ";
577 for(i=0; i<nPk; i++){
578 sqlite3_str_appendf(pSql, "%sB.%s", zSep, az[i]);
579 zSep = ", ";
581 sqlite3_str_appendf(pSql, ", 1 /* changed row */");
582 while( az[i] ){
583 sqlite3_str_appendf(pSql, ", A.%s IS NOT B.%s, B.%s",
584 az[i], az2[i], az2[i]);
585 i++;
587 while( az2[i] ){
588 sqlite3_str_appendf(pSql, ", B.%s IS NOT NULL, B.%s",
589 az2[i], az2[i]);
590 i++;
592 sqlite3_str_appendf(pSql, "\n FROM main.%s A, aux.%s B\n", zId, zId);
593 zSep = " WHERE";
594 for(i=0; i<nPk; i++){
595 sqlite3_str_appendf(pSql, "%s A.%s=B.%s", zSep, az[i], az[i]);
596 zSep = " AND";
598 zSep = "\n AND (";
599 while( az[i] ){
600 sqlite3_str_appendf(pSql, "%sA.%s IS NOT B.%s%s\n",
601 zSep, az[i], az2[i], az2[i+1]==0 ? ")" : "");
602 zSep = " OR ";
603 i++;
605 while( az2[i] ){
606 sqlite3_str_appendf(pSql, "%sB.%s IS NOT NULL%s\n",
607 zSep, az2[i], az2[i+1]==0 ? ")" : "");
608 zSep = " OR ";
609 i++;
611 sqlite3_str_appendf(pSql, " UNION ALL\n");
613 zSep = "SELECT ";
614 for(i=0; i<nPk; i++){
615 sqlite3_str_appendf(pSql, "%sA.%s", zSep, az[i]);
616 zSep = ", ";
618 sqlite3_str_appendf(pSql, ", 2 /* deleted row */");
619 while( az2[i] ){
620 sqlite3_str_appendf(pSql, ", NULL, NULL");
621 i++;
623 sqlite3_str_appendf(pSql, "\n FROM main.%s A\n", zId);
624 sqlite3_str_appendf(pSql, " WHERE NOT EXISTS(SELECT 1 FROM aux.%s B\n", zId);
625 zSep = " WHERE";
626 for(i=0; i<nPk; i++){
627 sqlite3_str_appendf(pSql, "%s A.%s=B.%s", zSep, az[i], az[i]);
628 zSep = " AND";
630 sqlite3_str_appendf(pSql, ")\n");
631 zSep = " UNION ALL\nSELECT ";
632 for(i=0; i<nPk; i++){
633 sqlite3_str_appendf(pSql, "%sB.%s", zSep, az[i]);
634 zSep = ", ";
636 sqlite3_str_appendf(pSql, ", 3 /* inserted row */");
637 while( az2[i] ){
638 sqlite3_str_appendf(pSql, ", 1, B.%s", az2[i]);
639 i++;
641 sqlite3_str_appendf(pSql, "\n FROM aux.%s B\n", zId);
642 sqlite3_str_appendf(pSql, " WHERE NOT EXISTS(SELECT 1 FROM main.%s A\n", zId);
643 zSep = " WHERE";
644 for(i=0; i<nPk; i++){
645 sqlite3_str_appendf(pSql, "%s A.%s=B.%s", zSep, az[i], az[i]);
646 zSep = " AND";
648 sqlite3_str_appendf(pSql, ")\n ORDER BY");
649 zSep = " ";
650 for(i=1; i<=nPk; i++){
651 sqlite3_str_appendf(pSql, "%s%d", zSep, i);
652 zSep = ", ";
654 sqlite3_str_appendf(pSql, ";\n");
656 if( g.fDebug & DEBUG_DIFF_SQL ){
657 printf("SQL for %s:\n%s\n", zId, sqlite3_str_value(pSql));
658 goto end_diff_one_table;
661 /* Drop indexes that are missing in the destination */
662 pStmt = db_prepare(
663 "SELECT name FROM main.sqlite_schema"
664 " WHERE type='index' AND tbl_name=%Q"
665 " AND sql IS NOT NULL"
666 " AND sql NOT IN (SELECT sql FROM aux.sqlite_schema"
667 " WHERE type='index' AND tbl_name=%Q"
668 " AND sql IS NOT NULL)",
669 zTab, zTab);
670 while( SQLITE_ROW==sqlite3_step(pStmt) ){
671 char *z = safeId((const char*)sqlite3_column_text(pStmt,0));
672 fprintf(out, "DROP INDEX %s;\n", z);
673 sqlite3_free(z);
675 sqlite3_finalize(pStmt);
677 /* Run the query and output differences */
678 if( !g.bSchemaOnly ){
679 pStmt = db_prepare("%s", sqlite3_str_value(pSql));
680 while( SQLITE_ROW==sqlite3_step(pStmt) ){
681 int iType = sqlite3_column_int(pStmt, nPk);
682 if( iType==1 || iType==2 ){
683 if( iType==1 ){ /* Change the content of a row */
684 fprintf(out, "%sUPDATE %s", zLead, zId);
685 zSep = " SET";
686 for(i=nPk+1; i<nQ; i+=2){
687 if( sqlite3_column_int(pStmt,i)==0 ) continue;
688 fprintf(out, "%s %s=", zSep, az2[(i+nPk-1)/2]);
689 zSep = ",";
690 printQuoted(out, sqlite3_column_value(pStmt,i+1));
692 }else{ /* Delete a row */
693 fprintf(out, "%sDELETE FROM %s", zLead, zId);
695 zSep = " WHERE";
696 for(i=0; i<nPk; i++){
697 fprintf(out, "%s %s=", zSep, az2[i]);
698 printQuoted(out, sqlite3_column_value(pStmt,i));
699 zSep = " AND";
701 fprintf(out, ";\n");
702 }else{ /* Insert a row */
703 fprintf(out, "%sINSERT INTO %s(%s", zLead, zId, az2[0]);
704 for(i=1; az2[i]; i++) fprintf(out, ",%s", az2[i]);
705 fprintf(out, ") VALUES");
706 zSep = "(";
707 for(i=0; i<nPk2; i++){
708 fprintf(out, "%s", zSep);
709 zSep = ",";
710 printQuoted(out, sqlite3_column_value(pStmt,i));
712 for(i=nPk2+2; i<nQ; i+=2){
713 fprintf(out, ",");
714 printQuoted(out, sqlite3_column_value(pStmt,i));
716 fprintf(out, ");\n");
719 sqlite3_finalize(pStmt);
720 } /* endif !g.bSchemaOnly */
722 /* Create indexes that are missing in the source */
723 pStmt = db_prepare(
724 "SELECT sql FROM aux.sqlite_schema"
725 " WHERE type='index' AND tbl_name=%Q"
726 " AND sql IS NOT NULL"
727 " AND sql NOT IN (SELECT sql FROM main.sqlite_schema"
728 " WHERE type='index' AND tbl_name=%Q"
729 " AND sql IS NOT NULL)",
730 zTab, zTab);
731 while( SQLITE_ROW==sqlite3_step(pStmt) ){
732 fprintf(out, "%s;\n", sqlite3_column_text(pStmt,0));
734 sqlite3_finalize(pStmt);
736 end_diff_one_table:
737 strFree(pSql);
738 sqlite3_free(zId);
739 namelistFree(az);
740 namelistFree(az2);
741 return;
745 ** Check that table zTab exists and has the same schema in both the "main"
746 ** and "aux" databases currently opened by the global db handle. If they
747 ** do not, output an error message on stderr and exit(1). Otherwise, if
748 ** the schemas do match, return control to the caller.
750 static void checkSchemasMatch(const char *zTab){
751 sqlite3_stmt *pStmt = db_prepare(
752 "SELECT A.sql=B.sql FROM main.sqlite_schema A, aux.sqlite_schema B"
753 " WHERE A.name=%Q AND B.name=%Q", zTab, zTab
755 if( SQLITE_ROW==sqlite3_step(pStmt) ){
756 if( sqlite3_column_int(pStmt,0)==0 ){
757 runtimeError("schema changes for table %s", safeId(zTab));
759 }else{
760 runtimeError("table %s missing from one or both databases", safeId(zTab));
762 sqlite3_finalize(pStmt);
765 /**************************************************************************
766 ** The following code is copied from fossil. It is used to generate the
767 ** fossil delta blobs sometimes used in RBU update records.
770 typedef unsigned short u16;
771 typedef unsigned int u32;
772 typedef unsigned char u8;
775 ** The width of a hash window in bytes. The algorithm only works if this
776 ** is a power of 2.
778 #define NHASH 16
781 ** The current state of the rolling hash.
783 ** z[] holds the values that have been hashed. z[] is a circular buffer.
784 ** z[i] is the first entry and z[(i+NHASH-1)%NHASH] is the last entry of
785 ** the window.
787 ** Hash.a is the sum of all elements of hash.z[]. Hash.b is a weighted
788 ** sum. Hash.b is z[i]*NHASH + z[i+1]*(NHASH-1) + ... + z[i+NHASH-1]*1.
789 ** (Each index for z[] should be module NHASH, of course. The %NHASH operator
790 ** is omitted in the prior expression for brevity.)
792 typedef struct hash hash;
793 struct hash {
794 u16 a, b; /* Hash values */
795 u16 i; /* Start of the hash window */
796 char z[NHASH]; /* The values that have been hashed */
800 ** Initialize the rolling hash using the first NHASH characters of z[]
802 static void hash_init(hash *pHash, const char *z){
803 u16 a, b, i;
804 a = b = 0;
805 for(i=0; i<NHASH; i++){
806 a += z[i];
807 b += (NHASH-i)*z[i];
808 pHash->z[i] = z[i];
810 pHash->a = a & 0xffff;
811 pHash->b = b & 0xffff;
812 pHash->i = 0;
816 ** Advance the rolling hash by a single character "c"
818 static void hash_next(hash *pHash, int c){
819 u16 old = pHash->z[pHash->i];
820 pHash->z[pHash->i] = (char)c;
821 pHash->i = (pHash->i+1)&(NHASH-1);
822 pHash->a = pHash->a - old + (char)c;
823 pHash->b = pHash->b - NHASH*old + pHash->a;
827 ** Return a 32-bit hash value
829 static u32 hash_32bit(hash *pHash){
830 return (pHash->a & 0xffff) | (((u32)(pHash->b & 0xffff))<<16);
834 ** Write an base-64 integer into the given buffer.
836 static void putInt(unsigned int v, char **pz){
837 static const char zDigits[] =
838 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz~";
839 /* 123456789 123456789 123456789 123456789 123456789 123456789 123 */
840 int i, j;
841 char zBuf[20];
842 if( v==0 ){
843 *(*pz)++ = '0';
844 return;
846 for(i=0; v>0; i++, v>>=6){
847 zBuf[i] = zDigits[v&0x3f];
849 for(j=i-1; j>=0; j--){
850 *(*pz)++ = zBuf[j];
855 ** Return the number digits in the base-64 representation of a positive integer
857 static int digit_count(int v){
858 unsigned int i, x;
859 for(i=1, x=64; (unsigned int)v>=x; i++, x <<= 6){}
860 return i;
864 ** Compute a 32-bit checksum on the N-byte buffer. Return the result.
866 static unsigned int checksum(const char *zIn, size_t N){
867 const unsigned char *z = (const unsigned char *)zIn;
868 unsigned sum0 = 0;
869 unsigned sum1 = 0;
870 unsigned sum2 = 0;
871 unsigned sum3 = 0;
872 while(N >= 16){
873 sum0 += ((unsigned)z[0] + z[4] + z[8] + z[12]);
874 sum1 += ((unsigned)z[1] + z[5] + z[9] + z[13]);
875 sum2 += ((unsigned)z[2] + z[6] + z[10]+ z[14]);
876 sum3 += ((unsigned)z[3] + z[7] + z[11]+ z[15]);
877 z += 16;
878 N -= 16;
880 while(N >= 4){
881 sum0 += z[0];
882 sum1 += z[1];
883 sum2 += z[2];
884 sum3 += z[3];
885 z += 4;
886 N -= 4;
888 sum3 += (sum2 << 8) + (sum1 << 16) + (sum0 << 24);
889 switch(N){
890 case 3: sum3 += (z[2] << 8);
891 case 2: sum3 += (z[1] << 16);
892 case 1: sum3 += (z[0] << 24);
893 default: ;
895 return sum3;
899 ** Create a new delta.
901 ** The delta is written into a preallocated buffer, zDelta, which
902 ** should be at least 60 bytes longer than the target file, zOut.
903 ** The delta string will be NUL-terminated, but it might also contain
904 ** embedded NUL characters if either the zSrc or zOut files are
905 ** binary. This function returns the length of the delta string
906 ** in bytes, excluding the final NUL terminator character.
908 ** Output Format:
910 ** The delta begins with a base64 number followed by a newline. This
911 ** number is the number of bytes in the TARGET file. Thus, given a
912 ** delta file z, a program can compute the size of the output file
913 ** simply by reading the first line and decoding the base-64 number
914 ** found there. The delta_output_size() routine does exactly this.
916 ** After the initial size number, the delta consists of a series of
917 ** literal text segments and commands to copy from the SOURCE file.
918 ** A copy command looks like this:
920 ** NNN@MMM,
922 ** where NNN is the number of bytes to be copied and MMM is the offset
923 ** into the source file of the first byte (both base-64). If NNN is 0
924 ** it means copy the rest of the input file. Literal text is like this:
926 ** NNN:TTTTT
928 ** where NNN is the number of bytes of text (base-64) and TTTTT is the text.
930 ** The last term is of the form
932 ** NNN;
934 ** In this case, NNN is a 32-bit bigendian checksum of the output file
935 ** that can be used to verify that the delta applied correctly. All
936 ** numbers are in base-64.
938 ** Pure text files generate a pure text delta. Binary files generate a
939 ** delta that may contain some binary data.
941 ** Algorithm:
943 ** The encoder first builds a hash table to help it find matching
944 ** patterns in the source file. 16-byte chunks of the source file
945 ** sampled at evenly spaced intervals are used to populate the hash
946 ** table.
948 ** Next we begin scanning the target file using a sliding 16-byte
949 ** window. The hash of the 16-byte window in the target is used to
950 ** search for a matching section in the source file. When a match
951 ** is found, a copy command is added to the delta. An effort is
952 ** made to extend the matching section to regions that come before
953 ** and after the 16-byte hash window. A copy command is only issued
954 ** if the result would use less space that just quoting the text
955 ** literally. Literal text is added to the delta for sections that
956 ** do not match or which can not be encoded efficiently using copy
957 ** commands.
959 static int rbuDeltaCreate(
960 const char *zSrc, /* The source or pattern file */
961 unsigned int lenSrc, /* Length of the source file */
962 const char *zOut, /* The target file */
963 unsigned int lenOut, /* Length of the target file */
964 char *zDelta /* Write the delta into this buffer */
966 unsigned int i, base;
967 char *zOrigDelta = zDelta;
968 hash h;
969 int nHash; /* Number of hash table entries */
970 int *landmark; /* Primary hash table */
971 int *collide; /* Collision chain */
972 int lastRead = -1; /* Last byte of zSrc read by a COPY command */
974 /* Add the target file size to the beginning of the delta
976 putInt(lenOut, &zDelta);
977 *(zDelta++) = '\n';
979 /* If the source file is very small, it means that we have no
980 ** chance of ever doing a copy command. Just output a single
981 ** literal segment for the entire target and exit.
983 if( lenSrc<=NHASH ){
984 putInt(lenOut, &zDelta);
985 *(zDelta++) = ':';
986 memcpy(zDelta, zOut, lenOut);
987 zDelta += lenOut;
988 putInt(checksum(zOut, lenOut), &zDelta);
989 *(zDelta++) = ';';
990 return (int)(zDelta - zOrigDelta);
993 /* Compute the hash table used to locate matching sections in the
994 ** source file.
996 nHash = lenSrc/NHASH;
997 collide = sqlite3_malloc( nHash*2*sizeof(int) );
998 landmark = &collide[nHash];
999 memset(landmark, -1, nHash*sizeof(int));
1000 memset(collide, -1, nHash*sizeof(int));
1001 for(i=0; i<lenSrc-NHASH; i+=NHASH){
1002 int hv;
1003 hash_init(&h, &zSrc[i]);
1004 hv = hash_32bit(&h) % nHash;
1005 collide[i/NHASH] = landmark[hv];
1006 landmark[hv] = i/NHASH;
1009 /* Begin scanning the target file and generating copy commands and
1010 ** literal sections of the delta.
1012 base = 0; /* We have already generated everything before zOut[base] */
1013 while( base+NHASH<lenOut ){
1014 int iSrc, iBlock;
1015 int bestCnt, bestOfst=0, bestLitsz=0;
1016 hash_init(&h, &zOut[base]);
1017 i = 0; /* Trying to match a landmark against zOut[base+i] */
1018 bestCnt = 0;
1019 while( 1 ){
1020 int hv;
1021 int limit = 250;
1023 hv = hash_32bit(&h) % nHash;
1024 iBlock = landmark[hv];
1025 while( iBlock>=0 && (limit--)>0 ){
1027 ** The hash window has identified a potential match against
1028 ** landmark block iBlock. But we need to investigate further.
1030 ** Look for a region in zOut that matches zSrc. Anchor the search
1031 ** at zSrc[iSrc] and zOut[base+i]. Do not include anything prior to
1032 ** zOut[base] or after zOut[outLen] nor anything after zSrc[srcLen].
1034 ** Set cnt equal to the length of the match and set ofst so that
1035 ** zSrc[ofst] is the first element of the match. litsz is the number
1036 ** of characters between zOut[base] and the beginning of the match.
1037 ** sz will be the overhead (in bytes) needed to encode the copy
1038 ** command. Only generate copy command if the overhead of the
1039 ** copy command is less than the amount of literal text to be copied.
1041 int cnt, ofst, litsz;
1042 int j, k, x, y;
1043 int sz;
1045 /* Beginning at iSrc, match forwards as far as we can. j counts
1046 ** the number of characters that match */
1047 iSrc = iBlock*NHASH;
1048 for(
1049 j=0, x=iSrc, y=base+i;
1050 (unsigned int)x<lenSrc && (unsigned int)y<lenOut;
1051 j++, x++, y++
1053 if( zSrc[x]!=zOut[y] ) break;
1055 j--;
1057 /* Beginning at iSrc-1, match backwards as far as we can. k counts
1058 ** the number of characters that match */
1059 for(k=1; k<iSrc && (unsigned int)k<=i; k++){
1060 if( zSrc[iSrc-k]!=zOut[base+i-k] ) break;
1062 k--;
1064 /* Compute the offset and size of the matching region */
1065 ofst = iSrc-k;
1066 cnt = j+k+1;
1067 litsz = i-k; /* Number of bytes of literal text before the copy */
1068 /* sz will hold the number of bytes needed to encode the "insert"
1069 ** command and the copy command, not counting the "insert" text */
1070 sz = digit_count(i-k)+digit_count(cnt)+digit_count(ofst)+3;
1071 if( cnt>=sz && cnt>bestCnt ){
1072 /* Remember this match only if it is the best so far and it
1073 ** does not increase the file size */
1074 bestCnt = cnt;
1075 bestOfst = iSrc-k;
1076 bestLitsz = litsz;
1079 /* Check the next matching block */
1080 iBlock = collide[iBlock];
1083 /* We have a copy command that does not cause the delta to be larger
1084 ** than a literal insert. So add the copy command to the delta.
1086 if( bestCnt>0 ){
1087 if( bestLitsz>0 ){
1088 /* Add an insert command before the copy */
1089 putInt(bestLitsz,&zDelta);
1090 *(zDelta++) = ':';
1091 memcpy(zDelta, &zOut[base], bestLitsz);
1092 zDelta += bestLitsz;
1093 base += bestLitsz;
1095 base += bestCnt;
1096 putInt(bestCnt, &zDelta);
1097 *(zDelta++) = '@';
1098 putInt(bestOfst, &zDelta);
1099 *(zDelta++) = ',';
1100 if( bestOfst + bestCnt -1 > lastRead ){
1101 lastRead = bestOfst + bestCnt - 1;
1103 bestCnt = 0;
1104 break;
1107 /* If we reach this point, it means no match is found so far */
1108 if( base+i+NHASH>=lenOut ){
1109 /* We have reached the end of the file and have not found any
1110 ** matches. Do an "insert" for everything that does not match */
1111 putInt(lenOut-base, &zDelta);
1112 *(zDelta++) = ':';
1113 memcpy(zDelta, &zOut[base], lenOut-base);
1114 zDelta += lenOut-base;
1115 base = lenOut;
1116 break;
1119 /* Advance the hash by one character. Keep looking for a match */
1120 hash_next(&h, zOut[base+i+NHASH]);
1121 i++;
1124 /* Output a final "insert" record to get all the text at the end of
1125 ** the file that does not match anything in the source file.
1127 if( base<lenOut ){
1128 putInt(lenOut-base, &zDelta);
1129 *(zDelta++) = ':';
1130 memcpy(zDelta, &zOut[base], lenOut-base);
1131 zDelta += lenOut-base;
1133 /* Output the final checksum record. */
1134 putInt(checksum(zOut, lenOut), &zDelta);
1135 *(zDelta++) = ';';
1136 sqlite3_free(collide);
1137 return (int)(zDelta - zOrigDelta);
1141 ** End of code copied from fossil.
1142 **************************************************************************/
1144 static void strPrintfArray(
1145 sqlite3_str *pStr, /* String object to append to */
1146 const char *zSep, /* Separator string */
1147 const char *zFmt, /* Format for each entry */
1148 char **az, int n /* Array of strings & its size (or -1) */
1150 int i;
1151 for(i=0; az[i] && (i<n || n<0); i++){
1152 if( i!=0 ) sqlite3_str_appendf(pStr, "%s", zSep);
1153 sqlite3_str_appendf(pStr, zFmt, az[i], az[i], az[i]);
1157 static void getRbudiffQuery(
1158 const char *zTab,
1159 char **azCol,
1160 int nPK,
1161 int bOtaRowid,
1162 sqlite3_str *pSql
1164 int i;
1166 /* First the newly inserted rows: **/
1167 sqlite3_str_appendf(pSql, "SELECT ");
1168 strPrintfArray(pSql, ", ", "%s", azCol, -1);
1169 sqlite3_str_appendf(pSql, ", 0, "); /* Set ota_control to 0 for an insert */
1170 strPrintfArray(pSql, ", ", "NULL", azCol, -1);
1171 sqlite3_str_appendf(pSql, " FROM aux.%Q AS n WHERE NOT EXISTS (\n", zTab);
1172 sqlite3_str_appendf(pSql, " SELECT 1 FROM ", zTab);
1173 sqlite3_str_appendf(pSql, " main.%Q AS o WHERE ", zTab);
1174 strPrintfArray(pSql, " AND ", "(n.%Q = o.%Q)", azCol, nPK);
1175 sqlite3_str_appendf(pSql, "\n) AND ");
1176 strPrintfArray(pSql, " AND ", "(n.%Q IS NOT NULL)", azCol, nPK);
1178 /* Deleted rows: */
1179 sqlite3_str_appendf(pSql, "\nUNION ALL\nSELECT ");
1180 strPrintfArray(pSql, ", ", "%s", azCol, nPK);
1181 if( azCol[nPK] ){
1182 sqlite3_str_appendf(pSql, ", ");
1183 strPrintfArray(pSql, ", ", "NULL", &azCol[nPK], -1);
1185 sqlite3_str_appendf(pSql, ", 1, "); /* Set ota_control to 1 for a delete */
1186 strPrintfArray(pSql, ", ", "NULL", azCol, -1);
1187 sqlite3_str_appendf(pSql, " FROM main.%Q AS n WHERE NOT EXISTS (\n", zTab);
1188 sqlite3_str_appendf(pSql, " SELECT 1 FROM ", zTab);
1189 sqlite3_str_appendf(pSql, " aux.%Q AS o WHERE ", zTab);
1190 strPrintfArray(pSql, " AND ", "(n.%Q = o.%Q)", azCol, nPK);
1191 sqlite3_str_appendf(pSql, "\n) AND ");
1192 strPrintfArray(pSql, " AND ", "(n.%Q IS NOT NULL)", azCol, nPK);
1194 /* Updated rows. If all table columns are part of the primary key, there
1195 ** can be no updates. In this case this part of the compound SELECT can
1196 ** be omitted altogether. */
1197 if( azCol[nPK] ){
1198 sqlite3_str_appendf(pSql, "\nUNION ALL\nSELECT ");
1199 strPrintfArray(pSql, ", ", "n.%s", azCol, nPK);
1200 sqlite3_str_appendf(pSql, ",\n");
1201 strPrintfArray(pSql, " ,\n",
1202 " CASE WHEN n.%s IS o.%s THEN NULL ELSE n.%s END", &azCol[nPK], -1
1205 if( bOtaRowid==0 ){
1206 sqlite3_str_appendf(pSql, ", '");
1207 strPrintfArray(pSql, "", ".", azCol, nPK);
1208 sqlite3_str_appendf(pSql, "' ||\n");
1209 }else{
1210 sqlite3_str_appendf(pSql, ",\n");
1212 strPrintfArray(pSql, " ||\n",
1213 " CASE WHEN n.%s IS o.%s THEN '.' ELSE 'x' END", &azCol[nPK], -1
1215 sqlite3_str_appendf(pSql, "\nAS ota_control, ");
1216 strPrintfArray(pSql, ", ", "NULL", azCol, nPK);
1217 sqlite3_str_appendf(pSql, ",\n");
1218 strPrintfArray(pSql, " ,\n",
1219 " CASE WHEN n.%s IS o.%s THEN NULL ELSE o.%s END", &azCol[nPK], -1
1222 sqlite3_str_appendf(pSql, "\nFROM main.%Q AS o, aux.%Q AS n\nWHERE ",
1223 zTab, zTab);
1224 strPrintfArray(pSql, " AND ", "(n.%Q = o.%Q)", azCol, nPK);
1225 sqlite3_str_appendf(pSql, " AND ota_control LIKE '%%x%%'");
1228 /* Now add an ORDER BY clause to sort everything by PK. */
1229 sqlite3_str_appendf(pSql, "\nORDER BY ");
1230 for(i=1; i<=nPK; i++) sqlite3_str_appendf(pSql, "%s%d", ((i>1)?", ":""), i);
1233 static void rbudiff_one_table(const char *zTab, FILE *out){
1234 int bOtaRowid; /* True to use an ota_rowid column */
1235 int nPK; /* Number of primary key columns in table */
1236 char **azCol; /* NULL terminated array of col names */
1237 int i;
1238 int nCol;
1239 sqlite3_str *pCt; /* The "CREATE TABLE data_xxx" statement */
1240 sqlite3_str *pSql; /* Query to find differences */
1241 sqlite3_str *pInsert; /* First part of output INSERT statement */
1242 sqlite3_stmt *pStmt = 0;
1243 int nRow = 0; /* Total rows in data_xxx table */
1245 /* --rbu mode must use real primary keys. */
1246 g.bSchemaPK = 1;
1247 pCt = sqlite3_str_new(0);
1248 pSql = sqlite3_str_new(0);
1249 pInsert = sqlite3_str_new(0);
1251 /* Check that the schemas of the two tables match. Exit early otherwise. */
1252 checkSchemasMatch(zTab);
1254 /* Grab the column names and PK details for the table(s). If no usable PK
1255 ** columns are found, bail out early. */
1256 azCol = columnNames("main", zTab, &nPK, &bOtaRowid);
1257 if( azCol==0 ){
1258 runtimeError("table %s has no usable PK columns", zTab);
1260 for(nCol=0; azCol[nCol]; nCol++);
1262 /* Build and output the CREATE TABLE statement for the data_xxx table */
1263 sqlite3_str_appendf(pCt, "CREATE TABLE IF NOT EXISTS 'data_%q'(", zTab);
1264 if( bOtaRowid ) sqlite3_str_appendf(pCt, "rbu_rowid, ");
1265 strPrintfArray(pCt, ", ", "%s", &azCol[bOtaRowid], -1);
1266 sqlite3_str_appendf(pCt, ", rbu_control);");
1268 /* Get the SQL for the query to retrieve data from the two databases */
1269 getRbudiffQuery(zTab, azCol, nPK, bOtaRowid, pSql);
1271 /* Build the first part of the INSERT statement output for each row
1272 ** in the data_xxx table. */
1273 sqlite3_str_appendf(pInsert, "INSERT INTO 'data_%q' (", zTab);
1274 if( bOtaRowid ) sqlite3_str_appendf(pInsert, "rbu_rowid, ");
1275 strPrintfArray(pInsert, ", ", "%s", &azCol[bOtaRowid], -1);
1276 sqlite3_str_appendf(pInsert, ", rbu_control) VALUES(");
1278 pStmt = db_prepare("%s", sqlite3_str_value(pSql));
1280 while( sqlite3_step(pStmt)==SQLITE_ROW ){
1282 /* If this is the first row output, print out the CREATE TABLE
1283 ** statement first. And reset pCt so that it will not be
1284 ** printed again. */
1285 if( sqlite3_str_length(pCt) ){
1286 fprintf(out, "%s\n", sqlite3_str_value(pCt));
1287 sqlite3_str_reset(pCt);
1290 /* Output the first part of the INSERT statement */
1291 fprintf(out, "%s", sqlite3_str_value(pInsert));
1292 nRow++;
1294 if( sqlite3_column_type(pStmt, nCol)==SQLITE_INTEGER ){
1295 for(i=0; i<=nCol; i++){
1296 if( i>0 ) fprintf(out, ", ");
1297 printQuoted(out, sqlite3_column_value(pStmt, i));
1299 }else{
1300 char *zOtaControl;
1301 int nOtaControl = sqlite3_column_bytes(pStmt, nCol);
1303 zOtaControl = (char*)sqlite3_malloc(nOtaControl+1);
1304 memcpy(zOtaControl, sqlite3_column_text(pStmt, nCol), nOtaControl+1);
1306 for(i=0; i<nCol; i++){
1307 int bDone = 0;
1308 if( i>=nPK
1309 && sqlite3_column_type(pStmt, i)==SQLITE_BLOB
1310 && sqlite3_column_type(pStmt, nCol+1+i)==SQLITE_BLOB
1312 const char *aSrc = sqlite3_column_blob(pStmt, nCol+1+i);
1313 int nSrc = sqlite3_column_bytes(pStmt, nCol+1+i);
1314 const char *aFinal = sqlite3_column_blob(pStmt, i);
1315 int nFinal = sqlite3_column_bytes(pStmt, i);
1316 char *aDelta;
1317 int nDelta;
1319 aDelta = sqlite3_malloc(nFinal + 60);
1320 nDelta = rbuDeltaCreate(aSrc, nSrc, aFinal, nFinal, aDelta);
1321 if( nDelta<nFinal ){
1322 int j;
1323 fprintf(out, "x'");
1324 for(j=0; j<nDelta; j++) fprintf(out, "%02x", (u8)aDelta[j]);
1325 fprintf(out, "'");
1326 zOtaControl[i-bOtaRowid] = 'f';
1327 bDone = 1;
1329 sqlite3_free(aDelta);
1332 if( bDone==0 ){
1333 printQuoted(out, sqlite3_column_value(pStmt, i));
1335 fprintf(out, ", ");
1337 fprintf(out, "'%s'", zOtaControl);
1338 sqlite3_free(zOtaControl);
1341 /* And the closing bracket of the insert statement */
1342 fprintf(out, ");\n");
1345 sqlite3_finalize(pStmt);
1346 if( nRow>0 ){
1347 sqlite3_str *pCnt = sqlite3_str_new(0);
1348 sqlite3_str_appendf(pCnt,
1349 "INSERT INTO rbu_count VALUES('data_%q', %d);", zTab, nRow);
1350 fprintf(out, "%s\n", sqlite3_str_value(pCnt));
1351 strFree(pCnt);
1354 strFree(pCt);
1355 strFree(pSql);
1356 strFree(pInsert);
1360 ** Display a summary of differences between two versions of the same
1361 ** table table.
1363 ** * Number of rows changed
1364 ** * Number of rows added
1365 ** * Number of rows deleted
1366 ** * Number of identical rows
1368 static void summarize_one_table(const char *zTab, FILE *out){
1369 char *zId = safeId(zTab); /* Name of table (translated for us in SQL) */
1370 char **az = 0; /* Columns in main */
1371 char **az2 = 0; /* Columns in aux */
1372 int nPk; /* Primary key columns in main */
1373 int nPk2; /* Primary key columns in aux */
1374 int n = 0; /* Number of columns in main */
1375 int n2; /* Number of columns in aux */
1376 int i; /* Loop counter */
1377 const char *zSep; /* Separator string */
1378 sqlite3_str *pSql; /* Comparison query */
1379 sqlite3_stmt *pStmt; /* Query statement to do the diff */
1380 sqlite3_int64 nUpdate; /* Number of updated rows */
1381 sqlite3_int64 nUnchanged; /* Number of unmodified rows */
1382 sqlite3_int64 nDelete; /* Number of deleted rows */
1383 sqlite3_int64 nInsert; /* Number of inserted rows */
1385 pSql = sqlite3_str_new(0);
1386 if( sqlite3_table_column_metadata(g.db,"aux",zTab,0,0,0,0,0,0) ){
1387 if( !sqlite3_table_column_metadata(g.db,"main",zTab,0,0,0,0,0,0) ){
1388 /* Table missing from second database. */
1389 Wfprintf(out, "%s: missing from second database\n", zTab);
1391 goto end_summarize_one_table;
1394 if( sqlite3_table_column_metadata(g.db,"main",zTab,0,0,0,0,0,0) ){
1395 /* Table missing from source */
1396 Wfprintf(out, "%s: missing from first database\n", zTab);
1397 goto end_summarize_one_table;
1400 az = columnNames("main", zTab, &nPk, 0);
1401 az2 = columnNames("aux", zTab, &nPk2, 0);
1402 if( az && az2 ){
1403 for(n=0; az[n]; n++){
1404 if( sqlite3_stricmp(az[n],az2[n])!=0 ) break;
1407 if( az==0
1408 || az2==0
1409 || nPk!=nPk2
1410 || az[n]
1412 /* Schema mismatch */
1413 Wfprintf(out, "%s: incompatible schema\n", zTab);
1414 goto end_summarize_one_table;
1417 /* Build the comparison query */
1418 for(n2=n; az[n2]; n2++){}
1419 sqlite3_str_appendf(pSql, "SELECT 1, count(*)");
1420 if( n2==nPk2 ){
1421 sqlite3_str_appendf(pSql, ", 0\n");
1422 }else{
1423 zSep = ", sum(";
1424 for(i=nPk; az[i]; i++){
1425 sqlite3_str_appendf(pSql, "%sA.%s IS NOT B.%s", zSep, az[i], az[i]);
1426 zSep = " OR ";
1428 sqlite3_str_appendf(pSql, ")\n");
1430 sqlite3_str_appendf(pSql, " FROM main.%s A, aux.%s B\n", zId, zId);
1431 zSep = " WHERE";
1432 for(i=0; i<nPk; i++){
1433 sqlite3_str_appendf(pSql, "%s A.%s=B.%s", zSep, az[i], az[i]);
1434 zSep = " AND";
1436 sqlite3_str_appendf(pSql, " UNION ALL\n");
1437 sqlite3_str_appendf(pSql, "SELECT 2, count(*), 0\n");
1438 sqlite3_str_appendf(pSql, " FROM main.%s A\n", zId);
1439 sqlite3_str_appendf(pSql, " WHERE NOT EXISTS(SELECT 1 FROM aux.%s B ", zId);
1440 zSep = "WHERE";
1441 for(i=0; i<nPk; i++){
1442 sqlite3_str_appendf(pSql, "%s A.%s=B.%s", zSep, az[i], az[i]);
1443 zSep = " AND";
1445 sqlite3_str_appendf(pSql, ")\n");
1446 sqlite3_str_appendf(pSql, " UNION ALL\n");
1447 sqlite3_str_appendf(pSql, "SELECT 3, count(*), 0\n");
1448 sqlite3_str_appendf(pSql, " FROM aux.%s B\n", zId);
1449 sqlite3_str_appendf(pSql, " WHERE NOT EXISTS(SELECT 1 FROM main.%s A ", zId);
1450 zSep = "WHERE";
1451 for(i=0; i<nPk; i++){
1452 sqlite3_str_appendf(pSql, "%s A.%s=B.%s", zSep, az[i], az[i]);
1453 zSep = " AND";
1455 sqlite3_str_appendf(pSql, ")\n ORDER BY 1;\n");
1457 if( (g.fDebug & DEBUG_DIFF_SQL)!=0 ){
1458 Wfprintf(stdout, "SQL for %s:\n%s\n", zId, sqlite3_str_value(pSql));
1459 goto end_summarize_one_table;
1462 /* Run the query and output difference summary */
1463 pStmt = db_prepare("%s", sqlite3_str_value(pSql));
1464 nUpdate = 0;
1465 nInsert = 0;
1466 nDelete = 0;
1467 nUnchanged = 0;
1468 while( SQLITE_ROW==sqlite3_step(pStmt) ){
1469 switch( sqlite3_column_int(pStmt,0) ){
1470 case 1:
1471 nUpdate = sqlite3_column_int64(pStmt,2);
1472 nUnchanged = sqlite3_column_int64(pStmt,1) - nUpdate;
1473 break;
1474 case 2:
1475 nDelete = sqlite3_column_int64(pStmt,1);
1476 break;
1477 case 3:
1478 nInsert = sqlite3_column_int64(pStmt,1);
1479 break;
1482 sqlite3_finalize(pStmt);
1483 Wfprintf(out,
1484 "%s: %lld changes, %lld inserts, %lld deletes, %lld unchanged\n",
1485 zTab, nUpdate, nInsert, nDelete, nUnchanged);
1487 end_summarize_one_table:
1488 strFree(pSql);
1489 sqlite3_free(zId);
1490 namelistFree(az);
1491 namelistFree(az2);
1492 return;
1496 ** Write a 64-bit signed integer as a varint onto out
1498 static void putsVarint(FILE *out, sqlite3_uint64 v){
1499 int i, n;
1500 unsigned char p[12];
1501 if( v & (((sqlite3_uint64)0xff000000)<<32) ){
1502 p[8] = (unsigned char)v;
1503 v >>= 8;
1504 for(i=7; i>=0; i--){
1505 p[i] = (unsigned char)((v & 0x7f) | 0x80);
1506 v >>= 7;
1508 fwrite(p, 8, 1, out);
1509 }else{
1510 n = 9;
1512 p[n--] = (unsigned char)((v & 0x7f) | 0x80);
1513 v >>= 7;
1514 }while( v!=0 );
1515 p[9] &= 0x7f;
1516 fwrite(p+n+1, 9-n, 1, out);
1521 ** Write an SQLite value onto out.
1523 static void putValue(FILE *out, sqlite3_stmt *pStmt, int k){
1524 int iDType = sqlite3_column_type(pStmt, k);
1525 sqlite3_int64 iX;
1526 double rX;
1527 sqlite3_uint64 uX;
1528 int j;
1530 putc(iDType, out);
1531 switch( iDType ){
1532 case SQLITE_INTEGER:
1533 iX = sqlite3_column_int64(pStmt, k);
1534 memcpy(&uX, &iX, 8);
1535 for(j=56; j>=0; j-=8) putc((uX>>j)&0xff, out);
1536 break;
1537 case SQLITE_FLOAT:
1538 rX = sqlite3_column_double(pStmt, k);
1539 memcpy(&uX, &rX, 8);
1540 for(j=56; j>=0; j-=8) putc((uX>>j)&0xff, out);
1541 break;
1542 case SQLITE_TEXT:
1543 iX = sqlite3_column_bytes(pStmt, k);
1544 putsVarint(out, (sqlite3_uint64)iX);
1545 fwrite(sqlite3_column_text(pStmt, k),1,(size_t)iX,out);
1546 break;
1547 case SQLITE_BLOB:
1548 iX = sqlite3_column_bytes(pStmt, k);
1549 putsVarint(out, (sqlite3_uint64)iX);
1550 fwrite(sqlite3_column_blob(pStmt, k),1,(size_t)iX,out);
1551 break;
1552 case SQLITE_NULL:
1553 break;
1558 ** Generate a CHANGESET for all differences from main.zTab to aux.zTab.
1560 static void changeset_one_table(const char *zTab, FILE *out){
1561 sqlite3_stmt *pStmt; /* SQL statment */
1562 char *zId = safeId(zTab); /* Escaped name of the table */
1563 char **azCol = 0; /* List of escaped column names */
1564 int nCol = 0; /* Number of columns */
1565 int *aiFlg = 0; /* 0 if column is not part of PK */
1566 int *aiPk = 0; /* Column numbers for each PK column */
1567 int nPk = 0; /* Number of PRIMARY KEY columns */
1568 sqlite3_str *pSql; /* SQL for the diff query */
1569 int i, k; /* Loop counters */
1570 const char *zSep; /* List separator */
1572 /* Check that the schemas of the two tables match. Exit early otherwise. */
1573 checkSchemasMatch(zTab);
1574 pSql = sqlite3_str_new(0);
1576 pStmt = db_prepare("PRAGMA main.table_info=%Q", zTab);
1577 while( SQLITE_ROW==sqlite3_step(pStmt) ){
1578 nCol++;
1579 azCol = sqlite3_realloc(azCol, sizeof(char*)*nCol);
1580 if( azCol==0 ) runtimeError("out of memory");
1581 aiFlg = sqlite3_realloc(aiFlg, sizeof(int)*nCol);
1582 if( aiFlg==0 ) runtimeError("out of memory");
1583 azCol[nCol-1] = safeId((const char*)sqlite3_column_text(pStmt,1));
1584 aiFlg[nCol-1] = i = sqlite3_column_int(pStmt,5);
1585 if( i>0 ){
1586 if( i>nPk ){
1587 nPk = i;
1588 aiPk = sqlite3_realloc(aiPk, sizeof(int)*nPk);
1589 if( aiPk==0 ) runtimeError("out of memory");
1591 aiPk[i-1] = nCol-1;
1594 sqlite3_finalize(pStmt);
1595 if( nPk==0 ) goto end_changeset_one_table;
1596 if( nCol>nPk ){
1597 sqlite3_str_appendf(pSql, "SELECT %d", SQLITE_UPDATE);
1598 for(i=0; i<nCol; i++){
1599 if( aiFlg[i] ){
1600 sqlite3_str_appendf(pSql, ",\n A.%s", azCol[i]);
1601 }else{
1602 sqlite3_str_appendf(pSql, ",\n A.%s IS NOT B.%s, A.%s, B.%s",
1603 azCol[i], azCol[i], azCol[i], azCol[i]);
1606 sqlite3_str_appendf(pSql,"\n FROM main.%s A, aux.%s B\n", zId, zId);
1607 zSep = " WHERE";
1608 for(i=0; i<nPk; i++){
1609 sqlite3_str_appendf(pSql, "%s A.%s=B.%s",
1610 zSep, azCol[aiPk[i]], azCol[aiPk[i]]);
1611 zSep = " AND";
1613 zSep = "\n AND (";
1614 for(i=0; i<nCol; i++){
1615 if( aiFlg[i] ) continue;
1616 sqlite3_str_appendf(pSql, "%sA.%s IS NOT B.%s", zSep, azCol[i], azCol[i]);
1617 zSep = " OR\n ";
1619 sqlite3_str_appendf(pSql,")\n UNION ALL\n");
1621 sqlite3_str_appendf(pSql, "SELECT %d", SQLITE_DELETE);
1622 for(i=0; i<nCol; i++){
1623 if( aiFlg[i] ){
1624 sqlite3_str_appendf(pSql, ",\n A.%s", azCol[i]);
1625 }else{
1626 sqlite3_str_appendf(pSql, ",\n 1, A.%s, NULL", azCol[i]);
1629 sqlite3_str_appendf(pSql, "\n FROM main.%s A\n", zId);
1630 sqlite3_str_appendf(pSql, " WHERE NOT EXISTS(SELECT 1 FROM aux.%s B\n", zId);
1631 zSep = " WHERE";
1632 for(i=0; i<nPk; i++){
1633 sqlite3_str_appendf(pSql, "%s A.%s=B.%s",
1634 zSep, azCol[aiPk[i]], azCol[aiPk[i]]);
1635 zSep = " AND";
1637 sqlite3_str_appendf(pSql, ")\n UNION ALL\n");
1638 sqlite3_str_appendf(pSql, "SELECT %d", SQLITE_INSERT);
1639 for(i=0; i<nCol; i++){
1640 if( aiFlg[i] ){
1641 sqlite3_str_appendf(pSql, ",\n B.%s", azCol[i]);
1642 }else{
1643 sqlite3_str_appendf(pSql, ",\n 1, NULL, B.%s", azCol[i]);
1646 sqlite3_str_appendf(pSql, "\n FROM aux.%s B\n", zId);
1647 sqlite3_str_appendf(pSql, " WHERE NOT EXISTS(SELECT 1 FROM main.%s A\n", zId);
1648 zSep = " WHERE";
1649 for(i=0; i<nPk; i++){
1650 sqlite3_str_appendf(pSql, "%s A.%s=B.%s",
1651 zSep, azCol[aiPk[i]], azCol[aiPk[i]]);
1652 zSep = " AND";
1654 sqlite3_str_appendf(pSql, ")\n");
1655 sqlite3_str_appendf(pSql, " ORDER BY");
1656 zSep = " ";
1657 for(i=0; i<nPk; i++){
1658 sqlite3_str_appendf(pSql, "%s %d", zSep, aiPk[i]+2);
1659 zSep = ",";
1661 sqlite3_str_appendf(pSql, ";\n");
1663 if( g.fDebug & DEBUG_DIFF_SQL ){
1664 Wfprintf(stdout, "SQL for %s:\n%s\n", zId, sqlite3_str_value(pSql));
1665 goto end_changeset_one_table;
1668 putc('T', out);
1669 putsVarint(out, (sqlite3_uint64)nCol);
1670 for(i=0; i<nCol; i++) putc(aiFlg[i], out);
1671 fwrite(zTab, 1, strlen(zTab), out);
1672 putc(0, out);
1674 pStmt = db_prepare("%s", sqlite3_str_value(pSql));
1675 while( SQLITE_ROW==sqlite3_step(pStmt) ){
1676 int iType = sqlite3_column_int(pStmt,0);
1677 putc(iType, out);
1678 putc(0, out);
1679 switch( sqlite3_column_int(pStmt,0) ){
1680 case SQLITE_UPDATE: {
1681 for(k=1, i=0; i<nCol; i++){
1682 if( aiFlg[i] ){
1683 putValue(out, pStmt, k);
1684 k++;
1685 }else if( sqlite3_column_int(pStmt,k) ){
1686 putValue(out, pStmt, k+1);
1687 k += 3;
1688 }else{
1689 putc(0, out);
1690 k += 3;
1693 for(k=1, i=0; i<nCol; i++){
1694 if( aiFlg[i] ){
1695 putc(0, out);
1696 k++;
1697 }else if( sqlite3_column_int(pStmt,k) ){
1698 putValue(out, pStmt, k+2);
1699 k += 3;
1700 }else{
1701 putc(0, out);
1702 k += 3;
1705 break;
1707 case SQLITE_INSERT: {
1708 for(k=1, i=0; i<nCol; i++){
1709 if( aiFlg[i] ){
1710 putValue(out, pStmt, k);
1711 k++;
1712 }else{
1713 putValue(out, pStmt, k+2);
1714 k += 3;
1717 break;
1719 case SQLITE_DELETE: {
1720 for(k=1, i=0; i<nCol; i++){
1721 if( aiFlg[i] ){
1722 putValue(out, pStmt, k);
1723 k++;
1724 }else{
1725 putValue(out, pStmt, k+1);
1726 k += 3;
1729 break;
1733 sqlite3_finalize(pStmt);
1735 end_changeset_one_table:
1736 while( nCol>0 ) sqlite3_free(azCol[--nCol]);
1737 sqlite3_free(azCol);
1738 sqlite3_free(aiPk);
1739 sqlite3_free(zId);
1740 sqlite3_free(aiFlg);
1741 strFree(pSql);
1745 ** Return true if the ascii character passed as the only argument is a
1746 ** whitespace character. Otherwise return false.
1748 static int is_whitespace(char x){
1749 return (x==' ' || x=='\t' || x=='\n' || x=='\r');
1753 ** Extract the next SQL keyword or quoted string from buffer zIn and copy it
1754 ** (or a prefix of it if it will not fit) into buffer zBuf, size nBuf bytes.
1755 ** Return a pointer to the character within zIn immediately following
1756 ** the token or quoted string just extracted.
1758 static const char *gobble_token(const char *zIn, char *zBuf, int nBuf){
1759 const char *p = zIn;
1760 char *pOut = zBuf;
1761 char *pEnd = &pOut[nBuf-1];
1762 char q = 0; /* quote character, if any */
1764 if( p==0 ) return 0;
1765 while( is_whitespace(*p) ) p++;
1766 switch( *p ){
1767 case '"': q = '"'; break;
1768 case '\'': q = '\''; break;
1769 case '`': q = '`'; break;
1770 case '[': q = ']'; break;
1773 if( q ){
1774 p++;
1775 while( *p && pOut<pEnd ){
1776 if( *p==q ){
1777 p++;
1778 if( *p!=q ) break;
1780 if( pOut<pEnd ) *pOut++ = *p;
1781 p++;
1783 }else{
1784 while( *p && !is_whitespace(*p) && *p!='(' ){
1785 if( pOut<pEnd ) *pOut++ = *p;
1786 p++;
1790 *pOut = '\0';
1791 return p;
1795 ** This function is the implementation of SQL scalar function "module_name":
1797 ** module_name(SQL)
1799 ** The only argument should be an SQL statement of the type that may appear
1800 ** in the sqlite_schema table. If the statement is a "CREATE VIRTUAL TABLE"
1801 ** statement, then the value returned is the name of the module that it
1802 ** uses. Otherwise, if the statement is not a CVT, NULL is returned.
1804 static void module_name_func(
1805 sqlite3_context *pCtx,
1806 int nVal, sqlite3_value **apVal
1808 const char *zSql;
1809 char zToken[32];
1811 assert( nVal==1 );
1812 zSql = (const char*)sqlite3_value_text(apVal[0]);
1814 zSql = gobble_token(zSql, zToken, sizeof(zToken));
1815 if( zSql==0 || sqlite3_stricmp(zToken, "create") ) return;
1816 zSql = gobble_token(zSql, zToken, sizeof(zToken));
1817 if( zSql==0 || sqlite3_stricmp(zToken, "virtual") ) return;
1818 zSql = gobble_token(zSql, zToken, sizeof(zToken));
1819 if( zSql==0 || sqlite3_stricmp(zToken, "table") ) return;
1820 zSql = gobble_token(zSql, zToken, sizeof(zToken));
1821 if( zSql==0 ) return;
1822 zSql = gobble_token(zSql, zToken, sizeof(zToken));
1823 if( zSql==0 || sqlite3_stricmp(zToken, "using") ) return;
1824 zSql = gobble_token(zSql, zToken, sizeof(zToken));
1826 sqlite3_result_text(pCtx, zToken, -1, SQLITE_TRANSIENT);
1830 ** Return the text of an SQL statement that itself returns the list of
1831 ** tables to process within the database.
1833 const char *all_tables_sql(){
1834 if( g.bHandleVtab ){
1835 int rc;
1837 rc = sqlite3_exec(g.db,
1838 "CREATE TEMP TABLE tblmap(module COLLATE nocase, postfix);"
1839 "INSERT INTO temp.tblmap VALUES"
1840 "('fts3', '_content'), ('fts3', '_segments'), ('fts3', '_segdir'),"
1842 "('fts4', '_content'), ('fts4', '_segments'), ('fts4', '_segdir'),"
1843 "('fts4', '_docsize'), ('fts4', '_stat'),"
1845 "('fts5', '_data'), ('fts5', '_idx'), ('fts5', '_content'),"
1846 "('fts5', '_docsize'), ('fts5', '_config'),"
1848 "('rtree', '_node'), ('rtree', '_rowid'), ('rtree', '_parent');"
1849 , 0, 0, 0
1851 assert( rc==SQLITE_OK );
1853 rc = sqlite3_create_function(
1854 g.db, "module_name", 1, SQLITE_UTF8, 0, module_name_func, 0, 0
1856 assert( rc==SQLITE_OK );
1858 return
1859 "SELECT name FROM main.sqlite_schema\n"
1860 " WHERE type='table' AND (\n"
1861 " module_name(sql) IS NULL OR \n"
1862 " module_name(sql) IN (SELECT module FROM temp.tblmap)\n"
1863 " ) AND name NOT IN (\n"
1864 " SELECT a.name || b.postfix \n"
1865 "FROM main.sqlite_schema AS a, temp.tblmap AS b \n"
1866 "WHERE module_name(a.sql) = b.module\n"
1867 " )\n"
1868 "UNION \n"
1869 "SELECT name FROM aux.sqlite_schema\n"
1870 " WHERE type='table' AND (\n"
1871 " module_name(sql) IS NULL OR \n"
1872 " module_name(sql) IN (SELECT module FROM temp.tblmap)\n"
1873 " ) AND name NOT IN (\n"
1874 " SELECT a.name || b.postfix \n"
1875 "FROM aux.sqlite_schema AS a, temp.tblmap AS b \n"
1876 "WHERE module_name(a.sql) = b.module\n"
1877 " )\n"
1878 " ORDER BY name";
1879 }else{
1880 return
1881 "SELECT name FROM main.sqlite_schema\n"
1882 " WHERE type='table' AND sql NOT LIKE 'CREATE VIRTUAL%%'\n"
1883 " UNION\n"
1884 "SELECT name FROM aux.sqlite_schema\n"
1885 " WHERE type='table' AND sql NOT LIKE 'CREATE VIRTUAL%%'\n"
1886 " ORDER BY name";
1891 ** Print sketchy documentation for this utility program
1893 static void showHelp(void){
1894 Wfprintf(stdout, "Usage: %s [options] DB1 DB2\n", g.zArgv0);
1895 Wfprintf(stdout,
1896 "Output SQL text that would transform DB1 into DB2.\n"
1897 "Options:\n"
1898 " --changeset FILE Write a CHANGESET into FILE\n"
1899 " -L|--lib LIBRARY Load an SQLite extension library\n"
1900 " --primarykey Use schema-defined PRIMARY KEYs\n"
1901 " --rbu Output SQL to create/populate RBU table(s)\n"
1902 " --schema Show only differences in the schema\n"
1903 " --summary Show only a summary of the differences\n"
1904 " --table TAB Show only differences in table TAB\n"
1905 " --transaction Show SQL output inside a transaction\n"
1906 " --vtab Handle fts3, fts4, fts5 and rtree tables\n"
1907 "See https://sqlite.org/sqldiff.html for detailed explanation.\n"
1911 int main(int argc, char **argv){
1912 const char *zDb1 = 0;
1913 const char *zDb2 = 0;
1914 int i;
1915 int rc;
1916 char *zErrMsg = 0;
1917 char *zSql;
1918 sqlite3_stmt *pStmt;
1919 char *zTab = 0;
1920 FILE *out = stdout;
1921 void (*xDiff)(const char*,FILE*) = diff_one_table;
1922 #ifndef SQLITE_OMIT_LOAD_EXTENSION
1923 int nExt = 0;
1924 char **azExt = 0;
1925 #endif
1926 int useTransaction = 0;
1927 int neverUseTransaction = 0;
1929 g.zArgv0 = argv[0];
1930 sqlite3_config(SQLITE_CONFIG_SINGLETHREAD);
1931 for(i=1; i<argc; i++){
1932 const char *z = argv[i];
1933 if( z[0]=='-' ){
1934 z++;
1935 if( z[0]=='-' ) z++;
1936 if( strcmp(z,"changeset")==0 ){
1937 if( i==argc-1 ) cmdlineError("missing argument to %s", argv[i]);
1938 out = fopen(argv[++i], "wb");
1939 if( out==0 ) cmdlineError("cannot open: %s", argv[i]);
1940 xDiff = changeset_one_table;
1941 neverUseTransaction = 1;
1942 }else
1943 if( strcmp(z,"debug")==0 ){
1944 if( i==argc-1 ) cmdlineError("missing argument to %s", argv[i]);
1945 g.fDebug = strtol(argv[++i], 0, 0);
1946 }else
1947 if( strcmp(z,"help")==0 ){
1948 showHelp();
1949 return 0;
1950 }else
1951 #ifndef SQLITE_OMIT_LOAD_EXTENSION
1952 if( strcmp(z,"lib")==0 || strcmp(z,"L")==0 ){
1953 if( i==argc-1 ) cmdlineError("missing argument to %s", argv[i]);
1954 azExt = realloc(azExt, sizeof(azExt[0])*(nExt+1));
1955 if( azExt==0 ) cmdlineError("out of memory");
1956 azExt[nExt++] = argv[++i];
1957 }else
1958 #endif
1959 if( strcmp(z,"primarykey")==0 ){
1960 g.bSchemaPK = 1;
1961 }else
1962 if( strcmp(z,"rbu")==0 ){
1963 xDiff = rbudiff_one_table;
1964 }else
1965 if( strcmp(z,"schema")==0 ){
1966 g.bSchemaOnly = 1;
1967 }else
1968 if( strcmp(z,"summary")==0 ){
1969 xDiff = summarize_one_table;
1970 }else
1971 if( strcmp(z,"table")==0 ){
1972 if( i==argc-1 ) cmdlineError("missing argument to %s", argv[i]);
1973 zTab = argv[++i];
1974 g.bSchemaCompare =
1975 sqlite3_stricmp(zTab, "sqlite_schema")==0
1976 || sqlite3_stricmp(zTab, "sqlite_master")==0;
1977 }else
1978 if( strcmp(z,"transaction")==0 ){
1979 useTransaction = 1;
1980 }else
1981 if( strcmp(z,"vtab")==0 ){
1982 g.bHandleVtab = 1;
1983 }else
1985 cmdlineError("unknown option: %s", argv[i]);
1987 }else if( zDb1==0 ){
1988 zDb1 = argv[i];
1989 }else if( zDb2==0 ){
1990 zDb2 = argv[i];
1991 }else{
1992 cmdlineError("unknown argument: %s", argv[i]);
1995 if( zDb2==0 ){
1996 cmdlineError("two database arguments required");
1998 if( g.bSchemaOnly && g.bSchemaCompare ){
1999 cmdlineError("The --schema option is useless with --table %s .", zTab);
2001 rc = sqlite3_open(zDb1, &g.db);
2002 if( rc ){
2003 cmdlineError("cannot open database file \"%s\"", zDb1);
2005 rc = sqlite3_exec(g.db, "SELECT * FROM sqlite_schema", 0, 0, &zErrMsg);
2006 if( rc || zErrMsg ){
2007 cmdlineError("\"%s\" does not appear to be a valid SQLite database", zDb1);
2009 #ifndef SQLITE_OMIT_LOAD_EXTENSION
2010 sqlite3_enable_load_extension(g.db, 1);
2011 for(i=0; i<nExt; i++){
2012 rc = sqlite3_load_extension(g.db, azExt[i], 0, &zErrMsg);
2013 if( rc || zErrMsg ){
2014 cmdlineError("error loading %s: %s", azExt[i], zErrMsg);
2017 free(azExt);
2018 #endif
2019 zSql = sqlite3_mprintf("ATTACH %Q as aux;", zDb2);
2020 rc = sqlite3_exec(g.db, zSql, 0, 0, &zErrMsg);
2021 sqlite3_free(zSql);
2022 zSql = 0;
2023 if( rc || zErrMsg ){
2024 cmdlineError("cannot attach database \"%s\"", zDb2);
2026 rc = sqlite3_exec(g.db, "SELECT * FROM aux.sqlite_schema", 0, 0, &zErrMsg);
2027 if( rc || zErrMsg ){
2028 cmdlineError("\"%s\" does not appear to be a valid SQLite database", zDb2);
2031 if( neverUseTransaction ) useTransaction = 0;
2032 if( useTransaction ) Wfprintf(out, "BEGIN TRANSACTION;\n");
2033 if( xDiff==rbudiff_one_table ){
2034 Wfprintf(out, "CREATE TABLE IF NOT EXISTS rbu_count"
2035 "(tbl TEXT PRIMARY KEY COLLATE NOCASE, cnt INTEGER) "
2036 "WITHOUT ROWID;\n"
2039 if( zTab ){
2040 xDiff(zTab, out);
2041 }else{
2042 /* Handle tables one by one */
2043 pStmt = db_prepare("%s", all_tables_sql() );
2044 while( SQLITE_ROW==sqlite3_step(pStmt) ){
2045 xDiff((const char*)sqlite3_column_text(pStmt,0), out);
2047 sqlite3_finalize(pStmt);
2049 if( useTransaction ) Wfprintf(stdout,"COMMIT;\n");
2051 /* TBD: Handle trigger differences */
2052 /* TBD: Handle view differences */
2053 sqlite3_close(g.db);
2054 return 0;