0
0
mirror of https://github.com/rbock/sqlpp11.git synced 2024-11-15 20:31:16 +08:00

Started to switch from member serialize -> non-member interpret

This will allow database connectors to specialize the interpretation of
the expression tree and interpret queries in vendor specific ways where
required.
This commit is contained in:
rbock 2014-01-11 00:11:47 +01:00
parent 46b4ac349e
commit bef7cea6a6
9 changed files with 253 additions and 135 deletions

View File

@ -28,12 +28,13 @@
#define SQLPP_COLUMN_H
#include <ostream>
#include <sqlpp11/column_fwd.h>
#include <sqlpp11/expression.h>
#include <sqlpp11/alias.h>
#include <sqlpp11/column_fwd.h>
#include <sqlpp11/detail/wrong.h>
#include <sqlpp11/type_traits.h>
#include <sqlpp11/sort_order.h>
#include <sqlpp11/interpreter.h>
namespace sqlpp
{
@ -58,12 +59,6 @@ namespace sqlpp
column_t& operator=(column_t&&) = default;
~column_t() = default;
template<typename Db>
void serialize(std::ostream& os, Db& db) const
{
os << Table::_name_t::_get_name() << '.' << _name_t::_get_name();
}
template<typename Db>
void serialize_name(std::ostream& os, Db& db) const
{
@ -85,6 +80,17 @@ namespace sqlpp
}
};
template<typename Db, typename... Args>
struct interpreter_t<Db, column_t<Args...>>
{
using T = column_t<Args...>;
template<typename Context>
static void _(const T& t, Context& context)
{
context << T::_table::_name_t::_get_name() << '.' << T::_name_t::_get_name();
}
};
}
#endif

View File

@ -28,7 +28,9 @@
#define SQLPP_DETAIL_WRAP_OPERAND_H
#include <ostream>
#include <type_traits>
#include <sqlpp11/interpreter.h>
// FIXME: must leave detail, since it is interpreted (and might require specializations)
namespace sqlpp
{
@ -38,11 +40,12 @@ namespace sqlpp
struct integral;
struct floating_point;
struct text;
}
struct bool_operand
{
static constexpr bool _is_expression = true;
using _value_type = boolean;
using _value_type = detail::boolean;
bool_operand(bool t): _t(t) {}
bool_operand(const bool_operand&) = default;
@ -66,7 +69,7 @@ namespace sqlpp
struct integral_operand
{
static constexpr bool _is_expression = true;
using _value_type = integral;
using _value_type = detail::integral;
integral_operand(T t): _t(t) {}
integral_operand(const integral_operand&) = default;
@ -86,11 +89,23 @@ namespace sqlpp
T _t;
};
template<typename Db, typename T>
struct interpreter_t<Db, integral_operand<T>>
{
using Operand = integral_operand<T>;
template<typename Context>
static void _(const Operand& t, Context& context)
{
context << t._t;
}
};
template<typename T>
struct floating_point_operand
{
static constexpr bool _is_expression = true;
using _value_type = floating_point;
using _value_type = detail::floating_point;
floating_point_operand(T t): _t(t) {}
floating_point_operand(const floating_point_operand&) = default;
@ -114,7 +129,7 @@ namespace sqlpp
struct text_operand
{
static constexpr bool _is_expression = true;
using _value_type = text;
using _value_type = detail::text;
text_operand(const T& t): _t(t) {}
text_operand(const text_operand&) = default;
@ -164,6 +179,5 @@ namespace sqlpp
using type = text_operand<T>;
};
}
}
#endif

View File

