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

Turned value_type::parameter_t into a non-template

This commit is contained in:
Roland Bock 2014-01-02 13:11:19 +01:00
parent 9d0746b15c
commit 1360b1d9db
13 changed files with 124 additions and 49 deletions

View File

@ -59,25 +59,31 @@ namespace sqlpp
using _is_expression = std::true_type; using _is_expression = std::true_type;
using _cpp_value_type = bool; using _cpp_value_type = bool;
template<bool TrivialValueIsNull>
struct _parameter_t struct _parameter_t
{ {
using _value_type = boolean; using _value_type = boolean;
_parameter_t(): _parameter_t(const std::true_type&):
_trivial_value_is_null(true),
_value(false), _value(false),
_is_null(TrivialValueIsNull and _is_trivial()) _is_null(_trivial_value_is_null and _is_trivial())
{}
_parameter_t(const std::false_type&):
_trivial_value_is_null(false),
_value(false),
_is_null(_trivial_value_is_null and _is_trivial())
{} {}
_parameter_t(const _cpp_value_type& value): _parameter_t(const _cpp_value_type& value):
_value(value), _value(value),
_is_null(TrivialValueIsNull and _is_trivial()) _is_null(_trivial_value_is_null and _is_trivial())
{} {}
_parameter_t& operator=(const _cpp_value_type& value) _parameter_t& operator=(const _cpp_value_type& value)
{ {
_value = value; _value = value;
_is_null = (TrivialValueIsNull and _is_trivial()); _is_null = (_trivial_value_is_null and _is_trivial());
return *this; return *this;
} }
@ -109,6 +115,7 @@ namespace sqlpp
operator _cpp_value_type() const { return value(); } operator _cpp_value_type() const { return value(); }
private: private:
bool _trivial_value_is_null;
_cpp_value_type _value; _cpp_value_type _value;
bool _is_null; bool _is_null;
}; };

View File

