0
0
mirror of https://github.com/rbock/sqlpp11.git synced 2024-11-15 20:31:16 +08:00

Intermediate state

This commit is contained in:
rbock 2016-04-06 10:16:35 +02:00
parent 90d769520c
commit 3e008a2b04
10 changed files with 222 additions and 53 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2013-2015, Roland Bock
* Copyright (c) 2013-2016, Roland Bock
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
@ -41,10 +41,11 @@
namespace sqlpp
{
SQLPP_PORTABLE_STATIC_ASSERT(assert_valid_rhs_comparison_operand_t, "invalid rhs operand in comparison");
SQLPP_PORTABLE_STATIC_ASSERT(assert_comparison_valid_rhs_operand_t, "invalid rhs operand in comparison");
SQLPP_PORTABLE_STATIC_ASSERT(assert_comparison_lhs_rhs_differ_t, "identical lhs and rhs operand types in comparison");
template <typename LhsValueType, typename RhsType>
using check_rhs_comparison_operand_t =
template <typename LhsType, typename LhsValueType, typename RhsType>
using check_rhs_comparison_operand_t = static_combined_check_t<
static_check_t<(is_expression_t<sqlpp::wrap_operand_t<RhsType>>::value // expressions are OK
or
is_multi_expression_t<sqlpp::wrap_operand_t<RhsType>>::value) // multi-expressions like ANY are
@ -52,14 +53,12 @@ namespace sqlpp
and
LhsValueType::template _is_valid_operand<
sqlpp::wrap_operand_t<RhsType>>::value, // the correct value type is required, of course
assert_valid_rhs_comparison_operand_t>;
assert_comparison_valid_rhs_operand_t>,
static_check_t<not std::is_same<LhsType, RhsType>::value, assert_comparison_lhs_rhs_differ_t>>;
SQLPP_PORTABLE_STATIC_ASSERT(assert_valid_in_arguments_t, "at least one operand of in() is not valid");
template <typename LhsValueType, typename... InTypes>
template <typename LhsType, typename LhsValueType, typename... InTypes>
using check_rhs_in_arguments_t =
static_check_t<logic::all_t<check_rhs_comparison_operand_t<LhsValueType, InTypes>::value...>::value,
assert_valid_in_arguments_t>;
static_combined_check_t<check_rhs_comparison_operand_t<LhsType, LhsValueType, InTypes>...>;
namespace detail
{
@ -118,7 +117,7 @@ namespace sqlpp
{
template <template <typename Lhs, typename Rhs> class NewExpr, typename T>
using _new_binary_expression_t =
new_binary_expression_t<check_rhs_comparison_operand_t<ValueType, wrap_operand_t<T>>,
new_binary_expression_t<check_rhs_comparison_operand_t<Expr, ValueType, wrap_operand_t<T>>,
NewExpr,
Expr,
wrap_operand_t<T>>;
@ -134,8 +133,8 @@ namespace sqlpp
template <template <typename Lhs, typename... Rhs> class NewExpr, typename... T>
struct _new_nary_expression
{
using type =
new_nary_expression_t<logic::all_t<check_rhs_comparison_operand_t<ValueType, wrap_operand_t<T>>::value...>,
using type = new_nary_expression_t<
logic::all_t<check_rhs_comparison_operand_t<Expr, ValueType, wrap_operand_t<T>>::value...>,
NewExpr,
Expr,
wrap_operand_t<T>...>;
@ -145,7 +144,7 @@ namespace sqlpp
_new_binary_expression_t<equal_to_t, T> operator==(T t) const
{
using rhs = wrap_operand_t<T>;
check_rhs_comparison_operand_t<ValueType, rhs>::_();
check_rhs_comparison_operand_t<Expr, ValueType, rhs>::_();
return {*static_cast<const Expr*>(this), {rhs{t}}};
}
@ -154,7 +153,7 @@ namespace sqlpp
_new_binary_expression_t<not_equal_to_t, T> operator!=(T t) const
{
using rhs = wrap_operand_t<T>;
check_rhs_comparison_operand_t<ValueType, rhs>::_();
check_rhs_comparison_operand_t<Expr, ValueType, rhs>::_();
return {*static_cast<const Expr*>(this), {rhs{t}}};
}
@ -163,7 +162,7 @@ namespace sqlpp
_new_binary_expression_t<less_than_t, T> operator<(T t) const
{
using rhs = wrap_operand_t<T>;
check_rhs_comparison_operand_t<ValueType, rhs>::_();
check_rhs_comparison_operand_t<Expr, ValueType, rhs>::_();
return {*static_cast<const Expr*>(this), rhs{t}};
}
@ -172,7 +171,7 @@ namespace sqlpp
_new_binary_expression_t<less_equal_t, T> operator<=(T t) const
{
using rhs = wrap_operand_t<T>;
check_rhs_comparison_operand_t<ValueType, rhs>::_();
check_rhs_comparison_operand_t<Expr, ValueType, rhs>::_();
return {*static_cast<const Expr*>(this), rhs{t}};
}
@ -181,7 +180,7 @@ namespace sqlpp
_new_binary_expression_t<greater_than_t, T> operator>(T t) const
{
using rhs = wrap_operand_t<T>;
check_rhs_comparison_operand_t<ValueType, rhs>::_();
check_rhs_comparison_operand_t<Expr, ValueType, rhs>::_();
return {*static_cast<const Expr*>(this), rhs{t}};
}
@ -190,7 +189,7 @@ namespace sqlpp
_new_binary_expression_t<greater_equal_t, T> operator>=(T t) const
{
using rhs = wrap_operand_t<T>;
check_rhs_comparison_operand_t<ValueType, rhs>::_();
check_rhs_comparison_operand_t<Expr, ValueType, rhs>::_();
return {*static_cast<const Expr*>(this), rhs{t}};
}
@ -220,13 +219,13 @@ namespace sqlpp
// template <typename... T>
// _new_nary_expression_t<in_t, T...> in(T... t) const
// {
// check_rhs_in_arguments_t<ValueType, wrap_operand_t<T>...>::_();
// check_rhs_in_arguments_t<Expr, ValueType, wrap_operand_t<T>...>::_();
// return {*static_cast<const Expr*>(this), wrap_operand_t<T>{t}...};
// }
template <typename... T>
typename _new_nary_expression<in_t, T...>::type in(T... t) const
{
check_rhs_in_arguments_t<ValueType, wrap_operand_t<T>...>::_();
check_rhs_in_arguments_t<Expr, ValueType, wrap_operand_t<T>...>::_();
return {*static_cast<const Expr*>(this), typename wrap_operand<T>::type{t}...};
}
@ -234,13 +233,13 @@ namespace sqlpp
// template <typename... T>
// _new_nary_expression_t<not_in_t, T...> not_in(T... t) const
// {
// check_rhs_in_arguments_t<ValueType, wrap_operand_t<T>...>::_();
// check_rhs_in_arguments_t<Expr, ValueType, wrap_operand_t<T>...>::_();
// return {*static_cast<const Expr*>(this), wrap_operand_t<T>{t}...};
// }
template <typename... T>
typename _new_nary_expression<not_in_t, T...>::type not_in(T... t) const
{
check_rhs_in_arguments_t<ValueType, wrap_operand_t<T>...>::_();
check_rhs_in_arguments_t<Expr, ValueType, wrap_operand_t<T>...>::_();
return {*static_cast<const Expr*>(this), typename wrap_operand<T>::type{t}...};
}

View File

@ -27,7 +27,6 @@
#ifndef SQLPP_FLOATING_POINT_RESULT_FIELD_H
#define SQLPP_FLOATING_POINT_RESULT_FIELD_H
#include <sqlpp11/basic_expression_operators.h>
#include <sqlpp11/exception.h>
#include <sqlpp11/result_field.h>
#include <sqlpp11/result_field_base.h>

View File

@ -44,10 +44,9 @@ int In(int, char* [])
const auto bar = test::TabBar{};
// Individual values
compare(__LINE__, foo.omega.in(foo.omega), "tab_foo.omega IN(tab_foo.omega)");
compare(__LINE__, foo.omega.in(foo.omega, bar.alpha), "tab_foo.omega IN(tab_foo.omega,tab_bar.alpha)");
compare(__LINE__, foo.omega.in(foo.omega, bar.alpha, sqlpp::value(17)),
"tab_foo.omega IN(tab_foo.omega,tab_bar.alpha,17)");
compare(__LINE__, foo.omega.in(17), "tab_foo.omega IN(17)");
compare(__LINE__, foo.omega.in(17, bar.alpha), "tab_foo.omega IN(17,tab_bar.alpha)");
compare(__LINE__, foo.omega.in(17, bar.alpha, sqlpp::value(19)), "tab_foo.omega IN(17,tab_bar.alpha,19)");
// Lists
compare(__LINE__, foo.omega.in(sqlpp::value_list(std::vector<float>{1.7f, 2.5f, 17.f, 0.f})),

View File

@ -36,5 +36,7 @@ test_compile(where)
test_compile(insert)
test_compile(date)
test_compile(date_time)
test_compile(text)
test_compile(no_self_compare)
test_compile(unwrapped_bool)

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2015-2015, Roland Bock
* Copyright (c) 2015-2016, Roland Bock
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
@ -66,20 +66,21 @@ namespace
void allowed_comparands()
{
static_check_comparison<sqlpp::consistent_t>(std::chrono::system_clock::now());
static_check_comparison<sqlpp::consistent_t>(t.someDayPoint);
static_check_comparison<sqlpp::consistent_t>(t.otherDayPoint);
static_check_comparison<sqlpp::consistent_t>(t.someTimePoint);
static_check_comparison<sqlpp::consistent_t>(t.otherTimePoint);
}
void disallowed_comparands()
{
static_check_comparison<sqlpp::assert_valid_rhs_comparison_operand_t>(17);
static_check_comparison<sqlpp::assert_valid_rhs_comparison_operand_t>('a');
static_check_comparison<sqlpp::assert_valid_rhs_comparison_operand_t>(std::string("a"));
static_check_comparison<sqlpp::assert_valid_rhs_comparison_operand_t>(t);
static_check_comparison<sqlpp::assert_valid_rhs_comparison_operand_t>(t.someBool);
static_check_comparison<sqlpp::assert_valid_rhs_comparison_operand_t>(t.someFloat);
static_check_comparison<sqlpp::assert_valid_rhs_comparison_operand_t>(t.someInt);
static_check_comparison<sqlpp::assert_valid_rhs_comparison_operand_t>(t.someString);
static_check_comparison<sqlpp::assert_comparison_valid_rhs_operand_t>(17);
static_check_comparison<sqlpp::assert_comparison_valid_rhs_operand_t>('a');
static_check_comparison<sqlpp::assert_comparison_valid_rhs_operand_t>(std::string("a"));
static_check_comparison<sqlpp::assert_comparison_valid_rhs_operand_t>(t);
static_check_comparison<sqlpp::assert_comparison_valid_rhs_operand_t>(t.someBool);
static_check_comparison<sqlpp::assert_comparison_valid_rhs_operand_t>(t.someFloat);
static_check_comparison<sqlpp::assert_comparison_valid_rhs_operand_t>(t.someInt);
static_check_comparison<sqlpp::assert_comparison_valid_rhs_operand_t>(t.someString);
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2015-2015, Roland Bock
* Copyright (c) 2015-2016, Roland Bock
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
@ -67,19 +67,20 @@ namespace
{
static_check_comparison<sqlpp::consistent_t>(std::chrono::system_clock::now());
static_check_comparison<sqlpp::consistent_t>(t.someDayPoint);
static_check_comparison<sqlpp::consistent_t>(t.someTimePoint);
static_check_comparison<sqlpp::consistent_t>(t.otherDayPoint);
static_check_comparison<sqlpp::consistent_t>(t.otherTimePoint);
}
void disallowed_comparands()
{
static_check_comparison<sqlpp::assert_valid_rhs_comparison_operand_t>(17);
static_check_comparison<sqlpp::assert_valid_rhs_comparison_operand_t>('a');
static_check_comparison<sqlpp::assert_valid_rhs_comparison_operand_t>(std::string("a"));
static_check_comparison<sqlpp::assert_valid_rhs_comparison_operand_t>(t);
static_check_comparison<sqlpp::assert_valid_rhs_comparison_operand_t>(t.someBool);
static_check_comparison<sqlpp::assert_valid_rhs_comparison_operand_t>(t.someFloat);
static_check_comparison<sqlpp::assert_valid_rhs_comparison_operand_t>(t.someInt);
static_check_comparison<sqlpp::assert_valid_rhs_comparison_operand_t>(t.someString);
static_check_comparison<sqlpp::assert_comparison_valid_rhs_operand_t>(17);
static_check_comparison<sqlpp::assert_comparison_valid_rhs_operand_t>('a');
static_check_comparison<sqlpp::assert_comparison_valid_rhs_operand_t>(std::string("a"));
static_check_comparison<sqlpp::assert_comparison_valid_rhs_operand_t>(t);
static_check_comparison<sqlpp::assert_comparison_valid_rhs_operand_t>(t.someBool);
static_check_comparison<sqlpp::assert_comparison_valid_rhs_operand_t>(t.someFloat);
static_check_comparison<sqlpp::assert_comparison_valid_rhs_operand_t>(t.someInt);
static_check_comparison<sqlpp::assert_comparison_valid_rhs_operand_t>(t.someString);
}
}

View File

@ -0,0 +1,74 @@
/*
* Copyright (c) 2015-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 <iostream>
#include <MockDb.h>
#include "AssertTables.h"
#include <sqlpp11/sqlpp11.h>
namespace
{
constexpr auto t = test::TabAllTypes{};
template <typename T>
void print_type_on_error(std::true_type)
{
}
template <typename T>
void print_type_on_error(std::false_type)
{
T::_print_me_;
}
template <typename Assert, typename Operand>
void static_check_comparison(const Operand& operand)
{
using CheckResult = sqlpp::check_rhs_comparison_operand_t<Operand, Operand>;
using ExpectedCheckResult = std::is_same<CheckResult, Assert>;
static_assert(ExpectedCheckResult::value, "Unexpected check result");
print_type_on_error<CheckResult>(ExpectedCheckResult{});
using ReturnType = sqlpp::detail::make_type_set_t<decltype(operand < operand), decltype(operand <= operand),
decltype(operand == operand), decltype(operand != operand),
decltype(operand >= operand), decltype(operand > operand)>;
using ExpectedReturnType =
sqlpp::logic::all_t<Assert::value xor
std::is_same<ReturnType, sqlpp::detail::type_set<sqlpp::bad_statement>>::value>;
static_assert(ExpectedReturnType::value, "Unexpected return type");
print_type_on_error<ReturnType>(ExpectedReturnType{});
}
void disallowed_self_comparison()
{
// static_check_comparison<sqlpp::assert_comparison_valid_rhs_operand_t>(t.someString);
}
}
int main(int, char* [])
{
t.someString == t.someString;
// disallowed_self_comparison();
}

View File

@ -0,0 +1,93 @@
/*
* Copyright (c) 2015-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 <iostream>
#include <MockDb.h>
#include "AssertTables.h"
#include <sqlpp11/sqlpp11.h>
namespace
{
constexpr auto t = test::TabAllTypes{};
template <typename T>
void print_type_on_error(std::true_type)
{
}
template <typename T>
void print_type_on_error(std::false_type)
{
T::_print_me_;
}
template <typename Assert, typename Operand>
void static_check_comparison(const Operand& operand)
{
using CheckResult = sqlpp::check_rhs_comparison_operand_t<decltype(t.someString), Operand>;
using ExpectedCheckResult = std::is_same<CheckResult, Assert>;
static_assert(ExpectedCheckResult::value, "Unexpected check result");
print_type_on_error<CheckResult>(ExpectedCheckResult{});
using ReturnType = sqlpp::detail::make_type_set_t<
decltype(t.someString < operand), decltype(t.someString <= operand), decltype(t.someString == operand),
decltype(t.someString != operand), decltype(t.someString >= operand), decltype(t.someString > operand),
decltype(t.someString.in(operand)), decltype(t.someString.in(operand, operand)),
decltype(t.someString.not_in(operand)), decltype(t.someString.not_in(operand, operand))>;
using ExpectedReturnType =
sqlpp::logic::all_t<Assert::value xor
std::is_same<ReturnType, sqlpp::detail::type_set<sqlpp::bad_statement>>::value>;
static_assert(ExpectedReturnType::value, "Unexpected return type");
print_type_on_error<ReturnType>(ExpectedReturnType{});
}
void allowed_comparands()
{
static_check_comparison<sqlpp::consistent_t>("");
static_check_comparison<sqlpp::consistent_t>('d');
static_check_comparison<sqlpp::consistent_t>(std::string(""));
static_check_comparison<sqlpp::consistent_t>(t.otherText);
}
void disallowed_comparands()
{
static_check_comparison<sqlpp::assert_comparison_valid_rhs_operand_t>(17);
static_check_comparison<sqlpp::assert_comparison_valid_rhs_operand_t>(17.4);
static_check_comparison<sqlpp::assert_comparison_valid_rhs_operand_t>(t);
static_check_comparison<sqlpp::assert_comparison_valid_rhs_operand_t>(t.someBool);
static_check_comparison<sqlpp::assert_comparison_valid_rhs_operand_t>(t.someFloat);
static_check_comparison<sqlpp::assert_comparison_valid_rhs_operand_t>(t.someInt);
static_check_comparison<sqlpp::assert_comparison_valid_rhs_operand_t>(t.someDayPoint);
static_check_comparison<sqlpp::assert_comparison_valid_rhs_operand_t>(t.someTimePoint);
}
}
int main(int, char* [])
{
#error : This must fail:
t.someString + 1;
allowed_comparands();
disallowed_comparands();
}

View File

@ -358,7 +358,8 @@ int SelectType(int, char* [])
static_assert(sqlpp::is_numeric_t<T>::value, "T has to be numeric");
static_assert(sqlpp::is_numeric_t<decltype(t.alpha)>::value, "TabBar.alpha has to be a numeric");
((t.alpha + 7) + 4).asc();
static_assert(sqlpp::is_boolean_t<decltype(t.gamma == t.gamma)>::value, "Comparison expression have to be boolean");
static_assert(sqlpp::is_boolean_t<decltype(t.gamma != not(t.gamma))>::value,
"Comparison expression have to be boolean");
!t.gamma;
serialize(t.beta < "kaesekuchen", printer).str();
serialize(t.beta + "hallenhalma", printer).str();

View File

@ -54,7 +54,7 @@ int Update(int, char* [])
serialize(update(t), printer).str();
serialize(update(t).set(t.gamma = false), printer).str();
serialize(update(t).set(t.gamma = false).where(t.beta != "transparent"), printer).str();
serialize(update(t).set(t.beta = "opaque").where(t.beta != t.beta), printer).str();
serialize(update(t).set(t.beta = "opaque").where(t.beta != t.beta + "this is nonsense"), printer).str();
auto u = dynamic_update(db, t).dynamic_set(t.gamma = false).dynamic_where();
u.assignments.add(t.beta = "cannot update gamma a second time");
u.where.add(t.gamma != false);