@ -27,11 +27,11 @@
#ifndef SQLPP_EXPRESSION_H
#define SQLPP_EXPRESSION_H
#include <sqlpp11/detail/wrap_operand.h>
#include <sqlpp11/detail/serialize_tuple.h>
#include <sqlpp11/alias.h>
#include <sqlpp11/noop.h>
#include <sqlpp11/parameter_list.h> // FIXME: a forward for set_parameter_index would be nice here
#include <sqlpp11/interpreter.h>
#include <sqlpp11/detail/wrap_operand.h>
#include <sqlpp11/detail/serialize_tuple.h>
namespace sqlpp
{
@ -41,45 +41,36 @@ namespace sqlpp
using _is_assignment = std::true_type;
using column_type = Lhs;
using value_type = Rhs;
using _parameter_tuple_t = std::tuple<Lhs, Rhs>;
size_t _set_parameter_index(size_t index)
{
index = set_parameter_index(_lhs, index);
index = set_parameter_index(_rhs, index);
return index;
}
template<typename Db>
void serialize(std::ostream& os, Db& db) const
{
_lhs.serialize_name(os, db);
if (trivial_value_is_null_t<Lhs>::value and _rhs._is_trivial())
{
os << "=NULL";
}
else
{
os << "=";
_rhs.serialize(os, db);
}
}
Lhs _lhs;
Rhs _rhs;
};
template<typename Db, typename Lhs, typename Rhs>
struct interpreter_t<Db, assignment_t<Lhs, Rhs>>
{
using T = assignment_t<Lhs, Rhs>;
template<typename Context>
static void _(const T& t, Context& context)
{
interpret(t._lhs, context);
if (trivial_value_is_null_t<Lhs>::value and t._rhs._is_trivial())
{
context << "=NULL";
}
else
{
context << "=";
interpret(t._rhs, context);
}
}
};
template<typename Lhs, typename Rhs, typename ValueType = detail::boolean>
struct equal_t: public ValueType::template operators<equal_t<Lhs, Rhs>>
{
using _value_type = ValueType;
using _parameter_tuple_t = std::tuple<Lhs, Rhs>;
size_t _set_parameter_index(size_t index)
{
index = set_parameter_index(_lhs, index);
return set_parameter_index(_rhs, index);
}
using _value_type = ValueType; // FIXME: Can we use boolean directly here?
template<typename L, typename R>
equal_t(L&& l, R&& r):
@ -93,39 +84,37 @@ namespace sqlpp
equal_t& operator=(equal_t&&) = default;
~equal_t() = default;
template<typename Db>
void serialize(std::ostream& os, Db& db) const
{
os << "(";
_lhs.serialize(os, db);
if (trivial_value_is_null_t<Lhs>::value and _rhs._is_trivial())
{
os << " IS NULL";
}
else
{
os << "=";
_rhs.serialize(os, db);
}
os << ")";
}
private:
Lhs _lhs;
Rhs _rhs;
};
template<typename Db, typename... Args>
struct interpreter_t<Db, equal_t<Args...>>
{
using T = equal_t<Args...>;
template<typename Context>
static void interpret(const T& t, Context& context)
{
context << "(";
interpret(t._lhs, context);
if (trivial_value_is_null_t<typename T::Lhs>::value and t._rhs._is_trivial())
{
context << "IS NULL";
}
else
{
context << "=";
interpret(t._rhs, context);
}
context << ")";
}
};
template<typename Lhs, typename Rhs, typename ValueType = detail::boolean>
struct not_equal_t: public ValueType::template operators<not_equal_t<Lhs, Rhs>>
{
using _value_type = ValueType;
using _parameter_tuple_t = std::tuple<Lhs, Rhs>;
size_t _set_parameter_index(size_t index)
{
index = set_parameter_index(_lhs, index);
return set_parameter_index(_rhs, index);
}
template<typename L, typename R>
not_equal_t(L&& l, R&& r):
@ -139,38 +128,36 @@ namespace sqlpp
not_equal_t& operator=(not_equal_t&&) = default;
~not_equal_t() = default;
template<typename Db>
void serialize(std::ostream& os, Db& db) const
Lhs _lhs;
Rhs _rhs;
};
template<typename Db, typename... Args>
struct interpreter_t<Db, not_equal_t<Args...>>
{
os << "(";
_lhs.serialize(os, db);
if (trivial_value_is_null_t<Lhs>::value and _rhs._is_trivial())
using T = not_equal_t<Args...>;
template<typename Context>
static void interpret(const T& t, Context& context)
{
os << " IS NOT NULL";
context << "(";
interpret(t._lhs, context);
if (trivial_value_is_null_t<typename T::Lhs>::value and t._rhs._is_trivial())
{
context << "IS NOT NULL";
}
else
{
os << "!=";
_rhs.serialize(os, db);
context << "!=";
interpret(t._rhs, context);
}
os << ")";
context << ")";
}
private:
Lhs _lhs;
Rhs _rhs;
};
template<typename Lhs, typename ValueType = detail::boolean>
struct not_t: public ValueType::template operators<not_t<Lhs>>
{
using _value_type = ValueType;
using _parameter_tuple_t = std::tuple<Lhs>;
size_t _set_parameter_index(size_t index)
{
return set_parameter_index(_lhs, index);
}
not_t(Lhs l):
_lhs(l)
@ -182,47 +169,39 @@ namespace sqlpp
not_t& operator=(not_t&&) = default;
~not_t() = default;
template<typename Db>
void serialize(std::ostream& os, Db& db) const
Lhs _lhs;
};
template<typename Db, typename... Args>
struct interpreter_t<Db, not_t<Args...>>
{
os << "(";
if (trivial_value_is_null_t<Lhs>::value and _lhs._is_trivial())
using T = not_t<Args...>;
template<typename Context>
static void interpret(const T& t, Context& context)
{
_lhs.serialize(os, db);
os << " IS NULL";
context << "(";
if (trivial_value_is_null_t<typename T::Lhs>::value and t._lhs._is_trivial())
{
interpret(t._lhs, context);
context << "IS NULL";
}
else
{
os << "NOT";
_lhs.serialize(os, db);
context << "NOT ";
interpret(t._lhs, context);
}
os << ")";
context << ")";
}
private:
Lhs _lhs;
};
template<typename Lhs, typename O, typename Rhs>
struct binary_expression_t: public O::_value_type::template operators<binary_expression_t<Lhs, O, Rhs>>
{
using _value_type = typename O::_value_type;
using _parameter_tuple_t = std::tuple<Lhs, Rhs>;
size_t _set_parameter_index(size_t index)
{
index = set_parameter_index(_lhs, index);
return set_parameter_index(_rhs, index);
}
binary_expression_t(Lhs&& l, Rhs&& r):
_lhs(std::move(l)),
_rhs(std::move(r))
{}
binary_expression_t(const Lhs& l, const Rhs& r):
_lhs(l),
_rhs(r)
binary_expression_t(Lhs lhs, Rhs rhs):
_lhs(lhs),
_rhs(rhs)
{}
binary_expression_t(const binary_expression_t&) = default;
@ -231,21 +210,25 @@ namespace sqlpp
binary_expression_t& operator=(binary_expression_t&&) = default;
~binary_expression_t() = default;
template<typename Db>
void serialize(std::ostream& os, Db& db) const
{
os << "(";
_lhs.serialize(os, db);
os << O::_name;
_rhs.serialize(os, db);
os << ")";
}
private:
Lhs _lhs;
Rhs _rhs;
};
template<typename Db, typename... Args>
struct interpreter_t<Db, binary_expression_t<Args...>>
{
using T = binary_expression_t<Args...>;
template<typename Context>
static void interpret(const T& t, Context& context)
{
context << "(";
interpret(t._lhs, context);
context << T::O::_name;
interpret(t._rhs, context);
context << ")";
}
};
}
#endif

