2013-11-07 13:36:54 +01:00
|
|
|
/*
|
2015-02-15 19:00:21 +01:00
|
|
|
* Copyright (c) 2013-2015, Roland Bock
|
2013-11-07 13:36:54 +01:00
|
|
|
* All rights reserved.
|
2015-09-13 21:33:19 +02:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without modification,
|
2013-11-07 13:36:54 +01:00
|
|
|
* are permitted provided that the following conditions are met:
|
2015-09-13 21:33:19 +02:00
|
|
|
*
|
|
|
|
* * Redistributions of source code must retain the above copyright notice,
|
2013-11-07 13:36:54 +01:00
|
|
|
* this list of conditions and the following disclaimer.
|
2015-09-13 21:33:19 +02:00
|
|
|
* * Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
2013-11-07 13:36:54 +01:00
|
|
|
* and/or other materials provided with the distribution.
|
2015-09-13 21:33:19 +02:00
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
|
|
|
|
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
|
|
|
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
|
|
|
|
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
2013-11-07 13:36:54 +01:00
|
|
|
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef SQLPP_MOCK_DB_H
|
2014-02-20 09:24:06 +01:00
|
|
|
#define SQLPP_MOCK_DB_H
|
2013-11-07 13:36:54 +01:00
|
|
|
|
2014-02-21 06:02:49 +01:00
|
|
|
#include <sstream>
|
2015-01-11 16:12:39 +01:00
|
|
|
#include <iostream>
|
2015-04-30 21:32:50 +02:00
|
|
|
#include <sqlpp11/schema.h>
|
2015-12-23 16:01:45 +01:00
|
|
|
#include <sqlpp11/data_types/no_value.h>
|
2015-10-06 22:08:44 +02:00
|
|
|
#include <sqlpp11/serialize.h>
|
2014-02-18 17:45:08 +01:00
|
|
|
#include <sqlpp11/serializer_context.h>
|
2014-02-20 09:24:06 +01:00
|
|
|
#include <sqlpp11/connection.h>
|
2013-11-07 13:36:54 +01:00
|
|
|
|
2015-09-13 21:33:19 +02:00
|
|
|
template <bool enforceNullResultTreatment>
|
|
|
|
struct MockDbT : public sqlpp::connection
|
2013-11-07 13:36:54 +01:00
|
|
|
{
|
2015-09-13 21:33:19 +02:00
|
|
|
using _traits =
|
|
|
|
::sqlpp::make_traits<::sqlpp::no_value_t,
|
|
|
|
::sqlpp::tag_if<::sqlpp::tag::enforce_null_result_treatment, enforceNullResultTreatment>>;
|
|
|
|
|
|
|
|
struct _serializer_context_t
|
|
|
|
{
|
|
|
|
std::ostringstream _os;
|
|
|
|
|
|
|
|
_serializer_context_t() = default;
|
|
|
|
_serializer_context_t(const _serializer_context_t& rhs)
|
|
|
|
{
|
|
|
|
_os << rhs._os.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string str() const
|
|
|
|
{
|
|
|
|
return _os.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
void reset()
|
|
|
|
{
|
|
|
|
_os.str("");
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
std::ostream& operator<<(T t)
|
|
|
|
{
|
|
|
|
return _os << t;
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::string escape(std::string arg)
|
|
|
|
{
|
|
|
|
return sqlpp::serializer_context_t::escape(arg);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
using _interpreter_context_t = _serializer_context_t;
|
|
|
|
|
|
|
|
_serializer_context_t get_serializer_context()
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
static _serializer_context_t& _serialize_interpretable(const T& t, _serializer_context_t& context)
|
|
|
|
{
|
|
|
|
sqlpp::serialize(t, context);
|
|
|
|
return context;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
static _serializer_context_t& _interpret_interpretable(const T& t, _interpreter_context_t& context)
|
|
|
|
{
|
|
|
|
sqlpp::serialize(t, context);
|
|
|
|
return context;
|
|
|
|
}
|
|
|
|
|
|
|
|
class result_t
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
constexpr bool operator==(const result_t&) const
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename ResultRow>
|
|
|
|
void next(ResultRow& result_row)
|
|
|
|
{
|
|
|
|
result_row._invalidate();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Directly executed statements start here
|
|
|
|
template <typename T>
|
|
|
|
auto _run(const T& t, const std::true_type&) -> decltype(t._run(*this))
|
|
|
|
{
|
|
|
|
return t._run(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2015-09-17 11:46:37 +02:00
|
|
|
auto _run(const T& t, const std::false_type&) -> void;
|
2015-09-13 21:33:19 +02:00
|
|
|
|
|
|
|
template <typename T>
|
2015-12-24 21:11:00 +01:00
|
|
|
auto operator()(const T& t) -> decltype(this->_run(t, sqlpp::run_check_t<_serializer_context_t, T>{}))
|
2015-09-13 21:33:19 +02:00
|
|
|
{
|
2015-09-17 11:46:37 +02:00
|
|
|
sqlpp::run_check_t<_serializer_context_t, T>::_();
|
2015-12-24 21:11:00 +01:00
|
|
|
return _run(t, sqlpp::run_check_t<_serializer_context_t, T>{});
|
2015-09-13 21:33:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t execute(const std::string&)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <
|
|
|
|
typename Statement,
|
|
|
|
typename Enable = typename std::enable_if<not std::is_convertible<Statement, std::string>::value, void>::type>
|
|
|
|
size_t execute(const Statement& x)
|
|
|
|
{
|
|
|
|
_serializer_context_t context;
|
|
|
|
::sqlpp::serialize(x, context);
|
|
|
|
std::cout << "Running execute call with\n" << context.str() << std::endl;
|
|
|
|
return execute(context.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Insert>
|
|
|
|
size_t insert(const Insert& x)
|
|
|
|
{
|
|
|
|
_serializer_context_t context;
|
|
|
|
::sqlpp::serialize(x, context);
|
|
|
|
std::cout << "Running insert call with\n" << context.str() << std::endl;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Update>
|
|
|
|
size_t update(const Update& x)
|
|
|
|
{
|
|
|
|
_serializer_context_t context;
|
|
|
|
::sqlpp::serialize(x, context);
|
|
|
|
std::cout << "Running update call with\n" << context.str() << std::endl;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Remove>
|
|
|
|
size_t remove(const Remove& x)
|
|
|
|
{
|
|
|
|
_serializer_context_t context;
|
|
|
|
::sqlpp::serialize(x, context);
|
|
|
|
std::cout << "Running remove call with\n" << context.str() << std::endl;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Select>
|
|
|
|
result_t select(const Select& x)
|
|
|
|
{
|
|
|
|
_serializer_context_t context;
|
|
|
|
::sqlpp::serialize(x, context);
|
|
|
|
std::cout << "Running select call with\n" << context.str() << std::endl;
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prepared statements start here
|
|
|
|
using _prepared_statement_t = std::nullptr_t;
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
auto _prepare(const T& t, const std::true_type&) -> decltype(t._prepare(*this))
|
|
|
|
{
|
|
|
|
return t._prepare(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2015-09-17 11:46:37 +02:00
|
|
|
auto _prepare(const T& t, const std::false_type&) -> void;
|
2015-09-13 21:33:19 +02:00
|
|
|
|
|
|
|
template <typename T>
|
2015-12-24 21:11:00 +01:00
|
|
|
auto prepare(const T& t) -> decltype(this->_prepare(t, sqlpp::prepare_check_t<_serializer_context_t, T>{}))
|
2015-09-13 21:33:19 +02:00
|
|
|
{
|
2015-09-17 11:46:37 +02:00
|
|
|
sqlpp::prepare_check_t<_serializer_context_t, T>::_();
|
2015-12-24 21:11:00 +01:00
|
|
|
return _prepare(t, sqlpp::prepare_check_t<_serializer_context_t, T>{});
|
2015-09-13 21:33:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Statement>
|
|
|
|
_prepared_statement_t prepare_execute(Statement& x)
|
|
|
|
{
|
|
|
|
_serializer_context_t context;
|
|
|
|
::sqlpp::serialize(x, context);
|
|
|
|
std::cout << "Running prepare execute call with\n" << context.str() << std::endl;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Insert>
|
|
|
|
_prepared_statement_t prepare_insert(Insert& x)
|
|
|
|
{
|
|
|
|
_serializer_context_t context;
|
|
|
|
::sqlpp::serialize(x, context);
|
|
|
|
std::cout << "Running prepare insert call with\n" << context.str() << std::endl;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename PreparedExecute>
|
|
|
|
size_t run_prepared_execute(const PreparedExecute&)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename PreparedInsert>
|
|
|
|
size_t run_prepared_insert(const PreparedInsert&)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Select>
|
|
|
|
_prepared_statement_t prepare_select(Select& x)
|
|
|
|
{
|
|
|
|
_serializer_context_t context;
|
|
|
|
::sqlpp::serialize(x, context);
|
|
|
|
std::cout << "Running prepare select call with\n" << context.str() << std::endl;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename PreparedSelect>
|
|
|
|
result_t run_prepared_select(PreparedSelect&)
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
auto attach(std::string name) -> ::sqlpp::schema_t
|
|
|
|
{
|
|
|
|
return {name};
|
|
|
|
}
|
2013-11-07 13:36:54 +01:00
|
|
|
};
|
|
|
|
|
2014-07-27 19:40:31 +02:00
|
|
|
using MockDb = MockDbT<false>;
|
|
|
|
using EnforceDb = MockDbT<true>;
|
|
|
|
|
2013-11-07 13:36:54 +01:00
|
|
|
#endif
|