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

Implemented parameter index determination

This commit is contained in:
Roland Bock 2014-01-05 19:25:52 +01:00
parent 51e0db883f
commit b8907df4ef
5 changed files with 96 additions and 8 deletions

View File

@ -31,6 +31,7 @@
#include <sqlpp11/detail/serialize_tuple.h> #include <sqlpp11/detail/serialize_tuple.h>
#include <sqlpp11/alias.h> #include <sqlpp11/alias.h>
#include <sqlpp11/noop.h> #include <sqlpp11/noop.h>
#include <sqlpp11/parameter_list.h> // FIXME: a forward for set_parameter_index would be nice here
namespace sqlpp namespace sqlpp
{ {
@ -45,7 +46,8 @@ namespace sqlpp
size_t _set_parameter_index(size_t index) size_t _set_parameter_index(size_t index)
{ {
index = set_parameter_index(_lhs, index); index = set_parameter_index(_lhs, index);
return set_parameter_index(_rhs, index); index = set_parameter_index(_rhs, index);
return index;
} }
template<typename Db> template<typename Db>

View File

@ -74,6 +74,13 @@ namespace sqlpp
like_t& operator=(like_t&&) = default; like_t& operator=(like_t&&) = default;
~like_t() = default; ~like_t() = default;
size_t _set_parameter_index(size_t index)
{
index = set_parameter_index(_operand, index);
index = set_parameter_index(_pattern, index);
return index;
}
template<typename Db> template<typename Db>
void serialize(std::ostream& os, Db& db) const void serialize(std::ostream& os, Db& db) const
{ {

View File

@ -52,7 +52,7 @@ namespace sqlpp
if (Db::_use_questionmark_parameter) if (Db::_use_questionmark_parameter)
os << '?'; os << '?';
else if (Db::_use_positional_dollar_parameter) else if (Db::_use_positional_dollar_parameter)
os << '$' << index + 1; os << '$' << _index + 1;
} }
constexpr bool _is_trivial() const constexpr bool _is_trivial() const
@ -60,7 +60,13 @@ namespace sqlpp
return false; return false;
} }
size_t index; size_t _set_parameter_index(size_t index)
{
_index = index;
return index + 1;
}
size_t _index;
}; };
template<typename NamedExpr, typename TrivialValueIsNull = trivial_value_is_null_t<typename std::decay<NamedExpr>::type>> template<typename NamedExpr, typename TrivialValueIsNull = trivial_value_is_null_t<typename std::decay<NamedExpr>::type>>

View File

@ -126,6 +126,70 @@ namespace sqlpp
using type = parameter_list_t<typename detail::get_parameter_tuple<typename std::decay<Exp>::type>::type>; using type = parameter_list_t<typename detail::get_parameter_tuple<typename std::decay<Exp>::type>::type>;
}; };
template<typename T>
size_t set_parameter_index(T& t, size_t index);
namespace detail
{
template<typename Exp, typename Enable = void>
struct set_parameter_index_t
{
size_t operator()(Exp& e, size_t index)
{
return index;
}
};
template<typename Exp>
struct set_parameter_index_t<Exp, typename std::enable_if<is_parameter_t<Exp>::value, void>::type>
{
size_t operator()(Exp& e, size_t index)
{
return e._set_parameter_index(index);
}
};
template<typename... Param>
struct set_parameter_index_t<std::tuple<Param...>, void>
{
template<size_t> struct type{};
size_t operator()(std::tuple<Param...>& t, size_t index)
{
return impl(t, index, type<0>());
}
private:
template<size_t pos>
size_t impl(std::tuple<Param...>& t, size_t index, const type<pos>&)
{
index = sqlpp::set_parameter_index(std::get<pos>(t), index);
return impl(t, index, type<pos + 1>());
}
size_t impl(std::tuple<Param...>& t, size_t index, const type<sizeof...(Param)>&)
{
return index;
}
};
template<typename Exp>
struct set_parameter_index_t<Exp, typename std::enable_if<not std::is_same<typename Exp::_parameter_tuple_t, void>::value, void>::type>
{
size_t operator()(Exp& e, size_t index)
{
return e._set_parameter_index(index);
}
};
}
template<typename T>
size_t set_parameter_index(T& t, size_t index)
{
return detail::set_parameter_index_t<T>()(t, index);
}
} }
#endif #endif

