diff options
author | randomdan <randomdan@localhost> | 2011-08-11 00:05:50 +0000 |
---|---|---|
committer | randomdan <randomdan@localhost> | 2011-08-11 00:05:50 +0000 |
commit | 6cba12b3ea6f28acfff53a94608a2510fa166921 (patch) | |
tree | 9d182f483ece612ca675b79b3dcd58cbac0e009b | |
parent | Create a variable for accessing parameters (ideal for rowsets, they can all w... (diff) | |
download | project2-6cba12b3ea6f28acfff53a94608a2510fa166921.tar.bz2 project2-6cba12b3ea6f28acfff53a94608a2510fa166921.tar.xz project2-6cba12b3ea6f28acfff53a94608a2510fa166921.zip |
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
-rw-r--r-- | project2/Jamfile.jam | 2 | ||||
-rw-r--r-- | project2/sqlRows.cpp | 126 | ||||
-rw-r--r-- | project2/sqlRows.h | 50 | ||||
-rw-r--r-- | project2/sqlWriter.cpp | 131 | ||||
-rw-r--r-- | project2/sqlWriter.h | 63 |
5 files changed, 198 insertions, 174 deletions
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 : <odbc>yes:<library>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 <string.h> -#include <libxml++/nodes/textnode.h> #include "xmlObjectLoader.h" #include "commonObjects.h" -#include "sqlVariableBinder.h" #include <boost/date_time/gregorian/gregorian_types.hpp> #include <boost/date_time/posix_time/posix_time.hpp> #include <boost/foreach.hpp> @@ -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<const xmlpp::TextNode *>(n); - if (t) { - writers.push_back(new SqlText(t)); - } - const xmlpp::Element * e = dynamic_cast<const xmlpp::Element *>(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<const xmlpp::TextNode *>(n); - if (t) { - writers.push_back(new SqlText(t)); - } - const xmlpp::Element * e = dynamic_cast<const xmlpp::Element *>(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<Glib::ustring>("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<const SqlVariableBinder, const VariableType>(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<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 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<SqlFilter> 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<Glib::ustring, SqlFilterPtr> Filters; - typedef std::pair<Filters::const_iterator, Filters::const_iterator> FiltersRangeType; - Filters filters; - private: - Writers writers; - }; - SqlCommand sqlCommand; + DynamicSql::SqlCommand sqlCommand; typedef boost::shared_ptr<DB::SelectCommand> 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 <boost/foreach.hpp> +#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<const xmlpp::TextNode *>(n); + if (t) { + writers.push_back(new SqlText(t)); + } + const xmlpp::Element * e = dynamic_cast<const xmlpp::Element *>(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<const xmlpp::TextNode *>(n); + if (t) { + writers.push_back(new SqlText(t)); + } + const xmlpp::Element * e = dynamic_cast<const xmlpp::Element *>(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<Glib::ustring>("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<const SqlVariableBinder, const VariableType>(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 <intrusivePtrBase.h> +#include <libxml++/nodes/textnode.h> +#include <command.h> +#include <glibmm/ustring.h> +#include <list> +#include <map> +#include "variables.h" + +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 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<SqlFilter> 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<Glib::ustring, SqlFilterPtr> Filters; + Glib::ustring getSqlFor(const Glib::ustring & f) const; + private: + Filters filters; + Writers writers; + }; +} + +#endif + |