/ Check-in [5611fa9b]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:First complete attempt to generate a working changeset. Still contains bugs.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | sqldiff-changeset
Files: files | file ages | folders
SHA1: 5611fa9bd5b8fd762d16ce9b0853c2e779a1a1b7
User & Date: drh 2015-04-11 12:07:40
Context
2015-04-11
13:48
The --changeset option now appears to be working. Closed-Leaf check-in: 1a2e2803 user: drh tags: sqldiff-changeset
12:07
First complete attempt to generate a working changeset. Still contains bugs. check-in: 5611fa9b user: drh tags: sqldiff-changeset
2015-04-10
19:41
Work toward adding the --changeset option to the sqldiff utility program. Changes are incomplete. This is an incremental check-in. check-in: 463e38d7 user: drh tags: sqldiff-changeset
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

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
...
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
...
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
...
806
807
808
809
810
811
812

813
814

815















































816
817
818
819
820
821
822
end_diff_one_table:
  strFree(&sql);
  sqlite3_free(zId);
  namelistFree(az);
  namelistFree(az2);
  return;
}


























/*
** Generate a CHANGESET for all differences from main.zTab to aux.zTab.
*/
static void changeset_one_table(const char *zTab, FILE *out){
  sqlite3_stmt *pStmt;          /* SQL statment */
  char *zId = safeId(zTab);     /* Escaped name of the table */
  char **azCol = 0;             /* List of escaped column names */
  int nCol = 0;                 /* Number of columns */
  int *aiFlg = 0;               /* 0 if column is not part of PK */
  int *aiPk = 0;                /* Column numbers for each PK column */
  int nPk = 0;                  /* Number of PRIMARY KEY columns */
  Str sql;                      /* SQL for the diff query */
  int i;                        /* Loop counter */
  const char *zSep;             /* List separator */

  pStmt = db_prepare(
      "SELECT A.sql=B.sql FROM main.sqlite_master A, aux.sqlite_master B"
      " WHERE A.name=%Q AND B.name=%Q", zTab, zTab
  );
  if( SQLITE_ROW==sqlite3_step(pStmt) ){
................................................................................
      aiPk[i-1] = nCol-1;
    }
  }
  sqlite3_finalize(pStmt);
  if( nPk==0 ) goto end_changeset_one_table; 
  strInit(&sql);
  if( nCol>nPk ){
    strPrintf(&sql, "SELECT 1");  /* Changes to non-PK columns */
    for(i=0; i<nCol; i++) strPrintf(&sql, ", A.%s", azCol[i]);
    for(i=0; i<nCol; i++) strPrintf(&sql, ", B.%s", azCol[i]);
    strPrintf(&sql,"\n  FROM main.%s A, aux.%s B\n", zId, zId);
    zSep = " WHERE";
    for(i=0; i<nPk; i++){
      strPrintf(&sql, "%s A.%s=B.%s", zSep, azCol[aiPk[i]], azCol[aiPk[i]]);
      zSep = " AND";
................................................................................
    for(i=0; i<nCol; i++){
      if( aiFlg[i] ) continue;
      strPrintf(&sql, "%sA.%s IS NOT B.%s", zSep, azCol[i], azCol[i]);
      zSep = " OR\n        ";
    }
    strPrintf(&sql,")\n UNION ALL\n");
  }
  strPrintf(&sql, "SELECT 2");   /* Deleted rows */
  for(i=0; i<nCol; i++) strPrintf(&sql, ", A.%s", azCol[i]);
  for(i=0; i<nCol; i++) strPrintf(&sql, ", 0");
  strPrintf(&sql, "  FROM main.%s A\n", zId);
  strPrintf(&sql, " WHERE NOT EXISTS(SELECT 1 FROM aux.%s B\n", zId);
  zSep =          "                   WHERE";
  for(i=0; i<nPk; i++){
    strPrintf(&sql, "%s A.%s=B.%s", zSep, azCol[aiPk[i]], azCol[aiPk[i]]);
    zSep = " AND";
  }
  strPrintf(&sql, ")\n UNION ALL\n");
  strPrintf(&sql, "SELECT 3");   /* Inserted rows */
  for(i=0; i<nCol; i++) strPrintf(&sql, ", 0");
  for(i=0; i<nCol; i++) strPrintf(&sql, ", B.%s", azCol[i]);
  strPrintf(&sql, "  FROM aux.%s B\n", zId);
  strPrintf(&sql, " WHERE NOT EXISTS(SELECT 1 FROM main.%s A\n", zId);
  zSep =          "                   WHERE";
  for(i=0; i<nPk; i++){
    strPrintf(&sql, "%s A.%s=B.%s", zSep, azCol[aiPk[i]], azCol[aiPk[i]]);
................................................................................
  zSep = " ";
  for(i=0; i<nPk; i++){
    strPrintf(&sql, "%s %d", zSep, aiPk[i]+1);
    zSep = ",";
  }
  strPrintf(&sql, ";\n");


printf("for table %s:\n%s\n", zId, sql.z);
strFree(&sql);

  















































  
end_changeset_one_table:
  while( nCol>0 ) sqlite3_free(azCol[--nCol]);
  sqlite3_free(azCol);
  sqlite3_free(aiPk);
  sqlite3_free(zId);
}







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>













|







 







|







 







|










|







 







>
|
<
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







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
...
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
...
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
...
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
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 buf[12], 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 );
    buf[9] &= 0x7f;
    fwrite(buf+n+1, 9-n, 1, out);
  }
}

