mirror of
https://github.com/rbock/sqlpp11.git
synced 2024-11-15 20:31:16 +08:00
Added value_or_null method/type
This commit is contained in:
parent
64c4ba029f
commit
18dc6b1a03
@ -39,7 +39,7 @@ namespace sqlpp
|
||||
// boolean value type
|
||||
struct boolean
|
||||
{
|
||||
using _traits = make_traits<boolean, ::sqlpp::tag::is_boolean, ::sqlpp::tag::is_expression>;
|
||||
using _traits = make_traits<boolean, ::sqlpp::tag::is_boolean, ::sqlpp::tag::is_value_type>;
|
||||
using _tag = ::sqlpp::tag::is_boolean;
|
||||
using _cpp_value_type = bool;
|
||||
|
||||
|
@ -38,7 +38,7 @@ namespace sqlpp
|
||||
// floating_point value type
|
||||
struct floating_point
|
||||
{
|
||||
using _traits = make_traits<floating_point, ::sqlpp::tag::is_floating_point, ::sqlpp::tag::is_expression>;
|
||||
using _traits = make_traits<floating_point, ::sqlpp::tag::is_floating_point, ::sqlpp::tag::is_value_type>;
|
||||
using _tag = ::sqlpp::tag::is_floating_point;
|
||||
using _cpp_value_type = double;
|
||||
|
||||
|
@ -42,10 +42,10 @@
|
||||
#include <sqlpp11/avg.h>
|
||||
#include <sqlpp11/sum.h>
|
||||
#include <sqlpp11/verbatim_table.h> // Csaba Csoma suggests: unsafe_sql instead of verbatim
|
||||
#include <sqlpp11/value_or_null.h>
|
||||
|
||||
namespace sqlpp
|
||||
{
|
||||
#warning add a value_or_null method that yields a type that can be NULL or have a value (very similar to an optional)
|
||||
#warning add a template<typename Db> bool_expression which takes any bool expression as constructor argument
|
||||
template<typename T>
|
||||
auto value(T t) -> wrap_operand_t<T>
|
||||
@ -101,6 +101,18 @@ namespace sqlpp
|
||||
return { context.str() };
|
||||
}
|
||||
|
||||
template<typename Expression>
|
||||
auto is_null(Expression e) -> decltype(e.is_null())
|
||||
{
|
||||
return e.is_null();
|
||||
}
|
||||
|
||||
template<typename Expression>
|
||||
auto is_not_null(Expression e) -> decltype(e.is_not_null())
|
||||
{
|
||||
return e.is_not_null();
|
||||
}
|
||||
|
||||
template<typename Container>
|
||||
struct value_list_t // to be used in .in() method
|
||||
{
|
||||
|
@ -40,7 +40,7 @@ namespace sqlpp
|
||||
// integral value type
|
||||
struct integral
|
||||
{
|
||||
using _traits = make_traits<integral, ::sqlpp::tag::is_integral, ::sqlpp::tag::is_expression>;
|
||||
using _traits = make_traits<integral, ::sqlpp::tag::is_integral, ::sqlpp::tag::is_value_type>;
|
||||
using _tag = ::sqlpp::tag::is_integral;
|
||||
using _cpp_value_type = int64_t;
|
||||
|
||||
|
@ -33,7 +33,7 @@ namespace sqlpp
|
||||
{
|
||||
struct null_t
|
||||
{
|
||||
using _traits = make_traits<no_value_t, tag::is_expression>;
|
||||
using _traits = make_traits<no_value_t, tag::is_expression, tag::is_sql_null>;
|
||||
using _recursive_traits = make_recursive_traits<>;
|
||||
};
|
||||
|
||||
|
@ -83,7 +83,7 @@ namespace sqlpp
|
||||
auto parameter(const ValueType&, const AliasProvider&)
|
||||
-> parameter_t<wrap_operand_t<ValueType>, AliasProvider>
|
||||
{
|
||||
static_assert(is_expression_t<ValueType>::value, "first argument is not a value type");
|
||||
static_assert(is_value_type_t<ValueType>::value, "first argument is not a value type");
|
||||
static_assert(is_alias_provider_t<AliasProvider>::value, "second argument is not an alias provider");
|
||||
return {};
|
||||
}
|
||||
|
@ -39,7 +39,7 @@ namespace sqlpp
|
||||
// text value type
|
||||
struct text
|
||||
{
|
||||
using _traits = make_traits<text, ::sqlpp::tag::is_text, ::sqlpp::tag::is_expression>;
|
||||
using _traits = make_traits<text, ::sqlpp::tag::is_text, ::sqlpp::tag::is_value_type>;
|
||||
using _tag = ::sqlpp::tag::is_text;
|
||||
using _cpp_value_type = std::string;
|
||||
|
||||
|
@ -71,6 +71,8 @@ namespace sqlpp
|
||||
template<typename T>\
|
||||
using name##_t = typename detail::name##_impl<T>::type;
|
||||
|
||||
SQLPP_VALUE_TRAIT_GENERATOR(is_value_type);
|
||||
SQLPP_VALUE_TRAIT_GENERATOR(is_sql_null);
|
||||
SQLPP_VALUE_TRAIT_GENERATOR(is_boolean);
|
||||
SQLPP_VALUE_TRAIT_GENERATOR(is_integral);
|
||||
SQLPP_VALUE_TRAIT_GENERATOR(is_floating_point);
|
||||
|
86
include/sqlpp11/value_or_null.h
Normal file
86
include/sqlpp11/value_or_null.h
Normal file
@ -0,0 +1,86 @@
|
||||
/*
|
||||
* Copyright (c) 2013-2014, 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_VALUE_OR_NULL_H
|
||||
#define SQLPP_VALUE_OR_NULL_H
|
||||
|
||||
#include <sqlpp11/parameter.h>
|
||||
#include <sqlpp11/parameter_list.h>
|
||||
#include <sqlpp11/column_types.h>
|
||||
#include <sqlpp11/in.h>
|
||||
#include <sqlpp11/value_type.h>
|
||||
#include <sqlpp11/exists.h>
|
||||
#include <sqlpp11/any.h>
|
||||
#include <sqlpp11/some.h>
|
||||
#include <sqlpp11/count.h>
|
||||
#include <sqlpp11/min.h>
|
||||
#include <sqlpp11/max.h>
|
||||
#include <sqlpp11/avg.h>
|
||||
#include <sqlpp11/sum.h>
|
||||
#include <sqlpp11/verbatim_table.h> // Csaba Csoma suggests: unsafe_sql instead of verbatim
|
||||
#include <sqlpp11/value_or_null.h>
|
||||
|
||||
namespace sqlpp
|
||||
{
|
||||
template<typename ValueType>
|
||||
struct value_or_null_t
|
||||
{
|
||||
using _cpp_value_type = typename ValueType::_cpp_value_type;
|
||||
|
||||
using _traits = make_traits<ValueType, tag::is_expression>;
|
||||
using _recursive_traits = make_recursive_traits<>;
|
||||
|
||||
value_or_null_t(_cpp_value_type value):
|
||||
_value(value),
|
||||
_is_null(false)
|
||||
{}
|
||||
|
||||
value_or_null_t(const null_t&):
|
||||
_value(),
|
||||
_is_null(true)
|
||||
{}
|
||||
|
||||
typename ValueType::_cpp_value_type _value;
|
||||
bool _is_null;
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
auto value_or_null(T t) -> value_or_null_t<value_type_of<wrap_operand_t<T>>>
|
||||
{
|
||||
static_assert(is_wrapped_value_t<wrap_operand_t<T>>::value, "value_or_null() is to be called with non-sql-type like int, or string or null");
|
||||
return { t };
|
||||
}
|
||||
|
||||
template<typename ValueType>
|
||||
auto value_or_null(null_t t) -> value_or_null_t<ValueType>
|
||||
{
|
||||
static_assert(is_value_type_t<ValueType>::value, "value_or_null() is to be called with non-sql-type like int, or string");
|
||||
return { t };
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif
|
@ -135,6 +135,12 @@ int main()
|
||||
using TI = decltype(t.alpha.is_null());
|
||||
using TF = decltype(f.omega.is_null());
|
||||
using TT = decltype(t.beta.is_null());
|
||||
using TTI = decltype(is_null(t.alpha));
|
||||
using TTF = decltype(is_null(f.omega));
|
||||
using TTT = decltype(is_null(t.beta));
|
||||
static_assert(std::is_same<TI, TTI>::value, "type requirement");
|
||||
static_assert(std::is_same<TF, TTF>::value, "type requirement");
|
||||
static_assert(std::is_same<TT, TTT>::value, "type requirement");
|
||||
static_assert(sqlpp::is_named_expression_t<TI>::value, "type requirement");
|
||||
static_assert(sqlpp::is_boolean_t<TI>::value, "type requirement");
|
||||
static_assert(not sqlpp::is_numeric_t<TI>::value, "type requirement");
|
||||
@ -154,6 +160,12 @@ int main()
|
||||
using TI = decltype(t.alpha.is_not_null());
|
||||
using TF = decltype(f.omega.is_not_null());
|
||||
using TT = decltype(t.beta.is_not_null());
|
||||
using TTI = decltype(is_not_null(t.alpha));
|
||||
using TTF = decltype(is_not_null(f.omega));
|
||||
using TTT = decltype(is_not_null(t.beta));
|
||||
static_assert(std::is_same<TI, TTI>::value, "type requirement");
|
||||
static_assert(std::is_same<TF, TTF>::value, "type requirement");
|
||||
static_assert(std::is_same<TT, TTT>::value, "type requirement");
|
||||
static_assert(sqlpp::is_named_expression_t<TI>::value, "type requirement");
|
||||
static_assert(sqlpp::is_boolean_t<TI>::value, "type requirement");
|
||||
static_assert(not sqlpp::is_numeric_t<TI>::value, "type requirement");
|
||||
@ -356,6 +368,30 @@ int main()
|
||||
static_assert(sqlpp::is_text_t<TT>::value, "type requirement");
|
||||
}
|
||||
|
||||
// test value_or_null
|
||||
{
|
||||
using TB = decltype(sqlpp::value_or_null(true));
|
||||
using TI = decltype(sqlpp::value_or_null(7));
|
||||
using TF = decltype(sqlpp::value_or_null(5.6));
|
||||
using TT = decltype(sqlpp::value_or_null("hallo"));
|
||||
using TBN = decltype(sqlpp::value_or_null<sqlpp::boolean>(sqlpp::null));
|
||||
using TIN = decltype(sqlpp::value_or_null<sqlpp::integral>(sqlpp::null));
|
||||
using TFN = decltype(sqlpp::value_or_null<sqlpp::floating_point>(sqlpp::null));
|
||||
using TTN = decltype(sqlpp::value_or_null<sqlpp::text>(sqlpp::null));
|
||||
static_assert(std::is_same<TB, TBN>::value, "type_requirement");
|
||||
static_assert(std::is_same<TI, TIN>::value, "type_requirement");
|
||||
static_assert(std::is_same<TF, TFN>::value, "type_requirement");
|
||||
static_assert(std::is_same<TT, TTN>::value, "type_requirement");
|
||||
static_assert(not sqlpp::is_named_expression_t<TB>::value, "type requirement");
|
||||
static_assert(sqlpp::is_boolean_t<TB>::value, "type requirement");
|
||||
static_assert(not sqlpp::is_named_expression_t<TB>::value, "type requirement");
|
||||
static_assert(sqlpp::is_integral_t<TI>::value, "type requirement");
|
||||
static_assert(not sqlpp::is_named_expression_t<TI>::value, "type requirement");
|
||||
static_assert(sqlpp::is_floating_point_t<TF>::value, "type requirement");
|
||||
static_assert(not sqlpp::is_named_expression_t<TT>::value, "type requirement");
|
||||
static_assert(sqlpp::is_text_t<TT>::value, "type requirement");
|
||||
}
|
||||
|
||||
// test verbatim
|
||||
{
|
||||
using TB = decltype(sqlpp::verbatim<sqlpp::boolean>("1"));
|
||||
|
Loading…
Reference in New Issue
Block a user