mirror of
https://github.com/rbock/sqlpp11.git
synced 2024-11-16 04:47:18 +08:00
More tests
This commit is contained in:
parent
d3daa90e0b
commit
25faf6c4bb
@ -78,6 +78,7 @@ namespace sqlpp
|
||||
template <typename T>
|
||||
using remove_dynamic_t = typename remove_dynamic<T>::type;
|
||||
|
||||
#warning: Should not turn Expr to into optional, but just the value type
|
||||
template <typename T>
|
||||
struct dynamic_to_optional
|
||||
{
|
||||
|
@ -44,6 +44,12 @@ namespace sqlpp
|
||||
using cpp_type = result_value_t<ValueType>;
|
||||
};
|
||||
|
||||
template <typename NameType, typename ValueType>
|
||||
struct name_tag_of<field_spec_t<NameType, ValueType>>
|
||||
{
|
||||
using type = NameType;
|
||||
};
|
||||
|
||||
template <typename Left, typename Right, typename Enable = void>
|
||||
struct is_field_compatible
|
||||
{
|
||||
|
@ -62,8 +62,8 @@ namespace sqlpp
|
||||
|
||||
struct op_is_null;
|
||||
struct op_is_not_null;
|
||||
struct is_distinct_from;
|
||||
struct is_not_distinct_from;
|
||||
struct op_is_distinct_from;
|
||||
struct op_is_not_distinct_from;
|
||||
|
||||
template <typename L>
|
||||
struct value_type_of<comparison_expression<L, op_is_null, sqlpp::compat::nullopt_t>> { using type = boolean; };
|
||||
@ -72,10 +72,10 @@ namespace sqlpp
|
||||
struct value_type_of<comparison_expression<L, op_is_not_null, sqlpp::compat::nullopt_t>> { using type = boolean; };
|
||||
|
||||
template <typename L, typename R>
|
||||
struct value_type_of<comparison_expression<L, is_distinct_from, R>> { using type = boolean; };
|
||||
struct value_type_of<comparison_expression<L, op_is_distinct_from, R>> { using type = boolean; };
|
||||
|
||||
template <typename L, typename R>
|
||||
struct value_type_of<comparison_expression<L, is_not_distinct_from, R>> { using type = boolean; };
|
||||
struct value_type_of<comparison_expression<L, op_is_not_distinct_from, R>> { using type = boolean; };
|
||||
|
||||
|
||||
template <typename L, typename Operator, typename R>
|
||||
|
@ -712,13 +712,21 @@ namespace sqlpp
|
||||
using parameters_of = typename detail::parameters_of_impl<T>::type;
|
||||
|
||||
struct name_tag_base{}; // Used by SQLPP_ALIAS_PROVIDER and ddl2cpp
|
||||
template <typename T, bool IsNameTag>
|
||||
struct name_tag_of_impl
|
||||
{
|
||||
using type = no_name_t;
|
||||
};
|
||||
template <typename T>
|
||||
struct name_tag_of_impl<T, true>
|
||||
{
|
||||
using type = typename T::_alias_t;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct name_tag_of
|
||||
{
|
||||
using type = typename std::conditional<std::is_base_of<name_tag_base, T>::value, // if T is a name tag
|
||||
typename T::_alias_t, // then it is embedded,
|
||||
no_name_t // else, there is no default name
|
||||
>::type;
|
||||
using type = typename name_tag_of_impl<T, std::is_base_of<name_tag_base, T>::value>::type;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
|
@ -37,6 +37,7 @@ endfunction()
|
||||
test_compile(any)
|
||||
test_compile(aggregate_functions)
|
||||
test_compile(comparison_expression)
|
||||
test_compile(dynamic)
|
||||
test_compile(in_expression)
|
||||
test_compile(logical_expression)
|
||||
test_compile(result_row)
|
||||
|
110
tests/core/types/dynamic.cpp
Normal file
110
tests/core/types/dynamic.cpp
Normal file
@ -0,0 +1,110 @@
|
||||
/*
|
||||
* 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 "MockDb.h"
|
||||
#include "Sample.h"
|
||||
#include <sqlpp11/sqlpp11.h>
|
||||
|
||||
namespace
|
||||
{
|
||||
auto db = MockDb{};
|
||||
}
|
||||
|
||||
SQLPP_ALIAS_PROVIDER(r_not_null);
|
||||
SQLPP_ALIAS_PROVIDER(r_maybe_null);
|
||||
|
||||
template <typename T, typename ValueType>
|
||||
using is_value_type = std::is_same<sqlpp::value_type_of_t<T>, ValueType>;
|
||||
|
||||
template<typename Value>
|
||||
void test_dynamic(Value v)
|
||||
{
|
||||
using ValueType = sqlpp::value_type_of_t<Value>;
|
||||
using OptValueType = sqlpp::compat::optional<ValueType>;
|
||||
|
||||
auto v_not_null= dynamic(true, sqlpp::value(v));
|
||||
auto v_maybe_null= dynamic(true, sqlpp::value(sqlpp::compat::make_optional(v)));
|
||||
auto v_not_null_alias = dynamic(true, sqlpp::value(v).as(r_not_null));
|
||||
auto v_maybe_null_alias = dynamic(true, sqlpp::value(sqlpp::compat::make_optional(v)).as(r_maybe_null));
|
||||
|
||||
static_assert(not sqlpp::has_value_type<decltype(v_not_null)>::value, "");
|
||||
static_assert(not sqlpp::has_value_type<decltype(v_maybe_null)>::value, "");
|
||||
static_assert(not sqlpp::has_value_type<decltype(v_not_null_alias)>::value, "");
|
||||
static_assert(not sqlpp::has_value_type<decltype(v_maybe_null_alias)>::value, "");
|
||||
|
||||
static_assert(not sqlpp::has_name<decltype(v_not_null)>::value, "");
|
||||
static_assert(not sqlpp::has_name<decltype(v_maybe_null)>::value, "");
|
||||
static_assert(sqlpp::has_name<decltype(v_not_null_alias)>::value, "");
|
||||
static_assert(sqlpp::has_name<decltype(v_maybe_null_alias)>::value, "");
|
||||
|
||||
static_assert(is_value_type<sqlpp::remove_dynamic_t<decltype(v_not_null)>, ValueType>::value, "");
|
||||
static_assert(is_value_type<sqlpp::remove_dynamic_t<decltype(v_maybe_null)>, OptValueType>::value, "");
|
||||
static_assert(is_value_type<sqlpp::remove_dynamic_t<decltype(v_not_null_alias)>, ValueType>::value, "");
|
||||
static_assert(is_value_type<sqlpp::remove_dynamic_t<decltype(v_maybe_null_alias)>, OptValueType>::value, "");
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
// boolean
|
||||
test_dynamic(bool{true});
|
||||
|
||||
// integral
|
||||
test_dynamic(int8_t{7});
|
||||
test_dynamic(int16_t{7});
|
||||
test_dynamic(int32_t{7});
|
||||
test_dynamic(int64_t{7});
|
||||
|
||||
// unsigned integral
|
||||
test_dynamic(uint8_t{7});
|
||||
test_dynamic(uint16_t{7});
|
||||
test_dynamic(uint32_t{7});
|
||||
test_dynamic(uint64_t{7});
|
||||
|
||||
// floating point
|
||||
test_dynamic(float{7.7});
|
||||
test_dynamic(double{7.7});
|
||||
|
||||
// text
|
||||
test_dynamic('7');
|
||||
test_dynamic("seven");
|
||||
test_dynamic(std::string("seven"));
|
||||
test_dynamic(sqlpp::compat::string_view("seven"));
|
||||
|
||||
// blob
|
||||
test_dynamic(std::vector<uint8_t>{});
|
||||
|
||||
// date
|
||||
test_dynamic(::sqlpp::chrono::day_point{});
|
||||
|
||||
// timestamp
|
||||
test_dynamic(::sqlpp::chrono::microsecond_point{});
|
||||
using minute_point = std::chrono::time_point<std::chrono::system_clock, std::chrono::minutes>;
|
||||
test_dynamic(minute_point{});
|
||||
|
||||
// time_of_day
|
||||
test_dynamic(std::chrono::microseconds{});
|
||||
|
||||
}
|
||||
|
@ -50,22 +50,16 @@ void test_value(Value v)
|
||||
auto v_maybe_null= sqlpp::value(sqlpp::compat::make_optional(v));
|
||||
auto v_not_null_alias = sqlpp::value(v).as(r_not_null);
|
||||
auto v_maybe_null_alias = sqlpp::value(sqlpp::compat::make_optional(v)).as(r_maybe_null);
|
||||
auto v_opt_not_null_alias = sqlpp::value(v).as(r_opt_not_null).if_(true);
|
||||
auto v_opt_maybe_null_alias = sqlpp::value(sqlpp::compat::make_optional(v)).as(r_opt_maybe_null).if_(true);
|
||||
|
||||
static_assert(is_value_type<decltype(v_not_null), ValueType>::value, "");
|
||||
static_assert(is_value_type<decltype(v_maybe_null), OptValueType>::value, "");
|
||||
static_assert(is_value_type<decltype(v_not_null_alias), ValueType>::value, "");
|
||||
static_assert(is_value_type<decltype(v_maybe_null_alias), OptValueType>::value, "");
|
||||
static_assert(is_value_type<decltype(v_opt_not_null_alias), OptValueType>::value, "");
|
||||
static_assert(is_value_type<decltype(v_opt_maybe_null_alias), OptValueType>::value, "");
|
||||
|
||||
static_assert(not sqlpp::can_be_null<decltype(v_not_null)>::value, "");
|
||||
static_assert(sqlpp::can_be_null<decltype(v_maybe_null)>::value, "");
|
||||
static_assert(not sqlpp::can_be_null<decltype(v_not_null_alias)>::value, "");
|
||||
static_assert(sqlpp::can_be_null<decltype(v_maybe_null_alias)>::value, "");
|
||||
static_assert(sqlpp::can_be_null<decltype(v_opt_not_null_alias)>::value, "");
|
||||
static_assert(sqlpp::can_be_null<decltype(v_opt_maybe_null_alias)>::value, "");
|
||||
}
|
||||
|
||||
int main()
|
||||
|
@ -86,16 +86,16 @@ struct MockDb : public sqlpp::connection
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static _serializer_context_t& _serialize_interpretable(const T& t, _serializer_context_t& context)
|
||||
static _serializer_context_t& _serialize_interpretable(const T& x, _serializer_context_t& context)
|
||||
{
|
||||
serialize(t, context);
|
||||
serialize(context, x);
|
||||
return context;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static _serializer_context_t& _interpret_interpretable(const T& t, _interpreter_context_t& context)
|
||||
static _serializer_context_t& _interpret_interpretable(const T& x, _interpreter_context_t& context)
|
||||
{
|
||||
serialize(t, context);
|
||||
serialize(context, x);
|
||||
return context;
|
||||
}
|
||||
|
||||
@ -141,7 +141,7 @@ struct MockDb : public sqlpp::connection
|
||||
size_t execute(const Statement& x)
|
||||
{
|
||||
_serializer_context_t context;
|
||||
serialize(x, context);
|
||||
serialize(context, x);
|
||||
std::cout << "Running execute call with\n" << context.str() << std::endl;
|
||||
return execute(context.str());
|
||||
}
|
||||
@ -150,7 +150,7 @@ struct MockDb : public sqlpp::connection
|
||||
size_t insert(const Insert& x)
|
||||
{
|
||||
_serializer_context_t context;
|
||||
serialize(x, context);
|
||||
serialize(context, x);
|
||||
std::cout << "Running insert call with\n" << context.str() << std::endl;
|
||||
return 0;
|
||||
}
|
||||
@ -159,7 +159,7 @@ struct MockDb : public sqlpp::connection
|
||||
size_t update(const Update& x)
|
||||
{
|
||||
_serializer_context_t context;
|
||||
serialize(x, context);
|
||||
serialize(context, x);
|
||||
std::cout << "Running update call with\n" << context.str() << std::endl;
|
||||
return 0;
|
||||
}
|
||||
@ -168,7 +168,7 @@ struct MockDb : public sqlpp::connection
|
||||
size_t remove(const Remove& x)
|
||||
{
|
||||
_serializer_context_t context;
|
||||
serialize(x, context);
|
||||
serialize(context, x);
|
||||
std::cout << "Running remove call with\n" << context.str() << std::endl;
|
||||
return 0;
|
||||
}
|
||||
@ -177,7 +177,7 @@ struct MockDb : public sqlpp::connection
|
||||
result_t select(const Select& x)
|
||||
{
|
||||
_serializer_context_t context;
|
||||
serialize(x, context);
|
||||
serialize(context, x);
|
||||
std::cout << "Running select call with\n" << context.str() << std::endl;
|
||||
return {};
|
||||
}
|
||||
@ -204,7 +204,7 @@ struct MockDb : public sqlpp::connection
|
||||
_prepared_statement_t prepare_execute(Statement& x)
|
||||
{
|
||||
_serializer_context_t context;
|
||||
serialize(x, context);
|
||||
serialize(context, x);
|
||||
std::cout << "Running prepare execute call with\n" << context.str() << std::endl;
|
||||
return nullptr;
|
||||
}
|
||||
@ -213,7 +213,7 @@ struct MockDb : public sqlpp::connection
|
||||
_prepared_statement_t prepare_insert(Insert& x)
|
||||
{
|
||||
_serializer_context_t context;
|
||||
serialize(x, context);
|
||||
serialize(context, x);
|
||||
std::cout << "Running prepare insert call with\n" << context.str() << std::endl;
|
||||
return nullptr;
|
||||
}
|
||||
@ -234,7 +234,7 @@ struct MockDb : public sqlpp::connection
|
||||
_prepared_statement_t prepare_select(Select& x)
|
||||
{
|
||||
_serializer_context_t context;
|
||||
serialize(x, context);
|
||||
serialize(context, x);
|
||||
std::cout << "Running prepare select call with\n" << context.str() << std::endl;
|
||||
return nullptr;
|
||||
}
|
||||
@ -300,16 +300,16 @@ struct MockSizeDb : public sqlpp::connection
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static _serializer_context_t& _serialize_interpretable(const T& t, _serializer_context_t& context)
|
||||
static _serializer_context_t& _serialize_interpretable(const T& x, _serializer_context_t& context)
|
||||
{
|
||||
serialize(t, context);
|
||||
serialize(context, x);
|
||||
return context;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static _serializer_context_t& _interpret_interpretable(const T& t, _interpreter_context_t& context)
|
||||
static _serializer_context_t& _interpret_interpretable(const T& x, _interpreter_context_t& context)
|
||||
{
|
||||
serialize(t, context);
|
||||
serialize(context, x);
|
||||
return context;
|
||||
}
|
||||
|
||||
@ -349,7 +349,7 @@ struct MockSizeDb : public sqlpp::connection
|
||||
size_t execute(const Statement& x)
|
||||
{
|
||||
_serializer_context_t context;
|
||||
serialize(x, context);
|
||||
serialize(context, x);
|
||||
std::cout << "Running execute call with\n" << context.str() << std::endl;
|
||||
return execute(context.str());
|
||||
}
|
||||
@ -358,7 +358,7 @@ struct MockSizeDb : public sqlpp::connection
|
||||
size_t insert(const Insert& x)
|
||||
{
|
||||
_serializer_context_t context;
|
||||
serialize(x, context);
|
||||
serialize(context, x);
|
||||
std::cout << "Running insert call with\n" << context.str() << std::endl;
|
||||
return 0;
|
||||
}
|
||||
@ -367,7 +367,7 @@ struct MockSizeDb : public sqlpp::connection
|
||||
size_t update(const Update& x)
|
||||
{
|
||||
_serializer_context_t context;
|
||||
serialize(x, context);
|
||||
serialize(context, x);
|
||||
std::cout << "Running update call with\n" << context.str() << std::endl;
|
||||
return 0;
|
||||
}
|
||||
@ -376,7 +376,7 @@ struct MockSizeDb : public sqlpp::connection
|
||||
size_t remove(const Remove& x)
|
||||
{
|
||||
_serializer_context_t context;
|
||||
serialize(x, context);
|
||||
serialize(context, x);
|
||||
std::cout << "Running remove call with\n" << context.str() << std::endl;
|
||||
return 0;
|
||||
}
|
||||
@ -385,7 +385,7 @@ struct MockSizeDb : public sqlpp::connection
|
||||
result_t select(const Select& x)
|
||||
{
|
||||
_serializer_context_t context;
|
||||
serialize(x, context);
|
||||
serialize(context, x);
|
||||
std::cout << "Running select call with\n" << context.str() << std::endl;
|
||||
return {};
|
||||
}
|
||||
@ -412,7 +412,7 @@ struct MockSizeDb : public sqlpp::connection
|
||||
_prepared_statement_t prepare_execute(Statement& x)
|
||||
{
|
||||
_serializer_context_t context;
|
||||
serialize(x, context);
|
||||
serialize(context, x);
|
||||
std::cout << "Running prepare execute call with\n" << context.str() << std::endl;
|
||||
return nullptr;
|
||||
}
|
||||
@ -421,7 +421,7 @@ struct MockSizeDb : public sqlpp::connection
|
||||
_prepared_statement_t prepare_insert(Insert& x)
|
||||
{
|
||||
_serializer_context_t context;
|
||||
serialize(x, context);
|
||||
serialize(context, x);
|
||||
std::cout << "Running prepare insert call with\n" << context.str() << std::endl;
|
||||
return nullptr;
|
||||
}
|
||||
@ -442,7 +442,7 @@ struct MockSizeDb : public sqlpp::connection
|
||||
_prepared_statement_t prepare_select(Select& x)
|
||||
{
|
||||
_serializer_context_t context;
|
||||
serialize(x, context);
|
||||
serialize(context, x);
|
||||
std::cout << "Running prepare select call with\n" << context.str() << std::endl;
|
||||
return nullptr;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user