summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2015-12-29 22:53:59 +0000
committerDan Goodliffe <dan@randomdan.homeip.net>2015-12-30 04:31:10 +0000
commit6693545ffb039489127a023bf3e6cf3fae279a74 (patch)
tree34273520b2cdb256d8b004a41a6ce01ed6d1ef9a
parentUnambigous sqlWriter[s].h name (diff)
downloadproject2-6693545ffb039489127a023bf3e6cf3fae279a74.tar.bz2
project2-6693545ffb039489127a023bf3e6cf3fae279a74.tar.xz
project2-6693545ffb039489127a023bf3e6cf3fae279a74.zip
Changes to allow DynamicSql Writers to interact with DB::SqlWriter
-rw-r--r--project2/sql/sqlRows.cpp5
-rw-r--r--project2/sql/sqlTask.cpp6
-rw-r--r--project2/sql/sqlTest.cpp6
-rw-r--r--project2/sql/sqlTest.h2
-rw-r--r--project2/sql/sqlWriters.cpp100
-rw-r--r--project2/sql/sqlWriters.h51
6 files changed, 118 insertions, 52 deletions
diff --git a/project2/sql/sqlRows.cpp b/project2/sql/sqlRows.cpp
index 22f6b35..5bc231b 100644
--- a/project2/sql/sqlRows.cpp
+++ b/project2/sql/sqlRows.cpp
@@ -43,10 +43,9 @@ SqlRows::SqlState::getColumns() const
void
SqlRows::execute(const Glib::ustring & filter, const RowProcessorCallback & rp, ExecContext * ec) const
{
- unsigned int offset = 0;
auto con = db->getReadonly();
- auto select = SelectPtr(con->newSelectCommand(sqlCommand.getSqlFor(filter)));
- sqlCommand.bindParams(ec, select.get(), offset);
+ DynamicSql::SqlWriterWrapper sww(ec, &sqlCommand);
+ auto select = sww.getCommandFor<DB::SelectCommand>(con.get(), filter);
SqlState ss(select);
while (ss.query->fetch()) {
HandleAsVariableType h;
diff --git a/project2/sql/sqlTask.cpp b/project2/sql/sqlTask.cpp
index bf8929f..71ccf62 100644
--- a/project2/sql/sqlTask.cpp
+++ b/project2/sql/sqlTask.cpp
@@ -28,10 +28,8 @@ void
SqlTask::execute(ExecContext * ec) const
{
auto con = db->getWritable();
- boost::shared_ptr<DB::ModifyCommand> modify = boost::shared_ptr<DB::ModifyCommand>(
- con->newModifyCommand(sqlCommand.getSqlFor(filter(NULL))));
- unsigned int offset = 0;
- sqlCommand.bindParams(ec, modify.get(), offset);
+ DynamicSql::SqlWriterWrapper sww(ec, &sqlCommand);
+ auto modify = sww.getCommandFor<DB::ModifyCommand>(con.get(), filter(ec));
if (modify->execute() == 0) {
for (const auto & sq : noChangesTasks) {
sq->execute(ec);
diff --git a/project2/sql/sqlTest.cpp b/project2/sql/sqlTest.cpp
index 705a58b..1bb6678 100644
--- a/project2/sql/sqlTest.cpp
+++ b/project2/sql/sqlTest.cpp
@@ -89,10 +89,8 @@ bool
SqlTest::passes(ExecContext * ec) const
{
auto con = db->getReadonly();
- boost::shared_ptr<DB::SelectCommand> query = boost::shared_ptr<DB::SelectCommand>(
- con->newSelectCommand(sqlCommand.getSqlFor(filter(NULL))));
- unsigned int offset = 0;
- sqlCommand.bindParams(ec, query.get(), offset);
+ DynamicSql::SqlWriterWrapper sww(ec, &sqlCommand);
+ auto query = sww.getCommandFor<DB::SelectCommand>(con.get(), filter(ec));
HandleDoCompare h(testValue(ec), testOp(ec));
while (query->fetch()) {
(*query)[0].apply(h);
diff --git a/project2/sql/sqlTest.h b/project2/sql/sqlTest.h
index 4476b64..d0a40ab 100644
--- a/project2/sql/sqlTest.h
+++ b/project2/sql/sqlTest.h
@@ -20,7 +20,7 @@ class SqlTest : public Test, SqlBase {
const Variable testValue;
private:
- DynamicSql::SqlCommand sqlCommand;
+ const DynamicSql::SqlCommand sqlCommand;
};
#endif
diff --git a/project2/sql/sqlWriters.cpp b/project2/sql/sqlWriters.cpp
index abffe72..bdc42cd 100644
--- a/project2/sql/sqlWriters.cpp
+++ b/project2/sql/sqlWriters.cpp
@@ -1,14 +1,13 @@
#include <pch.hpp>
+#include <selectcommand.h>
+#include <modifycommand.h>
#include "sqlWriters.h"
#include "scripts.h"
#include <boost/bind.hpp>
#include "sqlVariableBinder.h"
-DynamicSql::SqlWriter::SqlWriter()
-{
-}
-
-DynamicSql::SqlWriter::~SqlWriter()
+void
+DynamicSql::SqlWriter::setFilter(const Glib::ustring &) const
{
}
@@ -22,36 +21,47 @@ appendNew(DynamicSql::Writers * w, const Cons & cons)
static
void
-appendNewFromNode(DynamicSql::Writers * w, DynamicSql::SqlCommand::Filters * fs, ScriptNodePtr p)
+appendNewFromNode(DynamicSql::Writers * w, ScriptNodePtr p)
{
- if (fs && p->get_name() == "filter") {
- DynamicSql::SqlFilterPtr f = new DynamicSql::SqlFilter(p);
- w->push_back(f);
- fs->insert(DynamicSql::SqlCommand::Filters::value_type(f->name, f));
+ if (p->get_name() == "filter") {
+ w->push_back(new DynamicSql::SqlFilter(p));
}
else if (p->get_name() == "param") {
w->push_back(new DynamicSql::SqlParameter(p));
}
}
+static
+void
+compose(ScriptNodePtr s, DynamicSql::Writers * writers)
+{
+ s->composeWithCallbacks(
+ boost::bind(&appendNew<DynamicSql::SqlText, Glib::ustring>, writers, _1),
+ boost::bind(&appendNewFromNode, writers, _1));
+}
+
DynamicSql::SqlCommand::SqlCommand(ScriptNodePtr s)
{
if (s) {
- s->composeWithCallbacks(
- boost::bind(&appendNew<SqlText, Glib::ustring>, &writers, _1),
- boost::bind(&appendNewFromNode, &writers, &filters, _1));
+ compose(s, &writers);
}
}
-Glib::ustring
-DynamicSql::SqlCommand::getSqlFor(const Glib::ustring & f) const
+void
+DynamicSql::SqlCommand::setFilter(const Glib::ustring & f) const
+{
+ for (auto & w : writers) {
+ w->setFilter(f);
+ }
+}
+
+void
+DynamicSql::SqlFilter::setFilter(const Glib::ustring & f) const
{
- for (const SqlCommand::Filters::value_type & filter : filters) {
- filter.second->active = (filter.second->name == f);
+ active = (name == f);
+ for (auto & w : writers) {
+ w->setFilter(f);
}
- AdHoc::Buffer sql;
- writeSql(sql);
- return Glib::ustring(sql.str());
}
void
@@ -74,9 +84,7 @@ DynamicSql::SqlFilter::SqlFilter(ScriptNodePtr p) :
name(p->value("name", NULL).as<Glib::ustring>()),
active(false)
{
- p->composeWithCallbacks(
- boost::bind(&appendNew<SqlText, Glib::ustring>, &writers, _1),
- boost::bind(&appendNewFromNode, &writers, (DynamicSql::SqlCommand::Filters *)NULL, _1));
+ compose(p, &writers);
}
void
@@ -132,3 +140,49 @@ DynamicSql::SqlText::bindParams(ExecContext *, DB::Command *, unsigned int &) co
{
}
+namespace DynamicSql {
+ template <>
+ boost::shared_ptr<DB::SelectCommand>
+ SqlWriterWrapper::getCommandFor<DB::SelectCommand>(DB::Connection * db, const Glib::ustring & f) const
+ {
+ AdHoc::Buffer sql;
+ w->setFilter(f);
+ w->writeSql(sql);
+ auto cmd = db->select(sql);
+ unsigned int offset = 0;
+ w->bindParams(ec, cmd.get(), offset);
+ return cmd;
+ }
+
+ template <>
+ boost::shared_ptr<DB::ModifyCommand>
+ SqlWriterWrapper::getCommandFor<DB::ModifyCommand>(DB::Connection * db, const Glib::ustring & f) const
+ {
+ AdHoc::Buffer sql;
+ w->setFilter(f);
+ w->writeSql(sql);
+ auto cmd = db->modify(sql);
+ unsigned int offset = 0;
+ w->bindParams(ec, cmd.get(), offset);
+ return cmd;
+ }
+
+ SqlWriterWrapper::SqlWriterWrapper(ExecContext * e, const DynamicSql::SqlWriter * sw) :
+ ec(e),
+ w(sw)
+ {
+ }
+
+ void
+ SqlWriterWrapper::writeSql(AdHoc::Buffer & sql)
+ {
+ w->writeSql(sql);
+ }
+
+ void
+ SqlWriterWrapper::bindParams(DB::Command * cmd, unsigned int & offset)
+ {
+ w->bindParams(ec, cmd, offset);
+ }
+}
+
diff --git a/project2/sql/sqlWriters.h b/project2/sql/sqlWriters.h
index 7fb9fa0..878a12e 100644
--- a/project2/sql/sqlWriters.h
+++ b/project2/sql/sqlWriters.h
@@ -1,8 +1,11 @@
#ifndef SQLWRITER_H
#define SQLWRITER_H
-#include <intrusivePtrBase.h>
+#include <boost/enable_shared_from_this.hpp>
+#include <boost/shared_ptr.hpp>
#include <command.h>
+#include <connection.h>
+#include <sqlWriter.h>
#include <glibmm/ustring.h>
#include <buffer.h>
#include <list>
@@ -16,17 +19,17 @@ namespace DynamicSql {
class SqlWriter : public IntrusivePtrBase {
public:
- SqlWriter();
- virtual ~SqlWriter();
+ virtual ~SqlWriter() = default;
virtual void writeSql(AdHoc::Buffer & sql) const = 0;
virtual void bindParams(ExecContext *, DB::Command *, unsigned int & offset) const = 0;
+ virtual void setFilter(const Glib::ustring &) const;
};
class SqlText : public SqlWriter {
public:
SqlText(const Glib::ustring &);
- virtual void writeSql(AdHoc::Buffer & sql) const;
- virtual void bindParams(ExecContext *, DB::Command *, unsigned int & offset) const;
+ virtual void writeSql(AdHoc::Buffer & sql) const override;
+ virtual void bindParams(ExecContext *, DB::Command *, unsigned int & offset) const override;
const Glib::ustring text;
};
@@ -34,35 +37,49 @@ namespace DynamicSql {
class SqlParameter : public SqlWriter, Variable {
public:
SqlParameter(ScriptNodePtr);
- virtual void writeSql(AdHoc::Buffer & sql) const;
- virtual void bindParams(ExecContext *, DB::Command *, unsigned int & offset) const;
+ virtual void writeSql(AdHoc::Buffer & sql) const override;
+ virtual void bindParams(ExecContext *, DB::Command *, unsigned int & offset) const override;
};
class SqlFilter : public SqlWriter {
public:
SqlFilter(ScriptNodePtr);
- virtual void writeSql(AdHoc::Buffer & sql) const;
- virtual void bindParams(ExecContext *, DB::Command *, unsigned int & offset) const;
+ virtual void writeSql(AdHoc::Buffer & sql) const override;
+ virtual void bindParams(ExecContext *, DB::Command *, unsigned int & offset) const override;
+ void setFilter(const Glib::ustring &) const override;
const Glib::ustring name;
- bool active;
+ mutable bool active;
+
private:
Writers writers;
};
- typedef boost::intrusive_ptr<SqlFilter> SqlFilterPtr;
-
class SqlCommand : public SqlWriter {
public:
SqlCommand(ScriptNodePtr);
- virtual void writeSql(AdHoc::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;
+
+ virtual void writeSql(AdHoc::Buffer & sql) const override;
+ virtual void bindParams(ExecContext *, DB::Command *, unsigned int & offset) const override;
+ void setFilter(const Glib::ustring &) const override;
+
private:
- Filters filters;
Writers writers;
};
+
+ class SqlWriterWrapper : public DB::SqlWriter {
+ public:
+ SqlWriterWrapper(ExecContext *, const DynamicSql::SqlWriter *);
+
+ virtual void writeSql(AdHoc::Buffer & sql) override;
+ virtual void bindParams(DB::Command *, unsigned int & offset) override;
+
+ template <typename CommandType> boost::shared_ptr<CommandType> getCommandFor(DB::Connection *, const Glib::ustring & f) const;
+
+ private:
+ ExecContext * ec;
+ const DynamicSql::SqlWriter * w;
+ };
}
#endif