4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
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
30 /* Output function substitutions that cause UTF8 characters to be rendered
31 ** correctly on Windows:
33 ** fprintf() -> Wfprintf()
37 # include "console_io.h"
38 # define Wfprintf fPrintfUtf8
40 # define Wfprintf fprintf
44 ** All global variables are gathered into the "g" singleton.
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 */
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
73 static void cmdlineError(const char *zFormat
, ...){
74 sqlite3_str
*pOut
= sqlite3_str_new(0);
76 va_start(ap
, zFormat
);
77 sqlite3_str_vappendf(pOut
, zFormat
, ap
);
79 Wfprintf(stderr
, "%s: %s\n", g
.zArgv0
, sqlite3_str_value(pOut
));
81 Wfprintf(stderr
, "\"%s --help\" for more help\n", g
.zArgv0
);
86 ** Print an error message for an error that occurs at runtime, then
89 static void runtimeError(const char *zFormat
, ...){
90 sqlite3_str
*pOut
= sqlite3_str_new(0);
92 va_start(ap
, zFormat
);
93 sqlite3_str_vappendf(pOut
, zFormat
, ap
);
95 Wfprintf(stderr
, "%s: %s\n", g
.zArgv0
, sqlite3_str_value(pOut
));
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
108 static char *safeId(const char *zId
){
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
) ){
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
131 static sqlite3_stmt
*db_vprepare(const char *zFormat
, va_list ap
){
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);
140 runtimeError("SQL statement error: %s\n\"%s\"", sqlite3_errmsg(g
.db
),
146 static sqlite3_stmt
*db_prepare(const char *zFormat
, ...){
149 va_start(ap
, zFormat
);
150 pStmt
= db_vprepare(zFormat
, ap
);
156 ** Free a list of strings
158 static void namelistFree(char **az
){
161 for(i
=0; az
[i
]; i
++) sqlite3_free(az
[i
]);
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.]
188 ** CREATE TABLE t1(a INT UNIQUE, b INTEGER, c TEXT, PRIMARY KEY(c));
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));
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;
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));
239 sqlite3_finalize(pStmt
);
244 pStmt
= db_prepare("PRAGMA %s.index_xinfo=%Q", zDb
, zPkIdxName
);
245 while( SQLITE_ROW
==sqlite3_step(pStmt
) ){
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;
256 sqlite3_finalize(pStmt
);
257 sqlite3_free(zPkIdxName
);
262 pStmt
= db_prepare("PRAGMA %s.table_info=%Q", zDb
, zTab
);
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.
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;
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. */
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));
296 if( truePk
&& (iPKey
= sqlite3_column_int(pStmt
,5))>0 ){
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");
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. */
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;
329 az
[0] = sqlite3_mprintf("%s", azRowid
[i
]);
334 for(i
=1; i
<naz
; i
++) sqlite3_free(az
[i
]);
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
) ){
350 r1
= sqlite3_value_double(X
);
351 sqlite3_snprintf(sizeof(zBuf
), zBuf
, "%!.15g", r1
);
352 fprintf(out
, "%s", zBuf
);
355 case SQLITE_INTEGER
: {
356 fprintf(out
, "%lld", sqlite3_value_int64(X
));
360 const unsigned char *zBlob
= sqlite3_value_blob(X
);
361 int nBlob
= sqlite3_value_bytes(X
);
365 for(i
=0; i
<nBlob
; i
++){
366 fprintf(out
, "%02x", zBlob
[i
]);
370 /* Could be an OOM, could be a zero-byte blob */
376 const unsigned char *zArg
= sqlite3_value_text(X
);
379 fprintf(out
, "NULL");
384 for(i
=j
=0; zArg
[i
]; i
++){
386 int ctl
= iscntrl(c
);
389 fprintf(out
, "%.*s'||X'%02x", i
-j
, &zArg
[j
], c
);
392 fprintf(out
, "%02x", c
);
397 fprintf(out
, "'\n||'");
400 fprintf(out
, "%.*s'", i
-j
+1, &zArg
[j
]);
405 fprintf(out
, "%s'", &zArg
[j
]);
410 fprintf(out
, "NULL");
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);
438 pStmt
= db_prepare("SELECT * FROM aux.%s", zId
);
439 sqlite3_str_appendf(pIns
,"INSERT INTO %s VALUES", zId
);
441 sqlite3_str
*pSql
= sqlite3_str_new(0);
443 for(i
=0; az
[i
]; i
++){
444 sqlite3_str_appendf(pSql
, "%s %s", zSep
, az
[i
]);
447 sqlite3_str_appendf(pSql
," FROM aux.%s", zId
);
449 for(i
=1; i
<=nPk
; i
++){
450 sqlite3_str_appendf(pSql
, "%s %d", zSep
, i
);
453 pStmt
= db_prepare("%s", sqlite3_str_value(pSql
));
455 sqlite3_str_appendf(pIns
, "INSERT INTO %s", zId
);
457 for(i
=0; az
[i
]; i
++){
458 sqlite3_str_appendf(pIns
, "%s%s", zSep
, az
[i
]);
461 sqlite3_str_appendf(pIns
,") VALUES");
464 nCol
= sqlite3_column_count(pStmt
);
465 while( SQLITE_ROW
==sqlite3_step(pStmt
) ){
466 Wfprintf(out
, "%s",sqlite3_str_value(pIns
));
468 for(i
=0; i
<nCol
; i
++){
469 Wfprintf(out
, "%s",zSep
);
470 printQuoted(out
, sqlite3_column_value(pStmt
,i
));
473 Wfprintf(out
, ");\n");
475 sqlite3_finalize(pStmt
);
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",
481 while( SQLITE_ROW
==sqlite3_step(pStmt
) ){
482 Wfprintf(out
, "%s;\n", sqlite3_column_text(pStmt
,0));
484 sqlite3_finalize(pStmt
);
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);
517 Wfprintf(stdout
, "Rowid not accessible for %s\n", zId
);
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
);
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
);
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);
553 for(n
=0; az
[n
] && az2
[n
]; n
++){
554 if( sqlite3_stricmp(az
[n
],az2
[n
])!=0 ) break;
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
);
574 nQ
= nPk2
+1+2*(n2
-nPk2
);
577 for(i
=0; i
<nPk
; i
++){
578 sqlite3_str_appendf(pSql
, "%sB.%s", zSep
, az
[i
]);
581 sqlite3_str_appendf(pSql
, ", 1 /* changed row */");
583 sqlite3_str_appendf(pSql
, ", A.%s IS NOT B.%s, B.%s",
584 az
[i
], az2
[i
], az2
[i
]);
588 sqlite3_str_appendf(pSql
, ", B.%s IS NOT NULL, B.%s",
592 sqlite3_str_appendf(pSql
, "\n FROM main.%s A, aux.%s B\n", zId
, zId
);
594 for(i
=0; i
<nPk
; i
++){
595 sqlite3_str_appendf(pSql
, "%s A.%s=B.%s", zSep
, az
[i
], 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 ? ")" : "");
606 sqlite3_str_appendf(pSql
, "%sB.%s IS NOT NULL%s\n",
607 zSep
, az2
[i
], az2
[i
+1]==0 ? ")" : "");
611 sqlite3_str_appendf(pSql
, " UNION ALL\n");
614 for(i
=0; i
<nPk
; i
++){
615 sqlite3_str_appendf(pSql
, "%sA.%s", zSep
, az
[i
]);
618 sqlite3_str_appendf(pSql
, ", 2 /* deleted row */");
620 sqlite3_str_appendf(pSql
, ", NULL, NULL");
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
);
626 for(i
=0; i
<nPk
; i
++){
627 sqlite3_str_appendf(pSql
, "%s A.%s=B.%s", zSep
, az
[i
], az
[i
]);
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
]);
636 sqlite3_str_appendf(pSql
, ", 3 /* inserted row */");
638 sqlite3_str_appendf(pSql
, ", 1, B.%s", az2
[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
);
644 for(i
=0; i
<nPk
; i
++){
645 sqlite3_str_appendf(pSql
, "%s A.%s=B.%s", zSep
, az
[i
], az
[i
]);
648 sqlite3_str_appendf(pSql
, ")\n ORDER BY");
650 for(i
=1; i
<=nPk
; i
++){
651 sqlite3_str_appendf(pSql
, "%s%d", zSep
, i
);
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 */
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)",
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
);
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
);
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]);
690 printQuoted(out
, sqlite3_column_value(pStmt
,i
+1));
692 }else{ /* Delete a row */
693 fprintf(out
, "%sDELETE FROM %s", zLead
, zId
);
696 for(i
=0; i
<nPk
; i
++){
697 fprintf(out
, "%s %s=", zSep
, az2
[i
]);
698 printQuoted(out
, sqlite3_column_value(pStmt
,i
));
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");
707 for(i
=0; i
<nPk2
; i
++){
708 fprintf(out
, "%s", zSep
);
710 printQuoted(out
, sqlite3_column_value(pStmt
,i
));
712 for(i
=nPk2
+2; i
<nQ
; i
+=2){
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 */
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)",
731 while( SQLITE_ROW
==sqlite3_step(pStmt
) ){
732 fprintf(out
, "%s;\n", sqlite3_column_text(pStmt
,0));
734 sqlite3_finalize(pStmt
);
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
));
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
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
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
;
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
){
805 for(i
=0; i
<NHASH
; i
++){
810 pHash
->a
= a
& 0xffff;
811 pHash
->b
= b
& 0xffff;
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 */
846 for(i
=0; v
>0; i
++, v
>>=6){
847 zBuf
[i
] = zDigits
[v
&0x3f];
849 for(j
=i
-1; j
>=0; j
--){
855 ** Return the number digits in the base-64 representation of a positive integer
857 static int digit_count(int v
){
859 for(i
=1, x
=64; (unsigned int)v
>=x
; i
++, x
<<= 6){}
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
;
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]);
888 sum3
+= (sum2
<< 8) + (sum1
<< 16) + (sum0
<< 24);
890 case 3: sum3
+= (z
[2] << 8);
891 case 2: sum3
+= (z
[1] << 16);
892 case 1: sum3
+= (z
[0] << 24);
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.
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:
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:
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
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.
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
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
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
;
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
);
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.
984 putInt(lenOut
, &zDelta
);
986 memcpy(zDelta
, zOut
, lenOut
);
988 putInt(checksum(zOut
, lenOut
), &zDelta
);
990 return (int)(zDelta
- zOrigDelta
);
993 /* Compute the hash table used to locate matching sections in the
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
){
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
){
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] */
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
;
1045 /* Beginning at iSrc, match forwards as far as we can. j counts
1046 ** the number of characters that match */
1047 iSrc
= iBlock
*NHASH
;
1049 j
=0, x
=iSrc
, y
=base
+i
;
1050 (unsigned int)x
<lenSrc
&& (unsigned int)y
<lenOut
;
1053 if( zSrc
[x
]!=zOut
[y
] ) break;
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;
1064 /* Compute the offset and size of the matching region */
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 */
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.
1088 /* Add an insert command before the copy */
1089 putInt(bestLitsz
,&zDelta
);
1091 memcpy(zDelta
, &zOut
[base
], bestLitsz
);
1092 zDelta
+= bestLitsz
;
1096 putInt(bestCnt
, &zDelta
);
1098 putInt(bestOfst
, &zDelta
);
1100 if( bestOfst
+ bestCnt
-1 > lastRead
){
1101 lastRead
= bestOfst
+ bestCnt
- 1;
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
);
1113 memcpy(zDelta
, &zOut
[base
], lenOut
-base
);
1114 zDelta
+= lenOut
-base
;
1119 /* Advance the hash by one character. Keep looking for a match */
1120 hash_next(&h
, zOut
[base
+i
+NHASH
]);
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.
1128 putInt(lenOut
-base
, &zDelta
);
1130 memcpy(zDelta
, &zOut
[base
], lenOut
-base
);
1131 zDelta
+= lenOut
-base
;
1133 /* Output the final checksum record. */
1134 putInt(checksum(zOut
, lenOut
), &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) */
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(
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
);
1179 sqlite3_str_appendf(pSql
, "\nUNION ALL\nSELECT ");
1180 strPrintfArray(pSql
, ", ", "%s", 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. */
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
1206 sqlite3_str_appendf(pSql
, ", '");
1207 strPrintfArray(pSql
, "", ".", azCol
, nPK
);
1208 sqlite3_str_appendf(pSql
, "' ||\n");
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 ",
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 */
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. */
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
);
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
));
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
));
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
++){
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
);
1319 aDelta
= sqlite3_malloc(nFinal
+ 60);
1320 nDelta
= rbuDeltaCreate(aSrc
, nSrc
, aFinal
, nFinal
, aDelta
);
1321 if( nDelta
<nFinal
){
1324 for(j
=0; j
<nDelta
; j
++) fprintf(out
, "%02x", (u8
)aDelta
[j
]);
1326 zOtaControl
[i
-bOtaRowid
] = 'f';
1329 sqlite3_free(aDelta
);
1333 printQuoted(out
, sqlite3_column_value(pStmt
, i
));
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
);
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
));
1360 ** Display a summary of differences between two versions of the same
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);
1403 for(n
=0; az
[n
]; n
++){
1404 if( sqlite3_stricmp(az
[n
],az2
[n
])!=0 ) break;
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(*)");
1421 sqlite3_str_appendf(pSql
, ", 0\n");
1424 for(i
=nPk
; az
[i
]; i
++){
1425 sqlite3_str_appendf(pSql
, "%sA.%s IS NOT B.%s", zSep
, az
[i
], az
[i
]);
1428 sqlite3_str_appendf(pSql
, ")\n");
1430 sqlite3_str_appendf(pSql
, " FROM main.%s A, aux.%s B\n", zId
, zId
);
1432 for(i
=0; i
<nPk
; i
++){
1433 sqlite3_str_appendf(pSql
, "%s A.%s=B.%s", zSep
, az
[i
], az
[i
]);
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
);
1441 for(i
=0; i
<nPk
; i
++){
1442 sqlite3_str_appendf(pSql
, "%s A.%s=B.%s", zSep
, az
[i
], az
[i
]);
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
);
1451 for(i
=0; i
<nPk
; i
++){
1452 sqlite3_str_appendf(pSql
, "%s A.%s=B.%s", zSep
, az
[i
], az
[i
]);
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
));
1468 while( SQLITE_ROW
==sqlite3_step(pStmt
) ){
1469 switch( sqlite3_column_int(pStmt
,0) ){
1471 nUpdate
= sqlite3_column_int64(pStmt
,2);
1472 nUnchanged
= sqlite3_column_int64(pStmt
,1) - nUpdate
;
1475 nDelete
= sqlite3_column_int64(pStmt
,1);
1478 nInsert
= sqlite3_column_int64(pStmt
,1);
1482 sqlite3_finalize(pStmt
);
1484 "%s: %lld changes, %lld inserts, %lld deletes, %lld unchanged\n",
1485 zTab
, nUpdate
, nInsert
, nDelete
, nUnchanged
);
1487 end_summarize_one_table
:
1496 ** Write a 64-bit signed integer as a varint onto out
1498 static void putsVarint(FILE *out
, sqlite3_uint64 v
){
1500 unsigned char p
[12];
1501 if( v
& (((sqlite3_uint64
)0xff000000)<<32) ){
1502 p
[8] = (unsigned char)v
;
1504 for(i
=7; i
>=0; i
--){
1505 p
[i
] = (unsigned char)((v
& 0x7f) | 0x80);
1508 fwrite(p
, 8, 1, out
);
1512 p
[n
--] = (unsigned char)((v
& 0x7f) | 0x80);
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
);
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
);
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
);
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
);
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
);
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
) ){
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);
1588 aiPk
= sqlite3_realloc(aiPk
, sizeof(int)*nPk
);
1589 if( aiPk
==0 ) runtimeError("out of memory");
1594 sqlite3_finalize(pStmt
);
1595 if( nPk
==0 ) goto end_changeset_one_table
;
1597 sqlite3_str_appendf(pSql
, "SELECT %d", SQLITE_UPDATE
);
1598 for(i
=0; i
<nCol
; i
++){
1600 sqlite3_str_appendf(pSql
, ",\n A.%s", azCol
[i
]);
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
);
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
]]);
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
]);
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
++){
1624 sqlite3_str_appendf(pSql
, ",\n A.%s", azCol
[i
]);
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
);
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
]]);
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
++){
1641 sqlite3_str_appendf(pSql
, ",\n B.%s", azCol
[i
]);
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
);
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
]]);
1654 sqlite3_str_appendf(pSql
, ")\n");
1655 sqlite3_str_appendf(pSql
, " ORDER BY");
1657 for(i
=0; i
<nPk
; i
++){
1658 sqlite3_str_appendf(pSql
, "%s %d", zSep
, aiPk
[i
]+2);
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
;
1669 putsVarint(out
, (sqlite3_uint64
)nCol
);
1670 for(i
=0; i
<nCol
; i
++) putc(aiFlg
[i
], out
);
1671 fwrite(zTab
, 1, strlen(zTab
), 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);
1679 switch( sqlite3_column_int(pStmt
,0) ){
1680 case SQLITE_UPDATE
: {
1681 for(k
=1, i
=0; i
<nCol
; i
++){
1683 putValue(out
, pStmt
, k
);
1685 }else if( sqlite3_column_int(pStmt
,k
) ){
1686 putValue(out
, pStmt
, k
+1);
1693 for(k
=1, i
=0; i
<nCol
; i
++){
1697 }else if( sqlite3_column_int(pStmt
,k
) ){
1698 putValue(out
, pStmt
, k
+2);
1707 case SQLITE_INSERT
: {
1708 for(k
=1, i
=0; i
<nCol
; i
++){
1710 putValue(out
, pStmt
, k
);
1713 putValue(out
, pStmt
, k
+2);
1719 case SQLITE_DELETE
: {
1720 for(k
=1, i
=0; i
<nCol
; i
++){
1722 putValue(out
, pStmt
, k
);
1725 putValue(out
, pStmt
, k
+1);
1733 sqlite3_finalize(pStmt
);
1735 end_changeset_one_table
:
1736 while( nCol
>0 ) sqlite3_free(azCol
[--nCol
]);
1737 sqlite3_free(azCol
);
1740 sqlite3_free(aiFlg
);
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
;
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
++;
1767 case '"': q
= '"'; break;
1768 case '\'': q
= '\''; break;
1769 case '`': q
= '`'; break;
1770 case '[': q
= ']'; break;
1775 while( *p
&& pOut
<pEnd
){
1780 if( pOut
<pEnd
) *pOut
++ = *p
;
1784 while( *p
&& !is_whitespace(*p
) && *p
!='(' ){
1785 if( pOut
<pEnd
) *pOut
++ = *p
;
1795 ** This function is the implementation of SQL scalar function "module_name":
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
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
){
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');"
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
);
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"
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"
1881 "SELECT name FROM main.sqlite_schema\n"
1882 " WHERE type='table' AND sql NOT LIKE 'CREATE VIRTUAL%%'\n"
1884 "SELECT name FROM aux.sqlite_schema\n"
1885 " WHERE type='table' AND sql NOT LIKE 'CREATE VIRTUAL%%'\n"
1891 ** Print sketchy documentation for this utility program
1893 static void showHelp(void){
1894 Wfprintf(stdout
, "Usage: %s [options] DB1 DB2\n", g
.zArgv0
);
1896 "Output SQL text that would transform DB1 into DB2.\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;
1918 sqlite3_stmt
*pStmt
;
1921 void (*xDiff
)(const char*,FILE*) = diff_one_table
;
1922 #ifndef SQLITE_OMIT_LOAD_EXTENSION
1926 int useTransaction
= 0;
1927 int neverUseTransaction
= 0;
1930 sqlite3_config(SQLITE_CONFIG_SINGLETHREAD
);
1931 for(i
=1; i
<argc
; i
++){
1932 const char *z
= argv
[i
];
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;
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);
1947 if( strcmp(z
,"help")==0 ){
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
];
1959 if( strcmp(z
,"primarykey")==0 ){
1962 if( strcmp(z
,"rbu")==0 ){
1963 xDiff
= rbudiff_one_table
;
1965 if( strcmp(z
,"schema")==0 ){
1968 if( strcmp(z
,"summary")==0 ){
1969 xDiff
= summarize_one_table
;
1971 if( strcmp(z
,"table")==0 ){
1972 if( i
==argc
-1 ) cmdlineError("missing argument to %s", argv
[i
]);
1975 sqlite3_stricmp(zTab
, "sqlite_schema")==0
1976 || sqlite3_stricmp(zTab
, "sqlite_master")==0;
1978 if( strcmp(z
,"transaction")==0 ){
1981 if( strcmp(z
,"vtab")==0 ){
1985 cmdlineError("unknown option: %s", argv
[i
]);
1987 }else if( zDb1
==0 ){
1989 }else if( zDb2
==0 ){
1992 cmdlineError("unknown argument: %s", argv
[i
]);
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
);
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
);
2019 zSql
= sqlite3_mprintf("ATTACH %Q as aux;", zDb2
);
2020 rc
= sqlite3_exec(g
.db
, zSql
, 0, 0, &zErrMsg
);
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) "
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
);