0
0
mirror of https://github.com/rbock/sqlpp11.git synced 2024-11-15 20:31:16 +08:00

count is the first auto-aliased function

If used in an expression, count does not have an alias.
If used as a field in a select, it does have an alias.
This commit is contained in:
rbock 2015-07-05 11:08:13 +02:00
parent 303ee750ad
commit bbe933f943
6 changed files with 68 additions and 45 deletions

View File

@ -33,18 +33,8 @@
namespace sqlpp namespace sqlpp
{ {
template<typename Flag, typename Expr> struct count_alias_t
struct count_t:
public expression_operators<count_t<Flag, Expr>, integral>,
public alias_operators<count_t<Flag, Expr>>
{ {
using _traits = make_traits<integral, tag::is_expression, tag::is_selectable>;
using _nodes = detail::type_vector<Expr, aggregate_function>;
using _can_be_null = std::false_type;
static_assert(is_noop<Flag>::value or std::is_same<distinct_t, Flag>::value, "count() used with flag other than 'distinct'");
struct _alias_t struct _alias_t
{ {
static constexpr const char _literal[] = "count_"; static constexpr const char _literal[] = "count_";
@ -57,6 +47,21 @@ namespace sqlpp
const T& operator()() const { return count; } const T& operator()() const { return count; }
}; };
}; };
};
template<typename Flag, typename Expr>
struct count_t:
public expression_operators<count_t<Flag, Expr>, integral>,
public alias_operators<count_t<Flag, Expr>>
{
using _traits = make_traits<integral, tag::is_expression/*, tag::is_selectable*/>;
using _nodes = detail::type_vector<Expr, aggregate_function>;
using _can_be_null = std::false_type;
static_assert(is_noop<Flag>::value or std::is_same<distinct_t, Flag>::value, "count() used with flag other than 'distinct'");
using _auto_alias_t = count_alias_t;
count_t(const Expr expr): count_t(const Expr expr):
_expr(expr) _expr(expr)

View File

@ -28,6 +28,7 @@
#define SQLPP_DETAIL_COPY_TUPLE_ARGS_H #define SQLPP_DETAIL_COPY_TUPLE_ARGS_H
#include <tuple> #include <tuple>
#include <sqlpp11/alias.h>
namespace sqlpp namespace sqlpp
{ {
@ -36,22 +37,37 @@ namespace sqlpp
namespace detail namespace detail
{ {
template<typename T> template<typename T, typename Enable = void>
struct as_tuple struct auto_alias_impl
{ {
static std::tuple<T> _(T t) { return std::tuple<T>{ t }; } using type = T;
}; };
template<typename T> template<typename T>
struct as_tuple<all_of_t<T>> struct auto_alias_impl<T, typename std::enable_if<not wrong_t<typename T::_auto_alias_t>::value>::type>
{
using type = expression_alias_t<T, typename T::_auto_alias_t>;
};
template<typename T>
using auto_alias_t = typename detail::auto_alias_impl<T>::type;
template<typename T>
struct as_column_tuple
{
static std::tuple<auto_alias_t<T>> _(T t) { return std::tuple<auto_alias_t<T>>{ auto_alias_t<T>{t} }; }
};
template<typename T>
struct as_column_tuple<all_of_t<T>>
{ {
static typename all_of_t<T>::_column_tuple_t _(all_of_t<T>) { return { }; } static typename all_of_t<T>::_column_tuple_t _(all_of_t<T>) { return { }; }
}; };
template<typename... Args> template<typename... Args>
struct as_tuple<std::tuple<Args...>> struct as_column_tuple<std::tuple<Args...>>
{ {
static std::tuple<Args...> _(std::tuple<Args...> t) { return t; } static std::tuple<auto_alias_t<Args>...> _(std::tuple<Args...> t) { return t; }
}; };
template<template<typename, typename...> class Target, typename First, typename T> template<template<typename, typename...> class Target, typename First, typename T>

View File

@ -130,14 +130,14 @@ namespace sqlpp
template<typename... Columns> template<typename... Columns>
using make_multi_column_t = using make_multi_column_t =
detail::copy_tuple_args_t<multi_column_t, void, detail::copy_tuple_args_t<multi_column_t, void,
decltype(std::tuple_cat(detail::as_tuple<Columns>::_(std::declval<Columns>())...))>; decltype(std::tuple_cat(detail::as_column_tuple<Columns>::_(std::declval<Columns>())...))>;
} }
template<typename... Columns> template<typename... Columns>
auto multi_column(Columns... columns) auto multi_column(Columns... columns)
-> detail::make_multi_column_t<Columns...> -> detail::make_multi_column_t<Columns...>
{ {
return detail::make_multi_column_t<Columns...>(std::tuple_cat(detail::as_tuple<Columns>::_(columns)...)); return detail::make_multi_column_t<Columns...>(std::tuple_cat(detail::as_column_tuple<Columns>::_(columns)...));
} }

View File

@ -188,17 +188,18 @@ namespace sqlpp
template<typename NamedExpression, typename TableCheckRequired = std::true_type> template<typename NamedExpression, typename TableCheckRequired = std::true_type>
void add(NamedExpression namedExpression) void add(NamedExpression namedExpression)
{ {
using named_expression = detail::auto_alias_t<NamedExpression>;
static_assert(_is_dynamic::value, "selected_columns::add() can only be called for dynamic_column"); static_assert(_is_dynamic::value, "selected_columns::add() can only be called for dynamic_column");
static_assert(is_selectable_t<NamedExpression>::value, "invalid named expression argument in selected_columns::add()"); static_assert(is_selectable_t<named_expression>::value, "invalid named expression argument in selected_columns::add()");
static_assert(TableCheckRequired::value or Policies::template _no_unknown_tables<NamedExpression>::value, "named expression uses tables unknown to this statement in selected_columns::add()"); static_assert(TableCheckRequired::value or Policies::template _no_unknown_tables<named_expression>::value, "named expression uses tables unknown to this statement in selected_columns::add()");
using column_names = detail::make_type_set_t<typename Columns::_alias_t...>; using column_names = detail::make_type_set_t<typename Columns::_alias_t...>;
static_assert(not detail::is_element_of<typename NamedExpression::_alias_t, column_names>::value, "a column of this name is present in the select already"); static_assert(not detail::is_element_of<typename named_expression::_alias_t, column_names>::value, "a column of this name is present in the select already");
using _serialize_check = sqlpp::serialize_check_t<typename Database::_serializer_context_t, NamedExpression>; using _serialize_check = sqlpp::serialize_check_t<typename Database::_serializer_context_t, named_expression>;
_serialize_check::_(); _serialize_check::_();
using ok = logic::all_t< using ok = logic::all_t<
_is_dynamic::value, _is_dynamic::value,
is_selectable_t<NamedExpression>::value, is_selectable_t<named_expression>::value,
_serialize_check::type::value _serialize_check::type::value
>; >;
@ -209,7 +210,7 @@ namespace sqlpp
template<typename NamedExpression> template<typename NamedExpression>
void _add_impl(NamedExpression namedExpression, const std::true_type&) void _add_impl(NamedExpression namedExpression, const std::true_type&)
{ {
return _data._dynamic_columns.emplace_back(namedExpression); return _data._dynamic_columns.emplace_back(detail::auto_alias_t<NamedExpression>{namedExpression});
} }
template<typename NamedExpression> template<typename NamedExpression>
@ -338,15 +339,15 @@ namespace sqlpp
namespace detail namespace detail
{ {
template<typename... Columns> template<typename... Columns>
auto tuple_merge(Columns... columns) -> decltype(std::tuple_cat(as_tuple<Columns>::_(columns)...)) auto column_tuple_merge(Columns... columns) -> decltype(std::tuple_cat(as_column_tuple<Columns>::_(columns)...))
{ {
return std::tuple_cat(as_tuple<Columns>::_(columns)...); return std::tuple_cat(as_column_tuple<Columns>::_(columns)...);
} }
template<typename Database, typename... Columns> template<typename Database, typename... Columns>
using make_select_column_list_t = using make_select_column_list_t =
copy_tuple_args_t<select_column_list_t, Database, copy_tuple_args_t<select_column_list_t, Database,
decltype(tuple_merge(std::declval<Columns>()...))>; decltype(column_tuple_merge(std::declval<Columns>()...))>;
} }
@ -395,9 +396,9 @@ namespace sqlpp
} }
template<typename... T> template<typename... T>
static constexpr auto _check_args(T... args) -> decltype(_check_tuple(detail::tuple_merge(args...))) static constexpr auto _check_args(T... args) -> decltype(_check_tuple(detail::column_tuple_merge(args...)))
{ {
return _check_tuple(detail::tuple_merge(args...)); return _check_tuple(detail::column_tuple_merge(args...));
} }
template<typename Check, typename T> template<typename Check, typename T>
@ -412,7 +413,7 @@ namespace sqlpp
static_assert(sizeof...(Args), "at least one selectable expression (e.g. a column) required in columns()"); static_assert(sizeof...(Args), "at least one selectable expression (e.g. a column) required in columns()");
static_assert(decltype(_check_args(args...))::value, "at least one argument is not a selectable expression in columns()"); static_assert(decltype(_check_args(args...))::value, "at least one argument is not a selectable expression in columns()");
return _columns_impl<void>(_check_args(args...), detail::tuple_merge(args...)); return _columns_impl<void>(_check_args(args...), detail::column_tuple_merge(args...));
} }
template<typename... Args> template<typename... Args>
@ -422,7 +423,7 @@ namespace sqlpp
static_assert(not std::is_same<_database_t, void>::value, "dynamic_columns must not be called in a static statement"); static_assert(not std::is_same<_database_t, void>::value, "dynamic_columns must not be called in a static statement");
static_assert(decltype(_check_args(args...))::value, "at least one argument is not a selectable expression in columns()"); static_assert(decltype(_check_args(args...))::value, "at least one argument is not a selectable expression in columns()");
return _columns_impl<_database_t>(_check_args(args...), detail::tuple_merge(args...)); return _columns_impl<_database_t>(_check_args(args...), detail::column_tuple_merge(args...));
} }
private: private:

View File

@ -30,20 +30,20 @@ target_compile_options(sqlpp11_testing INTERFACE -Wall -Wextra -pedantic)
endif () endif ()
set(test_names set(test_names
BooleanExpression #BooleanExpression
CustomQuery #CustomQuery
Interpret #Interpret
Insert #Insert
Remove #Remove
Update #Update
Select Select
SelectType #SelectType
Function #Function
Prepared #Prepared
Minimalistic #Minimalistic
Result #Result
Union #Union
With #With
) )
create_test_sourcelist(test_sources test_main.cpp ${test_names}) create_test_sourcelist(test_sources test_main.cpp ${test_names})

View File

@ -53,6 +53,7 @@ int Select(int, char**)
const auto tab_a = f.as(sqlpp::alias::a); const auto tab_a = f.as(sqlpp::alias::a);
getColumn(db, t.alpha); getColumn(db, t.alpha);
select(count(t.alpha));
for (const auto& row : db(select(sqlpp::value(false).as(sqlpp::alias::a)))) for (const auto& row : db(select(sqlpp::value(false).as(sqlpp::alias::a))))
{ {