View File

@ -111,6 +111,8 @@ namespace sqlpp
using _result_row_t = typename ExpressionList::_result_row_t; using _result_row_t = typename ExpressionList::_result_row_t;
using _dynamic_names_t = typename ExpressionList::_dynamic_names_t; using _dynamic_names_t = typename ExpressionList::_dynamic_names_t;
using _parameter_tuple_t = std::tuple<Where, Having>;
using _parameter_list_t = typename make_parameter_list_t<select_t>::type;
// Indicators // Indicators
using _value_type = typename std::conditional< using _value_type = typename std::conditional<
@ -127,6 +129,7 @@ namespace sqlpp
{ {
static_assert(std::is_same<select_t, sqlpp::select_t<Database, Flags, ExpressionList>>::value, static_assert(std::is_same<select_t, sqlpp::select_t<Database, Flags, ExpressionList>>::value,
"basic constructor only available for select_t<Flags, ExpressionList> (default template parameters)"); "basic constructor only available for select_t<Flags, ExpressionList> (default template parameters)");
_set_parameter_index(0);
} }
select_t(const select_t& rhs) = default; select_t(const select_t& rhs) = default;
@ -137,7 +140,7 @@ namespace sqlpp
// Other constructors // Other constructors
constexpr select_t(Flags&& flags, ExpressionList&& expression_list, From&& from, select_t(Flags&& flags, ExpressionList&& expression_list, From&& from,
Where&& where, GroupBy&& group_by, Having&& having, Where&& where, GroupBy&& group_by, Having&& having,
OrderBy&& order_by, Limit&& limit, Offset&& offset): OrderBy&& order_by, Limit&& limit, Offset&& offset):
_flags(std::move(flags)), _flags(std::move(flags)),
@ -150,10 +153,10 @@ namespace sqlpp
_limit(std::move(limit)), _limit(std::move(limit)),
_offset(std::move(offset)) _offset(std::move(offset))
{ {
// FIXME: Need to calculate parameter positions here and in other constructors _set_parameter_index(0);
} }
constexpr select_t(const Flags& flags, const ExpressionList& expression_list, const From& from, select_t(const Flags& flags, const ExpressionList& expression_list, const From& from,
const Where& where, const GroupBy& group_by, const Having& having, const Where& where, const GroupBy& group_by, const Having& having,
const OrderBy& order_by, const Limit& limit, const Offset& offset): const OrderBy& order_by, const Limit& limit, const Offset& offset):
_flags(flags), _flags(flags),
@ -166,6 +169,7 @@ namespace sqlpp
_limit(limit), _limit(limit),
_offset(offset) _offset(offset)
{ {
_set_parameter_index(0);
} }
auto dynamic_columns() auto dynamic_columns()
@ -609,6 +613,13 @@ namespace sqlpp
return {{}, get_dynamic_names(), db.prepare_select(*this)}; return {{}, get_dynamic_names(), db.prepare_select(*this)};
} }
size_t _set_parameter_index(size_t index)
{
index = set_parameter_index(_where, index);
index = set_parameter_index(_having, index);
return index;
}
Flags _flags; Flags _flags;
ExpressionList _expression_list; ExpressionList _expression_list;
From _from; From _from;
@ -618,8 +629,6 @@ namespace sqlpp
OrderBy _order_by; OrderBy _order_by;
Limit _limit; Limit _limit;
Offset _offset; Offset _offset;
using _parameter_tuple_t = std::tuple<Where, Having>;
using _parameter_list_t = typename make_parameter_list_t<select_t>::type;
}; };
// construct select flag list // construct select flag list