From 9c2123473fa7c7399edc67d14c3675c2e11a42d3 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Tue, 2 Jul 2019 19:27:34 +0100 Subject: Swap AdHoc::Buffer for CTF in db lib --- slicer/db/sqlInsertSerializer.cpp | 27 ++++++++++++++------------- slicer/db/sqlInsertSerializer.h | 6 +++--- slicer/db/sqlUpdateSerializer.cpp | 19 ++++++++++--------- 3 files changed, 27 insertions(+), 25 deletions(-) diff --git a/slicer/db/sqlInsertSerializer.cpp b/slicer/db/sqlInsertSerializer.cpp index 17d33b2..dde8f1f 100644 --- a/slicer/db/sqlInsertSerializer.cpp +++ b/slicer/db/sqlInsertSerializer.cpp @@ -3,7 +3,7 @@ #include #include "sqlBinder.h" #include "sqlCommon.h" -#include +#include #include #include #include @@ -119,37 +119,38 @@ namespace Slicer { DB::ModifyCommandPtr SqlInsertSerializer::createInsert(const Slicer::ModelPartPtr & mp) const { - AdHoc::Buffer insert; - insert.appendbf("INSERT INTO %s(", tableName); + using namespace AdHoc::literals; + std::stringstream insert; + "INSERT INTO %?("_fmt(insert, tableName); int fieldNo = 0; mp->OnEachChild(std::bind(&SqlInsertSerializer::createInsertField, this, std::ref(fieldNo), std::ref(insert), _1, _3)); - insert.append(") VALUES (", AdHoc::Buffer::Use); + insert << ") VALUES ("; for (; fieldNo > 1; --fieldNo) { - insert.append("?, "); + insert << "?, "; } - insert.append("?)"); - return connection->modify(insert); + insert << "?)"; + return connection->modify(insert.str()); } void - SqlInsertSerializer::createInsertField(int & fieldNo, AdHoc::Buffer & insert, const std::string & name, const HookCommon * h) const + SqlInsertSerializer::createInsertField(int & fieldNo, std::ostream & insert, const std::string & name, const HookCommon * h) const { if (isBind(h)) { if (fieldNo++) { - insert.append(", "); + insert << ','; } - insert.append(name); + insert << name; } } void - SqlAutoIdInsertSerializer::createInsertField(int & fieldNo, AdHoc::Buffer & insert, const std::string & name, const HookCommon * h) const + SqlAutoIdInsertSerializer::createInsertField(int & fieldNo, std::ostream & insert, const std::string & name, const HookCommon * h) const { if (isNotAuto(h)) { if (fieldNo++) { - insert.append(", "); + insert << ','; } - insert.append(name); + insert << name; } } } diff --git a/slicer/db/sqlInsertSerializer.h b/slicer/db/sqlInsertSerializer.h index d3e2215..3534b87 100644 --- a/slicer/db/sqlInsertSerializer.h +++ b/slicer/db/sqlInsertSerializer.h @@ -4,7 +4,7 @@ #include #include #include -#include +#include namespace Slicer { class DLL_PUBLIC SqlInsertSerializer : public Slicer::Serializer { @@ -17,7 +17,7 @@ namespace Slicer { void SerializeObject(const Slicer::ModelPartPtr &) const; void SerializeSequence(const Slicer::ModelPartPtr &) const; DB::ModifyCommandPtr createInsert(const Slicer::ModelPartPtr &) const; - virtual void createInsertField(int & fieldNo, AdHoc::Buffer & insert, const std::string & name, const HookCommon * h) const; + virtual void createInsertField(int & fieldNo, std::ostream & insert, const std::string & name, const HookCommon * h) const; virtual void bindObjectAndExecute(const Slicer::ModelPartPtr &, DB::ModifyCommand *) const; virtual void bindObjectAndExecuteField(int & paramNo, DB::ModifyCommand *, const Slicer::ModelPartPtr &, const HookCommon *) const; @@ -31,7 +31,7 @@ namespace Slicer { SqlAutoIdInsertSerializer(const P & ... p) : SqlInsertSerializer(p...) { } protected: - virtual void createInsertField(int & fieldNo, AdHoc::Buffer & insert, const std::string & name, const HookCommon * h) const; + virtual void createInsertField(int & fieldNo, std::ostream & insert, const std::string & name, const HookCommon * h) const; virtual void bindObjectAndExecuteField(int & paramNo, DB::ModifyCommand *, const Slicer::ModelPartPtr &, const HookCommon *) const; }; diff --git a/slicer/db/sqlUpdateSerializer.cpp b/slicer/db/sqlUpdateSerializer.cpp index 3ed7c2b..64ff1a0 100644 --- a/slicer/db/sqlUpdateSerializer.cpp +++ b/slicer/db/sqlUpdateSerializer.cpp @@ -3,7 +3,7 @@ #include #include "sqlBinder.h" #include "sqlCommon.h" -#include +#include #include #include #include @@ -73,28 +73,29 @@ namespace Slicer { DB::ModifyCommandPtr SqlUpdateSerializer::createUpdate(const Slicer::ModelPartPtr & mp) const { - AdHoc::Buffer update; - update.appendbf("UPDATE %s SET ", tableName); + using namespace AdHoc::literals; + std::stringstream update; + "UPDATE %? SET "_fmt(update, tableName); int fieldNo = 0; mp->OnEachChild([&update, &fieldNo]( const std::string & name, const ModelPartPtr &, const HookCommon * h) { if (isValue(h)) { if (fieldNo++) { - update.append(", "); + update << ", "; } - update.appendbf("%s = ?", name); + "%? = ?"_fmt(update, name); } }); - update.append(" WHERE ", AdHoc::Buffer::Use); + update << " WHERE "; fieldNo = 0; mp->OnEachChild([&update, &fieldNo]( const std::string & name, const ModelPartPtr &, const HookCommon * h) { if (isPKey(h)) { if (fieldNo++) { - update.append(" AND "); + update << " AND "; } - update.appendbf("%s = ?", name); + "%? = ?"_fmt(update, name); } }); - return connection->modify(update); + return connection->modify(update.str()); } } -- cgit v1.2.3