@ -40,7 +40,7 @@ namespace sqlpp
using _is_assignment = std::true_type; using _is_assignment = std::true_type;
using column_type = Lhs; using column_type = Lhs;
using value_type = Rhs; using value_type = Rhs;
using _parameter_t = std::tuple<Lhs, Rhs>; using _parameter_tuple_t = std::tuple<Lhs, Rhs>;
size_t _set_parameter_index(size_t index) size_t _set_parameter_index(size_t index)
{ {
@ -71,7 +71,7 @@ namespace sqlpp
struct equal_t: public ValueType::template operators<equal_t<Lhs, Rhs>> struct equal_t: public ValueType::template operators<equal_t<Lhs, Rhs>>
{ {
using _value_type = ValueType; using _value_type = ValueType;
using _parameter_t = std::tuple<Lhs, Rhs>; using _parameter_tuple_t = std::tuple<Lhs, Rhs>;
size_t _set_parameter_index(size_t index) size_t _set_parameter_index(size_t index)
{ {
@ -117,7 +117,7 @@ namespace sqlpp
struct not_equal_t: public ValueType::template operators<not_equal_t<Lhs, Rhs>> struct not_equal_t: public ValueType::template operators<not_equal_t<Lhs, Rhs>>
{ {
using _value_type = ValueType; using _value_type = ValueType;
using _parameter_t = std::tuple<Lhs, Rhs>; using _parameter_tuple_t = std::tuple<Lhs, Rhs>;
size_t _set_parameter_index(size_t index) size_t _set_parameter_index(size_t index)
{ {
@ -163,7 +163,7 @@ namespace sqlpp
struct not_t: public ValueType::template operators<not_t<Lhs>> struct not_t: public ValueType::template operators<not_t<Lhs>>
{ {
using _value_type = ValueType; using _value_type = ValueType;
using _parameter_t = std::tuple<Lhs>; using _parameter_tuple_t = std::tuple<Lhs>;
size_t _set_parameter_index(size_t index) size_t _set_parameter_index(size_t index)
{ {
@ -205,7 +205,7 @@ namespace sqlpp
struct binary_expression_t: public O::_value_type::template operators<binary_expression_t<Lhs, O, 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 _value_type = typename O::_value_type;
using _parameter_t = std::tuple<Lhs, Rhs>; using _parameter_tuple_t = std::tuple<Lhs, Rhs>;
size_t _set_parameter_index(size_t index) size_t _set_parameter_index(size_t index)
{ {

View File

@ -70,8 +70,8 @@ namespace sqlpp
return set_parameter_index(_expressions, index); return set_parameter_index(_expressions, index);
} }
using _parameter_t = std::tuple<Expr...>; using _parameter_tuple_t = std::tuple<Expr...>;
_parameter_t _expressions; _parameter_tuple_t _expressions; // FIXME: Do we need those?
detail::serializable_list<Database> _dynamic_expressions; detail::serializable_list<Database> _dynamic_expressions;
}; };

View File

@ -48,25 +48,37 @@ namespace sqlpp
using _is_expression = std::true_type; using _is_expression = std::true_type;
using _cpp_value_type = int64_t; using _cpp_value_type = int64_t;
template<bool TrivialValueIsNull>
struct _parameter_t struct _parameter_t
{ {
using _value_type = integral; using _value_type = integral;
_parameter_t(): _parameter_t(const std::true_type&):
_trivial_value_is_null(true),
_value(0), _value(0),
_is_null(TrivialValueIsNull and _is_trivial()) _is_null(_trivial_value_is_null and _is_trivial())
{}
_parameter_t(const std::false_type&):
_trivial_value_is_null(false),
_value(0),
_is_null(_trivial_value_is_null and _is_trivial())
{}
_parameter_t(bool trivial_value_is_null):
_trivial_value_is_null(trivial_value_is_null),
_value(0),
_is_null(_trivial_value_is_null and _is_trivial())
{} {}
_parameter_t(const _cpp_value_type& value): _parameter_t(const _cpp_value_type& value):
_value(value), _value(value),
_is_null(TrivialValueIsNull and _is_trivial()) _is_null(_trivial_value_is_null and _is_trivial())
{} {}
_parameter_t& operator=(const _cpp_value_type& value) _parameter_t& operator=(const _cpp_value_type& value)
{ {
_value = value; _value = value;
_is_null = (TrivialValueIsNull and _is_trivial()); _is_null = (_trivial_value_is_null and _is_trivial());
return *this; return *this;
} }
@ -90,14 +102,21 @@ namespace sqlpp
return _is_null; return _is_null;
} }
const _cpp_value_type* value() const const _cpp_value_type& value() const
{ {
return &_value; return _value;
} }
operator _cpp_value_type() const { return _value; } operator _cpp_value_type() const { return _value; }
template<typename Target>
void bind(Target& target, size_t index) const
{
target.bind_integral_parameter(index, &_value, _is_null);
}
private: private:
bool _trivial_value_is_null;
_cpp_value_type _value; _cpp_value_type _value;
bool _is_null; bool _is_null;
}; };
@ -151,6 +170,12 @@ namespace sqlpp
operator _cpp_value_type() const { return value(); } operator _cpp_value_type() const { return value(); }
template<typename Target>
void bind(Target& target, size_t i) const // Hint: Cannot use index here because of dynamic result rows which can use index==0 for several fields
{
target.bind_integral_result(i, &_value, _is_null);
}
private: private:
bool _is_valid; bool _is_valid;
bool _is_null; bool _is_null;

View File

@ -41,7 +41,7 @@ namespace sqlpp
{ {
static_assert(is_text_t<Operand>::value, "Operand for like() has to be a text"); static_assert(is_text_t<Operand>::value, "Operand for like() has to be a text");
static_assert(is_text_t<Pattern>::value, "Pattern for like() has to be a text"); static_assert(is_text_t<Pattern>::value, "Pattern for like() has to be a text");
using _parameter_t = std::tuple<ValueType, Pattern>; using _parameter_tuple_t = std::tuple<ValueType, Pattern>;
struct _value_type: public ValueType::_base_value_type // we require fully defined boolean here struct _value_type: public ValueType::_base_value_type // we require fully defined boolean here
{ {

View File

@ -33,14 +33,16 @@
namespace sqlpp namespace sqlpp
{ {
template<typename ValueType, typename NameType, bool TrivialValueIsNull> template<typename ValueType, typename NameType, typename TrivialValueIsNull>
struct parameter_t struct parameter_t
{ {
using _value_type = ValueType; using _value_type = ValueType;
using _parameter_type = typename ValueType::template _parameter_t<TrivialValueIsNull>;
using _is_parameter = std::true_type; using _is_parameter = std::true_type;
using _is_expression_t = std::true_type; using _is_expression_t = std::true_type;
using _member_t = typename NameType::_name_t::template _member_t<_parameter_type>; using _instance_t = typename NameType::_name_t::template _member_t<typename ValueType::_parameter_t>;
using _trivial_value_is_null = TrivialValueIsNull;
static_assert(std::is_same<_trivial_value_is_null, std::true_type>::value or std::is_same<_trivial_value_is_null, std::false_type>::value, "Invalid template parameter TrivialValueIsNull");
template<typename Db> template<typename Db>
void serialize(std::ostream& os, Db& db) const void serialize(std::ostream& os, Db& db) const
@ -61,7 +63,7 @@ namespace sqlpp
size_t index; size_t index;
}; };
template<typename NamedExpr, bool TrivialValueIsNull = trivial_value_is_null_t<typename std::decay<NamedExpr>::type>::value> template<typename NamedExpr, typename TrivialValueIsNull = trivial_value_is_null_t<typename std::decay<NamedExpr>::type>>
auto parameter(NamedExpr&& namedExpr) auto parameter(NamedExpr&& namedExpr)
-> parameter_t<typename std::decay<NamedExpr>::type::_value_type, typename std::decay<NamedExpr>::type, TrivialValueIsNull> -> parameter_t<typename std::decay<NamedExpr>::type::_value_type, typename std::decay<NamedExpr>::type, TrivialValueIsNull>
{ {

View File

@ -39,11 +39,15 @@ namespace sqlpp
}; };
template<typename... Parameter> template<typename... Parameter>
struct parameter_list_t<std::tuple<Parameter...>>: public Parameter::_member_t... struct parameter_list_t<std::tuple<Parameter...>>: public Parameter::_instance_t...
{ {
using _member_tuple_t = std::tuple<typename Parameter::_member_t...>; using _member_tuple_t = std::tuple<typename Parameter::_instance_t...>;
using size = std::integral_constant<std::size_t, sizeof...(Parameter)>; using size = std::integral_constant<std::size_t, sizeof...(Parameter)>;
parameter_list_t():
Parameter::_instance_t({typename Parameter::_trivial_value_is_null()})...
{}
template<typename Target> template<typename Target>
void _bind(Target& target) const void _bind(Target& target) const
{ {
@ -56,7 +60,8 @@ namespace sqlpp
template<typename Target, size_t index> template<typename Target, size_t index>
void _bind_impl(Target& target, const index_t<index>&) const void _bind_impl(Target& target, const index_t<index>&) const
{ {
target.bind_param(index, static_cast<typename std::tuple_element<index, const _member_tuple_t>::type&>(*this)()); const auto& parameter = static_cast<typename std::tuple_element<index, const _member_tuple_t>::type&>(*this)();
parameter.bind(target, index);
_bind_impl(target, index_t<index + 1>()); _bind_impl(target, index_t<index + 1>());
} }
@ -88,9 +93,9 @@ namespace sqlpp
}; };
template<typename Exp> template<typename Exp>
struct get_parameter_tuple<Exp, typename std::enable_if<not std::is_same<typename Exp::_parameter_t, void>::value, void>::type> struct get_parameter_tuple<Exp, typename std::enable_if<not std::is_same<typename Exp::_parameter_tuple_t, void>::value, void>::type>
{ {
using type = typename get_parameter_tuple<typename Exp::_parameter_t>::type; using type = typename get_parameter_tuple<typename Exp::_parameter_tuple_t>::type;
}; };
} }

View File

@ -37,6 +37,8 @@ namespace sqlpp
{ {
namespace detail namespace detail
{ {
template<size_t> struct index_t {}; // this is just for overloading
template<size_t level, size_t index, typename... NamedExpr> template<size_t level, size_t index, typename... NamedExpr>
struct result_row_impl; struct result_row_impl;
@ -60,14 +62,22 @@ namespace sqlpp
_rest::operator=(raw_result_row); _rest::operator=(raw_result_row);
return *this; return *this;
} }
template<typename Target>
void _bind(Target& target)
{
_field::operator().bind(target, index);
std::cerr << "binding result " << index << std::endl;
_rest::_bind(target);
}
}; };
template<size_t level, size_t index, typename AliasProvider, typename... Col, typename... Rest> template<size_t level, size_t index, typename AliasProvider, typename... Col, typename... Rest>
struct result_row_impl<level, index, multi_field_t<AliasProvider, std::tuple<Col...>>, Rest...>: struct result_row_impl<level, index, multi_field_t<AliasProvider, std::tuple<Col...>>, Rest...>:
public AliasProvider::_name_t::template _member_t<result_row_impl<level + 1, index, Col...>>, // level prevents identical closures to be present twice in the inheritance tree public AliasProvider::_name_t::template _member_t<result_row_impl<level, index, Col...>>, // level prevents identical closures to be present twice in the inheritance tree
public result_row_impl<level, index + sizeof...(Col), Rest...> public result_row_impl<level, index + sizeof...(Col), Rest...>
{ {
using _multi_field = typename AliasProvider::_name_t::template _member_t<result_row_impl<level + 1, index, Col...>>; using _multi_field = typename AliasProvider::_name_t::template _member_t<result_row_impl<level, index, Col...>>;
using _rest = result_row_impl<level, index + sizeof...(Col), Rest...>; using _rest = result_row_impl<level, index + sizeof...(Col), Rest...>;
static constexpr size_t _last_index = _rest::_last_index; static constexpr size_t _last_index = _rest::_last_index;
@ -82,6 +92,13 @@ namespace sqlpp
_rest::operator=(raw_result_row); _rest::operator=(raw_result_row);
return *this; return *this;
} }
template<typename Target>
void _bind(const Target& target)
{
_multi_field::_bind(target);
_rest::_bind(target);
}
}; };
template<size_t level, size_t index> template<size_t level, size_t index>
@ -95,6 +112,11 @@ namespace sqlpp
{ {
return *this; return *this;
} }
template<typename Target>
void _bind(Target& target)
{
}
}; };
} }
@ -104,6 +126,7 @@ namespace sqlpp
using _impl = detail::result_row_impl<0, 0, NamedExpr...>; using _impl = detail::result_row_impl<0, 0, NamedExpr...>;
bool _is_row; bool _is_row;
raw_result_row_t _raw_result_row; raw_result_row_t _raw_result_row;
static constexpr size_t _last_static_index = _impl::_last_index;
result_row_t(): result_row_t():
_impl({}), _impl({}),
@ -145,8 +168,14 @@ namespace sqlpp
static constexpr size_t static_size() static constexpr size_t static_size()
{ {
return sizeof...(NamedExpr); return _last_static_index;
} }
template<typename Target>
void bind_result(Target& target)
{
_impl::_bind(target);
}
}; };
template<typename... NamedExpr> template<typename... NamedExpr>

View File

@ -617,7 +617,7 @@ namespace sqlpp
OrderBy _order_by; OrderBy _order_by;
Limit _limit; Limit _limit;
Offset _offset; Offset _offset;
using _parameter_t = std::tuple<Where, Having>; using _parameter_tuple_t = std::tuple<Where, Having>;
using _parameter_list_t = typename make_parameter_list_t<select_t>::type; using _parameter_list_t = typename make_parameter_list_t<select_t>::type;
}; };

View File

@ -48,25 +48,31 @@ namespace sqlpp
using _is_expression = std::true_type; using _is_expression = std::true_type;
using _cpp_value_type = std::string; using _cpp_value_type = std::string;
template<bool TrivialValueIsNull>
struct _parameter_t struct _parameter_t
{ {
using _value_type = integral; using _value_type = integral;
_parameter_t(): _parameter_t(const std::true_type&):
_trivial_value_is_null(true),
_value(""), _value(""),
_is_null(TrivialValueIsNull and _is_trivial()) _is_null(_trivial_value_is_null and _is_trivial())
{}
_parameter_t(const std::false_type&):
_trivial_value_is_null(false),
_value(""),
_is_null(_trivial_value_is_null and _is_trivial())
{} {}
_parameter_t(const _cpp_value_type& value): _parameter_t(const _cpp_value_type& value):
_value(value), _value(value),
_is_null(TrivialValueIsNull and _is_trivial()) _is_null(_trivial_value_is_null and _is_trivial())
{} {}
_parameter_t& operator=(const _cpp_value_type& value) _parameter_t& operator=(const _cpp_value_type& value)
{ {
_value = value; _value = value;
_is_null = (TrivialValueIsNull and _is_trivial()); _is_null = (_trivial_value_is_null and _is_trivial());
return *this; return *this;
} }
@ -98,6 +104,7 @@ namespace sqlpp
operator _cpp_value_type() const { return value(); } operator _cpp_value_type() const { return value(); }
private: private:
bool _trivial_value_is_null;
_cpp_value_type _value; _cpp_value_type _value;
bool _is_null; bool _is_null;
}; };

View File

@ -47,12 +47,12 @@ namespace sqlpp
namespace detail\ namespace detail\
{\ {\
template<typename T, typename Enable = void>\ template<typename T, typename Enable = void>\
struct name##_impl: std::false_type {};\ struct name##_impl { using type = std::false_type; };\
template<typename T>\ template<typename T>\
struct name##_impl<T, typename std::enable_if<std::is_same<typename T::_column_type::_##name, std::true_type>::value>::type>: std::true_type {};\ struct name##_impl<T, typename std::enable_if<std::is_same<typename T::_column_type::_##name, std::true_type>::value>::type> { using type = std::true_type; };\
}\ }\
template<typename T>\ template<typename T>\
struct name##_t: detail::name##_impl<T> {}; using name##_t = typename detail::name##_impl<T>::type;
#define SQLPP_TYPE_TRAIT_GENERATOR(name) \ #define SQLPP_TYPE_TRAIT_GENERATOR(name) \
namespace detail\ namespace detail\

View File

@ -69,8 +69,8 @@ namespace sqlpp
return set_parameter_index(_expressions, index); return set_parameter_index(_expressions, index);
} }
using _parameter_t = std::tuple<Expr...>; using _parameter_tuple_t = std::tuple<Expr...>;
_parameter_t _expressions; _parameter_tuple_t _expressions; // FIXME: Do we need those?
detail::serializable_list<Database> _dynamic_expressions; detail::serializable_list<Database> _dynamic_expressions;
}; };
} }

