From b031bda5fcc386930f2cd38855677834f0078714 Mon Sep 17 00:00:00 2001 From: Roland Bock Date: Sat, 28 Dec 2013 22:52:54 +0100 Subject: [PATCH] Can now prepare a select (tested with mysql) --- include/sqlpp11/alias.h | 8 ++----- include/sqlpp11/expression.h | 29 ++++++++++++++++++++++++ include/sqlpp11/from.h | 2 +- include/sqlpp11/having.h | 5 +++++ include/sqlpp11/like.h | 2 +- include/sqlpp11/parameter.h | 16 ++++++++++--- include/sqlpp11/parameter_list.h | 37 +------------------------------ include/sqlpp11/prepared_select.h | 2 +- include/sqlpp11/select.h | 8 ++----- include/sqlpp11/where.h | 5 +++++ tests/PreparedTest.cpp | 8 ------- tests/TabSample.h | 4 ++++ 12 files changed, 64 insertions(+), 62 deletions(-) diff --git a/include/sqlpp11/alias.h b/include/sqlpp11/alias.h index 32b1f165..0d281e56 100644 --- a/include/sqlpp11/alias.h +++ b/include/sqlpp11/alias.h @@ -41,13 +41,9 @@ namespace sqlpp template\ struct _member_t\ {\ - template\ - _member_t(TT&&... t): name(std::forward(t)...) {}\ - \ - template\ - _member_t& operator=(TT&& t) { name = std::forward(t); return *this; }\ - \ T name;\ + T& operator()() { return name; }\ + const T& operator()() const { return name; }\ };\ };\ };\ diff --git a/include/sqlpp11/expression.h b/include/sqlpp11/expression.h index 607bca33..b79a89de 100644 --- a/include/sqlpp11/expression.h +++ b/include/sqlpp11/expression.h @@ -42,6 +42,12 @@ namespace sqlpp using value_type = Rhs; using _parameter_t = std::tuple; + size_t _set_parameter_index(size_t index) + { + index = set_parameter_index(_lhs, index); + return set_parameter_index(_rhs, index); + } + template void serialize(std::ostream& os, Db& db) const { @@ -67,6 +73,12 @@ namespace sqlpp using _value_type = ValueType; using _parameter_t = std::tuple; + size_t _set_parameter_index(size_t index) + { + index = set_parameter_index(_lhs, index); + return set_parameter_index(_rhs, index); + } + template equal_t(L&& l, R&& r): _lhs(std::forward(l)), @@ -107,6 +119,12 @@ namespace sqlpp using _value_type = ValueType; using _parameter_t = std::tuple; + size_t _set_parameter_index(size_t index) + { + index = set_parameter_index(_lhs, index); + return set_parameter_index(_rhs, index); + } + template not_equal_t(L&& l, R&& r): _lhs(std::forward(l)), @@ -147,6 +165,11 @@ namespace sqlpp using _value_type = ValueType; using _parameter_t = std::tuple; + size_t _set_parameter_index(size_t index) + { + return set_parameter_index(_lhs, index); + } + not_t(Lhs l): _lhs(l) {} @@ -184,6 +207,12 @@ namespace sqlpp using _value_type = typename O::_value_type; using _parameter_t = std::tuple; + 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)) diff --git a/include/sqlpp11/from.h b/include/sqlpp11/from.h index 7e10c0b8..7e82de70 100644 --- a/include/sqlpp11/from.h +++ b/include/sqlpp11/from.h @@ -51,7 +51,7 @@ namespace sqlpp using _valid_expressions = typename detail::make_set_if::type; static_assert(_valid_expressions::size::value == sizeof...(TableOrJoin), "at least one argument is not a table or join in from()"); - // FIXME: Joins contain two tables. This is not being dealt with at the moment + // FIXME: Joins contain two tables. This is not being dealt with at the moment when looking at duplicates, for instance template diff --git a/include/sqlpp11/having.h b/include/sqlpp11/having.h index ec31d62e..d0c67e6f 100644 --- a/include/sqlpp11/having.h +++ b/include/sqlpp11/having.h @@ -65,6 +65,11 @@ namespace sqlpp _dynamic_expressions.serialize(os, db, " AND ", sizeof...(Expr) == 0); } + size_t _set_parameter_index(size_t index) + { + return set_parameter_index(_expressions, index); + } + using _parameter_t = std::tuple; _parameter_t _expressions; detail::serializable_list _dynamic_expressions; diff --git a/include/sqlpp11/like.h b/include/sqlpp11/like.h index d0d037b1..8e9b5bab 100644 --- a/include/sqlpp11/like.h +++ b/include/sqlpp11/like.h @@ -43,7 +43,7 @@ namespace sqlpp static_assert(is_text_t::value, "Pattern for like() has to be a text"); using _parameter_t = std::tuple; - struct _value_type: public ValueType::_base_value_type // we requite fully defined boolean here + struct _value_type: public ValueType::_base_value_type // we require fully defined boolean here { using _is_named_expression = std::true_type; }; diff --git a/include/sqlpp11/parameter.h b/include/sqlpp11/parameter.h index 054d5687..9959082d 100644 --- a/include/sqlpp11/parameter.h +++ b/include/sqlpp11/parameter.h @@ -40,15 +40,25 @@ namespace sqlpp using _parameter_type = typename ValueType::template _parameter_t; using _is_parameter = std::true_type; using _is_expression_t = std::true_type; + using _member_t = typename NameType::_name_t::template _member_t<_parameter_type>; template void serialize(std::ostream& os, Db& db) const { - static_assert(Db::_supports_parameter, "parameter not supported by current database"); - os << " ? "; // FIXME: Need to support positional placeholders and also type indicators for postgres for instance + static_assert(Db::_supports_prepared, "prepared statements not supported by current database"); + static_assert(Db::_use_questionmark_parameter or Db::_use_positional_dollar_parameter, "no known way to serialize parameter placeholders for current database"); + if (Db::_use_questionmark_parameter) + os << '?'; + else if (Db::_use_positional_dollar_parameter) + os << '$' << index + 1; } - using _member_t = typename NameType::_name_t::template _member_t<_parameter_type>; + constexpr bool _is_trivial() const + { + return false; + } + + size_t index; }; template::type>::value> diff --git a/include/sqlpp11/parameter_list.h b/include/sqlpp11/parameter_list.h index 4f21a557..d24bdeed 100644 --- a/include/sqlpp11/parameter_list.h +++ b/include/sqlpp11/parameter_list.h @@ -41,42 +41,7 @@ namespace sqlpp template struct parameter_list_t>: public Parameter::_member_t... { - parameter_list_t(): - Parameter::_member_t()..., - _parameter_tuple(static_cast(*this)()...) - {} - - parameter_list_t(const parameter_list_t& rhs) - noexcept(std::is_nothrow_copy_constructible>::value): - Parameter::_member_t(static_cast(rhs))..., - _parameter_tuple(static_cast(*this)()...) - {} - - parameter_list_t(parameter_list_t&& rhs) - noexcept(std::is_nothrow_move_constructible>::value): - Parameter::_member_t(std::move(static_cast(rhs)))..., - _parameter_tuple(static_cast(*this)()...) - {} - - parameter_list_t& operator=(const parameter_list_t& rhs) - noexcept(std::is_nothrow_copy_assignable>::value) - { - _parameter_tuple = rhs._parameter_tuple; - return *this; - } - - parameter_list_t& operator=(parameter_list_t&& rhs) - noexcept(std::is_nothrow_move_assignable>::value) - { - _parameter_tuple = std::move(rhs._parameter_tuple); - return *this; - } - - ~parameter_list_t() = default; - - using parameter_tuple_t = std::tuple; - using size = std::tuple_size; - parameter_tuple_t _parameter_tuple; + using size = std::integral_constant; }; namespace detail diff --git a/include/sqlpp11/prepared_select.h b/include/sqlpp11/prepared_select.h index 867c16fd..40cb2314 100644 --- a/include/sqlpp11/prepared_select.h +++ b/include/sqlpp11/prepared_select.h @@ -38,7 +38,7 @@ namespace sqlpp using _result_row_t = typename Select::_result_row_t; using _parameter_list_t = typename Select::_parameter_list_t; using _dynamic_names_t = typename Select::_dynamic_names_t; - using _handle_t = typename Db::_prepared_handle_t; + using _handle_t = typename Db::template _prepared_query_t