mirror of
https://github.com/rbock/sqlpp11.git
synced 2024-11-16 12:51:13 +08:00
More tests
This commit is contained in:
parent
d2cb720746
commit
01c1e69eb3
@ -26,21 +26,19 @@
|
|||||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <sqlpp11/enable_comparison.h>
|
||||||
#include <sqlpp11/type_traits.h>
|
#include <sqlpp11/type_traits.h>
|
||||||
#include <sqlpp11/alias_provider.h>
|
#include <sqlpp11/alias_provider.h>
|
||||||
#include <sqlpp11/expression_operators.h>
|
|
||||||
#include <sqlpp11/detail/type_set.h>
|
#include <sqlpp11/detail/type_set.h>
|
||||||
|
|
||||||
namespace sqlpp
|
namespace sqlpp
|
||||||
{
|
{
|
||||||
template <typename ValueType, typename NameType>
|
template <typename ValueType, typename NameType>
|
||||||
struct parameter_t : public expression_operators<parameter_t<ValueType, NameType>, ValueType>
|
struct parameter_t : public enable_comparison<parameter_t<ValueType, NameType>>
|
||||||
{
|
{
|
||||||
using _traits = make_traits<ValueType, tag::is_parameter, tag::is_expression>;
|
using _traits = make_traits<ValueType, tag::is_parameter, tag::is_expression>;
|
||||||
|
|
||||||
using _nodes = detail::type_vector<>;
|
|
||||||
using _parameters = detail::type_vector<parameter_t>;
|
using _parameters = detail::type_vector<parameter_t>;
|
||||||
using _can_be_null = std::true_type;
|
|
||||||
using _is_literal_expression = std::true_type;
|
using _is_literal_expression = std::true_type;
|
||||||
|
|
||||||
using _instance_t = member_t<NameType, parameter_value_t<ValueType>>;
|
using _instance_t = member_t<NameType, parameter_value_t<ValueType>>;
|
||||||
@ -54,6 +52,17 @@ namespace sqlpp
|
|||||||
~parameter_t() = default;
|
~parameter_t() = default;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template<typename ValueType, typename NameType>
|
||||||
|
struct value_type_of<parameter_t<ValueType, NameType>>
|
||||||
|
{
|
||||||
|
using type = ValueType;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename ValueType, typename NameType>
|
||||||
|
struct name_tag_of<parameter_t<ValueType, NameType>> : public name_tag_of<NameType>
|
||||||
|
{
|
||||||
|
};
|
||||||
|
|
||||||
template <typename Context, typename ValueType, typename NameType>
|
template <typename Context, typename ValueType, typename NameType>
|
||||||
Context& serialize(Context& context, const parameter_t<ValueType, NameType>&)
|
Context& serialize(Context& context, const parameter_t<ValueType, NameType>&)
|
||||||
{
|
{
|
||||||
@ -70,9 +79,9 @@ namespace sqlpp
|
|||||||
|
|
||||||
template <typename ValueType, typename AliasProvider>
|
template <typename ValueType, typename AliasProvider>
|
||||||
auto parameter(const ValueType& /*unused*/, const AliasProvider & /*unused*/)
|
auto parameter(const ValueType& /*unused*/, const AliasProvider & /*unused*/)
|
||||||
-> parameter_t<ValueType, AliasProvider>
|
-> parameter_t<value_type_of_t<ValueType>, AliasProvider>
|
||||||
{
|
{
|
||||||
static_assert(is_value_type_t<ValueType>::value, "first argument is not a value type");
|
static_assert(has_value_type<ValueType>::value, "first argument is not a value type");
|
||||||
static_assert(has_name<AliasProvider>::value, "second argument does not have a name");
|
static_assert(has_name<AliasProvider>::value, "second argument does not have a name");
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
@ -153,10 +153,14 @@ namespace sqlpp
|
|||||||
//
|
//
|
||||||
struct boolean{};
|
struct boolean{};
|
||||||
template<>
|
template<>
|
||||||
|
struct value_type_of<boolean> { using type = boolean; };
|
||||||
|
template<>
|
||||||
struct value_type_of<bool> { using type = boolean; };
|
struct value_type_of<bool> { using type = boolean; };
|
||||||
|
|
||||||
struct integral{};
|
struct integral{};
|
||||||
template<>
|
template<>
|
||||||
|
struct value_type_of<integral> { using type = integral; };
|
||||||
|
template<>
|
||||||
struct value_type_of<int8_t> { using type = integral; };
|
struct value_type_of<int8_t> { using type = integral; };
|
||||||
template<>
|
template<>
|
||||||
struct value_type_of<int16_t> { using type = integral; };
|
struct value_type_of<int16_t> { using type = integral; };
|
||||||
@ -167,6 +171,8 @@ namespace sqlpp
|
|||||||
|
|
||||||
struct unsigned_integral{};
|
struct unsigned_integral{};
|
||||||
template<>
|
template<>
|
||||||
|
struct value_type_of<unsigned_integral> { using type = unsigned_integral; };
|
||||||
|
template<>
|
||||||
struct value_type_of<uint8_t> { using type = unsigned_integral; };
|
struct value_type_of<uint8_t> { using type = unsigned_integral; };
|
||||||
template<>
|
template<>
|
||||||
struct value_type_of<uint16_t> { using type = unsigned_integral; };
|
struct value_type_of<uint16_t> { using type = unsigned_integral; };
|
||||||
@ -177,6 +183,8 @@ namespace sqlpp
|
|||||||
|
|
||||||
struct floating_point{};
|
struct floating_point{};
|
||||||
template<>
|
template<>
|
||||||
|
struct value_type_of<floating_point> { using type = floating_point; };
|
||||||
|
template<>
|
||||||
struct value_type_of<float> { using type = floating_point; };
|
struct value_type_of<float> { using type = floating_point; };
|
||||||
template<>
|
template<>
|
||||||
struct value_type_of<double> { using type = floating_point; };
|
struct value_type_of<double> { using type = floating_point; };
|
||||||
@ -185,6 +193,8 @@ namespace sqlpp
|
|||||||
|
|
||||||
struct text{};
|
struct text{};
|
||||||
template <>
|
template <>
|
||||||
|
struct value_type_of<text> { using type = text; };
|
||||||
|
template <>
|
||||||
struct value_type_of<char> { using type = text; };
|
struct value_type_of<char> { using type = text; };
|
||||||
template <>
|
template <>
|
||||||
struct value_type_of<const char*> { using type = text; };
|
struct value_type_of<const char*> { using type = text; };
|
||||||
@ -194,24 +204,30 @@ namespace sqlpp
|
|||||||
struct value_type_of<sqlpp::compat::string_view> { using type = text; };
|
struct value_type_of<sqlpp::compat::string_view> { using type = text; };
|
||||||
|
|
||||||
struct blob{};
|
struct blob{};
|
||||||
|
template <>
|
||||||
|
struct value_type_of<blob> { using type = blob; };
|
||||||
template <std::size_t N>
|
template <std::size_t N>
|
||||||
struct value_type_of<std::array<std::uint8_t, N>> { using type = blob; };
|
struct value_type_of<std::array<std::uint8_t, N>> { using type = blob; };
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
struct value_type_of<std::vector<std::uint8_t>> { using type = blob; };
|
struct value_type_of<std::vector<std::uint8_t>> { using type = blob; };
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
struct value_type_of<sqlpp::compat::span<std::uint8_t>> { using type = blob; };
|
struct value_type_of<sqlpp::compat::span<std::uint8_t>> { using type = blob; };
|
||||||
|
|
||||||
struct day_point{};
|
struct day_point{};
|
||||||
template <>
|
template <>
|
||||||
|
struct value_type_of<day_point> { using type = day_point; };
|
||||||
|
template <>
|
||||||
struct value_type_of<std::chrono::time_point<std::chrono::system_clock, sqlpp::chrono::days>> { using type = day_point; };
|
struct value_type_of<std::chrono::time_point<std::chrono::system_clock, sqlpp::chrono::days>> { using type = day_point; };
|
||||||
|
|
||||||
struct time_of_day{};
|
struct time_of_day{};
|
||||||
|
template <>
|
||||||
|
struct value_type_of<time_of_day> { using type = time_of_day; };
|
||||||
template <typename Rep, typename Period>
|
template <typename Rep, typename Period>
|
||||||
struct value_type_of<std::chrono::duration<Rep, Period>> { using type = time_of_day; };
|
struct value_type_of<std::chrono::duration<Rep, Period>> { using type = time_of_day; };
|
||||||
|
|
||||||
struct time_point{};
|
struct time_point{};
|
||||||
|
template <>
|
||||||
|
struct value_type_of<time_point> { using type = time_point; };
|
||||||
template <typename Period>
|
template <typename Period>
|
||||||
struct value_type_of<std::chrono::time_point<std::chrono::system_clock, Period>> { using type = time_point; };
|
struct value_type_of<std::chrono::time_point<std::chrono::system_clock, Period>> { using type = time_point; };
|
||||||
|
|
||||||
@ -726,8 +742,8 @@ namespace sqlpp
|
|||||||
using _contains_aggregate_function = std::true_type;
|
using _contains_aggregate_function = std::true_type;
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename NameProvider, typename Member>
|
template <typename NameTagProvider, typename Member>
|
||||||
using member_t = typename NameProvider::_alias_t::template _member_t<Member>;
|
using member_t = typename name_tag_of_t<NameTagProvider>::template _member_t<Member>;
|
||||||
|
|
||||||
template <typename Policies>
|
template <typename Policies>
|
||||||
using derived_statement_t = typename Policies::_statement_t;
|
using derived_statement_t = typename Policies::_statement_t;
|
||||||
|
@ -27,7 +27,6 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include <sqlpp11/operator/as_expression.h>
|
#include <sqlpp11/operator/as_expression.h>
|
||||||
#include <sqlpp11/expression_operators.h>
|
|
||||||
#include <sqlpp11/type_traits.h>
|
#include <sqlpp11/type_traits.h>
|
||||||
|
|
||||||
namespace sqlpp
|
namespace sqlpp
|
||||||
|
@ -30,10 +30,10 @@ set(test_files
|
|||||||
Blob.cpp
|
Blob.cpp
|
||||||
Count.cpp
|
Count.cpp
|
||||||
#CustomQuery.cpp
|
#CustomQuery.cpp
|
||||||
#DynamicWhere.cpp
|
DynamicWhere.cpp
|
||||||
Exists.cpp
|
Exists.cpp
|
||||||
Float.cpp
|
Float.cpp
|
||||||
#ForUpdate.cpp
|
ForUpdate.cpp
|
||||||
From.cpp
|
From.cpp
|
||||||
In.cpp
|
In.cpp
|
||||||
Insert.cpp
|
Insert.cpp
|
||||||
@ -44,6 +44,8 @@ set(test_files
|
|||||||
Min.cpp
|
Min.cpp
|
||||||
Operator.cpp
|
Operator.cpp
|
||||||
Over.cpp
|
Over.cpp
|
||||||
|
Parameter.cpp
|
||||||
|
ParameterizedVerbatim.cpp
|
||||||
SelectAs.cpp
|
SelectAs.cpp
|
||||||
SelectColumns.cpp
|
SelectColumns.cpp
|
||||||
SelectFlags.cpp
|
SelectFlags.cpp
|
||||||
@ -52,7 +54,6 @@ set(test_files
|
|||||||
Trim.cpp
|
Trim.cpp
|
||||||
Upper.cpp
|
Upper.cpp
|
||||||
Where.cpp
|
Where.cpp
|
||||||
#ParameterizedVerbatim.cpp
|
|
||||||
)
|
)
|
||||||
|
|
||||||
create_test_sourcelist(test_sources test_serializer_main.cpp ${test_files})
|
create_test_sourcelist(test_sources test_serializer_main.cpp ${test_files})
|
||||||
|
@ -31,12 +31,15 @@
|
|||||||
|
|
||||||
int DynamicWhere(int, char*[])
|
int DynamicWhere(int, char*[])
|
||||||
{
|
{
|
||||||
|
const auto foo = test::TabFoo{};
|
||||||
const auto bar = test::TabBar{};
|
const auto bar = test::TabBar{};
|
||||||
auto db = MockDb{};
|
auto db = MockDb{};
|
||||||
|
|
||||||
compare(__LINE__, sqlpp::unconditionally(), "");
|
compare(__LINE__, sqlpp::unconditionally(), "");
|
||||||
compare(__LINE__, where(bar.boolNn), " WHERE tab_bar.bool_nn");
|
compare(__LINE__, where(bar.boolNn), " WHERE tab_bar.bool_nn");
|
||||||
|
|
||||||
#warning add tests with optional expressions
|
compare(__LINE__, where(bar.boolNn and dynamic(true, foo.boolN)), " WHERE (tab_bar.bool_nn AND tab_foo.bool_n)");
|
||||||
|
compare(__LINE__, where(bar.boolNn and dynamic(false, foo.boolN)), " WHERE tab_bar.bool_nn");
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -27,36 +27,12 @@
|
|||||||
#include "Sample.h"
|
#include "Sample.h"
|
||||||
#include <sqlpp11/sqlpp11.h>
|
#include <sqlpp11/sqlpp11.h>
|
||||||
|
|
||||||
#include <iostream>
|
|
||||||
|
|
||||||
namespace
|
|
||||||
{
|
|
||||||
/*
|
|
||||||
auto getTrue() -> std::string
|
|
||||||
{
|
|
||||||
MockDb::_serializer_context_t printer = {};
|
|
||||||
return serialize(sqlpp::value(true), printer).str();
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
auto getFalse() -> std::string
|
|
||||||
{
|
|
||||||
MockDb::_serializer_context_t printer = {};
|
|
||||||
return serialize(sqlpp::value(false), printer).str();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int ForUpdate(int, char* [])
|
int ForUpdate(int, char* [])
|
||||||
{
|
{
|
||||||
const auto foo = test::TabFoo{};
|
const auto foo = test::TabFoo{};
|
||||||
// const auto bar = test::TabBar{};
|
|
||||||
|
|
||||||
// Unconditionally
|
|
||||||
compare(__LINE__, select(foo.doubleN).from(foo).unconditionally().for_update(),
|
compare(__LINE__, select(foo.doubleN).from(foo).unconditionally().for_update(),
|
||||||
"SELECT tab_foo.double_n FROM tab_foo FOR UPDATE ");
|
"SELECT tab_foo.double_n FROM tab_foo FOR UPDATE ");
|
||||||
|
|
||||||
// Never
|
|
||||||
compare(__LINE__, where(sqlpp::value(false)), " WHERE " + getFalse());
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
41
tests/core/serialize/Parameter.cpp
Normal file
41
tests/core/serialize/Parameter.cpp
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
/*
|
||||||
|
* 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 "compare.h"
|
||||||
|
#include "Sample.h"
|
||||||
|
#include <sqlpp11/sqlpp11.h>
|
||||||
|
|
||||||
|
int Parameter(int, char*[])
|
||||||
|
{
|
||||||
|
const auto foo = test::TabFoo{};
|
||||||
|
const auto bar = test::TabBar{};
|
||||||
|
|
||||||
|
compare(__LINE__, parameter(foo.doubleN), "?");
|
||||||
|
compare(__LINE__, bar.id > parameter(foo.doubleN), "(tab_bar.id > ?)");
|
||||||
|
|
||||||
|
#warning: Need type tests for parameter, too (do they have the right value type and the right paramter type?)
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
@ -35,7 +35,7 @@ int ParameterizedVerbatim(int, char* [])
|
|||||||
{
|
{
|
||||||
// An example thing that needs parameterized verbatim (as it's database specific)
|
// An example thing that needs parameterized verbatim (as it's database specific)
|
||||||
auto checking_value_in_range = sqlpp::parameterized_verbatim<sqlpp::boolean>(
|
auto checking_value_in_range = sqlpp::parameterized_verbatim<sqlpp::boolean>(
|
||||||
"(quests.spawn_level_range @> CAST(", parameter(sqlpp::integer(), quester_player_level), " AS integer))");
|
"(quests.spawn_level_range @> CAST(", parameter(sqlpp::integral(), quester_player_level), " AS integer))");
|
||||||
|
|
||||||
compare(__LINE__, checking_value_in_range, "(quests.spawn_level_range @> CAST(? AS integer))");
|
compare(__LINE__, checking_value_in_range, "(quests.spawn_level_range @> CAST(? AS integer))");
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user