From 6bdddcc3438aaa5f10aa2e83b5e4dcb3a0ac5a11 Mon Sep 17 00:00:00 2001 From: rbock Date: Tue, 13 Sep 2016 21:08:01 +0200 Subject: [PATCH] Fix braces around sub-selects used as tables --- include/sqlpp11/select_pseudo_table.h | 5 +++- include/sqlpp11/table_alias.h | 12 ++++---- test_serializer/CMakeLists.txt | 1 + test_serializer/TableAlias.cpp | 43 +++++++++++++++++++++++++++ 4 files changed, 54 insertions(+), 7 deletions(-) create mode 100644 test_serializer/TableAlias.cpp diff --git a/include/sqlpp11/select_pseudo_table.h b/include/sqlpp11/select_pseudo_table.h index 679019e2..321df845 100644 --- a/include/sqlpp11/select_pseudo_table.h +++ b/include/sqlpp11/select_pseudo_table.h @@ -55,7 +55,10 @@ namespace sqlpp struct select_pseudo_table_t : public table_t, select_column_spec_t...> { - using _traits = make_traits; + using _traits = make_traits::value>>; using _nodes = detail::type_vector<>; select_pseudo_table_t(Select select) : _select(select) diff --git a/include/sqlpp11/table_alias.h b/include/sqlpp11/table_alias.h index 52e04c31..ae534463 100644 --- a/include/sqlpp11/table_alias.h +++ b/include/sqlpp11/table_alias.h @@ -27,12 +27,12 @@ #ifndef SQLPP_TABLE_ALIAS_H #define SQLPP_TABLE_ALIAS_H -#include -#include -#include -#include #include +#include #include +#include +#include +#include namespace sqlpp { @@ -107,10 +107,10 @@ namespace sqlpp static Context& _(const T& t, Context& context) { - if (requires_braces_t::value) + if (requires_braces_t::value) context << "("; serialize(t._table, context); - if (requires_braces_t::value) + if (requires_braces_t
::value) context << ")"; context << " AS " << name_of::char_ptr(); return context; diff --git a/test_serializer/CMakeLists.txt b/test_serializer/CMakeLists.txt index 3a83d6df..b00c6860 100644 --- a/test_serializer/CMakeLists.txt +++ b/test_serializer/CMakeLists.txt @@ -28,6 +28,7 @@ set(test_serializer_names In Insert Where + TableAlias ) create_test_sourcelist(test_serializer_sources test_serializer_main.cpp ${test_serializer_names}) diff --git a/test_serializer/TableAlias.cpp b/test_serializer/TableAlias.cpp new file mode 100644 index 00000000..aef0b57b --- /dev/null +++ b/test_serializer/TableAlias.cpp @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2016-2016, 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. + */ + +#include "Sample.h" +#include "compare.h" +#include + +#include + +int TableAlias(int, char* []) +{ + const auto foo = test::TabFoo{}; + const auto bar = test::TabBar{}; + + // Individual values + compare(__LINE__, foo.as(bar), "tab_foo AS tab_bar"); + compare(__LINE__, select(foo.omega).from(foo).unconditionally().as(bar), + "(SELECT tab_foo.omega FROM tab_foo) AS tab_bar"); + + return 0; +}