From e4c7b5235fac7cca3eea456c2b37907641ac42c3 Mon Sep 17 00:00:00 2001 From: rbock Date: Mon, 10 Feb 2014 09:46:31 +0100 Subject: [PATCH] Added several checks for correct types in insert columns/values --- include/sqlpp11/any.h | 4 +++- include/sqlpp11/default_value.h | 1 + include/sqlpp11/null.h | 1 + include/sqlpp11/parameter.h | 2 ++ include/sqlpp11/tvin.h | 3 +++ include/sqlpp11/vendor/assignment.h | 1 + include/sqlpp11/vendor/insert_value.h | 3 +++ include/sqlpp11/vendor/insert_value_list.h | 5 ++++- include/sqlpp11/vendor/wrap_operand.h | 5 +++++ tests/InsertTest.cpp | 1 + tests/UpdateTest.cpp | 3 +++ 11 files changed, 27 insertions(+), 2 deletions(-) diff --git a/include/sqlpp11/any.h b/include/sqlpp11/any.h index 004a4cfd..e4c9ceef 100644 --- a/include/sqlpp11/any.h +++ b/include/sqlpp11/any.h @@ -27,8 +27,8 @@ #ifndef SQLPP_ANY_H #define SQLPP_ANY_H -#include #include +#include namespace sqlpp { @@ -57,6 +57,8 @@ namespace sqlpp }; }; + using _table_set = ::sqlpp::detail::type_set<>; // FIXME: Can selects leak tables here? + any_t(Select select): _select(select) {} diff --git a/include/sqlpp11/default_value.h b/include/sqlpp11/default_value.h index 78eb71c5..b4125efe 100644 --- a/include/sqlpp11/default_value.h +++ b/include/sqlpp11/default_value.h @@ -35,6 +35,7 @@ namespace sqlpp { static constexpr bool _is_expression = true; using _value_type = no_value_t; + using _table_set = ::sqlpp::detail::type_set<>; static constexpr bool _is_trivial() { return false; } }; diff --git a/include/sqlpp11/null.h b/include/sqlpp11/null.h index c07a3001..643511a8 100644 --- a/include/sqlpp11/null.h +++ b/include/sqlpp11/null.h @@ -35,6 +35,7 @@ namespace sqlpp { static constexpr bool _is_expression = true; using _value_type = no_value_t; + using _table_set = ::sqlpp::detail::type_set<>; static constexpr bool _is_trivial() { return false; } }; diff --git a/include/sqlpp11/parameter.h b/include/sqlpp11/parameter.h index 3f33b808..875e738a 100644 --- a/include/sqlpp11/parameter.h +++ b/include/sqlpp11/parameter.h @@ -28,6 +28,7 @@ #define SQLPP_PARAMETER_H #include +#include namespace sqlpp { @@ -38,6 +39,7 @@ namespace sqlpp using _is_parameter = std::true_type; using _is_expression_t = std::true_type; using _instance_t = typename NameType::_name_t::template _member_t; + using _table_set = sqlpp::detail::type_set<>; parameter_t() {} diff --git a/include/sqlpp11/tvin.h b/include/sqlpp11/tvin.h index a3462d08..2b98c3ef 100644 --- a/include/sqlpp11/tvin.h +++ b/include/sqlpp11/tvin.h @@ -39,6 +39,7 @@ namespace sqlpp { using _operand_t = Operand; using _value_type = typename _operand_t::_value_type; + using _table_set = typename _operand_t::_table_set; tvin_t(Operand operand): _value(operand) @@ -74,6 +75,7 @@ namespace sqlpp template struct tvin_wrap_t { + using _table_set = typename T::_table_set; static constexpr bool _is_trivial() { return false; @@ -94,6 +96,7 @@ namespace sqlpp template struct tvin_wrap_t> { + using _table_set = typename T::_table_set; bool _is_trivial() const { return _value._is_trivial(); diff --git a/include/sqlpp11/vendor/assignment.h b/include/sqlpp11/vendor/assignment.h index 5d2cfc67..d4e0f660 100644 --- a/include/sqlpp11/vendor/assignment.h +++ b/include/sqlpp11/vendor/assignment.h @@ -44,6 +44,7 @@ namespace sqlpp using _column_t = Lhs; using value_type = Rhs; using _parameter_tuple_t = std::tuple<_column_t, Rhs>; + using _table_set = typename Lhs::_table_set::template join::type; static_assert(not std::is_same::value or can_be_null_t<_column_t>::value, "column cannot be null"); diff --git a/include/sqlpp11/vendor/insert_value.h b/include/sqlpp11/vendor/insert_value.h index 88559474..440c88ec 100644 --- a/include/sqlpp11/vendor/insert_value.h +++ b/include/sqlpp11/vendor/insert_value.h @@ -32,6 +32,7 @@ #include #include #include +#include namespace sqlpp { @@ -43,6 +44,7 @@ namespace sqlpp struct type_if { using type = Type; + using _table_set = typename Type::_table_set; }; template @@ -50,6 +52,7 @@ namespace sqlpp { struct type { + using _table_set = sqlpp::detail::type_set<>; }; }; } diff --git a/include/sqlpp11/vendor/insert_value_list.h b/include/sqlpp11/vendor/insert_value_list.h index 22668b6a..31ea1db6 100644 --- a/include/sqlpp11/vendor/insert_value_list.h +++ b/include/sqlpp11/vendor/insert_value_list.h @@ -63,7 +63,10 @@ namespace sqlpp static_assert(not sqlpp::detail::or_t::value, "at least one assignment is prohibited by its column definition in set()"); - using _table_set = typename ::sqlpp::detail::make_joined_set::type; + using _table_set = typename ::sqlpp::detail::make_joined_set< + typename Assignments::_column_t::_table_set..., + typename Assignments::value_type::_table_set... + >::type; static_assert(_is_dynamic::value ? (_table_set::size::value < 2) : (_table_set::size::value == 1), "set() contains assignments for tables from several columns"); insert_list_t(Assignments... assignment): diff --git a/include/sqlpp11/vendor/wrap_operand.h b/include/sqlpp11/vendor/wrap_operand.h index 106a6349..97d9cd8a 100644 --- a/include/sqlpp11/vendor/wrap_operand.h +++ b/include/sqlpp11/vendor/wrap_operand.h @@ -29,6 +29,7 @@ #include #include +#include namespace sqlpp { @@ -47,6 +48,7 @@ namespace sqlpp static constexpr bool _is_expression = true; using _value_type = sqlpp::detail::boolean; using _value_t = bool; + using _table_set = ::sqlpp::detail::type_set<>; bool _is_trivial() const { return _t == false; } @@ -71,6 +73,7 @@ namespace sqlpp static constexpr bool _is_expression = true; using _value_type = ::sqlpp::detail::integral; using _value_t = T; + using _table_set = ::sqlpp::detail::type_set<>; bool _is_trivial() const { return _t == 0; } @@ -96,6 +99,7 @@ namespace sqlpp static constexpr bool _is_expression = true; using _value_type = ::sqlpp::detail::floating_point; using _value_t = T; + using _table_set = ::sqlpp::detail::type_set<>; bool _is_trivial() const { return _t == 0; } @@ -119,6 +123,7 @@ namespace sqlpp static constexpr bool _is_expression = true; using _value_type = ::sqlpp::detail::text; using _value_t = std::string; + using _table_set = ::sqlpp::detail::type_set<>; bool _is_trivial() const { return _t.empty(); } diff --git a/tests/InsertTest.cpp b/tests/InsertTest.cpp index 29a3e7c5..93d7e6c7 100644 --- a/tests/InsertTest.cpp +++ b/tests/InsertTest.cpp @@ -35,6 +35,7 @@ DbMock::_context_t printer(std::cerr); int main() { test::TabBar t; + test::TabFoo f; auto x = t.alpha = 7; auto y = t.beta = "kaesekuchen"; diff --git a/tests/UpdateTest.cpp b/tests/UpdateTest.cpp index a4f07fe4..93dbf7f0 100644 --- a/tests/UpdateTest.cpp +++ b/tests/UpdateTest.cpp @@ -35,6 +35,7 @@ DbMock::_context_t printer(std::cerr); int main() { test::TabBar t; + test::TabFoo f; auto x = t.alpha = 7; auto y = t.beta = "kaesekuchen"; @@ -58,6 +59,8 @@ int main() interpret(update(t), printer).flush(); interpret(update(t).set(t.gamma = false), printer).flush(); interpret(update(t).set(t.gamma = false).where(t.beta != "transparent"), printer).flush(); +#warning make this fail! + interpret(update(t).set(t.beta = f.delta).where(t.beta != "transparent"), printer).flush(); auto u = dynamic_update(db, t).dynamic_set(t.gamma = false).dynamic_where(); u.add_set(t.gamma = false); interpret(u, printer).flush();