View File

@ -80,9 +80,9 @@ int main()
using Exp = decltype(t.beta.like(parameter(t.beta)) and t.alpha == parameter(t.alpha) or t.gamma != parameter(t.gamma)); using Exp = decltype(t.beta.like(parameter(t.beta)) and t.alpha == parameter(t.alpha) or t.gamma != parameter(t.gamma));
using T = sqlpp::make_parameter_list_t<Exp>::type; using T = sqlpp::make_parameter_list_t<Exp>::type;
T npl; T npl;
static_assert(std::is_same<typename decltype(t.alpha)::_value_type::_parameter_t<true>, decltype(npl.alpha)>::value, "type requirement"); static_assert(std::is_same<typename decltype(t.alpha)::_value_type::_parameter_t, decltype(npl.alpha)>::value, "type requirement");
static_assert(std::is_same<typename decltype(t.beta)::_value_type::_parameter_t<true>, decltype(npl.beta)>::value, "type requirement"); static_assert(std::is_same<typename decltype(t.beta)::_value_type::_parameter_t, decltype(npl.beta)>::value, "type requirement");
static_assert(std::is_same<typename decltype(t.gamma)::_value_type::_parameter_t<false>, decltype(npl.gamma)>::value, "type requirement"); static_assert(std::is_same<typename decltype(t.gamma)::_value_type::_parameter_t, decltype(npl.gamma)>::value, "type requirement");
} }
// Wonderful, now take a look at the parameter list of a select // Wonderful, now take a look at the parameter list of a select
@ -92,9 +92,9 @@ int main()
using T = sqlpp::make_parameter_list_t<S>::type; using T = sqlpp::make_parameter_list_t<S>::type;
T npl; T npl;
static_assert(std::is_same<typename decltype(t.alpha)::_value_type::_parameter_t<true>, decltype(npl.alpha)>::value, "type requirement"); static_assert(std::is_same<typename decltype(t.alpha)::_value_type::_parameter_t, decltype(npl.alpha)>::value, "type requirement");
static_assert(std::is_same<typename decltype(t.beta)::_value_type::_parameter_t<true>, decltype(npl.beta)>::value, "type requirement"); static_assert(std::is_same<typename decltype(t.beta)::_value_type::_parameter_t, decltype(npl.beta)>::value, "type requirement");
static_assert(std::is_same<typename decltype(t.gamma)::_value_type::_parameter_t<false>, decltype(npl.gamma)>::value, "type requirement"); static_assert(std::is_same<typename decltype(t.gamma)::_value_type::_parameter_t, decltype(npl.gamma)>::value, "type requirement");
npl.alpha = 7; npl.alpha = 7;
auto x = npl; auto x = npl;
x = npl; x = npl;