0
0
mirror of https://github.com/rbock/sqlpp11.git synced 2024-11-16 04:47:18 +08:00

custom_query tests

This commit is contained in:
Roland Bock 2024-07-21 12:01:37 +02:00
parent 01c1e69eb3
commit d3daa90e0b
11 changed files with 77 additions and 124 deletions

View File

@ -32,7 +32,6 @@
#include <sqlpp11/operator/assign_expression.h>
#include <sqlpp11/column_fwd.h>
#include <sqlpp11/default_value.h>
#include <sqlpp11/sort_order.h>
#include <sqlpp11/type_traits.h>
#include <sqlpp11/wrong.h>
#include <sqlpp11/detail/type_set.h>

View File

@ -28,6 +28,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <sqlpp11/operator/comparison_expression.h>
#include <sqlpp11/operator/in_expression.h>
#include <sqlpp11/operator/sort_order_expression.h>
#include <sqlpp11/type_traits.h>
namespace sqlpp
@ -100,6 +101,21 @@ namespace sqlpp
return ::sqlpp::is_not_distinct_from(this->derived(), std::move(r));
}
constexpr auto asc() const ->sort_order_expression<Expr>
{
return ::sqlpp::asc(this->derived());
}
constexpr auto desc() const ->sort_order_expression<Expr>
{
return ::sqlpp::desc(this->derived());
}
constexpr auto order(::sqlpp::sort_type t) const ->sort_order_expression<Expr>
{
return ::sqlpp::order(this->derived(), t);
}
};
} // namespace sqlpp

View File

@ -90,9 +90,6 @@ namespace sqlpp
};
};
SQLPP_PORTABLE_STATIC_ASSERT(assert_having_not_cpp_bool_t,
"having() argument has to be an sqlpp boolean expression. Please use "
"sqlpp::value(bool_expresson) if you really want to use a bool value here");
SQLPP_PORTABLE_STATIC_ASSERT(assert_having_boolean_expression_t,
"having() argument has to be an sqlpp boolean expression.");
@ -100,9 +97,7 @@ namespace sqlpp
struct check_having
{
using type =
static_combined_check_t<static_check_t<is_not_cpp_bool_t<Expression>::value, assert_having_not_cpp_bool_t>,
static_check_t<is_expression_t<Expression>::value, assert_having_boolean_expression_t>,
static_check_t<is_boolean<Expression>::value, assert_having_boolean_expression_t>>;
static_combined_check_t<static_check_t<is_boolean<Expression>::value, assert_having_boolean_expression_t>>;
};
template <typename Expression>

View File

