From 161da757232dd1aac3570c5ed0a78405e1a5d233 Mon Sep 17 00:00:00 2001 From: Roland Bock Date: Fri, 13 Sep 2013 09:18:15 +0200 Subject: [PATCH] Stripped a lot of superfluouos type information from result rows. Moved member template into name class to utilize the fact the class aliases are really just aliases. This makes the code leaner in many cases and less complex for the compiler (I guess). It also has the benefit, that the field name is available as string in the result_rows. This might be useful for debugging one day. --- include/sqlpp11/alias.h | 22 ++++---- include/sqlpp11/column.h | 2 - include/sqlpp11/expression.h | 2 - include/sqlpp11/field.h | 67 ++++++++++++++++++++++++ include/sqlpp11/functions.h | 10 ++-- include/sqlpp11/multi_column.h | 2 - include/sqlpp11/result_row.h | 8 +-- include/sqlpp11/select.h | 2 - include/sqlpp11/select_expression_list.h | 7 +-- include/sqlpp11/select_pseudo_table.h | 2 - include/sqlpp11/table_base.h | 4 +- tests/SelectTest.cpp | 5 +- tests/TabSample.h | 54 +++++++++---------- 13 files changed, 119 insertions(+), 68 deletions(-) create mode 100644 include/sqlpp11/field.h diff --git a/include/sqlpp11/alias.h b/include/sqlpp11/alias.h index 70afa75c..4ea1f859 100644 --- a/include/sqlpp11/alias.h +++ b/include/sqlpp11/alias.h @@ -38,17 +38,17 @@ namespace sqlpp struct _name_t\ {\ static constexpr const char* _get_name() { return #name; }\ - };\ - template\ - struct _member_t\ - {\ - template\ - _member_t(TT&&... t): name(std::forward(t)...) {}\ - \ - template\ + 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 name;\ + };\ };\ };\ constexpr name##_t name = {}; @@ -93,8 +93,6 @@ namespace sqlpp }; using _name_t = typename AliasProvider::_name_t; - template - using _member_t = typename AliasProvider::template _member_t; template void serialize(std::ostream& os, Db& db) const diff --git a/include/sqlpp11/column.h b/include/sqlpp11/column.h index fc286302..a3aa6e73 100644 --- a/include/sqlpp11/column.h +++ b/include/sqlpp11/column.h @@ -50,8 +50,6 @@ namespace sqlpp }; using _name_t = typename ColumnSpec::_name_t; - template - using _member_t = typename ColumnSpec::template _member_t; template void serialize(std::ostream& os, Db& db) const diff --git a/include/sqlpp11/expression.h b/include/sqlpp11/expression.h index 4a07a3b4..7b0e473e 100644 --- a/include/sqlpp11/expression.h +++ b/include/sqlpp11/expression.h @@ -320,8 +320,6 @@ namespace sqlpp { using _value_type = typename F::_value_type; using _name_t = typename F::_name_t; - template - using _member_t = typename F::template _member_t; named_nary_function_t(Rhs&&... r): _rhs(std::move(r)...) diff --git a/include/sqlpp11/field.h b/include/sqlpp11/field.h new file mode 100644 index 00000000..f4ca0cf9 --- /dev/null +++ b/include/sqlpp11/field.h @@ -0,0 +1,67 @@ +/* + * 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_FIELD_H +#define SQLPP_FIELD_H + +#include +#include + +namespace sqlpp +{ + template + struct field_t + { + using _name_t = NameType; + using _value_type = ValueType; + }; + + template + struct multi_field_t + { + }; + + namespace detail + { + template + struct make_field_t_impl + { + using type = field_t; + }; + + template + struct make_field_t_impl>> + { + using type = multi_field_t::type...>>; + }; + } + + template + using make_field_t = typename detail::make_field_t_impl::type; + +} + +#endif diff --git a/include/sqlpp11/functions.h b/include/sqlpp11/functions.h index 34878a78..d1c48020 100644 --- a/include/sqlpp11/functions.h +++ b/include/sqlpp11/functions.h @@ -52,11 +52,11 @@ namespace sqlpp struct _name_t\ {\ static constexpr const char* _get_name() { return #SQL; }\ - };\ - template\ - struct _member_t\ - {\ - M NAME;\ + template\ + struct _member_t\ + {\ + M NAME;\ + };\ };\ };\ \ diff --git a/include/sqlpp11/multi_column.h b/include/sqlpp11/multi_column.h index a5c62050..69043074 100644 --- a/include/sqlpp11/multi_column.h +++ b/include/sqlpp11/multi_column.h @@ -45,8 +45,6 @@ namespace sqlpp static_assert(_named_expr_set::size::value == sizeof...(NamedExpr), "multi_column parameters need to be named expressions"); using _name_t = typename AliasProvider::_name_t; - template - using _member_t = typename AliasProvider::template _member_t; struct _value_type: public no_value_t { diff --git a/include/sqlpp11/result_row.h b/include/sqlpp11/result_row.h index 150e5101..a96fc6dd 100644 --- a/include/sqlpp11/result_row.h +++ b/include/sqlpp11/result_row.h @@ -39,10 +39,10 @@ namespace sqlpp template struct result_row_impl: - public NamedExpr::template _member_t>, + public NamedExpr::_name_t::template _member_t>, public result_row_impl { - using _field = typename NamedExpr::template _member_t>; + using _field = typename NamedExpr::_name_t::template _member_t>; using _rest = result_row_impl; result_row_impl(const raw_result_row_t& raw_result_row): @@ -60,10 +60,10 @@ namespace sqlpp template struct result_row_impl>, Rest...>: - public AliasProvider::template _member_t>, // level prevents identical closures to be present twice in the inheritance tree + public AliasProvider::_name_t::template _member_t>, // level prevents identical closures to be present twice in the inheritance tree public result_row_impl { - using _multi_field = typename AliasProvider::template _member_t>; + using _multi_field = typename AliasProvider::_name_t::template _member_t>; using _rest = result_row_impl; result_row_impl(const raw_result_row_t& raw_result_row): diff --git a/include/sqlpp11/select.h b/include/sqlpp11/select.h index 62a67703..722d4e40 100644 --- a/include/sqlpp11/select.h +++ b/include/sqlpp11/select.h @@ -112,8 +112,6 @@ namespace sqlpp typename ExpressionList::_value_type>::type; using _name_t = typename ExpressionList::_name_t; - template - using _member_t = typename ExpressionList::template _member_t; // The standard constructors, assigment operators and destructor select_t(Flags&& flags, ExpressionList&& expression_list): diff --git a/include/sqlpp11/select_expression_list.h b/include/sqlpp11/select_expression_list.h index 1a7949a0..634db5c0 100644 --- a/include/sqlpp11/select_expression_list.h +++ b/include/sqlpp11/select_expression_list.h @@ -78,7 +78,7 @@ namespace sqlpp static_assert(_valid_expressions::size::value == sizeof...(NamedExpr), "at least one argument is not a named expression"); // check for duplicate select expression names - static_assert(not detail::has_duplicates...>::value, "at least one duplicate name detected"); + static_assert(not detail::has_duplicates...>::value, "at least one duplicate name detected"); // declare this to be a select expression using _is_select_expression_list = tag_yes; @@ -94,11 +94,6 @@ namespace sqlpp struct _no_name_t {}; using _name_t = typename std::conditional::type::_name_t, _no_name_t>::type; - template - struct _no_member_t {}; - template - using _member_t = typename std::conditional::type::template _member_t, _no_member_t>::type; - template void serialize(std::ostream& os, Db& db) const { diff --git a/include/sqlpp11/select_pseudo_table.h b/include/sqlpp11/select_pseudo_table.h index 3c9a026d..f0a663f0 100644 --- a/include/sqlpp11/select_pseudo_table.h +++ b/include/sqlpp11/select_pseudo_table.h @@ -36,8 +36,6 @@ namespace sqlpp struct select_column_spec_t { using _name_t = typename Expr::_name_t; - template - using _member_t = typename Expr::template _member_t; using _value_type = typename Expr::_value_type; struct _column_type {}; }; diff --git a/include/sqlpp11/table_base.h b/include/sqlpp11/table_base.h index 1c54b36f..13c18cd5 100644 --- a/include/sqlpp11/table_base.h +++ b/include/sqlpp11/table_base.h @@ -38,7 +38,7 @@ namespace sqlpp { template - struct table_base_t: public ColumnSpec::template _member_t>... + struct table_base_t: public ColumnSpec::_name_t::template _member_t>... { using _table_set = detail::set; // Hint need a set here to be similar to a join (which always represents more than one table) using _all_columns = typename detail::make_set...>::type; @@ -55,7 +55,7 @@ namespace sqlpp } template - struct alias_t: public ColumnSpec::template _member_t>... + struct alias_t: public ColumnSpec::_name_t::template _member_t>... { using _is_table = tag_yes; using _table_set = detail::set; diff --git a/tests/SelectTest.cpp b/tests/SelectTest.cpp index 2aef3709..9d4fb378 100644 --- a/tests/SelectTest.cpp +++ b/tests/SelectTest.cpp @@ -256,8 +256,9 @@ int main() auto b = select(f.omega.as(t.alpha)); using A = typename decltype(a)::_result_row_t; using B = typename decltype(b)::_result_row_t; - static_assert(std::is_same::value, "Two bigint columns must have identical base_value_type"); - //A x = std::declval(); + static_assert(std::is_same< + decltype(t.alpha)::_value_type::_base_value_type, + decltype(f.omega)::_value_type::_base_value_type>::value, "Two bigint columns must have identical base_value_type"); static_assert(std::is_same::value, "select with identical columns(name/value_type) need to have identical result_types"); } diff --git a/tests/TabSample.h b/tests/TabSample.h index 2e554771..5dea865e 100644 --- a/tests/TabSample.h +++ b/tests/TabSample.h @@ -38,7 +38,6 @@ namespace TabFoo_ struct _name_t { static constexpr const char* _get_name() { return "omega"; } - }; template struct _member_t { @@ -52,6 +51,7 @@ namespace TabFoo_ T omega; }; + }; using _value_type = sqlpp::bigint; struct _column_type { @@ -88,19 +88,19 @@ namespace TabSample_ struct _name_t { static constexpr const char* _get_name() { return "alpha"; } - }; - template - struct _member_t - { - /* - template - _name_t(TT&&... t): alpha(std::forward(t)...) {} + template + struct _member_t + { + /* + template + _name_t(TT&&... t): alpha(std::forward(t)...) {} - template - _name_t& operator=(TT&& t) { alpha = std::forward(t); return *this; } - */ + template + _name_t& operator=(TT&& t) { alpha = std::forward(t); return *this; } + */ - T alpha; + T alpha; + }; }; using _value_type = sqlpp::bigint; struct _column_type @@ -118,11 +118,11 @@ namespace TabSample_ struct _name_t { static constexpr const char* _get_name() { return "beta"; } - }; - template - struct _member_t - { - T beta; + template + struct _member_t + { + T beta; + }; }; using _value_type = sqlpp::varchar; struct _column_type @@ -138,11 +138,11 @@ namespace TabSample_ struct _name_t { static constexpr const char* _get_name() { return "gamma"; } - }; - template - struct _member_t - { - T gamma; + template + struct _member_t + { + T gamma; + }; }; using _value_type = sqlpp::boolean; struct _column_type @@ -163,12 +163,12 @@ struct TabSample: sqlpp::table_base_t< struct _name_t { static constexpr const char* _get_name() { return "tab_sample"; } + template + struct _member_t + { + T tabSample; + }; }; - template - struct _member_t - { - T tabSample; - }; template void serialize(std::ostream& os, Db& db) const {