From 3bbe343a77733672671f10108437c66f1361f76d Mon Sep 17 00:00:00 2001 From: rbock Date: Thu, 20 Feb 2014 09:24:06 +0100 Subject: [PATCH] Added a better MockDb that can execute statements Currently all executions are no-ops, but it will be rather simple to yield select results too (to be configured at runtime). --- include/sqlpp11/vendor/interpretable.h | 12 ++- include/sqlpp11/vendor/named_interpretable.h | 12 ++- tests/MockDb.h | 90 +++++++++++++++++--- 3 files changed, 94 insertions(+), 20 deletions(-) diff --git a/include/sqlpp11/vendor/interpretable.h b/include/sqlpp11/vendor/interpretable.h index d919406d..de878779 100644 --- a/include/sqlpp11/vendor/interpretable.h +++ b/include/sqlpp11/vendor/interpretable.h @@ -59,9 +59,13 @@ namespace sqlpp return _impl->serialize(context); } - _serializer_context_t& serialize(_serializer_context_t& context) const + // This method only exists if Db::_serializer_context_t and sqlpp::serializer_context_t are not the same + template + auto serialize(Context& context) const + -> typename std::enable_if::value + and not std::is_same::value, void>::type { - return _impl->serialize(context); + return _impl->db_serialize(context); } _interpreter_context_t& interpret(_interpreter_context_t& context) const @@ -73,7 +77,7 @@ namespace sqlpp struct _impl_base { virtual sqlpp::serializer_context_t& serialize(sqlpp::serializer_context_t& context) const = 0; - virtual _serializer_context_t& serialize(_serializer_context_t& context) const = 0; + virtual _serializer_context_t& db_serialize(_serializer_context_t& context) const = 0; virtual _interpreter_context_t& interpret(_interpreter_context_t& context) const = 0; }; @@ -91,7 +95,7 @@ namespace sqlpp return context; } - _serializer_context_t& serialize(_serializer_context_t& context) const + _serializer_context_t& db_serialize(_serializer_context_t& context) const { Db::_serialize_interpretable(_t, context); return context; diff --git a/include/sqlpp11/vendor/named_interpretable.h b/include/sqlpp11/vendor/named_interpretable.h index 69de2d21..505c66ab 100644 --- a/include/sqlpp11/vendor/named_interpretable.h +++ b/include/sqlpp11/vendor/named_interpretable.h @@ -57,9 +57,13 @@ namespace sqlpp return _impl->serialize(context); } - _serializer_context_t& serialize(_serializer_context_t& context) const + // This method only exists if Db::_serializer_context_t and sqlpp::serializer_context_t are not the same + template + auto serialize(Context& context) const + -> typename std::enable_if::value + and not std::is_same::value, void>::type { - return _impl->serialize(context); + return _impl->db_serialize(context); } _interpreter_context_t& interpret(_interpreter_context_t& context) const @@ -76,7 +80,7 @@ namespace sqlpp struct _impl_base { virtual sqlpp::serializer_context_t& serialize(sqlpp::serializer_context_t& context) const = 0; - virtual _serializer_context_t& serialize(_serializer_context_t& context) const = 0; + virtual _serializer_context_t& db_serialize(_serializer_context_t& context) const = 0; virtual _interpreter_context_t& interpret(_interpreter_context_t& context) const = 0; virtual std::string _get_name() const = 0; }; @@ -95,7 +99,7 @@ namespace sqlpp return context; } - _serializer_context_t& serialize(_serializer_context_t& context) const + _serializer_context_t& db_serialize(_serializer_context_t& context) const { Db::_serialize_interpretable(_t, context); return context; diff --git a/tests/MockDb.h b/tests/MockDb.h index 8d377a78..990280b8 100644 --- a/tests/MockDb.h +++ b/tests/MockDb.h @@ -24,32 +24,98 @@ */ #ifndef SQLPP_MOCK_DB_H -#define SQLPP_MOCK_DB_H +#define SQLPP_MOCK_DB_H -#include #include -#include +#include struct DbMock: public sqlpp::connection { - struct _serializer_context_t : public sqlpp::serializer_context_t - { - _serializer_context_t(std::ostream& os): sqlpp::serializer_context_t(os) {} - }; - + using _serializer_context_t = sqlpp::serializer_context_t; using _interpreter_context_t = _serializer_context_t; template - static _serializer_context_t& _serialize_interpretable(const T& t, _serializer_context_t& context) + static _serializer_context_t _serialize_interpretable(const T& t, _serializer_context_t& context) { - return ::sqlpp::serialize(t, context); + sqlpp::serialize(t, context); + return context; } template - static _interpreter_context_t& _interpret_interpretable(const T& t, _interpreter_context_t& context) + static _serializer_context_t _interpret_interpretable(const T& t, _interpreter_context_t& context) { - return ::sqlpp::serialize(t, context); + sqlpp::serialize(t, context); + return context; } + + class result_t + { + public: + constexpr bool operator==(const result_t& rhs) const + { + return true; + } + + template + void next(ResultRow& result_row) + { + result_row.invalidate(); + }; + }; + + // Directly executed statements start here + template + auto operator() (const T& t) -> decltype(t._run(*this)) + { + return t._run(*this); + } + + template + size_t insert(const Insert& x) + { + return 0; + } + + template + size_t update(const Update& x) + { + return 0; + } + + template + size_t remove(const Remove& x) + { + return 0; + } + + template + result_t select(const Select& s) + { + return {}; + } + + // Prepared statements start here + using _prepared_statement_t = std::nullptr_t; + + template + auto prepare(const T& t) -> decltype(t._prepare(*this)) + { + return t._prepare(*this); + } + + + template + _prepared_statement_t prepare_insert(Insert& x) + { + return nullptr; + } + + template + size_t run_prepared_insert(const PreparedInsert& x) + { + return 0; + } + }; #endif