/* * Copyright (c) 2013-2014, 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_SELECT_COLUMN_LIST_H #define SQLPP_SELECT_COLUMN_LIST_H #include #include #include #include #include #include #include #include #include #include #include #include namespace sqlpp { namespace detail { template struct select_traits { using _traits = make_traits; struct _name_t {}; }; template struct select_traits { using _traits = make_traits, tag::is_select_column_list, tag::is_return_value, tag::is_expression, tag::is_named_expression>; using _name_t = typename Column::_name_t; }; } template struct dynamic_select_column_list { using _names_t = std::vector; std::vector> _dynamic_columns; _names_t _dynamic_expression_names; template void emplace_back(Expr expr) { _dynamic_expression_names.push_back(Expr::_name_t::_get_name()); _dynamic_columns.emplace_back(expr); } bool empty() const { return _dynamic_columns.empty(); } }; template<> struct dynamic_select_column_list { struct _names_t { static constexpr size_t size() { return 0; } }; _names_t _dynamic_expression_names; static constexpr bool empty() { return true; } }; template struct serializer_t> { using T = dynamic_select_column_list; static Context& _(const T& t, Context& context) { bool first = true; for (const auto column : t._dynamic_columns) { if (first) first = false; else context << ','; serialize(column, context); } return context; } }; template struct serializer_t> { using T = dynamic_select_column_list; static Context& _(const T& t, Context& context) { return context; } }; // SELECTED COLUMNS DATA template struct select_column_list_data_t { select_column_list_data_t(Columns... columns): _columns(columns...) {} select_column_list_data_t(std::tuple columns): _columns(columns) {} select_column_list_data_t(const select_column_list_data_t&) = default; select_column_list_data_t(select_column_list_data_t&&) = default; select_column_list_data_t& operator=(const select_column_list_data_t&) = default; select_column_list_data_t& operator=(select_column_list_data_t&&) = default; ~select_column_list_data_t() = default; std::tuple _columns; dynamic_select_column_list _dynamic_columns; }; // SELECTED COLUMNS template struct select_column_list_t { using _traits = typename ::sqlpp::detail::select_traits::_traits; using _recursive_traits = make_recursive_traits; using _name_t = typename ::sqlpp::detail::select_traits::_name_t; using _is_dynamic = is_database; static_assert(_is_dynamic::value or sizeof...(Columns), "at least one select expression required"); static_assert(not ::sqlpp::detail::has_duplicates::value, "at least one duplicate argument detected"); static_assert(::sqlpp::detail::all_t<(is_named_expression_t::value or is_multi_column_t::value)...>::value, "at least one argument is not a named expression"); static_assert(not ::sqlpp::detail::has_duplicates::value, "at least one duplicate name detected"); struct _column_type {}; // Data using _data_t = select_column_list_data_t; // Member implementation with data and methods template struct _impl_t { template void add_ntc(NamedExpression namedExpression) { add(namedExpression); } template void add(NamedExpression namedExpression) { static_assert(_is_dynamic::value, "selected_columns::add() can only be called for dynamic_column"); static_assert(is_named_expression_t::value, "invalid named expression argument in selected_columns::add()"); static_assert(TableCheckRequired::value or Policies::template _no_unknown_tables::value, "named expression uses tables unknown to this statement in selected_columns::add()"); using column_names = ::sqlpp::detail::make_type_set_t; static_assert(not ::sqlpp::detail::is_element_of::value, "a column of this name is present in the select already"); using ok = ::sqlpp::detail::all_t< _is_dynamic::value, is_named_expression_t::value >; _add_impl(namedExpression, ok()); // dispatch to prevent compile messages after the static_assert } //private: template void _add_impl(NamedExpression namedExpression, const std::true_type&) { return _data._dynamic_columns.emplace_back(namedExpression); } template void _add_column_impl(NamedExpression namedExpression, const std::false_type&); public: _data_t _data; }; // Member template for adding the named member to a statement template struct _member_t { using _data_t = select_column_list_data_t; _impl_t selected_columns; _impl_t& operator()() { return selected_columns; } const _impl_t& operator()() const { return selected_columns; } template static auto _get_member(T t) -> decltype(t.selected_columns) { return t.selected_columns; } }; // Additional methods for the statement template struct _methods_t { static void _check_consistency() {} }; // Result methods template struct _result_methods_t { using _statement_t = typename Policies::_statement_t; const _statement_t& _get_statement() const { return static_cast(*this); } template struct _deferred_field_t { using type = make_field_spec_t<_statement_t, Column>; }; template using _field_t = typename _deferred_field_t::type; template using _result_row_t = typename std::conditional<_is_dynamic::value, dynamic_result_row_t...>, result_row_t...>>::type; using _dynamic_names_t = typename dynamic_select_column_list::_names_t; template struct _deferred_table_t { using table = select_pseudo_table_t<_statement_t, Columns...>; using alias = typename table::template _alias_t; }; template using _table_t = typename _deferred_table_t::table; template using _alias_t = typename _deferred_table_t::alias; template _alias_t as(const AliasProvider& aliasProvider) const { static_assert(Policies::_can_be_used_as_table::value, "statement cannot be used as table, e.g. due to missing tables"); static_assert(::sqlpp::detail::none_t::value...>::value, "cannot use multi-columns in sub selects"); return _table_t(_get_statement()).as(aliasProvider); } const _dynamic_names_t& get_dynamic_names() const { return _get_statement().selected_columns._data._dynamic_columns._dynamic_expression_names; } size_t get_no_of_result_columns() const { return sizeof...(Columns) + get_dynamic_names().size(); } // Execute template auto _run(Db& db) const -> result_t_get_statement())), _result_row_t> { _statement_t::_check_consistency(); static_assert(_statement_t::_get_static_no_of_parameters() == 0, "cannot run select directly with parameters, use prepare instead"); return {db.select(_get_statement()), get_dynamic_names()}; } // Prepare template auto _prepare(Db& db) const -> prepared_select_t { _statement_t::_check_consistency(); return {{}, get_dynamic_names(), db.prepare_select(_get_statement())}; } }; }; namespace detail { template using make_select_column_list_t = copy_tuple_args_t::_(std::declval())...))>; } struct no_select_column_list_t { using _traits = make_traits; using _recursive_traits = make_recursive_traits<>; struct _name_t {}; // Data using _data_t = no_data_t; // Member implementation with data and methods template struct _impl_t { _data_t _data; }; // Member template for adding the named member to a statement template struct _member_t { using _data_t = no_data_t; _impl_t no_selected_columns; _impl_t& operator()() { return no_selected_columns; } const _impl_t& operator()() const { return no_selected_columns; } template static auto _get_member(T t) -> decltype(t.no_selected_columns) { return t.no_selected_columns; } }; // Additional methods for the statement template struct _methods_t { using _database_t = typename Policies::_database_t; template using _new_statement_t = typename Policies::template _new_statement_t; static void _check_consistency() {} template auto columns(Args... args) -> _new_statement_t<::sqlpp::detail::make_select_column_list_t> { return { *static_cast(this), typename ::sqlpp::detail::make_select_column_list_t::_data_t{std::tuple_cat(::sqlpp::detail::as_tuple::_(args)...)} }; } template auto dynamic_columns(Args... args) -> _new_statement_t<::sqlpp::detail::make_select_column_list_t<_database_t, Args...>> { static_assert(not std::is_same<_database_t, void>::value, "dynamic_columns must not be called in a static statement"); return { *static_cast(this), typename ::sqlpp::detail::make_select_column_list_t<_database_t, Args...>::_data_t{std::tuple_cat(::sqlpp::detail::as_tuple::_(args)...)} }; } }; }; // Interpreters template struct serializer_t> { using T = select_column_list_data_t; static Context& _(const T& t, Context& context) { interpret_tuple(t._columns, ',', context); if (sizeof...(Columns) and not t._dynamic_columns.empty()) context << ','; serialize(t._dynamic_columns, context); return context; } }; } #endif