Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
Comment: | Add the "sqldiff" utility program. |
---|---|
Downloads: | Tarball | ZIP archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA1: |
88da5bf5d2c4f848dbd4b5ffb1539abf |
User & Date: | drh 2015-04-09 13:34:29.580 |
Context
2015-04-09
| ||
13:40 | Fix comment typos in the sqldiff.c utility program. (check-in: 32ab2bb990 user: drh tags: trunk) | |
13:34 | Add the "sqldiff" utility program. (check-in: 88da5bf5d2 user: drh tags: trunk) | |
11:24 | Remove a variable initializion to silence a harmless compiler warning. (check-in: 79861adbef user: drh tags: trunk) | |
Changes
Changes to Makefile.in.
︙ | ︙ | |||
532 533 534 535 536 537 538 539 540 541 542 543 544 545 | -avoid-version sqlite3$(TEXE): $(TOP)/src/shell.c libsqlite3.la sqlite3.h $(LTLINK) $(READLINE_FLAGS) \ -o $@ $(TOP)/src/shell.c libsqlite3.la \ $(LIBREADLINE) $(TLIBS) -rpath "$(libdir)" mptester$(EXE): sqlite3.c $(TOP)/mptest/mptest.c $(LTLINK) -o $@ -I. $(TOP)/mptest/mptest.c sqlite3.c \ $(TLIBS) -rpath "$(libdir)" MPTEST1=./mptester$(EXE) mptest.db $(TOP)/mptest/crash01.test --repeat 20 MPTEST2=./mptester$(EXE) mptest.db $(TOP)/mptest/multiwrite01.test --repeat 20 mptest: mptester$(EXE) | > > > | 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 | -avoid-version sqlite3$(TEXE): $(TOP)/src/shell.c libsqlite3.la sqlite3.h $(LTLINK) $(READLINE_FLAGS) \ -o $@ $(TOP)/src/shell.c libsqlite3.la \ $(LIBREADLINE) $(TLIBS) -rpath "$(libdir)" sqldiff$(EXE): $(TOP)/tool/sqldiff.c sqlite3.c sqlite3.h $(LTLINK) -o $@ $(TOP)/tool/sqldiff.c sqlite3.c $(TLIBS) mptester$(EXE): sqlite3.c $(TOP)/mptest/mptest.c $(LTLINK) -o $@ -I. $(TOP)/mptest/mptest.c sqlite3.c \ $(TLIBS) -rpath "$(libdir)" MPTEST1=./mptester$(EXE) mptest.db $(TOP)/mptest/crash01.test --repeat 20 MPTEST2=./mptester$(EXE) mptest.db $(TOP)/mptest/multiwrite01.test --repeat 20 mptest: mptester$(EXE) |
︙ | ︙ |
Changes to Makefile.msc.
︙ | ︙ | |||
1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 | libtclsqlite3.lib: tclsqlite.lo libsqlite3.lib $(LTLIB) $(LTLIBOPTS) $(LTLIBPATHS) /OUT:$@ tclsqlite.lo libsqlite3.lib $(LIBTCL:tcl=tclstub) $(TLIBS) sqlite3.exe: $(TOP)\src\shell.c $(SHELL_CORE_DEP) $(LIBRESOBJS) sqlite3.h $(LTLINK) $(SHELL_COMPILE_OPTS) $(READLINE_FLAGS) $(TOP)\src\shell.c \ /link /pdb:sqlite3sh.pdb $(LTLINKOPTS) $(SHELL_LINK_OPTS) $(LTLIBPATHS) $(LIBRESOBJS) $(LIBREADLINE) $(LTLIBS) $(TLIBS) mptester.exe: $(TOP)\mptest\mptest.c $(SHELL_CORE_DEP) $(LIBRESOBJS) sqlite3.h $(LTLINK) $(SHELL_COMPILE_OPTS) $(TOP)\mptest\mptest.c \ /link $(LTLINKOPTS) $(LTLIBPATHS) $(SHELL_LINK_OPTS) $(LIBRESOBJS) $(LIBREADLINE) $(LTLIBS) $(TLIBS) MPTEST1 = mptester mptest.db $(TOP)/mptest/crash01.test --repeat 20 MPTEST2 = mptester mptest.db $(TOP)/mptest/multiwrite01.test --repeat 20 | > > > | 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 | libtclsqlite3.lib: tclsqlite.lo libsqlite3.lib $(LTLIB) $(LTLIBOPTS) $(LTLIBPATHS) /OUT:$@ tclsqlite.lo libsqlite3.lib $(LIBTCL:tcl=tclstub) $(TLIBS) sqlite3.exe: $(TOP)\src\shell.c $(SHELL_CORE_DEP) $(LIBRESOBJS) sqlite3.h $(LTLINK) $(SHELL_COMPILE_OPTS) $(READLINE_FLAGS) $(TOP)\src\shell.c \ /link /pdb:sqlite3sh.pdb $(LTLINKOPTS) $(SHELL_LINK_OPTS) $(LTLIBPATHS) $(LIBRESOBJS) $(LIBREADLINE) $(LTLIBS) $(TLIBS) sqldiff.exe: $(TOP)\tool\sqldiff.c sqlite3.c sqlite3.h $(LTLINK) $(TOP)\tool\sqldiff.c sqlite3.c mptester.exe: $(TOP)\mptest\mptest.c $(SHELL_CORE_DEP) $(LIBRESOBJS) sqlite3.h $(LTLINK) $(SHELL_COMPILE_OPTS) $(TOP)\mptest\mptest.c \ /link $(LTLINKOPTS) $(LTLIBPATHS) $(SHELL_LINK_OPTS) $(LIBRESOBJS) $(LIBREADLINE) $(LTLIBS) $(TLIBS) MPTEST1 = mptester mptest.db $(TOP)/mptest/crash01.test --repeat 20 MPTEST2 = mptester mptest.db $(TOP)/mptest/multiwrite01.test --repeat 20 |
︙ | ︙ |
Changes to main.mk.
︙ | ︙ | |||
399 400 401 402 403 404 405 406 407 408 409 410 411 412 | $(AR) libsqlite3.a $(LIBOBJ) $(RANLIB) libsqlite3.a sqlite3$(EXE): $(TOP)/src/shell.c libsqlite3.a sqlite3.h $(TCCX) $(READLINE_FLAGS) -o sqlite3$(EXE) \ $(TOP)/src/shell.c \ libsqlite3.a $(LIBREADLINE) $(TLIBS) $(THREADLIB) mptester$(EXE): sqlite3.c $(TOP)/mptest/mptest.c $(TCCX) -o $@ -I. $(TOP)/mptest/mptest.c sqlite3.c \ $(TLIBS) $(THREADLIB) MPTEST1=./mptester$(EXE) mptest.db $(TOP)/mptest/crash01.test --repeat 20 MPTEST2=./mptester$(EXE) mptest.db $(TOP)/mptest/multiwrite01.test --repeat 20 | > > > > | 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 | $(AR) libsqlite3.a $(LIBOBJ) $(RANLIB) libsqlite3.a sqlite3$(EXE): $(TOP)/src/shell.c libsqlite3.a sqlite3.h $(TCCX) $(READLINE_FLAGS) -o sqlite3$(EXE) \ $(TOP)/src/shell.c \ libsqlite3.a $(LIBREADLINE) $(TLIBS) $(THREADLIB) sqldiff$(EXE): $(TOP)/tool/sqldiff.c sqlite3.c sqlite3.h $(TCCX) -o sqldiff$(EXE) -DSQLITE_THREADSAFE=0 \ $(TOP)/tool/sqldiff.c sqlite3.c $(TLIBS) $(THREADLIB) mptester$(EXE): sqlite3.c $(TOP)/mptest/mptest.c $(TCCX) -o $@ -I. $(TOP)/mptest/mptest.c sqlite3.c \ $(TLIBS) $(THREADLIB) MPTEST1=./mptester$(EXE) mptest.db $(TOP)/mptest/crash01.test --repeat 20 MPTEST2=./mptester$(EXE) mptest.db $(TOP)/mptest/multiwrite01.test --repeat 20 |
︙ | ︙ |
Added tool/sqldiff.c.
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 | /* ** 2015-04-06 ** ** The author disclaims copyright to this source code. In place of ** a legal notice, here is a blessing: ** ** May you do good and not evil. ** May you find forgiveness for yourself and forgive others. ** May you share freely, never taking more than you give. ** ************************************************************************* ** ** This is a utility problem that computes the differences in content ** between two SQLite databases. */ #include <stdio.h> #include <stdlib.h> #include <stdarg.h> #include <ctype.h> #include <string.h> #include "sqlite3.h" /* ** All global variables are gathered into the "g" singleton. */ struct GlobalVars { const char *zArgv0; /* Name of program */ int bSchemaOnly; /* Only show schema differences */ unsigned fDebug; /* Debug flags */ sqlite3 *db; /* The database connection */ } g; /* ** Allowed values for g.fDebug */ #define DEBUG_COLUMN_NAMES 0x000001 #define DEBUG_DIFF_SQL 0x000002 /* ** Dynamic string object */ typedef struct Str Str; struct Str { char *z; /* Text of the string */ int nAlloc; /* Bytes allocated in z[] */ int nUsed; /* Bytes actually used in z[] */ }; /* ** Initialize a Str object */ static void strInit(Str *p){ p->z = 0; p->nAlloc = 0; p->nUsed = 0; } /* ** Print an error resulting from faulting command-line arguments and ** abort the program. */ static void cmdlineError(const char *zFormat, ...){ va_list ap; fprintf(stderr, "%s: ", g.zArgv0); va_start(ap, zFormat); vfprintf(stderr, zFormat, ap); va_end(ap); fprintf(stderr, "\n\"%s --help\" for more help\n", g.zArgv0); exit(1); } /* ** Print an error message for an error that occurs at runtime, then ** abort the program. */ static void runtimeError(const char *zFormat, ...){ va_list ap; fprintf(stderr, "%s: ", g.zArgv0); va_start(ap, zFormat); vfprintf(stderr, zFormat, ap); va_end(ap); fprintf(stderr, "\n"); exit(1); } /* ** Free all memory held by a Str object */ static void strFree(Str *p){ sqlite3_free(p->z); strInit(p); } /* ** Add formatted text to the end of a Str object */ static void strPrintf(Str *p, const char *zFormat, ...){ int nNew; for(;;){ if( p->z ){ va_list ap; va_start(ap, zFormat); sqlite3_vsnprintf(p->nAlloc-p->nUsed, p->z+p->nUsed, zFormat, ap); va_end(ap); nNew = (int)strlen(p->z + p->nUsed); }else{ nNew = p->nAlloc; } if( p->nUsed+nNew < p->nAlloc-1 ){ p->nUsed += nNew; break; } p->nAlloc = p->nAlloc*2 + 1000; p->z = sqlite3_realloc(p->z, p->nAlloc); if( p->z==0 ) runtimeError("out of memory"); } } /* Safely quote an SQL identifier. Use the minimum amount of transformation ** necessary to allow the string to be used with %s. ** ** Space to hold the returned string is obtained from sqlite3_malloc(). The ** caller is responsible for ensuring this space is freed when no longer ** needed. */ static char *safeId(const char *zId){ /* All SQLite keywords, in alphabetical order */ static const char *azKeywords[] = { "ABORT", "ACTION", "ADD", "AFTER", "ALL", "ALTER", "ANALYZE", "AND", "AS", "ASC", "ATTACH", "AUTOINCREMENT", "BEFORE", "BEGIN", "BETWEEN", "BY", "CASCADE", "CASE", "CAST", "CHECK", "COLLATE", "COLUMN", "COMMIT", "CONFLICT", "CONSTRAINT", "CREATE", "CROSS", "CURRENT_DATE", "CURRENT_TIME", "CURRENT_TIMESTAMP", "DATABASE", "DEFAULT", "DEFERRABLE", "DEFERRED", "DELETE", "DESC", "DETACH", "DISTINCT", "DROP", "EACH", "ELSE", "END", "ESCAPE", "EXCEPT", "EXCLUSIVE", "EXISTS", "EXPLAIN", "FAIL", "FOR", "FOREIGN", "FROM", "FULL", "GLOB", "GROUP", "HAVING", "IF", "IGNORE", "IMMEDIATE", "IN", "INDEX", "INDEXED", "INITIALLY", "INNER", "INSERT", "INSTEAD", "INTERSECT", "INTO", "IS", "ISNULL", "JOIN", "KEY", "LEFT", "LIKE", "LIMIT", "MATCH", "NATURAL", "NO", "NOT", "NOTNULL", "NULL", "OF", "OFFSET", "ON", "OR", "ORDER", "OUTER", "PLAN", "PRAGMA", "PRIMARY", "QUERY", "RAISE", "RECURSIVE", "REFERENCES", "REGEXP", "REINDEX", "RELEASE", "RENAME", "REPLACE", "RESTRICT", "RIGHT", "ROLLBACK", "ROW", "SAVEPOINT", "SELECT", "SET", "TABLE", "TEMP", "TEMPORARY", "THEN", "TO", "TRANSACTION", "TRIGGER", "UNION", "UNIQUE", "UPDATE", "USING", "VACUUM", "VALUES", "VIEW", "VIRTUAL", "WHEN", "WHERE", "WITH", "WITHOUT", }; int lwr, upr, mid, c, i, x; for(i=x=0; (c = zId[i])!=0; i++){ if( !isalpha(c) && c!='_' ){ if( i>0 && isdigit(c) ){ x++; }else{ return sqlite3_mprintf("\"%w\"", zId); } } } if( x ) return sqlite3_mprintf("%s", zId); lwr = 0; upr = sizeof(azKeywords)/sizeof(azKeywords[0]) - 1; while( lwr<=upr ){ mid = (lwr+upr)/2; c = sqlite3_stricmp(azKeywords[mid], zId); if( c==0 ) return sqlite3_mprintf("\"%w\"", zId); if( c<0 ){ lwr = mid+1; }else{ upr = mid-1; } } return sqlite3_mprintf("%s", zId); } /* ** Prepare a new SQL statement. Print an error and abort if anything ** goes wrong. */ static sqlite3_stmt *db_vprepare(const char *zFormat, va_list ap){ char *zSql; int rc; sqlite3_stmt *pStmt; zSql = sqlite3_vmprintf(zFormat, ap); if( zSql==0 ) runtimeError("out of memory"); rc = sqlite3_prepare_v2(g.db, zSql, -1, &pStmt, 0); if( rc ){ runtimeError("SQL statement error: %s\n\"%s\"", sqlite3_errmsg(g.db), zSql); } sqlite3_free(zSql); return pStmt; } static sqlite3_stmt *db_prepare(const char *zFormat, ...){ va_list ap; sqlite3_stmt *pStmt; va_start(ap, zFormat); pStmt = db_vprepare(zFormat, ap); va_end(ap); return pStmt; } /* ** Free a list of strings */ static void namelistFree(char **az){ if( az ){ int i; for(i=0; az[i]; i++) sqlite3_free(az[i]); sqlite3_free(az); } } /* ** Return a list of column names for the table zDb.zTab. Space to ** old the list is obtained from malloc() and should released by calling ** namelistFree() when no longer needed. ** ** Primary key columns are listed first, followed by data columns. The ** "primary key" in the previous sentence is the true primary key - the ** rowid or INTEGER PRIMARY KEY for ordinary tables or the declared ** PRIMARY KEY for WITHOUT ROWID tables. The number of columns in the ** primary key is returned in *pNPkey. ** ** If the table is a rowid table for which the rowid is inaccessible, ** then this routine returns a NULL pointer. ** ** Examples: ** CREATE TABLE t1(a INT UNIQUE, b INTEGER, c TEXT, PRIMARY KEY(c)); ** *pnPKey = 1; ** az = { "rowid", "a", "b", "c", 0 } ** ** CREATE TABLE t2(a INT UNIQUE, b INTEGER, c TEXT, PRIMARY KEY(b)); ** *pnPKey = 1; ** az = { "b", "a", "c", 0 } ** ** CREATE TABLE t3(x,y,z,PRIMARY KEY(y,z)); ** *pnPKey = 1 ** az = { "rowid", "x", "y", "z", 0 } ** ** CREATE TABLE t4(x,y,z,PRIMARY KEY(y,z)) WITHOUT ROWID; ** *pnPKey = 2 ** az = { "y", "z", "x", 0 } ** ** CREATE TABLE t5(rowid,_rowid_,oid); ** az = 0 // The rowid is not accessible */ static char **columnNames(const char *zDb, const char *zTab, int *pnPKey){ char **az = 0; int naz = 0; sqlite3_stmt *pStmt; char *zPkIdxName = 0; /* Name of the PRIMARY KEY index */ int truePk = 0; /* PRAGMA table_info indentifies the true PK */ int nPK = 0; /* Number of PRIMARY KEY columns */ int i, j; pStmt = db_prepare("PRAGMA %s.index_list=%Q", zDb, zTab); while( SQLITE_ROW==sqlite3_step(pStmt) ){ if( sqlite3_stricmp((const char*)sqlite3_column_text(pStmt,3),"pk")==0 ){ zPkIdxName = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 1)); break; } } sqlite3_finalize(pStmt); if( zPkIdxName ){ int nKey = 0; int nCol = 0; truePk = 0; pStmt = db_prepare("PRAGMA %s.index_xinfo=%Q", zDb, zPkIdxName); while( SQLITE_ROW==sqlite3_step(pStmt) ){ nCol++; if( sqlite3_column_int(pStmt,5) ){ nKey++; continue; } if( sqlite3_column_int(pStmt,1)>=0 ) truePk = 1; } if( nCol==nKey ) truePk = 1; if( truePk ){ nPK = nKey; }else{ nPK = 1; } sqlite3_finalize(pStmt); sqlite3_free(zPkIdxName); }else{ truePk = 1; nPK = 1; } *pnPKey = nPK; naz = nPK; az = sqlite3_malloc( sizeof(char*)*(nPK+1) ); if( az==0 ) runtimeError("out of memory"); memset(az, 0, sizeof(char*)*(nPK+1)); pStmt = db_prepare("PRAGMA %s.table_info=%Q", zDb, zTab); while( SQLITE_ROW==sqlite3_step(pStmt) ){ int iPKey; if( truePk && (iPKey = sqlite3_column_int(pStmt,5))>0 ){ az[iPKey-1] = safeId((char*)sqlite3_column_text(pStmt,1)); }else{ az = sqlite3_realloc(az, sizeof(char*)*(naz+2) ); if( az==0 ) runtimeError("out of memory"); az[naz++] = safeId((char*)sqlite3_column_text(pStmt,1)); } } sqlite3_finalize(pStmt); if( az ) az[naz] = 0; if( az[0]==0 ){ const char *azRowid[] = { "rowid", "_rowid_", "oid" }; for(i=0; i<sizeof(azRowid)/sizeof(azRowid[0]); i++){ for(j=1; j<naz; j++){ if( sqlite3_stricmp(az[j], azRowid[i])==0 ) break; } if( j>=naz ){ az[0] = sqlite3_mprintf("%s", azRowid[i]); break; } } if( az[0]==0 ){ for(i=1; i<naz; i++) sqlite3_free(az[i]); sqlite3_free(az); az = 0; } } return az; } /* ** Print the sqlite3_value X as an SQL literal. */ static void printQuoted(sqlite3_value *X){ switch( sqlite3_value_type(X) ){ case SQLITE_FLOAT: { double r1; char zBuf[50]; r1 = sqlite3_value_double(X); sqlite3_snprintf(sizeof(zBuf), zBuf, "%!.15g", r1); printf("%s", zBuf); break; } case SQLITE_INTEGER: { printf("%lld", sqlite3_value_int64(X)); break; } case SQLITE_BLOB: { const unsigned char *zBlob = sqlite3_value_blob(X); int nBlob = sqlite3_value_bytes(X); if( zBlob ){ int i; printf("x'"); for(i=0; i<nBlob; i++){ printf("%02x", zBlob[i]); } printf("'"); }else{ printf("NULL"); } break; } case SQLITE_TEXT: { const unsigned char *zArg = sqlite3_value_text(X); int i, j; if( zArg==0 ){ printf("NULL"); }else{ printf("'"); for(i=j=0; zArg[i]; i++){ if( zArg[i]=='\'' ){ printf("%.*s'", i-j+1, &zArg[j]); j = i+1; } } printf("%s'", &zArg[j]); } break; } case SQLITE_NULL: { printf("NULL"); break; } } } /* ** Output SQL that will recreate the aux.zTab table. */ static void dump_table(const char *zTab){ char *zId = safeId(zTab); /* Name of the table */ char **az = 0; /* List of columns */ int nPk; /* Number of true primary key columns */ int nCol; /* Number of data columns */ int i; /* Loop counter */ sqlite3_stmt *pStmt; /* SQL statement */ const char *zSep; /* Separator string */ Str ins; /* Beginning of the INSERT statement */ pStmt = db_prepare("SELECT sql FROM aux.sqlite_master WHERE name=%Q", zTab); if( SQLITE_ROW==sqlite3_step(pStmt) ){ printf("%s;\n", sqlite3_column_text(pStmt,0)); } sqlite3_finalize(pStmt); if( !g.bSchemaOnly ){ az = columnNames("aux", zTab, &nPk); strInit(&ins); if( az==0 ){ pStmt = db_prepare("SELECT * FROM aux.%s", zId); strPrintf(&ins,"INSERT INTO %s VALUES", zId); }else{ Str sql; strInit(&sql); zSep = "SELECT"; for(i=0; az[i]; i++){ strPrintf(&sql, "%s %s", zSep, az[i]); zSep = ","; } strPrintf(&sql," FROM aux.%s", zId); zSep = " ORDER BY"; for(i=1; i<=nPk; i++){ strPrintf(&sql, "%s %d", zSep, i); zSep = ","; } pStmt = db_prepare("%s", sql.z); strFree(&sql); strPrintf(&ins, "INSERT INTO %s", zId); zSep = "("; for(i=0; az[i]; i++){ strPrintf(&ins, "%s%s", zSep, az[i]); zSep = ","; } strPrintf(&ins,") VALUES"); namelistFree(az); } nCol = sqlite3_column_count(pStmt); while( SQLITE_ROW==sqlite3_step(pStmt) ){ printf("%s",ins.z); zSep = "("; for(i=0; i<nCol; i++){ printf("%s",zSep); printQuoted(sqlite3_column_value(pStmt,i)); zSep = ","; } printf(");\n"); } sqlite3_finalize(pStmt); strFree(&ins); } /* endif !g.bSchemaOnly */ pStmt = db_prepare("SELECT sql FROM aux.sqlite_master" " WHERE type='index' AND tbl_name=%Q AND sql IS NOT NULL", zTab); while( SQLITE_ROW==sqlite3_step(pStmt) ){ printf("%s;\n", sqlite3_column_text(pStmt,0)); } sqlite3_finalize(pStmt); } /* ** Compute all differences for a single table. */ static void diff_one_table(const char *zTab){ char *zId = safeId(zTab); /* Name of table (translated for us in SQL) */ char **az = 0; /* Columns in main */ char **az2 = 0; /* Columns in aux */ int nPk; /* Primary key columns in main */ int nPk2; /* Primary key columns in aux */ int n; /* Number of columns in main */ int n2; /* Number of columns in aux */ int nQ; /* Number of output columns in the diff query */ int i; /* Loop counter */ const char *zSep; /* Separator string */ Str sql; /* Comparison query */ sqlite3_stmt *pStmt; /* Query statement to do the diff */ strInit(&sql); if( g.fDebug==DEBUG_COLUMN_NAMES ){ /* Simply run columnNames() on all tables of the origin ** database and show the results. This is used for testing ** and debugging of the columnNames() function. */ az = columnNames("aux",zTab, &nPk); if( az==0 ){ printf("Rowid not accessible for %s\n", zId); }else{ printf("%s:", zId); for(i=0; az[i]; i++){ printf(" %s", az[i]); if( i+1==nPk ) printf(" *"); } printf("\n"); } goto end_diff_one_table; } if( sqlite3_table_column_metadata(g.db,"aux",zTab,0,0,0,0,0,0) ){ if( !sqlite3_table_column_metadata(g.db,"main",zTab,0,0,0,0,0,0) ){ /* Table missing from second database. */ printf("DROP TABLE %s;\n", zId); } goto end_diff_one_table; } if( sqlite3_table_column_metadata(g.db,"main",zTab,0,0,0,0,0,0) ){ /* Table missing from source */ dump_table(zTab); goto end_diff_one_table; } az = columnNames("main", zTab, &nPk); az2 = columnNames("aux", zTab, &nPk2); if( az && az2 ){ for(n=0; az[n]; n++){ if( sqlite3_stricmp(az[n],az2[n])!=0 ) break; } } if( az==0 || az2==0 || nPk!=nPk2 || az[n] ){ /* Schema mismatch */ printf("DROP TABLE %s;\n", zId); dump_table(zTab); goto end_diff_one_table; } /* Build the comparison query */ for(n2=n; az[n2]; n2++){} nQ = nPk2+1+2*(n2-nPk2); if( n2>nPk2 ){ zSep = "SELECT "; for(i=0; i<nPk; i++){ strPrintf(&sql, "%sB.%s", zSep, az[i]); zSep = ", "; } strPrintf(&sql, ", 1%s -- changed row\n", nPk==n ? "" : ","); while( az[i] ){ strPrintf(&sql, " A.%s IS NOT B.%s, B.%s%s\n", az[i], az[i], az[i], i==n2-1 ? "" : ","); i++; } strPrintf(&sql, " FROM main.%s A, aux.%s B\n", zId, zId); zSep = " WHERE"; for(i=0; i<nPk; i++){ strPrintf(&sql, "%s A.%s=B.%s", zSep, az[i], az[i]); zSep = " AND"; } zSep = "\n AND ("; while( az[i] ){ strPrintf(&sql, "%sA.%s IS NOT B.%s%s\n", zSep, az[i], az[i], i==n2-1 ? ")" : ""); zSep = " OR "; i++; } strPrintf(&sql, " UNION ALL\n"); } zSep = "SELECT "; for(i=0; i<nPk; i++){ strPrintf(&sql, "%sA.%s", zSep, az[i]); zSep = ", "; } strPrintf(&sql, ", 2%s -- deleted row\n", nPk==n ? "" : ","); while( az[i] ){ strPrintf(&sql, " NULL, NULL%s\n", i==n2-1 ? "" : ","); i++; } strPrintf(&sql, " FROM main.%s A\n", zId); strPrintf(&sql, " WHERE NOT EXISTS(SELECT 1 FROM aux.%s B\n", zId); zSep = " WHERE"; for(i=0; i<nPk; i++){ strPrintf(&sql, "%s A.%s=B.%s", zSep, az[i], az[i]); zSep = " AND"; } strPrintf(&sql, ")\n"); zSep = " UNION ALL\nSELECT "; for(i=0; i<nPk; i++){ strPrintf(&sql, "%sB.%s", zSep, az[i]); zSep = ", "; } strPrintf(&sql, ", 3%s -- inserted row\n", nPk==n ? "" : ","); while( az2[i] ){ strPrintf(&sql, " 1, B.%s%s\n", az[i], i==n2-1 ? "" : ","); i++; } strPrintf(&sql, " FROM aux.%s B\n", zId); strPrintf(&sql, " WHERE NOT EXISTS(SELECT 1 FROM main.%s A\n", zId); zSep = " WHERE"; for(i=0; i<nPk; i++){ strPrintf(&sql, "%s A.%s=B.%s", zSep, az[i], az[i]); zSep = " AND"; } strPrintf(&sql, ")\n ORDER BY"); zSep = " "; for(i=1; i<=nPk; i++){ strPrintf(&sql, "%s%d", zSep, i); zSep = ", "; } strPrintf(&sql, ";\n"); if( g.fDebug & DEBUG_DIFF_SQL ){ printf("SQL for %s:\n%s\n", zId, sql.z); goto end_diff_one_table; } /* Drop indexes that are missing in the destination */ pStmt = db_prepare( "SELECT name FROM main.sqlite_master" " WHERE type='index' AND tbl_name=%Q" " AND sql IS NOT NULL" " AND sql NOT IN (SELECT sql FROM aux.sqlite_master" " WHERE type='index' AND tbl_name=%Q" " AND sql IS NOT NULL)", zTab, zTab); while( SQLITE_ROW==sqlite3_step(pStmt) ){ char *z = safeId((const char*)sqlite3_column_text(pStmt,0)); printf("DROP INDEX %s;\n", z); sqlite3_free(z); } sqlite3_finalize(pStmt); /* Run the query and output differences */ if( !g.bSchemaOnly ){ pStmt = db_prepare(sql.z); while( SQLITE_ROW==sqlite3_step(pStmt) ){ int iType = sqlite3_column_int(pStmt, nPk); if( iType==1 || iType==2 ){ if( iType==1 ){ /* Change the content of a row */ printf("UPDATE %s", zId); zSep = " SET"; for(i=nPk+1; i<nQ; i+=2){ if( sqlite3_column_int(pStmt,i)==0 ) continue; printf("%s %s=", zSep, az2[(i-1)/2]); zSep = ","; printQuoted(sqlite3_column_value(pStmt,i+1)); } }else{ /* Delete a row */ printf("DELETE FROM %s", zId); } zSep = " WHERE"; for(i=0; i<nPk; i++){ printf("%s %s=", zSep, az2[i]); printQuoted(sqlite3_column_value(pStmt,i)); zSep = ","; } printf(";\n"); }else{ /* Insert a row */ printf("INSERT INTO %s(%s", zId, az2[0]); for(i=1; az2[i]; i++) printf(",%s", az2[i]); printf(") VALUES"); zSep = "("; for(i=0; i<nPk2; i++){ printf("%s", zSep); zSep = ","; printQuoted(sqlite3_column_value(pStmt,i)); } for(i=nPk2+2; i<nQ; i+=2){ printf(","); printQuoted(sqlite3_column_value(pStmt,i)); } printf(");\n"); } } sqlite3_finalize(pStmt); } /* endif !g.bSchemaOnly */ /* Create indexes that are missing in the source */ pStmt = db_prepare( "SELECT sql FROM aux.sqlite_master" " WHERE type='index' AND tbl_name=%Q" " AND sql IS NOT NULL" " AND sql NOT IN (SELECT sql FROM main.sqlite_master" " WHERE type='index' AND tbl_name=%Q" " AND sql IS NOT NULL)", zTab, zTab); while( SQLITE_ROW==sqlite3_step(pStmt) ){ printf("%s;\n", sqlite3_column_text(pStmt,0)); } sqlite3_finalize(pStmt); end_diff_one_table: strFree(&sql); sqlite3_free(zId); namelistFree(az); namelistFree(az2); return; } /* ** Print sketchy documentation for this utility program */ static void showHelp(void){ printf("Usage: %s [options] DB1 DB2\n", g.zArgv0); printf( "Output SQL text that would transform DB1 into DB2.\n" "Options:\n" " --schema Show only differences in the schema\n" " --table TAB Show only differences in table TAB\n" ); } int main(int argc, char **argv){ const char *zDb1 = 0; const char *zDb2 = 0; int i; int rc; char *zErrMsg = 0; char *zSql; sqlite3_stmt *pStmt; char *zTab = 0; g.zArgv0 = argv[0]; for(i=1; i<argc; i++){ const char *z = argv[i]; if( z[0]=='-' ){ z++; if( z[0]=='-' ) z++; if( strcmp(z,"debug")==0 ){ g.fDebug = strtol(argv[++i], 0, 0); }else if( strcmp(z,"help")==0 ){ showHelp(); return 0; }else if( strcmp(z,"schema")==0 ){ g.bSchemaOnly = 1; }else if( strcmp(z,"table")==0 ){ zTab = argv[++i]; }else { cmdlineError("unknown option: %s", argv[i]); } }else if( zDb1==0 ){ zDb1 = argv[i]; }else if( zDb2==0 ){ zDb2 = argv[i]; }else{ cmdlineError("unknown argument: %s", argv[i]); } } if( zDb2==0 ){ cmdlineError("two database arguments required"); } rc = sqlite3_open(zDb1, &g.db); if( rc ){ cmdlineError("cannot open database file \"%s\"", zDb1); } rc = sqlite3_exec(g.db, "SELECT * FROM sqlite_master", 0, 0, &zErrMsg); if( rc || zErrMsg ){ cmdlineError("\"%s\" does not appear to be a valid SQLite database", zDb1); } zSql = sqlite3_mprintf("ATTACH %Q as aux;", zDb2); rc = sqlite3_exec(g.db, zSql, 0, 0, &zErrMsg); if( rc || zErrMsg ){ cmdlineError("cannot attach database \"%s\"", zDb2); } rc = sqlite3_exec(g.db, "SELECT * FROM aux.sqlite_master", 0, 0, &zErrMsg); if( rc || zErrMsg ){ cmdlineError("\"%s\" does not appear to be a valid SQLite database", zDb2); } if( zTab ){ diff_one_table(zTab); }else{ /* Handle tables one by one */ pStmt = db_prepare( "SELECT name FROM main.sqlite_master\n" " WHERE type='table' AND sql NOT LIKE 'CREATE VIRTUAL%%'\n" " UNION\n" "SELECT name FROM aux.sqlite_master\n" " WHERE type='table' AND sql NOT LIKE 'CREATE VIRTUAL%%'\n" " ORDER BY name" ); while( SQLITE_ROW==sqlite3_step(pStmt) ){ diff_one_table((const char*)sqlite3_column_text(pStmt, 0)); } sqlite3_finalize(pStmt); } /* TBD: Handle trigger differences */ /* TBD: Handle view differences */ sqlite3_close(g.db); return 0; } |