/*
** 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, j;                     /* 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) ){
................................................................................
      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++) strPrintf(&sql, ", A.%s", azCol[i]);
    for(i=0; i<nCol; i++) strPrintf(&sql, ", B.%s", azCol[i]);
    strPrintf(&sql,"\n  FROM main.%s A, aux.%s B\n", zId, zId);
    zSep = " WHERE";
    for(i=0; i<nPk; i++){
      strPrintf(&sql, "%s A.%s=B.%s", zSep, azCol[aiPk[i]], azCol[aiPk[i]]);
      zSep = " AND";
................................................................................
    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++) strPrintf(&sql, ", A.%s", azCol[i]);
  for(i=0; i<nCol; i++) strPrintf(&sql, ", 0");
  strPrintf(&sql, "  FROM main.%s A\n", zId);
  strPrintf(&sql, " WHERE NOT EXISTS(SELECT 1 FROM aux.%s B\n", zId);
  zSep =          "                   WHERE";
  for(i=0; i<nPk; i++){
    strPrintf(&sql, "%s A.%s=B.%s", zSep, azCol[aiPk[i]], azCol[aiPk[i]]);
    zSep = " AND";
  }
  strPrintf(&sql, ")\n UNION ALL\n");
  strPrintf(&sql, "SELECT %d", SQLITE_INSERT);
  for(i=0; i<nCol; i++) strPrintf(&sql, ", 0");
  for(i=0; i<nCol; i++) strPrintf(&sql, ", B.%s", azCol[i]);
  strPrintf(&sql, "  FROM aux.%s B\n", zId);
  strPrintf(&sql, " WHERE NOT EXISTS(SELECT 1 FROM main.%s A\n", zId);
  zSep =          "                   WHERE";
  for(i=0; i<nPk; i++){
    strPrintf(&sql, "%s A.%s=B.%s", zSep, azCol[aiPk[i]], azCol[aiPk[i]]);
................................................................................
  zSep = " ";
  for(i=0; i<nPk; i++){
    strPrintf(&sql, "%s %d", zSep, aiPk[i]+1);
    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);
    int iFirst = iType==SQLITE_INSERT ? nCol+1 : 1;
    int iLast = iType==SQLITE_DELETE ? nCol+1 : 2*nCol+1;
    putc(iType, out);
    putc(0, out);
    for(i=iFirst; i<=iLast; i++){
      int iDType = sqlite3_column_type(pStmt,i);
      sqlite3_int64 iX;
      double rX;
      sqlite3_uint64 uX;
      putc(iDType, out);
      switch( iDType ){
        case SQLITE_INTEGER:
          iX = sqlite3_column_int64(pStmt,i);
          memcpy(&uX, &iX, 8);
          for(j=56; j>=0; j-=8) putc((uX>>j)&0xff, out);
          break;
        case SQLITE_FLOAT:
          rX = sqlite3_column_int64(pStmt,i);
          memcpy(&uX, &rX, 8);
          for(j=56; j>=0; j-=8) putc((uX>>j)&0xff, out);
          break;
        case SQLITE_TEXT:
          iX = sqlite3_column_bytes(pStmt,i);
          putsVarint(out, (sqlite3_uint64)iX);
          fwrite(sqlite3_column_text(pStmt,i),1,iX,out);
          break;
        case SQLITE_BLOB:
          iX = sqlite3_column_bytes(pStmt,i);
          putsVarint(out, (sqlite3_uint64)iX);
          fwrite(sqlite3_column_blob(pStmt,i),1,iX,out);
          break;
        case SQLITE_NULL:
          break;
      }
    }
  }
  sqlite3_finalize(pStmt);
  
end_changeset_one_table:
  while( nCol>0 ) sqlite3_free(azCol[--nCol]);
  sqlite3_free(azCol);
  sqlite3_free(aiPk);
  sqlite3_free(zId);
}