Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Changes In Branch sqldiff-changeset Excluding Merge-Ins
This is equivalent to a diff from 860e4f8a94 to 1a2e280392
2015-04-11
| ||
13:49 | Add the --changeset option to the sqldiff utility program, for generating a sessions changeset file instead of an SQL diff. (check-in: f9a3a8391c user: drh tags: trunk) | |
13:48 | The --changeset option now appears to be working. (Closed-Leaf check-in: 1a2e280392 user: drh tags: sqldiff-changeset) | |
12:07 | First complete attempt to generate a working changeset. Still contains bugs. (check-in: 5611fa9bd5 user: drh tags: sqldiff-changeset) | |
2015-04-10
| ||
21:16 | Minor build enhancements for MSVC. (check-in: 40c417a7ef user: mistachkin tags: trunk) | |
19:41 | Work toward adding the --changeset option to the sqldiff utility program. Changes are incomplete. This is an incremental check-in. (check-in: 463e38d765 user: drh tags: sqldiff-changeset) | |
16:05 | In sqlite3_declare_vtab(), avoid accessing the database structure until after the "api-armour" safety-check has completed and the db mutex has been obtained. (check-in: 860e4f8a94 user: dan tags: trunk) | |
12:04 | Fix foreign key CASCADE for cases where the parent key is an INTEGER PRIMARY KEY and the parent table contains other columns named "rowid", "_rowid_", and "oid". (check-in: ed3cbaab6a user: drh tags: trunk) | |
Changes to tool/sqldiff.c.
︙ | ︙ | |||
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 | 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" " --primarykey Use schema-defined PRIMARY KEYs\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 | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 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 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 | end_diff_one_table: strFree(&sql); sqlite3_free(zId); namelistFree(az); namelistFree(az2); return; } /* ** Write a 64-bit signed integer as a varint onto out */ static void putsVarint(FILE *out, sqlite3_uint64 v){ int i, n; unsigned char p[12]; if( v & (((sqlite3_uint64)0xff000000)<<32) ){ p[8] = (unsigned char)v; v >>= 8; for(i=7; i>=0; i--){ p[i] = (unsigned char)((v & 0x7f) | 0x80); v >>= 7; } fwrite(p, 8, 1, out); }else{ n = 9; do{ p[n--] = (unsigned char)((v & 0x7f) | 0x80); v >>= 7; }while( v!=0 ); p[9] &= 0x7f; fwrite(p+n+1, 9-n, 1, out); } } /* ** Write an SQLite value onto out. */ static void putValue(FILE *out, sqlite3_value *pVal){ int iDType = sqlite3_value_type(pVal); sqlite3_int64 iX; double rX; sqlite3_uint64 uX; int j; putc(iDType, out); switch( iDType ){ case SQLITE_INTEGER: iX = sqlite3_value_int64(pVal); memcpy(&uX, &iX, 8); for(j=56; j>=0; j-=8) putc((uX>>j)&0xff, out); break; case SQLITE_FLOAT: rX = sqlite3_value_int64(pVal); memcpy(&uX, &rX, 8); for(j=56; j>=0; j-=8) putc((uX>>j)&0xff, out); break; case SQLITE_TEXT: iX = sqlite3_value_bytes(pVal); putsVarint(out, (sqlite3_uint64)iX); fwrite(sqlite3_value_text(pVal),1,iX,out); break; case SQLITE_BLOB: iX = sqlite3_value_bytes(pVal); putsVarint(out, (sqlite3_uint64)iX); fwrite(sqlite3_value_blob(pVal),1,iX,out); break; case SQLITE_NULL: break; } } /* ** Generate a CHANGESET for all differences from main.zTab to aux.zTab. */ static void changeset_one_table(const char *zTab, FILE *out){ sqlite3_stmt *pStmt; /* SQL statment */ char *zId = safeId(zTab); /* Escaped name of the table */ char **azCol = 0; /* List of escaped column names */ int nCol = 0; /* Number of columns */ int *aiFlg = 0; /* 0 if column is not part of PK */ int *aiPk = 0; /* Column numbers for each PK column */ int nPk = 0; /* Number of PRIMARY KEY columns */ Str sql; /* SQL for the diff query */ int i, k; /* Loop counters */ const char *zSep; /* List separator */ pStmt = db_prepare( "SELECT A.sql=B.sql FROM main.sqlite_master A, aux.sqlite_master B" " WHERE A.name=%Q AND B.name=%Q", zTab, zTab ); if( SQLITE_ROW==sqlite3_step(pStmt) ){ if( sqlite3_column_int(pStmt,0)==0 ){ runtimeError("schema changes for table %s", safeId(zTab)); } }else{ runtimeError("table %s missing from one or both databases", safeId(zTab)); } sqlite3_finalize(pStmt); pStmt = db_prepare("PRAGMA main.table_info=%Q", zTab); while( SQLITE_ROW==sqlite3_step(pStmt) ){ nCol++; azCol = sqlite3_realloc(azCol, sizeof(char*)*nCol); if( azCol==0 ) runtimeError("out of memory"); aiFlg = sqlite3_realloc(aiFlg, sizeof(int)*nCol); if( aiFlg==0 ) runtimeError("out of memory"); azCol[nCol-1] = safeId((const char*)sqlite3_column_text(pStmt,1)); aiFlg[nCol-1] = i = sqlite3_column_int(pStmt,5); if( i>0 ){ if( i>nPk ){ nPk = i; aiPk = sqlite3_realloc(aiPk, sizeof(int)*nPk); if( aiPk==0 ) runtimeError("out of memory"); } aiPk[i-1] = nCol-1; } } sqlite3_finalize(pStmt); if( nPk==0 ) goto end_changeset_one_table; strInit(&sql); if( nCol>nPk ){ strPrintf(&sql, "SELECT %d", SQLITE_UPDATE); for(i=0; i<nCol; i++){ if( aiFlg[i] ){ strPrintf(&sql, ",\n A.%s", azCol[i]); }else{ strPrintf(&sql, ",\n A.%s IS NOT B.%s, A.%s, B.%s", azCol[i], azCol[i], azCol[i], azCol[i]); } } strPrintf(&sql,"\n FROM main.%s A, aux.%s B\n", zId, zId); zSep = " WHERE"; for(i=0; i<nPk; i++){ strPrintf(&sql, "%s A.%s=B.%s", zSep, azCol[aiPk[i]], azCol[aiPk[i]]); zSep = " AND"; } zSep = "\n AND ("; for(i=0; i<nCol; i++){ if( aiFlg[i] ) continue; strPrintf(&sql, "%sA.%s IS NOT B.%s", zSep, azCol[i], azCol[i]); zSep = " OR\n "; } strPrintf(&sql,")\n UNION ALL\n"); } strPrintf(&sql, "SELECT %d", SQLITE_DELETE); for(i=0; i<nCol; i++){ if( aiFlg[i] ){ strPrintf(&sql, ",\n A.%s", azCol[i]); }else{ strPrintf(&sql, ",\n 1, A.%s, NULL", azCol[i]); } } strPrintf(&sql, "\n FROM main.%s A\n", zId); strPrintf(&sql, " WHERE NOT EXISTS(SELECT 1 FROM aux.%s B\n", zId); zSep = " WHERE"; for(i=0; i<nPk; i++){ strPrintf(&sql, "%s A.%s=B.%s", zSep, azCol[aiPk[i]], azCol[aiPk[i]]); zSep = " AND"; } strPrintf(&sql, ")\n UNION ALL\n"); strPrintf(&sql, "SELECT %d", SQLITE_INSERT); for(i=0; i<nCol; i++){ if( aiFlg[i] ){ strPrintf(&sql, ",\n B.%s", azCol[i]); }else{ strPrintf(&sql, ",\n 1, NULL, B.%s", azCol[i]); } } strPrintf(&sql, "\n FROM aux.%s B\n", zId); strPrintf(&sql, " WHERE NOT EXISTS(SELECT 1 FROM main.%s A\n", zId); zSep = " WHERE"; for(i=0; i<nPk; i++){ strPrintf(&sql, "%s A.%s=B.%s", zSep, azCol[aiPk[i]], azCol[aiPk[i]]); zSep = " AND"; } strPrintf(&sql, ")\n"); strPrintf(&sql, " ORDER BY"); zSep = " "; for(i=0; i<nPk; i++){ strPrintf(&sql, "%s %d", zSep, aiPk[i]+2); zSep = ","; } strPrintf(&sql, ";\n"); if( g.fDebug & DEBUG_DIFF_SQL ){ printf("SQL for %s:\n%s\n", zId, sql.z); goto end_changeset_one_table; } putc('T', out); putsVarint(out, (sqlite3_uint64)nCol); for(i=0; i<nCol; i++) putc(aiFlg[i]!=0, out); fwrite(zTab, 1, strlen(zTab), out); putc(0, out); pStmt = db_prepare("%s", sql.z); while( SQLITE_ROW==sqlite3_step(pStmt) ){ int iType = sqlite3_column_int(pStmt,0); putc(iType, out); putc(0, out); switch( sqlite3_column_int(pStmt,0) ){ case SQLITE_UPDATE: { for(k=1, i=0; i<nCol; i++){ if( aiFlg[i] ){ putValue(out, sqlite3_column_value(pStmt,k)); k++; }else if( sqlite3_column_int(pStmt,k) ){ putValue(out, sqlite3_column_value(pStmt,k+1)); k += 3; }else{ putc(0, out); k += 3; } } for(k=1, i=0; i<nCol; i++){ if( aiFlg[i] ){ putc(0, out); k++; }else if( sqlite3_column_int(pStmt,k) ){ putValue(out, sqlite3_column_value(pStmt,k+2)); k += 3; }else{ putc(0, out); k += 3; } } break; } case SQLITE_INSERT: { for(k=1, i=0; i<nCol; i++){ if( aiFlg[i] ){ putValue(out, sqlite3_column_value(pStmt,k)); k++; }else{ putValue(out, sqlite3_column_value(pStmt,k+2)); k += 3; } } break; } case SQLITE_DELETE: { for(k=1, i=0; i<nCol; i++){ if( aiFlg[i] ){ putValue(out, sqlite3_column_value(pStmt,k)); k++; }else{ putValue(out, sqlite3_column_value(pStmt,k+1)); k += 3; } } break; } } } sqlite3_finalize(pStmt); end_changeset_one_table: while( nCol>0 ) sqlite3_free(azCol[--nCol]); sqlite3_free(azCol); sqlite3_free(aiPk); sqlite3_free(zId); } /* ** Print sketchy documentation for this utility program */ 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" " --changeset FILE Write a CHANGESET into FILE\n" " --primarykey Use schema-defined PRIMARY KEYs\n" " --schema Show only differences in the schema\n" " --table TAB Show only differences in table TAB\n" ); } 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; FILE *out = 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,"changeset")==0 ){ out = fopen(argv[++i], "wb"); if( out==0 ) cmdlineError("cannot open: %s", argv[i]); }else if( strcmp(z,"debug")==0 ){ g.fDebug = strtol(argv[++i], 0, 0); }else if( strcmp(z,"help")==0 ){ showHelp(); return 0; }else |
︙ | ︙ | |||
789 790 791 792 793 794 795 | } 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 ){ | > > > | > | > > > > > | 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 | } 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 ){ if( out ){ changeset_one_table(zTab, out); }else{ 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) ){ const char *zTab = (const char*)sqlite3_column_text(pStmt,0); if( out ){ changeset_one_table(zTab, out); }else{ diff_one_table(zTab); } } sqlite3_finalize(pStmt); } /* TBD: Handle trigger differences */ /* TBD: Handle view differences */ sqlite3_close(g.db); return 0; } |