@ -56,22 +56,15 @@ namespace sqlpp
struct insert_value_t
{
using _is_insert_value = std::true_type;
using _column_t = Column;
using _pure_value_t = typename value_type_of_t<Column>::_cpp_value_type;
using _value_t = value_t<typename Column::_traits::_value_type>;
using _value_t = parameter_value_t<value_type_of_t<Column>>;
insert_value_t(_pure_value_t rhs)
: _is_null(false), _is_default(false), _value(rhs._t)
insert_value_t(_value_t value)
: _is_default(false), _value(std::move(value))
{
}
insert_value_t(const default_value_t& /*unused*/)
: _is_null(false), _is_default(true), _value{}
{
}
insert_value_t(const _value_t& rhs)
: _is_null(rhs._is_null), _is_default(false), _value{rhs._value}
: _is_default(true), _value{}
{
}
@ -81,19 +74,14 @@ namespace sqlpp
insert_value_t& operator=(insert_value_t&&) = default;
~insert_value_t() = default;
bool _is_null;
bool _is_default;
_pure_value_t _value;
_value_t _value;
};
template <typename Context, typename ValueType>
Context& serialize(Context& context, const insert_value_t<ValueType>& t)
auto serialize(Context& context, const insert_value_t<ValueType>& t) -> Context&
{
if (t._is_null)
{
context << "NULL";
}
else if (t._is_default)
if (t._is_default)
{
context << "DEFAULT";
}

View File

@ -271,7 +271,7 @@ namespace sqlpp
template <typename... Assignments>
void _add_impl(const std::true_type& /*unused*/, Assignments... assignments)
{
_data._insert_values.emplace_back(insert_value_t<lhs_t<Assignments>>{assignments._rhs}...);
_data._insert_values.emplace_back(insert_value_t<lhs_t<Assignments>>{assignments._r}...);
}
template <typename... Assignments>

View File

@ -32,7 +32,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
namespace sqlpp
{
enum class sort_order
enum class sort_type
{
asc,
desc,
@ -41,7 +41,7 @@ namespace sqlpp
template <typename L>
struct sort_order_expression
{
constexpr sort_order_expression(L l, sort_order r) : _l(std::move(l)), _r(std::move(r))
constexpr sort_order_expression(L l, sort_type r) : _l(std::move(l)), _r(std::move(r))
{
}
sort_order_expression(const sort_order_expression&) = default;
@ -51,58 +51,76 @@ namespace sqlpp
~sort_order_expression() = default;
L _l;
sort_order _r;
sort_type _r;
};
template <typename L>
using check_sort_order_args = std::enable_if_t<values_are_comparable<L, L>::value>;
template <typename L>
struct nodes_of<sort_order_t<L>>
struct nodes_of<sort_order_expression<L>>
{
using type = detail::type_vector<L>;
};
template <typename L>
struct is_sort_order<sort_order_expression<L>> : std::true_type {};
/*
template <typename L>
constexpr auto requires_braces_v<sort_order_t<L>> = false;
template <typename L>
constexpr auto is_sort_order_v<sort_order_t<L>> = true;
template <typename Context>
[[nodiscard]] auto to_sql_string(Context& context, const sort_order& t)
[[nodiscard]] auto to_sql_string(Context& context, const sort_type& t)
{
switch (t)
{
case sort_order::asc:
case sort_type::asc:
return std::string(" ASC");
case sort_order::desc:
case sort_type::desc:
return std::string(" DESC");
}
}
template <typename Context, typename L>
[[nodiscard]] auto to_sql_string(Context& context, const sort_order_t<L>& t)
{
return to_sql_string(context, embrace(t.l)) + to_sql_string(context, t.order);
}
*/
template <typename Context>
auto serialize(Context& context, const sort_type& t) -> Context&
{
switch (t)
{
case sort_type::asc:
context << " ASC";
break;
case sort_type::desc:
context << " DESC";
break;
}
return context;
}
template <typename Context, typename L>
auto serialize(Context& context, const sort_order_expression<L>& t) -> Context&
{
serialize_operand(context, t._l);
serialize(context, t._r);
return context;
}
template <typename L, typename = check_sort_order_args<L>>
constexpr auto asc(L l) -> sort_order_expression<L>
{
return {l, sort_order::asc};
return {l, sort_type::asc};
}
template <typename L, typename = check_sort_order_args<L>>
constexpr auto desc(L l) -> sort_order_expression<L>
{
return {l, sort_order::desc};
return {l, sort_type::desc};
}
template <typename L, typename = check_sort_order_args<L>>
constexpr auto order(L l, sort_order order) -> sort_order_expression<L>
constexpr auto order(L l, sort_type order) -> sort_order_expression<L>
{
return {l, order};
}

View File

@ -86,7 +86,7 @@ namespace sqlpp
template <typename... Exprs>
struct check_order_by
{
using type = static_combined_check_t<static_check_t<logic::all_t<is_sort_order_t<Exprs>::value...>::value,
using type = static_combined_check_t<static_check_t<logic::all_t<is_sort_order<Exprs>::value...>::value,
assert_order_by_args_are_sort_order_expressions_t>>;
};
template <typename... Exprs>
@ -124,7 +124,7 @@ namespace sqlpp
{
static_assert(sizeof...(Expressions), "at least one expression (e.g. a column) required in order_by()");
return _order_by_impl(check_order_by_t<Expressions...>{}, expressions...);
return _order_by_impl(check_order_by_t<Expressions...>{}, std::move(expressions)...);
}
private:
@ -139,7 +139,7 @@ namespace sqlpp
"at least one duplicate argument detected in order_by()");
return {static_cast<const derived_statement_t<Policies>&>(*this),
order_by_data_t<Expressions...>{expressions...}};
order_by_data_t<Expressions...>{std::move(expressions)...}};
}
};
};
@ -154,9 +154,9 @@ namespace sqlpp
}
template <typename... T>
auto order_by(T&&... t) -> decltype(statement_t<no_order_by_t>().order_by(std::forward<T>(t)...))
auto order_by(T... t) -> decltype(statement_t<no_order_by_t>().order_by(std::forward<T>(t)...))
{
return statement_t<no_order_by_t>().order_by(std::forward<T>(t)...);
return statement_t<no_order_by_t>().order_by(std::move(t)...);
}
} // namespace sqlpp

View File

