mirror of
https://github.com/rbock/sqlpp11.git
synced 2024-11-16 12:51:13 +08:00
Add dynamic select flags
This commit is contained in:
parent
eaa7d24924
commit
8764f3862e
@ -81,7 +81,7 @@ namespace sqlpp
|
|||||||
struct check_select_flags
|
struct check_select_flags
|
||||||
{
|
{
|
||||||
using type = static_combined_check_t<
|
using type = static_combined_check_t<
|
||||||
static_check_t<logic::all<is_select_flag_t<Flags>::value...>::value, assert_select_flags_are_flags_t>>;
|
static_check_t<logic::all<is_select_flag<remove_dynamic_t<Flags>>::value...>::value, assert_select_flags_are_flags_t>>;
|
||||||
};
|
};
|
||||||
template <typename... Flags>
|
template <typename... Flags>
|
||||||
using check_select_flags_t = typename check_select_flags<Flags...>::type;
|
using check_select_flags_t = typename check_select_flags<Flags...>::type;
|
||||||
@ -137,13 +137,17 @@ namespace sqlpp
|
|||||||
template <typename Context, typename... Flags>
|
template <typename Context, typename... Flags>
|
||||||
auto to_sql_string(Context& context, const select_flag_list_data_t<Flags...>& t) -> std::string
|
auto to_sql_string(Context& context, const select_flag_list_data_t<Flags...>& t) -> std::string
|
||||||
{
|
{
|
||||||
return tuple_to_sql_string(context, t._flags, tuple_operand{" "});
|
auto flags = tuple_to_sql_string(context, t._flags, tuple_operand_no_dynamic{" "});
|
||||||
|
if (flags.empty()) {
|
||||||
|
return flags;
|
||||||
|
}
|
||||||
|
return flags + " ";
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename... T>
|
||||||
auto select_flags(T&& t) -> decltype(statement_t<no_select_flag_list_t>().flags(std::forward<T>(t)))
|
auto select_flags(T... t) -> decltype(statement_t<no_select_flag_list_t>().flags(std::forward<T>(t)...))
|
||||||
{
|
{
|
||||||
return statement_t<no_select_flag_list_t>().flags(std::forward<T>(t));
|
return statement_t<no_select_flag_list_t>().flags(std::forward<T>(t)...);
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace sqlpp
|
} // namespace sqlpp
|
||||||
|
@ -36,11 +36,12 @@ namespace sqlpp
|
|||||||
// standard select flags
|
// standard select flags
|
||||||
struct all_t
|
struct all_t
|
||||||
{
|
{
|
||||||
using _traits = make_traits<no_value_t, tag::is_select_flag>;
|
|
||||||
using _nodes = detail::type_vector<>;
|
|
||||||
};
|
};
|
||||||
static constexpr all_t all = {};
|
static constexpr all_t all = {};
|
||||||
|
|
||||||
|
template <>
|
||||||
|
struct is_select_flag<all_t> : public std::true_type {};
|
||||||
|
|
||||||
template <typename Context>
|
template <typename Context>
|
||||||
auto to_sql_string(Context& , const all_t&) -> std::string
|
auto to_sql_string(Context& , const all_t&) -> std::string
|
||||||
{
|
{
|
||||||
@ -49,28 +50,16 @@ namespace sqlpp
|
|||||||
|
|
||||||
struct distinct_t
|
struct distinct_t
|
||||||
{
|
{
|
||||||
using _traits = make_traits<no_value_t, tag::is_select_flag>;
|
|
||||||
using _nodes = detail::type_vector<>;
|
|
||||||
};
|
};
|
||||||
static constexpr distinct_t distinct = {};
|
static constexpr distinct_t distinct = {};
|
||||||
|
|
||||||
|
template <>
|
||||||
|
struct is_select_flag<distinct_t> : public std::true_type {};
|
||||||
|
|
||||||
template <typename Context>
|
template <typename Context>
|
||||||
auto to_sql_string(Context& , const distinct_t&) -> std::string
|
auto to_sql_string(Context& , const distinct_t&) -> std::string
|
||||||
{
|
{
|
||||||
return "DISTINCT";
|
return "DISTINCT";
|
||||||
}
|
}
|
||||||
|
|
||||||
struct straight_join_t
|
|
||||||
{
|
|
||||||
using _traits = make_traits<no_value_t, tag::is_select_flag>;
|
|
||||||
using _nodes = detail::type_vector<>;
|
|
||||||
};
|
|
||||||
static constexpr straight_join_t straight_join = {};
|
|
||||||
|
|
||||||
#warning: Why is this in the select flags?
|
|
||||||
template <typename Context>
|
|
||||||
auto to_sql_string(Context& , const straight_join_t&) -> std::string
|
|
||||||
{
|
|
||||||
return "STRAIGHT_JOIN";
|
|
||||||
}
|
|
||||||
} // namespace sqlpp
|
} // namespace sqlpp
|
||||||
|
@ -97,7 +97,8 @@ namespace sqlpp
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <typename Expr>
|
template <typename Expr>
|
||||||
using check_dynamic_args = ::sqlpp::enable_if_t<has_value_type<Expr>::value>;
|
using check_dynamic_args = ::sqlpp::enable_if_t<has_value_type<Expr>::value or is_table<Expr>::value or
|
||||||
|
is_select_flag<Expr>::value or is_sort_order<Expr>::value>;
|
||||||
|
|
||||||
template <typename Expr, typename = check_dynamic_args<Expr>>
|
template <typename Expr, typename = check_dynamic_args<Expr>>
|
||||||
auto dynamic(bool condition, Expr t) -> dynamic_t<Expr>
|
auto dynamic(bool condition, Expr t) -> dynamic_t<Expr>
|
||||||
@ -111,18 +112,11 @@ namespace sqlpp
|
|||||||
return {condition, std::move(t)};
|
return {condition, std::move(t)};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#warning: Check if we can dynamically join a table_as?
|
||||||
template <typename Expr, typename = check_dynamic_args<Expr>>
|
template <typename Expr, typename = check_dynamic_args<Expr>>
|
||||||
auto dynamic(bool condition, sort_order_expression<Expr> t) -> dynamic_t<sort_order_expression<Expr>>
|
auto dynamic(bool condition, sort_order_expression<Expr> t) -> dynamic_t<sort_order_expression<Expr>>
|
||||||
{
|
{
|
||||||
return {condition, std::move(t)};
|
return {condition, std::move(t)};
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename TableSpec>
|
|
||||||
struct table_t;
|
|
||||||
|
|
||||||
template <typename TableSpec>
|
|
||||||
auto dynamic(bool condition, table_t<TableSpec> t) -> dynamic_t<table_t<TableSpec>>
|
|
||||||
{
|
|
||||||
return {condition, std::move(t)};
|
|
||||||
}
|
|
||||||
} // namespace sqlpp
|
} // namespace sqlpp
|
||||||
|
@ -216,7 +216,6 @@ namespace sqlpp
|
|||||||
SQLPP_VALUE_TRAIT_GENERATOR(is_expression)
|
SQLPP_VALUE_TRAIT_GENERATOR(is_expression)
|
||||||
SQLPP_VALUE_TRAIT_GENERATOR(is_multi_expression)
|
SQLPP_VALUE_TRAIT_GENERATOR(is_multi_expression)
|
||||||
SQLPP_VALUE_TRAIT_GENERATOR(is_alias)
|
SQLPP_VALUE_TRAIT_GENERATOR(is_alias)
|
||||||
SQLPP_VALUE_TRAIT_GENERATOR(is_select_flag)
|
|
||||||
SQLPP_VALUE_TRAIT_GENERATOR(is_union_flag)
|
SQLPP_VALUE_TRAIT_GENERATOR(is_union_flag)
|
||||||
SQLPP_VALUE_TRAIT_GENERATOR(is_result_field)
|
SQLPP_VALUE_TRAIT_GENERATOR(is_result_field)
|
||||||
|
|
||||||
@ -564,6 +563,9 @@ namespace sqlpp
|
|||||||
using type = typename Db::_serializer_context_t;
|
using type = typename Db::_serializer_context_t;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
struct is_select_flag : public std::false_type {};
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
struct is_sort_order : public std::false_type {};
|
struct is_sort_order : public std::false_type {};
|
||||||
|
|
||||||
|
@ -35,7 +35,6 @@ set(test_files
|
|||||||
ParameterizedVerbatim.cpp
|
ParameterizedVerbatim.cpp
|
||||||
SelectAs.cpp
|
SelectAs.cpp
|
||||||
SelectColumns.cpp
|
SelectColumns.cpp
|
||||||
SelectFlags.cpp
|
|
||||||
TableAlias.cpp
|
TableAlias.cpp
|
||||||
Trim.cpp
|
Trim.cpp
|
||||||
Upper.cpp
|
Upper.cpp
|
||||||
|
@ -31,4 +31,5 @@ endfunction()
|
|||||||
|
|
||||||
create_test(group_by)
|
create_test(group_by)
|
||||||
create_test(select_columns)
|
create_test(select_columns)
|
||||||
|
create_test(select_flags)
|
||||||
|
|
||||||
|
@ -23,21 +23,19 @@
|
|||||||
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "compare.h"
|
#include "../compare.h"
|
||||||
#include "Sample.h"
|
#include "Sample.h"
|
||||||
#include <sqlpp11/sqlpp11.h>
|
#include <sqlpp11/sqlpp11.h>
|
||||||
|
|
||||||
int SelectFlags(int, char*[])
|
int main(int, char*[])
|
||||||
{
|
{
|
||||||
const auto foo = test::TabFoo{};
|
const auto foo = test::TabFoo{};
|
||||||
const auto bar = test::TabBar{};
|
|
||||||
|
|
||||||
// No flags
|
// No flags
|
||||||
SQLPP_COMPARE(select(foo.doubleN), "SELECT tab_foo.double_n");
|
SQLPP_COMPARE(select(foo.doubleN), "SELECT tab_foo.double_n");
|
||||||
|
|
||||||
// No flags
|
// No flags
|
||||||
#warning: This should work
|
SQLPP_COMPARE(sqlpp::select_flags(), "");
|
||||||
//SQLPP_COMPARE(sqlpp::select_flags(), "");
|
|
||||||
|
|
||||||
// One flag
|
// One flag
|
||||||
SQLPP_COMPARE(select(foo.doubleN).flags(sqlpp::distinct), "SELECT DISTINCT tab_foo.double_n");
|
SQLPP_COMPARE(select(foo.doubleN).flags(sqlpp::distinct), "SELECT DISTINCT tab_foo.double_n");
|
||||||
@ -45,7 +43,29 @@ int SelectFlags(int, char*[])
|
|||||||
// One flag
|
// One flag
|
||||||
SQLPP_COMPARE(select_flags(sqlpp::distinct), "DISTINCT ");
|
SQLPP_COMPARE(select_flags(sqlpp::distinct), "DISTINCT ");
|
||||||
|
|
||||||
#warning: Add tests for dynamic select flags
|
// Two flags
|
||||||
|
SQLPP_COMPARE(select_flags(sqlpp::distinct, sqlpp::all), "DISTINCT ALL ");
|
||||||
|
|
||||||
|
// Two flags
|
||||||
|
SQLPP_COMPARE(select(foo.doubleN).flags(sqlpp::distinct, sqlpp::all), "SELECT DISTINCT ALL tab_foo.double_n");
|
||||||
|
|
||||||
|
// Dynamic flags
|
||||||
|
|
||||||
|
// One flag
|
||||||
|
SQLPP_COMPARE(select(foo.doubleN).flags(sqlpp::dynamic(true, sqlpp::distinct)), "SELECT DISTINCT tab_foo.double_n");
|
||||||
|
SQLPP_COMPARE(select(foo.doubleN).flags(sqlpp::dynamic(false, sqlpp::distinct)), "SELECT tab_foo.double_n");
|
||||||
|
|
||||||
|
// One flag
|
||||||
|
SQLPP_COMPARE(select_flags(sqlpp::dynamic(true, sqlpp::distinct)), "DISTINCT ");
|
||||||
|
SQLPP_COMPARE(select_flags(sqlpp::dynamic(false, sqlpp::distinct)), "");
|
||||||
|
|
||||||
|
// Two flags
|
||||||
|
SQLPP_COMPARE(select_flags(sqlpp::distinct, sqlpp::dynamic(true, sqlpp::all)), "DISTINCT ALL ");
|
||||||
|
SQLPP_COMPARE(select_flags(sqlpp::distinct, sqlpp::dynamic(false, sqlpp::all)), "DISTINCT ");
|
||||||
|
|
||||||
|
// Two flags
|
||||||
|
SQLPP_COMPARE(select(foo.doubleN).flags(sqlpp::distinct, sqlpp::dynamic(true, sqlpp::all)), "SELECT DISTINCT ALL tab_foo.double_n");
|
||||||
|
SQLPP_COMPARE(select(foo.doubleN).flags(sqlpp::distinct, sqlpp::dynamic(false, sqlpp::all)), "SELECT DISTINCT tab_foo.double_n");
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user