mirror of
https://github.com/rbock/sqlpp11.git
synced 2024-11-16 04:47:18 +08:00
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).
This commit is contained in:
parent
abf4bb8e9a
commit
3bbe343a77
12
include/sqlpp11/vendor/interpretable.h
vendored
12
include/sqlpp11/vendor/interpretable.h
vendored
@ -59,9 +59,13 @@ namespace sqlpp
|
|||||||
return _impl->serialize(context);
|
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<typename Context>
|
||||||
|
auto serialize(Context& context) const
|
||||||
|
-> typename std::enable_if<std::is_same<Context, _serializer_context_t>::value
|
||||||
|
and not std::is_same<Context, sqlpp::serializer_context_t>::value, void>::type
|
||||||
{
|
{
|
||||||
return _impl->serialize(context);
|
return _impl->db_serialize(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
_interpreter_context_t& interpret(_interpreter_context_t& context) const
|
_interpreter_context_t& interpret(_interpreter_context_t& context) const
|
||||||
@ -73,7 +77,7 @@ namespace sqlpp
|
|||||||
struct _impl_base
|
struct _impl_base
|
||||||
{
|
{
|
||||||
virtual sqlpp::serializer_context_t& serialize(sqlpp::serializer_context_t& context) const = 0;
|
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 _interpreter_context_t& interpret(_interpreter_context_t& context) const = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -91,7 +95,7 @@ namespace sqlpp
|
|||||||
return context;
|
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);
|
Db::_serialize_interpretable(_t, context);
|
||||||
return context;
|
return context;
|
||||||
|
12
include/sqlpp11/vendor/named_interpretable.h
vendored
12
include/sqlpp11/vendor/named_interpretable.h
vendored
@ -57,9 +57,13 @@ namespace sqlpp
|
|||||||
return _impl->serialize(context);
|
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<typename Context>
|
||||||
|
auto serialize(Context& context) const
|
||||||
|
-> typename std::enable_if<std::is_same<Context, _serializer_context_t>::value
|
||||||
|
and not std::is_same<Context, sqlpp::serializer_context_t>::value, void>::type
|
||||||
{
|
{
|
||||||
return _impl->serialize(context);
|
return _impl->db_serialize(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
_interpreter_context_t& interpret(_interpreter_context_t& context) const
|
_interpreter_context_t& interpret(_interpreter_context_t& context) const
|
||||||
@ -76,7 +80,7 @@ namespace sqlpp
|
|||||||
struct _impl_base
|
struct _impl_base
|
||||||
{
|
{
|
||||||
virtual sqlpp::serializer_context_t& serialize(sqlpp::serializer_context_t& context) const = 0;
|
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 _interpreter_context_t& interpret(_interpreter_context_t& context) const = 0;
|
||||||
virtual std::string _get_name() const = 0;
|
virtual std::string _get_name() const = 0;
|
||||||
};
|
};
|
||||||
@ -95,7 +99,7 @@ namespace sqlpp
|
|||||||
return context;
|
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);
|
Db::_serialize_interpretable(_t, context);
|
||||||
return context;
|
return context;
|
||||||
|
@ -26,30 +26,96 @@
|
|||||||
#ifndef SQLPP_MOCK_DB_H
|
#ifndef SQLPP_MOCK_DB_H
|
||||||
#define SQLPP_MOCK_DB_H
|
#define SQLPP_MOCK_DB_H
|
||||||
|
|
||||||
#include <sqlpp11/connection.h>
|
|
||||||
#include <sqlpp11/serializer_context.h>
|
#include <sqlpp11/serializer_context.h>
|
||||||
#include <sqlpp11/serialize.h>
|
#include <sqlpp11/connection.h>
|
||||||
|
|
||||||
struct DbMock: public sqlpp::connection
|
struct DbMock: public sqlpp::connection
|
||||||
{
|
{
|
||||||
struct _serializer_context_t : public sqlpp::serializer_context_t
|
using _serializer_context_t = sqlpp::serializer_context_t;
|
||||||
{
|
|
||||||
_serializer_context_t(std::ostream& os): sqlpp::serializer_context_t(os) {}
|
|
||||||
};
|
|
||||||
|
|
||||||
using _interpreter_context_t = _serializer_context_t;
|
using _interpreter_context_t = _serializer_context_t;
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
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<typename T>
|
template<typename T>
|
||||||
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<typename ResultRow>
|
||||||
|
void next(ResultRow& result_row)
|
||||||
|
{
|
||||||
|
result_row.invalidate();
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
// Directly executed statements start here
|
||||||
|
template<typename T>
|
||||||
|
auto operator() (const T& t) -> decltype(t._run(*this))
|
||||||
|
{
|
||||||
|
return t._run(*this);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename Insert>
|
||||||
|
size_t insert(const Insert& x)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename Update>
|
||||||
|
size_t update(const Update& x)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename Remove>
|
||||||
|
size_t remove(const Remove& x)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename Select>
|
||||||
|
result_t select(const Select& s)
|
||||||
|
{
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
// Prepared statements start here
|
||||||
|
using _prepared_statement_t = std::nullptr_t;
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
auto prepare(const T& t) -> decltype(t._prepare(*this))
|
||||||
|
{
|
||||||
|
return t._prepare(*this);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
template<typename Insert>
|
||||||
|
_prepared_statement_t prepare_insert(Insert& x)
|
||||||
|
{
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename PreparedInsert>
|
||||||
|
size_t run_prepared_insert(const PreparedInsert& x)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user