diff options
Diffstat (limited to 'project2/tablepatch.cpp')
-rw-r--r-- | project2/tablepatch.cpp | 65 |
1 files changed, 30 insertions, 35 deletions
diff --git a/project2/tablepatch.cpp b/project2/tablepatch.cpp index 0decc1e..9ee5165 100644 --- a/project2/tablepatch.cpp +++ b/project2/tablepatch.cpp @@ -5,6 +5,7 @@ #include <selectcommand.h> #include <column.h> #include <buffer.h> +#include <boost/algorithm/string/join.hpp> TablePatch::TablePatch(const Connection & wdb, const TablePatch::Table & s, const TablePatch::Table & d, const TablePatch::Columns & c) : @@ -45,10 +46,22 @@ void TablePatch::doDeletes(const char * where, const char * order) { // ----------------------------------------------------------------- - // Build SQL to select keys to delete ------------------------------ + // Build SQL to delete keys ---------------------------------------- // ----------------------------------------------------------------- Buffer toDelSql; - toDelSql.append("SELECT "); + toDelSql.appendf("DELETE FROM %s d WHERE (", + dest.c_str()); + foreach (PKI, pk, pki) { + if (pki != pk.begin()) { + toDelSql.append(", "); + } + toDelSql.appendf("d.%s", + pki->c_str()); + } + // ----------------------------------------------------------------- + // Build SQL to select keys to delete ------------------------------ + // ----------------------------------------------------------------- + toDelSql.append(") IN (SELECT "); foreach (PKI, pk, pki) { if (pki != pk.begin()) { toDelSql.append(", "); @@ -56,13 +69,10 @@ TablePatch::doDeletes(const char * where, const char * order) toDelSql.appendf("a.%s", pki->c_str()); } - toDelSql.appendf(" FROM %s a LEFT OUTER JOIN %s b", + toDelSql.appendf(" FROM %s a LEFT OUTER JOIN %s b ON ", dest.c_str(), src.c_str()); foreach (PKI, pk, pki) { - if (pki == pk.begin()) { - toDelSql.append(" ON "); - } - else { + if (pki != pk.begin()) { toDelSql.append(" AND "); } toDelSql.appendf(" a.%s = b.%s", @@ -84,21 +94,8 @@ TablePatch::doDeletes(const char * where, const char * order) if (order) { toDelSql.appendf(" ORDER BY %s", order); } - // ----------------------------------------------------------------- - // Build SQL to delete keys ---------------------------------------- - // ----------------------------------------------------------------- - Buffer delSql; - delSql.appendf("DELETE FROM %s d WHERE (", - dest.c_str()); - foreach (PKI, pk, pki) { - if (pki != pk.begin()) { - delSql.append(", "); - } - delSql.appendf("d.%s", - pki->c_str()); - } - delSql.appendf(") IN (%s)", toDelSql.c_str()); - ModifyCommand del(db, delSql.c_str()); + toDelSql.append(")"); + ModifyCommand del(db, toDelSql); del.execute(); } @@ -113,23 +110,21 @@ TablePatch::doUpdates(const char * where, const char * order) // Build SQL for list of updates to perform ------------------------ // ----------------------------------------------------------------- Buffer toUpdSel; - Buffer ch; - Buffer k; + toUpdSel.append("SELECT "); foreach (Columns::const_iterator, cols, col) { if (pk.find(*col) == pk.end()) { - ch.appendf("b.%s, ", + toUpdSel.appendf("b.%s, ", col->c_str()); } - else { - if (k.length()) { - k.append(", "); - } - k.appendf("b.%s ", + } + foreach (Columns::const_iterator, cols, col) { + if (pk.find(*col) != pk.end()) { + toUpdSel.appendf("b.%s, ", col->c_str()); } } - toUpdSel.appendf("SELECT %s %s FROM %s a, %s b", - ch.c_str(), k.c_str(), dest.c_str(), src.c_str()); + toUpdSel.appendf("0 FROM %s a, %s b", + dest.c_str(), src.c_str()); foreach (PKI, pk, pki) { if (pki == pk.begin()) { toUpdSel.append(" WHERE "); @@ -191,8 +186,8 @@ TablePatch::doUpdates(const char * where, const char * order) // ----------------------------------------------------------------- // Iterator over update list make changes -------------------------- // ----------------------------------------------------------------- - SelectCommand toUpd(db, toUpdSel.c_str()); - ModifyCommand upd(db, updSql.c_str()); + SelectCommand toUpd(db, toUpdSel); + ModifyCommand upd(db, updSql); int cs = cols.size(); toUpd.execute(); for (int c = 0; c < cs; c += 1) { @@ -255,7 +250,7 @@ TablePatch::doInserts(const char * order) if (order) { toInsSql.appendf(" ORDER BY %s", order); } - ModifyCommand(db, toInsSql.c_str()).execute(); + ModifyCommand(db, toInsSql).execute(); } const char * |