summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--slicer/db/sqlCommon.cpp34
-rw-r--r--slicer/db/sqlCommon.h16
-rw-r--r--slicer/db/sqlInsertSerializer.cpp39
-rw-r--r--slicer/db/sqlTablePatchSerializer.cpp8
-rw-r--r--slicer/db/sqlUpdateSerializer.cpp51
-rw-r--r--slicer/db/testPatch.cpp7
6 files changed, 106 insertions, 49 deletions
diff --git a/slicer/db/sqlCommon.cpp b/slicer/db/sqlCommon.cpp
index d484967..5618b29 100644
--- a/slicer/db/sqlCommon.cpp
+++ b/slicer/db/sqlCommon.cpp
@@ -1,7 +1,41 @@
#include <sqlExceptions.h>
#include <compileTimeFormatter.h>
+#include "sqlCommon.h"
+#include <metadata.h>
namespace Slicer {
+ const std::string md_pkey = "db:pkey";
+ const std::string md_auto = "db:auto";
+ const std::string md_ignore = "db:ignore";
+ const std::string md_global_ignore = "ignore";
+
+ bool isPKey(HookCommonPtr h)
+ {
+ return metaDataFlagSet(h->GetMetadata(), md_pkey) && isBind(h);
+ }
+
+ bool isAuto(HookCommonPtr h)
+ {
+ return metaDataFlagSet(h->GetMetadata(), md_auto) && isBind(h);
+ }
+
+ bool isNotAuto(HookCommonPtr h)
+ {
+ return metaDataFlagNotSet(h->GetMetadata(), md_auto) && isBind(h);
+ }
+
+ bool isBind(HookCommonPtr h)
+ {
+ return metaDataFlagNotSet(h->GetMetadata(), md_global_ignore) &&
+ metaDataFlagNotSet(h->GetMetadata(), md_ignore);
+ }
+
+ bool isValue(HookCommonPtr h)
+ {
+ return metaDataFlagNotSet(h->GetMetadata(), md_auto) &&
+ metaDataFlagNotSet(h->GetMetadata(), md_pkey) && isBind(h);
+ }
+
void TooManyRowsReturned::ice_print(std::ostream & s) const
{
s << "Too many rows returned";
diff --git a/slicer/db/sqlCommon.h b/slicer/db/sqlCommon.h
new file mode 100644
index 0000000..2471d4c
--- /dev/null
+++ b/slicer/db/sqlCommon.h
@@ -0,0 +1,16 @@
+#ifndef SLICER_DB_SQLCOMMON_H
+#define SLICER_DB_SQLCOMMON_H
+
+#include <string>
+#include <modelParts.h>
+
+namespace Slicer {
+ bool isPKey(HookCommonPtr);
+ bool isAuto(HookCommonPtr);
+ bool isNotAuto(HookCommonPtr);
+ bool isBind(HookCommonPtr);
+ bool isValue(HookCommonPtr);
+}
+
+#endif
+
diff --git a/slicer/db/sqlInsertSerializer.cpp b/slicer/db/sqlInsertSerializer.cpp
index 39925cd..04eae93 100644
--- a/slicer/db/sqlInsertSerializer.cpp
+++ b/slicer/db/sqlInsertSerializer.cpp
@@ -2,14 +2,13 @@
#include <common.h>
#include <sqlExceptions.h>
#include "sqlBinder.h"
+#include "sqlCommon.h"
#include <buffer.h>
#include <modifycommand.h>
#include <slicer/metadata.h>
#include <boost/numeric/conversion/cast.hpp>
namespace Slicer {
- const std::string md_auto = "db:auto";
-
SqlInsertSerializer::SqlInsertSerializer(DB::Connection * const c, const std::string & t) :
connection(c),
tableName(t)
@@ -87,27 +86,29 @@ namespace Slicer {
{
SqlAutoIdInsertSerializer::bindObjectAndExecute(cmp, ins);
cmp->OnEachChild([&ins, this](const std::string &, ModelPartPtr cmp, HookCommonPtr h) {
- if (metaDataFlagSet(h->GetMetadata(), md_auto)) {
- cmp->SetValue(new IdSave(connection));
- }
- });
+ if (isAuto(h)) {
+ cmp->SetValue(new IdSave(connection));
+ }
+ });
}
void
- SqlInsertSerializer::bindObjectAndExecuteField(int & paramNo, DB::ModifyCommand * ins, Slicer::ModelPartPtr cmp, HookCommonPtr) const
+ SqlInsertSerializer::bindObjectAndExecuteField(int & paramNo, DB::ModifyCommand * ins, Slicer::ModelPartPtr cmp, HookCommonPtr h) const
{
- if (cmp->HasValue()) {
- cmp->GetValue(new SqlBinder(*ins, paramNo++));
- }
- else {
- ins->bindNull(paramNo++);
+ if (isBind(h)) {
+ if (cmp->HasValue()) {
+ cmp->GetValue(new SqlBinder(*ins, paramNo++));
+ }
+ else {
+ ins->bindNull(paramNo++);
+ }
}
}
void
SqlAutoIdInsertSerializer::bindObjectAndExecuteField(int & paramNo, DB::ModifyCommand * ins, Slicer::ModelPartPtr cmp, HookCommonPtr h) const
{
- if (metaDataFlagNotSet(h->GetMetadata(), md_auto)) {
+ if (isNotAuto(h)) {
SqlInsertSerializer::bindObjectAndExecuteField(paramNo, ins, cmp, h);
}
}
@@ -128,18 +129,20 @@ namespace Slicer {
}
void
- SqlInsertSerializer::createInsertField(int & fieldNo, AdHoc::Buffer & insert, const std::string & name, HookCommonPtr) const
+ SqlInsertSerializer::createInsertField(int & fieldNo, AdHoc::Buffer & insert, const std::string & name, HookCommonPtr h) const
{
- if (fieldNo++) {
- insert.append(", ");
+ if (isBind(h)) {
+ if (fieldNo++) {
+ insert.append(", ");
+ }
+ insert.append(name);
}
- insert.append(name);
}
void
SqlAutoIdInsertSerializer::createInsertField(int & fieldNo, AdHoc::Buffer & insert, const std::string & name, HookCommonPtr h) const
{
- if (metaDataFlagNotSet(h->GetMetadata(), md_auto)) {
+ if (isNotAuto(h)) {
if (fieldNo++) {
insert.append(", ");
}
diff --git a/slicer/db/sqlTablePatchSerializer.cpp b/slicer/db/sqlTablePatchSerializer.cpp
index 20b0d82..50505cb 100644
--- a/slicer/db/sqlTablePatchSerializer.cpp
+++ b/slicer/db/sqlTablePatchSerializer.cpp
@@ -1,13 +1,11 @@
#include "sqlTablePatchSerializer.h"
#include "sqlInsertSerializer.h"
+#include "sqlCommon.h"
#include <slicer/metadata.h>
#include <compileTimeFormatter.h>
#include <scopeExit.h>
namespace Slicer {
- const std::string md_pkey = "db:pkey";
- const std::string ignore = "ignore";
-
AdHocFormatter(ttname, "slicer_tmp_%?");
SqlTablePatchSerializer::SqlTablePatchSerializer(DB::Connection * db, DB::TablePatch & tp) :
db(db),
@@ -34,12 +32,12 @@ namespace Slicer {
auto mp = mpr->GetContainedModelPart();
mp->OnEachChild([this](const auto & name, const auto &, const auto & h) {
- if (metaDataFlagSet(h->GetMetadata(), md_pkey)) {
+ if (isPKey(h)) {
tablePatch.pk.insert(name);
}
});
mp->OnEachChild([this](const auto & name, const auto &, const auto & h) {
- if (metaDataFlagNotSet(h->GetMetadata(), ignore)) {
+ if (isBind(h)) {
tablePatch.cols.insert(name);
}
});
diff --git a/slicer/db/sqlUpdateSerializer.cpp b/slicer/db/sqlUpdateSerializer.cpp
index 0289a90..f07c421 100644
--- a/slicer/db/sqlUpdateSerializer.cpp
+++ b/slicer/db/sqlUpdateSerializer.cpp
@@ -2,13 +2,12 @@
#include <sqlExceptions.h>
#include <common.h>
#include "sqlBinder.h"
+#include "sqlCommon.h"
#include <buffer.h>
#include <modifycommand.h>
#include <slicer/metadata.h>
namespace Slicer {
- const std::string md_pkey = "db:pkey";
-
SqlUpdateSerializer::SqlUpdateSerializer(DB::Connection * const c, const std::string & t) :
connection(c),
tableName(t)
@@ -51,20 +50,20 @@ namespace Slicer {
{
int paramNo = 0;
cmp->OnEachChild([&upd, &paramNo](const std::string &, ModelPartPtr cmp, HookCommonPtr h) {
- if (metaDataFlagNotSet(h->GetMetadata(), md_pkey)) {
- if (cmp->HasValue()) {
- cmp->GetValue(new SqlBinder(*upd, paramNo++));
- }
- else {
- upd->bindNull(paramNo++);
- }
- }
- });
- cmp->OnEachChild([&upd, &paramNo](const std::string &, ModelPartPtr cmp, HookCommonPtr h) {
- if (metaDataFlagSet(h->GetMetadata(), md_pkey)) {
+ if (isValue(h)) {
+ if (cmp->HasValue()) {
cmp->GetValue(new SqlBinder(*upd, paramNo++));
}
- });
+ else {
+ upd->bindNull(paramNo++);
+ }
+ }
+ });
+ cmp->OnEachChild([&upd, &paramNo](const std::string &, ModelPartPtr cmp, HookCommonPtr h) {
+ if (isPKey(h)) {
+ cmp->GetValue(new SqlBinder(*upd, paramNo++));
+ }
+ });
if (upd->execute() == 0) {
throw NoRowsFound();
}
@@ -77,23 +76,23 @@ namespace Slicer {
update.appendbf("UPDATE %s SET ", tableName);
int fieldNo = 0;
mp->OnEachChild([&update, &fieldNo]( const std::string & name, ModelPartPtr, HookCommonPtr h) {
- if (metaDataFlagNotSet(h->GetMetadata(), md_pkey)) {
- if (fieldNo++) {
- update.append(", ");
- }
- update.appendbf("%s = ?", name);
+ if (isValue(h)) {
+ if (fieldNo++) {
+ update.append(", ");
}
- });
+ update.appendbf("%s = ?", name);
+ }
+ });
update.append(" WHERE ", AdHoc::Buffer::Use);
fieldNo = 0;
mp->OnEachChild([&update, &fieldNo]( const std::string & name, ModelPartPtr, HookCommonPtr h) {
- if (metaDataFlagSet(h->GetMetadata(), md_pkey)) {
- if (fieldNo++) {
- update.append(" AND ");
- }
- update.appendbf("%s = ?", name);
+ if (isPKey(h)) {
+ if (fieldNo++) {
+ update.append(" AND ");
}
- });
+ update.appendbf("%s = ?", name);
+ }
+ });
return ModifyPtr(connection->newModifyCommand(update));
}
}
diff --git a/slicer/db/testPatch.cpp b/slicer/db/testPatch.cpp
index bb7979b..333b806 100644
--- a/slicer/db/testPatch.cpp
+++ b/slicer/db/testPatch.cpp
@@ -14,6 +14,7 @@
BOOST_TEST_DONT_PRINT_LOG_VALUE(TestModule::DateTime);
BOOST_TEST_DONT_PRINT_LOG_VALUE(TestModule::IsoDate);
BOOST_TEST_DONT_PRINT_LOG_VALUE(TestDatabase::Timespan);
+BOOST_TEST_DONT_PRINT_LOG_VALUE(DB::PrimaryKey);
// LCOV_EXCL_STOP
class StandardMockDatabase : public PQ::Mock {
@@ -43,5 +44,11 @@ BOOST_AUTO_TEST_CASE( insert_builtins )
auto cmd = db->select("SELECT COUNT(*) FROM builtins");
auto c = Slicer::DeserializeAny<Slicer::SqlSelectDeserializer, int>(*cmd);
BOOST_REQUIRE_EQUAL(2, c);
+ BOOST_REQUIRE_EQUAL(2, tp.pk.size());
+ DB::PrimaryKey pk = {"mint", "mlong"};
+ BOOST_REQUIRE_EQUAL(pk, tp.pk);
+ BOOST_REQUIRE_EQUAL(8, tp.cols.size());
+ DB::ColumnNames cols = {"mbool", "mbyte", "mdouble", "mfloat", "mint", "mlong", "mshort", "mstring"};
+ BOOST_REQUIRE_EQUAL(cols, tp.cols);
}