COOPY » Guide
version 0.6.5
|
00001 #include <coopy/MergeOutputSqlDiff.h> 00002 00003 #include <stdio.h> 00004 #include <stdlib.h> 00005 00006 #define WANT_MAP2STRING 00007 #define WANT_VECTOR2STRING 00008 #include <coopy/Stringer.h> 00009 00010 using namespace std; 00011 using namespace coopy::cmp; 00012 00013 static void getSqlQuote(const CompareFlags *flags, char *key, char *val) { 00014 char k = '\"'; 00015 char v = '\''; 00016 if (flags!=NULL) { 00017 if (flags->variant=="access") { 00018 k = '['; 00019 v = '\''; 00020 } 00021 } 00022 *key = k; 00023 *val = v; 00024 } 00025 00026 bool MergeOutputSqlDiff::changeColumn(const OrderChange& change) { 00027 char k, v; 00028 getSqlQuote(&getFlags(),&k,&v); 00029 string name = quoteSql(sheet_name,k,false); 00030 switch (change.mode) { 00031 case ORDER_CHANGE_DELETE: 00032 { 00033 int idx = change.identityToIndex(change.subject); 00034 if (change.namesBefore.size()<=idx) { 00035 fprintf(stderr, "Could not find column to remove\n"); 00036 exit(1); 00037 } else { 00038 string c = quoteSql(change.namesBefore[idx],k,false); 00039 fprintf(out,"ALTER TABLE %s DROP COLUMN %s;\n", 00040 name.c_str(), 00041 c.c_str()); 00042 } 00043 } 00044 break; 00045 case ORDER_CHANGE_INSERT: 00046 { 00047 int idx = change.identityToIndexAfter(change.subject); 00048 if (change.namesAfter.size()<=idx) { 00049 fprintf(stderr, "Could not find column to insert\n"); 00050 exit(1); 00051 } else { 00052 string c = quoteSql(change.namesAfter[idx],k,false); 00053 fprintf(out,"ALTER TABLE %s ADD COLUMN %s;\n", 00054 name.c_str(), 00055 c.c_str()); 00056 } 00057 } 00058 break; 00059 case ORDER_CHANGE_RENAME: 00060 { 00061 int idx = change.identityToIndexAfter(change.subject); 00062 if (change.namesAfter.size()<=idx) { 00063 fprintf(stderr, "Could not find column to rename\n"); 00064 exit(1); 00065 } else { 00066 string c = quoteSql(change.namesAfter[idx],k,false); 00067 string c2 = quoteSql(change.namesBefore[idx],k,false); 00068 fprintf(out,"ALTER TABLE %s RENAME COLUMN %s TO %s;\n", 00069 name.c_str(), 00070 c.c_str(), 00071 c2.c_str()); 00072 } 00073 } 00074 break; 00075 } 00076 return true; 00077 } 00078 00079 00080 SqlText MergeOutputSqlDiff::getText(const RowChange& change, const char *sheet_name, const CompareFlags *flags) { 00081 SqlText text; 00082 string& name = text.name; 00083 string& vals = text.vals; 00084 string& conds = text.conds; 00085 string& val_columns = text.val_columns; 00086 string& val_values = text.val_values; 00087 char del1 = '\"'; 00088 char del2 = '\''; 00089 getSqlQuote(flags,&del1,&del2); 00090 00091 name = quoteSql(sheet_name,del1,false); 00092 for (RowChange::txt2cell::const_iterator it=change.val.begin(); 00093 it!=change.val.end(); 00094 it++) { 00095 if (it!=change.val.begin()) { 00096 vals += ", "; 00097 val_columns += ", "; 00098 val_values += ", "; 00099 } 00100 // Should dig up good default quoting rules 00101 string c = quoteSql(it->first,del1,false); 00102 vals += c; 00103 val_columns += c; 00104 vals += "="; 00105 if (it->second.escaped) { 00106 vals += "NULL"; 00107 val_values += "NULL"; 00108 } else { 00109 string q = quoteSql(it->second.text,del2,true); 00110 vals += q; 00111 val_values += q; 00112 } 00113 } 00114 00115 for (RowChange::txt2cell::const_iterator it=change.cond.begin(); 00116 it!=change.cond.end(); 00117 it++) { 00118 if (it!=change.cond.begin()) { 00119 conds += " AND "; 00120 } 00121 string c = quoteSql(it->first,del1,false); 00122 conds += c; 00123 conds += "="; 00124 if (it->second.escaped) { 00125 conds += "NULL"; 00126 } else { 00127 string q = quoteSql(it->second.text,del2,true); 00128 conds += q; 00129 } 00130 } 00131 return text; 00132 } 00133 00134 bool MergeOutputSqlDiff::changeRow(const RowChange& change) { 00135 SqlText text = getText(change,sheet_name.c_str(), 00136 &getFlags()); 00137 string& name = text.name; 00138 string& vals = text.vals; 00139 string& conds = text.conds; 00140 string& val_columns = text.val_columns; 00141 string& val_values = text.val_values; 00142 00143 switch (change.mode) { 00144 case ROW_CHANGE_INSERT: 00145 fprintf(out,"INSERT INTO %s (%s) VALUES (%s);\n", 00146 name.c_str(), 00147 val_columns.c_str(), 00148 val_values.c_str()); 00149 break; 00150 case ROW_CHANGE_DELETE: 00151 fprintf(out,"DELETE FROM %s WHERE %s;\n", 00152 name.c_str(), 00153 conds.c_str()); 00154 break; 00155 case ROW_CHANGE_UPDATE: 00156 fprintf(out,"UPDATE %s SET %s WHERE %s;\n", 00157 name.c_str(), 00158 vals.c_str(), 00159 conds.c_str()); 00160 break; 00161 default: 00162 // skip 00163 break; 00164 } 00165 return true; 00166 } 00167 00168