diff --git a/include/sqlpp11/core/basic/column.h b/include/sqlpp11/core/basic/column.h index 20c3b807..8d92b5e7 100644 --- a/include/sqlpp11/core/basic/column.h +++ b/include/sqlpp11/core/basic/column.h @@ -85,28 +85,20 @@ namespace sqlpp } }; + template + struct has_default> : public ColumnSpec::has_default + { + }; + + // Can be used in group_by template struct is_group_by_column> : public std::true_type { }; - template - struct value_type_of> - { - using type = typename ColumnSpec::value_type; - }; - template struct name_tag_of> : public name_tag_of{}; - template - struct has_default> : public ColumnSpec::has_default - { - }; - - template - struct table_t; - template struct required_tables_of> { @@ -118,6 +110,12 @@ namespace sqlpp { }; + template + struct value_type_of> + { + using type = typename ColumnSpec::value_type; + }; + template auto to_sql_string(Context& context, const column_t&) -> std::string { diff --git a/include/sqlpp11/core/basic/table.h b/include/sqlpp11/core/basic/table.h index 46565538..5e848ae2 100644 --- a/include/sqlpp11/core/basic/table.h +++ b/include/sqlpp11/core/basic/table.h @@ -46,11 +46,9 @@ namespace sqlpp //using _column_tuple_t = std::tuple...>; template using _foreign_table_as_t = table_as_t; - template - using _sqlpp_name_tag = table_as_t; template - _sqlpp_name_tag as(const NameTagProvider& /*unused*/) const + constexpr auto as(const NameTagProvider& /*unused*/) const -> table_as_t> { return {}; } diff --git a/include/sqlpp11/core/basic/table_as.h b/include/sqlpp11/core/basic/table_as.h index a3019304..924ea8f7 100644 --- a/include/sqlpp11/core/basic/table_as.h +++ b/include/sqlpp11/core/basic/table_as.h @@ -35,13 +35,12 @@ namespace sqlpp { - template - struct table_as_t : public TableSpec::_table_columns>, - public enable_join> + template + struct table_as_t : public TableSpec::_table_columns>, + public enable_join> { using _nodes = detail::type_vector<>; using _required_ctes = required_ctes_of; - using _provided_tables = detail::type_set; static_assert(required_tables_of_t::empty(), "table aliases must not depend on external tables"); @@ -49,32 +48,35 @@ namespace sqlpp //using _column_tuple_t = std::tuple...>; }; - template - struct is_table> : public std::true_type{}; + template + struct is_table> : public std::true_type{}; - template - struct name_tag_of> : public name_tag_of{}; - - template - struct provided_tables_of> + template + struct name_tag_of> { - using type = sqlpp::detail::type_vector>; + using type = NameTag; }; - template - struct provided_static_tables_of> : public provided_tables_of> + template + struct provided_tables_of> + { + using type = sqlpp::detail::type_vector>; + }; + + template + struct provided_static_tables_of> : public provided_tables_of> { }; - template - struct provided_optional_tables_of> : public provided_tables_of> + template + struct provided_optional_tables_of> : public provided_tables_of> { }; - template - auto to_sql_string(Context& context, const table_as_t&) -> std::string + template + auto to_sql_string(Context& context, const table_as_t&) -> std::string { return name_to_sql_string(context, name_tag_of_t::name) + " AS " + - name_to_sql_string(context, name_tag_of_t::name); + name_to_sql_string(context, NameTag::name); } } // namespace sqlpp diff --git a/include/sqlpp11/core/operator/assign_expression.h b/include/sqlpp11/core/operator/assign_expression.h index 93312197..6309f8b6 100644 --- a/include/sqlpp11/core/operator/assign_expression.h +++ b/include/sqlpp11/core/operator/assign_expression.h @@ -102,7 +102,7 @@ namespace sqlpp return {std::move(column), std::move(value)}; } -#warning: need to add type tests and serialiaze tests +#warning: need to add type tests and serialize tests struct op_plus_assign { static constexpr auto symbol = " += "; diff --git a/include/sqlpp11/core/operator/expression_as.h b/include/sqlpp11/core/operator/expression_as.h index 151cdaac..000eb957 100644 --- a/include/sqlpp11/core/operator/expression_as.h +++ b/include/sqlpp11/core/operator/expression_as.h @@ -34,6 +34,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. namespace sqlpp { #warning: need type tests +#warning: Should only use NameTag, not NameTagProvider here! template struct expression_as { diff --git a/tests/core/types/basic/CMakeLists.txt b/tests/core/types/basic/CMakeLists.txt index 20dc2537..014f4a8b 100644 --- a/tests/core/types/basic/CMakeLists.txt +++ b/tests/core/types/basic/CMakeLists.txt @@ -28,5 +28,8 @@ function(test_compile name) target_link_libraries(${target} PRIVATE sqlpp11::sqlpp11 sqlpp11_testing) endfunction() +test_compile(column) test_compile(join) +test_compile(table) +test_compile(table_as) diff --git a/tests/core/types/basic/column.cpp b/tests/core/types/basic/column.cpp new file mode 100644 index 00000000..f4adabec --- /dev/null +++ b/tests/core/types/basic/column.cpp @@ -0,0 +1,172 @@ +/* + * Copyright (c) 2024, 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 + +SQLPP_CREATE_NAME_TAG(cheese); + +void test_column() +{ + { + // Column integer with default (auto-increment). + auto foo = test::TabFoo{}; + using Foo = decltype(foo); + using Id = decltype(foo.id); + using Cheese = decltype(foo.id.as(cheese)); + using BarId = decltype(foo.as(test::TabBar{}).id); + using BarCheese = decltype(foo.as(test::TabBar{}).id.as(cheese)); + + static_assert(not sqlpp::is_table::value, ""); + static_assert(sqlpp::has_default::value, ""); + static_assert(sqlpp::is_group_by_column::value, ""); + + static_assert(std::is_same, test::TabFoo_::Id::_sqlpp_name_tag>::value, ""); + static_assert(std::is_same, sqlpp::detail::type_vector<>>::value, ""); + static_assert(std::is_same, sqlpp::provided_tables_of_t>::value, ""); + static_assert(std::is_same, sqlpp::provided_tables_of_t>::value, ""); + static_assert(std::is_same, sqlpp::detail::type_vector>::value, ""); + static_assert(std::is_same, sqlpp::required_tables_of_t>::value, ""); + + static_assert(std::is_same, sqlpp::integral>::value, ""); + + foo.as(test::TabBar{}).id = 7; + + // tab_foo.id AS cheese + // This is only useful SELECT. It therefore exposes neither name nor value directly. + // It does require its table, though. + static_assert(not sqlpp::is_table::value, ""); + static_assert(not sqlpp::has_default::value, ""); + static_assert(not sqlpp::is_group_by_column::value, ""); + + static_assert(std::is_same, sqlpp::no_name_t>::value, ""); + static_assert(std::is_same, sqlpp::detail::type_vector<>>::value, ""); + static_assert(std::is_same, sqlpp::provided_tables_of_t>::value, + ""); + static_assert( + std::is_same, sqlpp::provided_tables_of_t>::value, ""); + static_assert(std::is_same, sqlpp::detail::type_vector>::value, ""); + static_assert(std::is_same, sqlpp::required_tables_of_t>::value, + ""); + + static_assert(std::is_same, sqlpp::no_value_t>::value, ""); + + // (tab_foo AS bar).id +#warning : insert must not accept table_as! + static_assert(not sqlpp::is_table::value, ""); + static_assert(sqlpp::has_default::value, ""); + static_assert(sqlpp::is_group_by_column::value, ""); + + static_assert(std::is_same, test::TabFoo_::Id::_sqlpp_name_tag>::value, ""); + static_assert(std::is_same, sqlpp::detail::type_vector<>>::value, ""); + static_assert(std::is_same, sqlpp::provided_tables_of_t>::value, + ""); + static_assert(std::is_same, sqlpp::provided_tables_of_t>::value, + ""); + static_assert( + std::is_same< + sqlpp::required_tables_of_t, + sqlpp::detail::type_vector>>::value, + ""); + static_assert(std::is_same, sqlpp::required_tables_of_t>::value, + ""); + + static_assert(std::is_same, sqlpp::integral>::value, ""); + + // (tab_foo as bar).id.as(cheese) + static_assert(not sqlpp::is_table::value, ""); + static_assert(not sqlpp::has_default::value, ""); + static_assert(not sqlpp::is_group_by_column::value, ""); + + static_assert(std::is_same, sqlpp::no_name_t>::value, ""); + static_assert(std::is_same, sqlpp::detail::type_vector<>>::value, ""); + static_assert( + std::is_same, sqlpp::provided_tables_of_t>::value, ""); + static_assert( + std::is_same, sqlpp::provided_tables_of_t>::value, + ""); + static_assert( + std::is_same< + sqlpp::required_tables_of_t, + sqlpp::detail::type_vector>>::value, + ""); + static_assert( + std::is_same, sqlpp::required_tables_of_t>::value, ""); + + static_assert(std::is_same, sqlpp::no_value_t>::value, ""); + } + + { + // Column optional (can be null) text with default. + auto bar = test::TabBar{}; + using Bar = decltype(bar); + using TextN = decltype(bar.textN); + + static_assert(not sqlpp::is_table::value, ""); + static_assert(sqlpp::has_default::value, ""); + static_assert(sqlpp::is_group_by_column::value, ""); + + static_assert(std::is_same, test::TabBar_::TextN::_sqlpp_name_tag>::value, ""); + static_assert(std::is_same, sqlpp::detail::type_vector<>>::value, ""); + static_assert(std::is_same, sqlpp::provided_tables_of_t>::value, + ""); + static_assert(std::is_same, sqlpp::provided_tables_of_t>::value, + ""); + static_assert(std::is_same, sqlpp::detail::type_vector>::value, ""); + static_assert(std::is_same, sqlpp::required_tables_of_t>::value, + ""); + + static_assert(std::is_same, sqlpp::optional>::value, ""); + } + + { + // Column bool without default. + auto bar = test::TabBar{}; + using Bar = decltype(bar); + using BoolNn = decltype(bar.boolNn); + + static_assert(not sqlpp::is_table::value, ""); + static_assert(not sqlpp::has_default::value, ""); + static_assert(sqlpp::is_group_by_column::value, ""); + + static_assert(std::is_same, test::TabBar_::BoolNn::_sqlpp_name_tag>::value, ""); + static_assert(std::is_same, sqlpp::detail::type_vector<>>::value, ""); + static_assert(std::is_same, sqlpp::provided_tables_of_t>::value, + ""); + static_assert( + std::is_same, sqlpp::provided_tables_of_t>::value, ""); + static_assert(std::is_same, sqlpp::detail::type_vector>::value, ""); + static_assert(std::is_same, sqlpp::required_tables_of_t>::value, + ""); + + static_assert(std::is_same, sqlpp::boolean>::value, ""); + } +} + +int main() +{ + void test_column(); +} + diff --git a/tests/core/types/basic/table.cpp b/tests/core/types/basic/table.cpp new file mode 100644 index 00000000..2c448088 --- /dev/null +++ b/tests/core/types/basic/table.cpp @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2024, 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 + +void test_table() +{ + auto v = sqlpp::value(17); + auto foo = test::TabFoo{}; + auto bar = test::TabBar{}; + + static_assert(sqlpp::is_table::value, ""); + static_assert(std::is_same, test::TabFoo_::_sqlpp_name_tag>::value, ""); + static_assert(std::is_same, sqlpp::detail::type_vector>::value, ""); + static_assert(std::is_same, sqlpp::provided_tables_of_t>::value, ""); + static_assert(std::is_same, sqlpp::provided_tables_of_t>::value, ""); +} + +int main() +{ + void test_table(); +} + + diff --git a/tests/core/types/basic/table_as.cpp b/tests/core/types/basic/table_as.cpp new file mode 100644 index 00000000..12d43fb3 --- /dev/null +++ b/tests/core/types/basic/table_as.cpp @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2024, 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 + +void test_table() +{ + auto v = sqlpp::value(17); + auto foo = test::TabFoo{}; + auto bar = test::TabBar{}; + using FooBar = decltype(foo.as(bar)); + using Id = decltype(foo.as(bar).id); + + static_assert(std::is_same>::value, ""); + static_assert(sqlpp::is_table::value, ""); + static_assert(std::is_same, test::TabBar_::_sqlpp_name_tag>::value, ""); + static_assert(std::is_same, sqlpp::detail::type_vector>::value, ""); + static_assert(std::is_same, sqlpp::provided_tables_of_t>::value, ""); + static_assert(std::is_same, sqlpp::provided_tables_of_t>::value, ""); + static_assert(std::is_same, sqlpp::detail::type_vector<>>::value, ""); + static_assert(std::is_same, sqlpp::required_tables_of_t>::value, ""); + + static_assert(not sqlpp::is_table::value, ""); + static_assert(std::is_same, test::TabFoo_::Id::_sqlpp_name_tag>::value, ""); + static_assert(std::is_same, sqlpp::detail::type_vector<>>::value, ""); + static_assert(std::is_same, sqlpp::provided_tables_of_t>::value, ""); + static_assert(std::is_same, sqlpp::provided_tables_of_t>::value, ""); + static_assert(std::is_same, sqlpp::detail::type_vector>::value, ""); + static_assert(std::is_same, sqlpp::required_tables_of_t>::value, ""); +} + +int main() +{ + void test_table(); +} + +