diff options
Diffstat (limited to 'project2/sql/sqlWriter.cpp')
-rw-r--r-- | project2/sql/sqlWriter.cpp | 131 |
1 files changed, 131 insertions, 0 deletions
diff --git a/project2/sql/sqlWriter.cpp b/project2/sql/sqlWriter.cpp new file mode 100644 index 0000000..88d9801 --- /dev/null +++ b/project2/sql/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 +{ +} + |