@ -1,66 +0,0 @@
#pragma once
/*
* Copyright (c) 2013-2015, 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 <sqlpp11/detail/type_set.h>
namespace sqlpp
{
struct no_value_t;
enum class sort_type
{
asc,
desc
};
template <typename Expression>
struct sort_order_t
{
using _traits = make_traits<no_value_t, tag::is_sort_order>;
using _nodes = detail::type_vector<Expression>;
Expression _expression;
sort_type _sort_type;
};
template <typename Context, typename Expression>
Context& serialize(Context& context, const sort_order_t<Expression>& t)
{
serialize_operand(context, t._expression);
switch (t._sort_type)
{
case sort_type::asc:
context << " ASC";
break;
default:
context << " DESC";
break;
}
return context;
}
} // namespace sqlpp

View File

@ -404,7 +404,7 @@ namespace sqlpp
struct parameter_value<unsigned_integral> { using type = uint64_t; };
template<>
struct parameter_value<floating_point> { using type = floating_point; };
struct parameter_value<floating_point> { using type = double; };
template<>
struct parameter_value<text> { using type = std::string; };
@ -491,7 +491,6 @@ namespace sqlpp
SQLPP_VALUE_TRAIT_GENERATOR(is_insert_list)
SQLPP_VALUE_TRAIT_GENERATOR(is_insert_value)
SQLPP_VALUE_TRAIT_GENERATOR(is_insert_value_list)
SQLPP_VALUE_TRAIT_GENERATOR(is_sort_order)
SQLPP_VALUE_TRAIT_GENERATOR(is_parameter)
SQLPP_VALUE_TRAIT_GENERATOR(requires_parens)
@ -875,6 +874,10 @@ namespace sqlpp
using type = typename Db::_serializer_context_t;
};
template <typename T>
struct is_sort_order : public std::false_type {};
template <typename Db>
using serializer_context_of = typename serializer_context_of_impl<Db>::type;
} // namespace sqlpp

View File

@ -29,7 +29,7 @@ set(test_files
Avg.cpp
Blob.cpp
Count.cpp
#CustomQuery.cpp
CustomQuery.cpp
DynamicWhere.cpp
Exists.cpp
Float.cpp

View File

@ -47,8 +47,8 @@ int CustomQuery(int, char*[])
custom_query(sqlpp::select(), select_flags(sqlpp::distinct), select_columns(foo.doubleN),
from(foo.join(bar).on(foo.doubleN == bar.id)), where(bar.id > 17), group_by(foo.doubleN),
having(avg(bar.id) > 19), order_by(foo.doubleN.asc()), sqlpp::limit(10u), sqlpp::offset(100u)),
"SELECT DISTINCT tab_foo.double_n FROM tab_foo INNER JOIN tab_bar ON (tab_foo.double_n=tab_bar.id) WHERE "
"(tab_bar.id>17) GROUP BY tab_foo.double_n HAVING (AVG(tab_bar.id)>19) ORDER BY tab_foo.double_n ASC "
"SELECT DISTINCT tab_foo.double_n FROM tab_foo INNER JOIN tab_bar ON (tab_foo.double_n = tab_bar.id) WHERE "
"(tab_bar.id > 17) GROUP BY tab_foo.double_n HAVING (AVG(tab_bar.id) > 19) ORDER BY tab_foo.double_n ASC "
"LIMIT 10 OFFSET 100");
// A full select statement made individual clauses
@ -59,8 +59,8 @@ int CustomQuery(int, char*[])
group_by(foo.doubleN), having(avg(bar.id) > 19),
order_by(foo.doubleN.asc(), foo.uIntN.order(sqlpp::sort_type::desc)),
sqlpp::limit(7u), sqlpp::offset(3u)),
"SELECT DISTINCT tab_foo.double_n FROM tab_foo INNER JOIN tab_bar ON (tab_foo.double_n=tab_bar.id) WHERE "
"(tab_bar.id>17) GROUP BY tab_foo.double_n HAVING (AVG(tab_bar.id)>19) ORDER BY tab_foo.double_n "
"SELECT DISTINCT tab_foo.double_n FROM tab_foo INNER JOIN tab_bar ON (tab_foo.double_n = tab_bar.id) WHERE "
"(tab_bar.id > 17) GROUP BY tab_foo.double_n HAVING (AVG(tab_bar.id) > 19) ORDER BY tab_foo.double_n "
"ASC,tab_foo.u_int_n DESC LIMIT 7 OFFSET 3");
// A pragma query for sqlite
@ -77,7 +77,7 @@ int CustomQuery(int, char*[])
.where(not exists(select(foo.doubleN).from(foo).where(foo.doubleN == x)))),
"INSERT INTO tab_foo (double_n) "
"SELECT 17 AS double_n FROM tab_foo "
"WHERE (NOT EXISTS(SELECT tab_foo.double_n FROM tab_foo WHERE (tab_foo.double_n=17)))");
"WHERE (NOT EXISTS(SELECT tab_foo.double_n FROM tab_foo WHERE (tab_foo.double_n = 17)))");
// A multi-row "insert or ignore"
auto batch = insert_columns(bar.textN, bar.boolNn);