From 6cba12b3ea6f28acfff53a94608a2510fa166921 Mon Sep 17 00:00:00 2001 From: randomdan Date: Thu, 11 Aug 2011 00:05:50 +0000 Subject: Move the SqlWriter outside of SqlRows and make it work on generic commands Also fix bug that could occur with multiple uses with different filters --- project2/Jamfile.jam | 2 +- project2/sqlRows.cpp | 126 +---------------------------------------------- project2/sqlRows.h | 50 +------------------ project2/sqlWriter.cpp | 131 +++++++++++++++++++++++++++++++++++++++++++++++++ project2/sqlWriter.h | 63 ++++++++++++++++++++++++ 5 files changed, 198 insertions(+), 174 deletions(-) create mode 100644 project2/sqlWriter.cpp create mode 100644 project2/sqlWriter.h diff --git a/project2/Jamfile.jam b/project2/Jamfile.jam index f7dd242..aed9e3d 100644 --- a/project2/Jamfile.jam +++ b/project2/Jamfile.jam @@ -153,7 +153,7 @@ obj sql-modPQ : ; lib p2sql : - sqlCheck.cpp sqlTask.cpp sqlMergeTask.cpp sqlRows.cpp sqlVariableBinder.cpp tablepatch.cpp rdbmsDataSource.cpp + sqlCheck.cpp sqlWriter.cpp sqlTask.cpp sqlMergeTask.cpp sqlRows.cpp sqlVariableBinder.cpp tablepatch.cpp rdbmsDataSource.cpp ../libdbpp//dbpp : yes:sql-modODBC diff --git a/project2/sqlRows.cpp b/project2/sqlRows.cpp index 48774ae..31d86dd 100644 --- a/project2/sqlRows.cpp +++ b/project2/sqlRows.cpp @@ -5,10 +5,8 @@ #include "rdbmsDataSource.h" #include "column.h" #include -#include #include "xmlObjectLoader.h" #include "commonObjects.h" -#include "sqlVariableBinder.h" #include #include #include @@ -72,14 +70,8 @@ SqlRows::SqlState::getColumns() const void SqlRows::execute(const Glib::ustring & filter, const RowProcessor * rp) const { - SqlCommand::FiltersRangeType r = sqlCommand.filters.equal_range(filter); - for (SqlCommand::Filters::const_iterator i = r.first; i != r.second; i++) { - i->second->active = true; - } unsigned int offset = 0; - Glib::ustring sql; - sqlCommand.writeSql(sql); - SqlState ss(SelectPtr(db->getReadonly().newSelectCommand(sql))); + SqlState ss(SelectPtr(db->getReadonly().newSelectCommand(sqlCommand.getSqlFor(filter)))); sqlCommand.bindParams(ss.query.get(), offset); while (ss.query->fetch()) { HandleAsVariableType h; @@ -95,119 +87,3 @@ SqlRows::execute(const Glib::ustring & filter, const RowProcessor * rp) const } } -SqlRows::SqlWriter::SqlWriter() -{ -} - -SqlRows::SqlWriter::~SqlWriter() -{ -} - -SqlRows::SqlCommand::SqlCommand(const xmlpp::Element * N) -{ - BOOST_FOREACH(xmlpp::Node * n, N->get_children()) { - const xmlpp::TextNode * t = dynamic_cast(n); - if (t) { - writers.push_back(new SqlText(t)); - } - const xmlpp::Element * e = dynamic_cast(n); - if (e) { - if (e->get_name() == "filter") { - SqlFilterPtr f = new SqlFilter(e); - writers.push_back(f); - filters.insert(Filters::value_type(f->name, f)); - } - else if (e->get_name() == "param") { - writers.push_back(new SqlParameter(e)); - } - } - } -} - -void -SqlRows::SqlCommand::writeSql(Glib::ustring & sql) const -{ - BOOST_FOREACH(const SqlWriterPtr & w, writers) { - w->writeSql(sql); - } -} - -void -SqlRows::SqlCommand::bindParams(DB::SelectCommand * cmd, unsigned int & offset) const -{ - BOOST_FOREACH(const SqlWriterPtr & w, writers) { - w->bindParams(cmd, offset); - } -} - -SqlRows::SqlFilter::SqlFilter(const xmlpp::Element * N) : - name(N->get_attribute_value("name")), - active(false) -{ - BOOST_FOREACH(xmlpp::Node * n, N->get_children()) { - const xmlpp::TextNode * t = dynamic_cast(n); - if (t) { - writers.push_back(new SqlText(t)); - } - const xmlpp::Element * e = dynamic_cast(n); - if (e) { - if (e->get_name() == "param") { - writers.push_back(new SqlParameter(e)); - } - } - } -} - -void -SqlRows::SqlFilter::writeSql(Glib::ustring & sql) const -{ - if (active) { - BOOST_FOREACH(const SqlWriterPtr & w, writers) { - w->writeSql(sql); - } - } -} - -void -SqlRows::SqlFilter::bindParams(DB::SelectCommand * cmd, unsigned int & offset) const -{ - if (active) { - BOOST_FOREACH(const SqlWriterPtr & w, writers) { - w->bindParams(cmd, offset); - } - } -} - -SqlRows::SqlParameter::SqlParameter(const xmlpp::Element * n) : - Variable(n, boost::optional("local")) -{ -} - -void -SqlRows::SqlParameter::writeSql(Glib::ustring & sql) const -{ - sql.append("?"); -} - -void -SqlRows::SqlParameter::bindParams(DB::SelectCommand * cmd, unsigned int & offset) const -{ - boost::apply_visitor(SqlVariableBinder(cmd, offset++), (*this)); -} - -SqlRows::SqlText::SqlText(const xmlpp::TextNode * n) : - text(n->get_content()) -{ -} - -void -SqlRows::SqlText::writeSql(Glib::ustring & sql) const -{ - sql.append(text); -} - -void -SqlRows::SqlText::bindParams(DB::SelectCommand *, unsigned int &) const -{ -} - diff --git a/project2/sqlRows.h b/project2/sqlRows.h index 494f188..6cdd716 100644 --- a/project2/sqlRows.h +++ b/project2/sqlRows.h @@ -7,6 +7,7 @@ #include "selectcommand.h" #include "iHaveParameters.h" #include "rowSet.h" +#include "sqlWriter.h" class RdbmsDataSource; @@ -22,54 +23,7 @@ class SqlRows : public RowSet { const Variable dataSource; private: - class SqlWriter; - typedef boost::intrusive_ptr SqlWriterPtr; - typedef std::list Writers; - class SqlWriter : public IntrusivePtrBase { - public: - SqlWriter(); - virtual ~SqlWriter(); - virtual void writeSql(Glib::ustring & sql) const = 0; - virtual void bindParams(DB::SelectCommand *, unsigned int & offset) const = 0; - }; - class SqlText : public SqlWriter { - public: - SqlText(const xmlpp::TextNode *); - virtual void writeSql(Glib::ustring & sql) const; - virtual void bindParams(DB::SelectCommand *, unsigned int & offset) const; - - const Glib::ustring text; - }; - class SqlParameter : public SqlWriter, Variable { - public: - SqlParameter(const xmlpp::Element *); - virtual void writeSql(Glib::ustring & sql) const; - virtual void bindParams(DB::SelectCommand *, unsigned int & offset) const; - }; - class SqlFilter : public SqlWriter { - public: - SqlFilter(const xmlpp::Element *); - virtual void writeSql(Glib::ustring & sql) const; - virtual void bindParams(DB::SelectCommand *, unsigned int & offset) const; - - const Glib::ustring name; - bool active; - private: - Writers writers; - }; - typedef boost::intrusive_ptr SqlFilterPtr; - class SqlCommand : public SqlWriter { - public: - SqlCommand(const xmlpp::Element *); - virtual void writeSql(Glib::ustring & sql) const; - virtual void bindParams(DB::SelectCommand *, unsigned int & offset) const; - typedef std::multimap Filters; - typedef std::pair FiltersRangeType; - Filters filters; - private: - Writers writers; - }; - SqlCommand sqlCommand; + DynamicSql::SqlCommand sqlCommand; typedef boost::shared_ptr SelectPtr; class SqlState : public RowState { public: diff --git a/project2/sqlWriter.cpp b/project2/sqlWriter.cpp new file mode 100644 index 0000000..88d9801 --- /dev/null +++ b/project2/sqlWriter.cpp @@ -0,0 +1,131 @@ +#include "sqlWriter.h" +#include +#include "sqlVariableBinder.h" + +DynamicSql::SqlWriter::SqlWriter() +{ +} + +DynamicSql::SqlWriter::~SqlWriter() +{ +} + +DynamicSql::SqlCommand::SqlCommand(const xmlpp::Element * N) +{ + BOOST_FOREACH(xmlpp::Node * n, N->get_children()) { + const xmlpp::TextNode * t = dynamic_cast(n); + if (t) { + writers.push_back(new SqlText(t)); + } + const xmlpp::Element * e = dynamic_cast(n); + if (e) { + if (e->get_name() == "filter") { + SqlFilterPtr f = new SqlFilter(e); + writers.push_back(f); + filters.insert(Filters::value_type(f->name, f)); + } + else if (e->get_name() == "param") { + writers.push_back(new SqlParameter(e)); + } + } + } +} + +Glib::ustring +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; + writeSql(sql); + return sql; +} + +void +DynamicSql::SqlCommand::writeSql(Glib::ustring & sql) const +{ + BOOST_FOREACH(const SqlWriterPtr & w, writers) { + w->writeSql(sql); + } +} + +void +DynamicSql::SqlCommand::bindParams(DB::Command * cmd, unsigned int & offset) const +{ + BOOST_FOREACH(const SqlWriterPtr & w, writers) { + w->bindParams(cmd, offset); + } +} + +DynamicSql::SqlFilter::SqlFilter(const xmlpp::Element * N) : + name(N->get_attribute_value("name")), + active(false) +{ + BOOST_FOREACH(xmlpp::Node * n, N->get_children()) { + const xmlpp::TextNode * t = dynamic_cast(n); + if (t) { + writers.push_back(new SqlText(t)); + } + const xmlpp::Element * e = dynamic_cast(n); + if (e) { + if (e->get_name() == "param") { + writers.push_back(new SqlParameter(e)); + } + } + } +} + +void +DynamicSql::SqlFilter::writeSql(Glib::ustring & sql) const +{ + if (active) { + BOOST_FOREACH(const SqlWriterPtr & w, writers) { + w->writeSql(sql); + } + } +} + +void +DynamicSql::SqlFilter::bindParams(DB::Command * cmd, unsigned int & offset) const +{ + if (active) { + BOOST_FOREACH(const SqlWriterPtr & w, writers) { + w->bindParams(cmd, offset); + } + } +} + +DynamicSql::SqlParameter::SqlParameter(const xmlpp::Element * n) : + Variable(n, boost::optional("local")) +{ +} + +void +DynamicSql::SqlParameter::writeSql(Glib::ustring & sql) const +{ + sql.append("?"); +} + +void +DynamicSql::SqlParameter::bindParams(DB::Command * cmd, unsigned int & offset) const +{ + boost::apply_visitor(SqlVariableBinder(cmd, offset++), (*this)); +} + +DynamicSql::SqlText::SqlText(const xmlpp::TextNode * n) : + text(n->get_content()) +{ +} + +void +DynamicSql::SqlText::writeSql(Glib::ustring & sql) const +{ + sql.append(text); +} + +void +DynamicSql::SqlText::bindParams(DB::Command *, unsigned int &) const +{ +} + diff --git a/project2/sqlWriter.h b/project2/sqlWriter.h new file mode 100644 index 0000000..7693e19 --- /dev/null +++ b/project2/sqlWriter.h @@ -0,0 +1,63 @@ +#ifndef SQLWRITER_H +#define SQLWRITER_H + +#include +#include +#include +#include +#include +#include +#include "variables.h" + +namespace DynamicSql { + class SqlWriter; + typedef boost::intrusive_ptr SqlWriterPtr; + typedef std::list Writers; + class SqlWriter : public IntrusivePtrBase { + public: + SqlWriter(); + virtual ~SqlWriter(); + virtual void writeSql(Glib::ustring & sql) const = 0; + virtual void bindParams(DB::Command *, unsigned int & offset) const = 0; + }; + class SqlText : public SqlWriter { + public: + SqlText(const xmlpp::TextNode *); + virtual void writeSql(Glib::ustring & sql) const; + virtual void bindParams(DB::Command *, unsigned int & offset) const; + + const Glib::ustring text; + }; + class SqlParameter : public SqlWriter, Variable { + public: + SqlParameter(const xmlpp::Element *); + virtual void writeSql(Glib::ustring & sql) const; + virtual void bindParams(DB::Command *, unsigned int & offset) const; + }; + class SqlFilter : public SqlWriter { + public: + SqlFilter(const xmlpp::Element *); + virtual void writeSql(Glib::ustring & sql) const; + virtual void bindParams(DB::Command *, unsigned int & offset) const; + + const Glib::ustring name; + bool active; + private: + Writers writers; + }; + typedef boost::intrusive_ptr SqlFilterPtr; + class SqlCommand : public SqlWriter { + public: + SqlCommand(const xmlpp::Element *); + virtual void writeSql(Glib::ustring & sql) const; + virtual void bindParams(DB::Command *, unsigned int & offset) const; + typedef std::multimap Filters; + Glib::ustring getSqlFor(const Glib::ustring & f) const; + private: + Filters filters; + Writers writers; + }; +} + +#endif + -- cgit v1.2.3