diff options
-rw-r--r-- | project2/sql/sqlMergeTask.cpp | 29 | ||||
-rw-r--r-- | project2/sql/sqlMergeTask.h | 4 | ||||
-rw-r--r-- | project2/sql/sqlWriter.cpp | 12 | ||||
-rw-r--r-- | project2/sql/sqlWriter.h | 17 | ||||
-rw-r--r-- | project2/sql/tablepatch.cpp | 95 | ||||
-rw-r--r-- | project2/sql/tablepatch.h | 15 |
6 files changed, 93 insertions, 79 deletions
diff --git a/project2/sql/sqlMergeTask.cpp b/project2/sql/sqlMergeTask.cpp index 880a85e..2bd767b 100644 --- a/project2/sql/sqlMergeTask.cpp +++ b/project2/sql/sqlMergeTask.cpp @@ -48,7 +48,7 @@ SqlMergeTask::SqlMergeTask(const std::string & datasource, const std::string & t SourceObject(__PRETTY_FUNCTION__), Task(NULL), updateWhere(NULL), - patchOrder(Null()), + patchOrder(NULL), earlyKeys(VariableType(false)), useView(VariableType(false)), doDelete(true), @@ -69,7 +69,8 @@ SqlMergeTask::SqlMergeTask(ScriptNodePtr p) : Task(p), updateWhere([=](ScriptNodePtr c){ return c ? new DynamicSql::SqlCommand(c) : NULL; }(p->child("updatewhere", false))), - patchOrder(p, "patchorder", Null()), + patchOrder([=](ScriptNodePtr c){ + return c ? new DynamicSql::SqlCommand(c) : NULL; }(p->child("patchorder", false))), earlyKeys(p, "earlykeys", false), useView(p, "useview", false), doDelete(p, "delete", true), @@ -138,27 +139,6 @@ SqlMergeTask::TargetColumn::Sort::operator()(const TargetColumnPtr & a, const Ta return a->column < b->column; } -class MergeWhere : public TablePatch::WhereProvider { - public: - MergeWhere(DynamicSql::SqlCommand * u, ExecContext * e) : - ec(e), - updateWhere(u) - { - } - void appendWhere(Buffer * buf) const - { - buf->appendf(" AND %s ", updateWhere->getSqlFor("").c_str()); - } - void bindWhere(DB::Command * cmd) const - { - unsigned int off = 0; - updateWhere->bindParams(ec, cmd, off); - } - private: - ExecContext * ec; - const DynamicSql::SqlCommand * updateWhere; -}; - void SqlMergeTask::execute(ExecContext * ec) const { @@ -187,8 +167,7 @@ SqlMergeTask::execute(ExecContext * ec) const BOOST_FOREACH(const Keys::value_type & k, keys) { tp.addKey(k); } - MergeWhere mw(updateWhere.get(), ec); - tp.patch(updateWhere ? &mw : NULL, patchOrder(NULL)); + tp.patch(ec, updateWhere, patchOrder); dropTempTable(); } diff --git a/project2/sql/sqlMergeTask.h b/project2/sql/sqlMergeTask.h index bc97b7c..626814c 100644 --- a/project2/sql/sqlMergeTask.h +++ b/project2/sql/sqlMergeTask.h @@ -50,8 +50,8 @@ class SqlMergeTask : public Task { Columns cols; Keys keys; Keys indexes; - boost::intrusive_ptr<DynamicSql::SqlCommand> updateWhere; - Variable patchOrder; + DynamicSql::SqlWriterPtr updateWhere; + DynamicSql::SqlWriterPtr patchOrder; Variable earlyKeys; Variable useView; Variable doDelete; diff --git a/project2/sql/sqlWriter.cpp b/project2/sql/sqlWriter.cpp index 5278759..cd9108d 100644 --- a/project2/sql/sqlWriter.cpp +++ b/project2/sql/sqlWriter.cpp @@ -50,13 +50,13 @@ DynamicSql::SqlCommand::getSqlFor(const Glib::ustring & f) const BOOST_FOREACH(const SqlCommand::Filters::value_type & filter, filters) { filter.second->active = (filter.second->name == f); } - Glib::ustring sql; + Buffer sql; writeSql(sql); - return sql; + return Glib::ustring(sql.str()); } void -DynamicSql::SqlCommand::writeSql(Glib::ustring & sql) const +DynamicSql::SqlCommand::writeSql(Buffer & sql) const { BOOST_FOREACH(const SqlWriterPtr & w, writers) { w->writeSql(sql); @@ -81,7 +81,7 @@ DynamicSql::SqlFilter::SqlFilter(ScriptNodePtr p) : } void -DynamicSql::SqlFilter::writeSql(Glib::ustring & sql) const +DynamicSql::SqlFilter::writeSql(Buffer & sql) const { if (active) { BOOST_FOREACH(const SqlWriterPtr & w, writers) { @@ -106,7 +106,7 @@ DynamicSql::SqlParameter::SqlParameter(ScriptNodePtr n) : } void -DynamicSql::SqlParameter::writeSql(Glib::ustring & sql) const +DynamicSql::SqlParameter::writeSql(Buffer & sql) const { sql.append("?"); } @@ -123,7 +123,7 @@ DynamicSql::SqlText::SqlText(const Glib::ustring & n) : } void -DynamicSql::SqlText::writeSql(Glib::ustring & sql) const +DynamicSql::SqlText::writeSql(Buffer & sql) const { sql.append(text); } diff --git a/project2/sql/sqlWriter.h b/project2/sql/sqlWriter.h index 5eef9ac..33a57fb 100644 --- a/project2/sql/sqlWriter.h +++ b/project2/sql/sqlWriter.h @@ -4,6 +4,7 @@ #include <intrusivePtrBase.h> #include <command.h> #include <glibmm/ustring.h> +#include <buffer.h> #include <list> #include <map> #include "variables.h" @@ -12,31 +13,35 @@ namespace DynamicSql { class SqlWriter; typedef boost::intrusive_ptr<SqlWriter> SqlWriterPtr; typedef std::list<SqlWriterPtr> Writers; + class SqlWriter : public IntrusivePtrBase { public: SqlWriter(); virtual ~SqlWriter(); - virtual void writeSql(Glib::ustring & sql) const = 0; + virtual void writeSql(Buffer & sql) const = 0; virtual void bindParams(ExecContext *, DB::Command *, unsigned int & offset) const = 0; }; + class SqlText : public SqlWriter { public: SqlText(const Glib::ustring &); - virtual void writeSql(Glib::ustring & sql) const; + virtual void writeSql(Buffer & sql) const; virtual void bindParams(ExecContext *, DB::Command *, unsigned int & offset) const; const Glib::ustring text; }; + class SqlParameter : public SqlWriter, Variable { public: SqlParameter(ScriptNodePtr); - virtual void writeSql(Glib::ustring & sql) const; + virtual void writeSql(Buffer & sql) const; virtual void bindParams(ExecContext *, DB::Command *, unsigned int & offset) const; }; + class SqlFilter : public SqlWriter { public: SqlFilter(ScriptNodePtr); - virtual void writeSql(Glib::ustring & sql) const; + virtual void writeSql(Buffer & sql) const; virtual void bindParams(ExecContext *, DB::Command *, unsigned int & offset) const; const Glib::ustring name; @@ -44,11 +49,13 @@ namespace DynamicSql { private: Writers writers; }; + typedef boost::intrusive_ptr<SqlFilter> SqlFilterPtr; + class SqlCommand : public SqlWriter { public: SqlCommand(ScriptNodePtr); - virtual void writeSql(Glib::ustring & sql) const; + virtual void writeSql(Buffer & sql) const; virtual void bindParams(ExecContext *, DB::Command *, unsigned int & offset) const; typedef std::multimap<Glib::ustring, SqlFilterPtr> Filters; Glib::ustring getSqlFor(const Glib::ustring & f) const; diff --git a/project2/sql/tablepatch.cpp b/project2/sql/tablepatch.cpp index 8d61b66..6836829 100644 --- a/project2/sql/tablepatch.cpp +++ b/project2/sql/tablepatch.cpp @@ -37,24 +37,24 @@ TablePatch::addKey(const TablePatch::Column & c) } void -TablePatch::patch(const WhereProvider * where, const char * order) +TablePatch::patch(ExecContext * ec, DynamicSql::SqlWriterPtr where, DynamicSql::SqlWriterPtr order) { if (pk.empty()) { throw PatchCheckFailure(); } if (doDelete) { - doDeletes(where, order); + doDeletes(ec, where, order); } if (doUpdate) { - doUpdates(where, order); + doUpdates(ec, where, order); } if (doInsert) { - doInserts(order); + doInserts(ec, order); } } void -TablePatch::doDeletes(const WhereProvider * where, const char * order) +TablePatch::doDeletes(ExecContext * ec, DynamicSql::SqlWriterPtr where, DynamicSql::SqlWriterPtr order) { switch (db.bulkDeleteStyle()) { case BulkDeleteUsingSubSelect: @@ -104,16 +104,21 @@ TablePatch::doDeletes(const WhereProvider * where, const char * order) pki->c_str()); } if (where) { - //toDelSql.appendf(" AND %s", where); - where->appendWhere(&toDelSql); + toDelSql.append(" AND "); + where->writeSql(toDelSql); } - if (order && *order) { - toDelSql.appendf(" ORDER BY %s", order); + if (order) { + toDelSql.append(" ORDER BY "); + order->writeSql(toDelSql); } toDelSql.append(")"); ModifyCommand * del = db.newModifyCommand(toDelSql); + unsigned int offset = 0; if (where) { - where->bindWhere(del); + where->bindParams(ec, del, offset); + } + if (order) { + order->bindParams(ec, del, offset); } del->execute(); delete del; @@ -147,15 +152,20 @@ TablePatch::doDeletes(const WhereProvider * where, const char * order) pki->c_str()); } if (where) { - //toDelSql.appendf(" AND %s", where); - where->appendWhere(&toDelSql); + toDelSql.append(" AND "); + where->writeSql(toDelSql); } - if (order && *order) { - toDelSql.appendf(" ORDER BY %s", order); + if (order) { + toDelSql.append(" ORDER BY "); + order->writeSql(toDelSql); } ModifyCommand * del = db.newModifyCommand(toDelSql); + unsigned int offset = 0; if (where) { - where->bindWhere(del); + where->bindParams(ec, del, offset); + } + if (order) { + order->bindParams(ec, del, offset); } del->execute(); delete del; @@ -165,7 +175,7 @@ TablePatch::doDeletes(const WhereProvider * where, const char * order) } void -TablePatch::doUpdates(const WhereProvider * where, const char * order) +TablePatch::doUpdates(ExecContext * ec, DynamicSql::SqlWriterPtr where, DynamicSql::SqlWriterPtr order) { if (cols.size() == pk.size()) { // Can't "change" anything... it's all part of the key @@ -204,7 +214,8 @@ TablePatch::doUpdates(const WhereProvider * where, const char * order) pki->c_str(), pki->c_str()); } if (where) { - where->appendWhere(&toUpdSel); + toUpdSel.append(" AND "); + where->writeSql(toUpdSel); } toUpdSel.append(" AND ("); bool first = true; @@ -221,8 +232,9 @@ TablePatch::doUpdates(const WhereProvider * where, const char * order) } } toUpdSel.append(")"); - if (order && *order) { - toUpdSel.appendf(" ORDER BY %s", order); + if (order) { + toUpdSel.append(" ORDER BY "); + order->writeSql(toUpdSel); } // ----------------------------------------------------------------- // Build SQL to perform updates ------------------------------------ @@ -255,8 +267,12 @@ TablePatch::doUpdates(const WhereProvider * where, const char * order) // Iterator over update list make changes -------------------------- // ----------------------------------------------------------------- SelectCommand * toUpd = db.newSelectCommand(toUpdSel); + unsigned int offset = 0; if (where) { - where->bindWhere(toUpd); + where->bindParams(ec, toUpd, offset); + } + if (order) { + order->bindParams(ec, toUpd, offset); } ModifyCommand * upd = db.newModifyCommand(updSql); int cs = cols.size(); @@ -319,17 +335,23 @@ TablePatch::doUpdates(const WhereProvider * where, const char * order) } updSql.append(")"); if (where) { - where->appendWhere(&updSql); + updSql.append(" AND "); + where->writeSql(updSql); } - if (order && *order) { - updSql.appendf(" ORDER BY %s", order); + if (order) { + updSql.append(" ORDER BY "); + order->writeSql(updSql); } // ----------------------------------------------------------------- // Execute the bulk update command --------------------------------- // ----------------------------------------------------------------- ModifyCommand * upd = db.newModifyCommand(updSql); + unsigned int offset = 0; if (where) { - where->bindWhere(upd); + where->bindParams(ec, upd, offset); + } + if (order) { + order->bindParams(ec, upd, offset); } upd->execute(true); delete upd; @@ -381,17 +403,23 @@ TablePatch::doUpdates(const WhereProvider * where, const char * order) } updSql.append(")"); if (where) { - where->appendWhere(&updSql); + updSql.append(" AND "); + where->writeSql(updSql); } - if (order && *order) { - updSql.appendf(" ORDER BY %s", order); + if (order) { + updSql.append(" ORDER BY "); + order->writeSql(updSql); } // ----------------------------------------------------------------- // Execute the bulk update command --------------------------------- // ----------------------------------------------------------------- ModifyCommand * upd = db.newModifyCommand(updSql); + unsigned int offset = 0; if (where) { - where->bindWhere(upd); + where->bindParams(ec, upd, offset); + } + if (order) { + order->bindParams(ec, upd, offset); } upd->execute(true); delete upd; @@ -401,7 +429,7 @@ TablePatch::doUpdates(const WhereProvider * where, const char * order) } void -TablePatch::doInserts(const char * order) +TablePatch::doInserts(ExecContext * ec, DynamicSql::SqlWriterPtr order) { // ----------------------------------------------------------------- // Build SQL for copying new records ------------------------------- @@ -449,10 +477,15 @@ TablePatch::doInserts(const char * order) toInsSql.appendf(" a.%s IS NULL", pki->c_str()); } - if (order && *order) { - toInsSql.appendf(" ORDER BY %s", order); + if (order) { + toInsSql.appendf(" ORDER BY "); + order->writeSql(toInsSql); } ModifyCommand * ins = db.newModifyCommand(toInsSql); + unsigned int offset = 0; + if (order) { + order->bindParams(ec, ins, offset); + } ins->execute(); delete ins; } diff --git a/project2/sql/tablepatch.h b/project2/sql/tablepatch.h index 6903dc1..490450b 100644 --- a/project2/sql/tablepatch.h +++ b/project2/sql/tablepatch.h @@ -8,6 +8,7 @@ #include <modifycommand.h> #include <selectcommand.h> #include <buffer.h> +#include "sqlWriter.h" class TablePatch { public: @@ -22,25 +23,19 @@ class TablePatch { const char * what() const throw(); }; - class WhereProvider { - public: - virtual void appendWhere(Buffer *) const = 0; - virtual void bindWhere(DB::Command *) const = 0; - }; - TablePatch(const DB::Connection & db, const Table & src, const Table & dest, const Columns & cols); void addKey(const Column & col); - void patch(const WhereProvider * where, const char * order); + void patch(ExecContext *, DynamicSql::SqlWriterPtr where, DynamicSql::SqlWriterPtr order); bool doDelete; bool doUpdate; bool doInsert; private: - void doDeletes(const WhereProvider * where, const char * order); - void doUpdates(const WhereProvider * where, const char * order); - void doInserts(const char * order); + void doDeletes(ExecContext *, DynamicSql::SqlWriterPtr where, DynamicSql::SqlWriterPtr order); + void doUpdates(ExecContext *, DynamicSql::SqlWriterPtr where, DynamicSql::SqlWriterPtr order); + void doInserts(ExecContext *, DynamicSql::SqlWriterPtr order); Table src; Table dest; |