View File

@ -0,0 +1,54 @@
/*
* Copyright (c) 2013, Roland Bock
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice, this
* list of conditions and the following disclaimer in the documentation and/or
* other materials provided with the distribution.
*
* 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 OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef SQLPP_INTERPRETER_H
#define SQLPP_INTERPRETER_H
#include <type_traits>
#include <sqlpp11/detail/wrong.h>
namespace sqlpp
{
template<typename Db, typename T>
struct interpreter_t
{
template<typename Context>
static void _(const T& t, Context& context)
{
static_assert(detail::wrong<Db, T>::value, "missing interpreter specialization");
}
};
template<typename Context, typename T>
void interpret(const T& t, Context& context)
{
using Db = typename std::decay<Context>::type::_database_t;
interpreter_t<Db, typename std::decay<T>::type>::_(t, context);
}
}
#endif

View File

@ -42,6 +42,7 @@
#include <sqlpp11/expression.h>
#include <sqlpp11/parameter_list.h>
#include <sqlpp11/prepared_select.h>
#include <sqlpp11/interpreter.h>
#include <sqlpp11/detail/wrong.h>
#include <sqlpp11/detail/make_flag_tuple.h>

View File

@ -126,7 +126,7 @@ namespace sqlpp
template<typename T, template<typename> class IsCorrectType>
struct operand_t
{
using type = typename detail::wrap_operand<typename std::decay<T>::type>::type;
using type = typename wrap_operand<typename std::decay<T>::type>::type;
static_assert(not is_alias_t<type>::value, "expression operand must not be an alias");
static_assert(is_expression_t<type>::value, "expression required");
static_assert(IsCorrectType<type>::value, "invalid operand type");

View File

@ -3,10 +3,11 @@ macro (build_and_run arg)
add_test(${arg} ${arg})
endmacro ()
build_and_run(InsertTest)
build_and_run(RemoveTest)
build_and_run(UpdateTest)
build_and_run(SelectTest)
build_and_run(FunctionTest)
build_and_run(PreparedTest)
build_and_run(InterpretTest)
#build_and_run(InsertTest)
#build_and_run(RemoveTest)
#build_and_run(UpdateTest)
#build_and_run(SelectTest)
#build_and_run(FunctionTest)
#build_and_run(PreparedTest)

43
tests/InterpretTest.cpp Normal file
View File

@ -0,0 +1,43 @@
/*
* Copyright (c) 2013, Roland Bock
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 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
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "TabSample.h"
#include "MockDb.h"
#include <iostream>
DbMock db = {};
DbMock::Printer printer(std::cerr);
SQLPP_ALIAS_PROVIDER_GENERATOR(kaesekuchen);
int main()
{
TabSample t;
interpret(t.alpha, printer);
interpret(t.alpha = 7, printer);
return 0;
}

View File

@ -31,6 +31,22 @@
class DbMock: public sqlpp::connection
{
public:
struct Printer
{
using _database_t = DbMock;
Printer(std::ostream& os):
_os(os)
{}
template<typename T>
std::ostream& operator<<(const T& t)
{
return _os << t;
}
std::ostream& _os;
};
// join types
static constexpr bool _supports_inner_join = true;
static constexpr bool _supports_outer_join = true;