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

Added tag to indicate that an expression contains an aggregate function

This commit is contained in:
rbock 2014-09-17 22:22:45 +02:00
parent 88bd56ff2d
commit 3cab459077
19 changed files with 147 additions and 95 deletions

View File

@ -34,6 +34,7 @@
#include "MockDb.h"
#include <sqlpp11/sqlpp11.h>
SQLPP_ALIAS_PROVIDER(cheesecake);
MockDb db;
@ -42,7 +43,7 @@ test::TabFeature f;
int main()
{
for (const auto& row : db(select(all_of(p)).from(p).where(true)))
for (const auto& row : db(select(all_of(p)).from(p).where(p.id > 7)))
{
int64_t id = row.id;
std::string name = row.name;
@ -51,7 +52,7 @@ int main()
#if 0
for (const auto& row : db(select(p.name).from(p).where(true)))
for (const auto& row : db(select(p.name).from(p).where(p.name.like("Herb%"))))
{
int64_t id = row.id;
std::string name = row.name;
@ -62,12 +63,12 @@ int main()
#if 0
for (const auto& row : db(select(p.name, f.name).from(p,f).where(true)))
for (const auto& row : db(select(p.name, f.name.as(cheesecake)).from(p,f).where(p.id > 7 and p.feature == 3)))
{
//int64_t id = row.id;
//std::string a = row.a;
std::string name = row.name;
//int64_t feature = row.feature;
std::string feature = row.cheesecake;
}
#endif
@ -90,7 +91,7 @@ int main()
#if 0
#if !0
auto s = select(all_of(p))
.from(p, f)
.where(p.name == any(select(f.name)

View File

@ -37,7 +37,7 @@ namespace sqlpp
public alias_operators<avg_t<Flag, Expr>>
{
using _traits = make_traits<floating_point, tag::is_expression, tag::is_named_expression>;
using _recursive_traits = make_recursive_traits<Expr>;
using _recursive_traits = make_recursive_traits<Expr, aggregate_function>;
static_assert(is_noop<Flag>::value or std::is_same<distinct_t, Flag>::value, "avg() used with flag other than 'distinct'");
static_assert(is_numeric_t<Expr>::value, "avg() requires a value expression as argument");
@ -89,6 +89,7 @@ namespace sqlpp
template<typename T>
auto avg(T t) -> avg_t<noop, wrap_operand_t<T>>
{
static_assert(not contains_aggregate_function_t<wrap_operand_t<T>>::value, "avg() cannot be used on an aggregate function");
static_assert(is_numeric_t<wrap_operand_t<T>>::value, "avg() requires a value expression as argument");
return { t };
}
@ -96,6 +97,7 @@ namespace sqlpp
template<typename T>
auto avg(const distinct_t&, T t) -> avg_t<distinct_t, wrap_operand_t<T>>
{
static_assert(not contains_aggregate_function_t<wrap_operand_t<T>>::value, "avg() cannot be used on an aggregate function");
static_assert(is_numeric_t<wrap_operand_t<T>>::value, "avg() requires a value expression as argument");
return { t };
}

View File

@ -59,7 +59,9 @@ namespace sqlpp
using _provided_outer_tables = detail::type_set<>;
using _required_tables = detail::type_set<Table>;
using _extra_tables = detail::type_set<>;
using _can_be_null = column_spec_can_be_null_t<ColumnSpec>;
using _tags = typename std::conditional<column_spec_can_be_null_t<ColumnSpec>::value,
detail::type_set<tag::can_be_null>,
detail::type_set<>>::type;
};
using _spec_t = ColumnSpec;

View File

@ -38,7 +38,17 @@ namespace sqlpp
public alias_operators<count_t<Flag, Expr>>
{
using _traits = make_traits<integral, tag::is_expression, tag::is_named_expression>;
using _recursive_traits = make_recursive_traits<Expr>;
struct _recursive_traits
{
using _required_tables = required_tables_of<Expr>;
using _provided_tables = provided_tables_of<Expr>;
using _provided_outer_tables = provided_outer_tables_of<Expr>;
using _extra_tables = extra_tables_of<Expr>;
using _parameters = parameters_of<Expr>;
using _tags = detail::make_difference_set_t<detail::joined_set_t<recursive_tags_of<Expr>, recursive_tags_of<aggregate_function>>,
detail::type_set<tag::can_be_null>>;
};
static_assert(is_noop<Flag>::value or std::is_same<distinct_t, Flag>::value, "count() used with flag other than 'distinct'");
static_assert(is_expression_t<Expr>::value, "count() requires a sql expression as argument");
@ -90,6 +100,7 @@ namespace sqlpp
template<typename T>
auto count(T t) -> count_t<noop, wrap_operand_t<T>>
{
static_assert(not contains_aggregate_function_t<wrap_operand_t<T>>::value, "count() cannot be used on an aggregate function");
static_assert(is_expression_t<wrap_operand_t<T>>::value, "count() requires an expression as argument");
return { t };
}
@ -97,6 +108,7 @@ namespace sqlpp
template<typename T>
auto count(const distinct_t&, T t) -> count_t<distinct_t, wrap_operand_t<T>>
{
static_assert(not contains_aggregate_function_t<wrap_operand_t<T>>::value, "count() cannot be used on an aggregate function");
static_assert(is_expression_t<wrap_operand_t<T>>::value, "count() requires an expression as argument");
return { t };
}

View File

@ -59,7 +59,7 @@ namespace sqlpp
using _provided_outer_tables = detail::type_set<>;
using _provided_tables = detail::type_set<>;
using _extra_tables = detail::type_set<Tables...>;
using _can_be_null = std::false_type;
using _tags = detail::type_set<>;
};
// FIXME: extra_tables must not require tables!

View File

@ -73,7 +73,7 @@ namespace sqlpp
using _provided_outer_tables = typename JoinType::template _provided_outer_tables<Lhs, Rhs>;
using _extra_tables = detail::make_joined_set_t<extra_tables_of<Lhs>, extra_tables_of<Rhs>>;
using _parameters = detail::make_parameter_tuple_t<parameters_of<Lhs>, parameters_of<Rhs>>;
using _can_be_null = std::false_type;
using _tags = detail::type_set<>;
};
@ -94,6 +94,8 @@ namespace sqlpp
-> set_on_t<on_t<void, Expr...>>
{
static_assert(is_noop<On>::value, "cannot call on() twice for a single join()");
static_assert(detail::all_t<is_expression_t<Expr>::value...>::value, "at least one argument is not an expression in on()");
return { _lhs,
_rhs,
{std::tuple<Expr...>{expr...}}

View File

@ -37,7 +37,7 @@ namespace sqlpp
public alias_operators<max_t<Expr>>
{
using _traits = make_traits<value_type_of<Expr>, tag::is_expression, tag::is_named_expression>;
using _recursive_traits = make_recursive_traits<Expr>;
using _recursive_traits = make_recursive_traits<Expr, aggregate_function>;
static_assert(is_expression_t<Expr>::value, "max() requires a value expression as argument");
@ -83,6 +83,7 @@ namespace sqlpp
template<typename T>
auto max(T t) -> max_t<wrap_operand_t<T>>
{
static_assert(not contains_aggregate_function_t<wrap_operand_t<T>>::value, "max() cannot be used on an aggregate function");
static_assert(is_expression_t<wrap_operand_t<T>>::value, "max() requires a value expression as argument");
return { t };
}

View File

@ -37,7 +37,7 @@ namespace sqlpp
public alias_operators<min_t<Expr>>
{
using _traits = make_traits<value_type_of<Expr>, tag::is_expression, tag::is_named_expression>;
using _recursive_traits = make_recursive_traits<Expr>;
using _recursive_traits = make_recursive_traits<Expr, aggregate_function>;
static_assert(is_expression_t<Expr>::value, "min() requires a value expression as argument");
@ -83,6 +83,7 @@ namespace sqlpp
template<typename T>
auto min(T t) -> min_t<wrap_operand_t<T>>
{
static_assert(not contains_aggregate_function_t<wrap_operand_t<T>>::value, "min() cannot be used on an aggregate function");
static_assert(is_expression_t<wrap_operand_t<T>>::value, "min() requires a value expression as argument");
return { t };
}

View File

@ -43,7 +43,6 @@ namespace sqlpp
using _is_dynamic = is_database<Database>;
static_assert(_is_dynamic::value or sizeof...(Expr), "at least one expression argument required in on()");
static_assert(detail::all_t<is_expression_t<Expr>::value...>::value, "at least one argument is not an expression in on()");
template<typename E>
void add(E expr)

View File

@ -45,7 +45,7 @@ namespace sqlpp
using _provided_outer_tables = detail::type_set<>;
using _required_tables = detail::type_set<>;
using _extra_tables = detail::type_set<>;
using _can_be_null = std::true_type;
using _tags = detail::type_set<tag::can_be_null>;
};
using _instance_t = member_t<NameType, parameter_value_t<ValueType>>;

View File

@ -88,7 +88,9 @@ namespace sqlpp
using _provided_outer_tables = detail::type_set<>;
using _required_tables = detail::type_set<>;
using _extra_tables = detail::type_set<>;
using _can_be_null = column_spec_can_be_null_t<_field_spec_t>;
using _tags = typename std::conditional<column_spec_can_be_null_t<_field_spec_t>::value,
detail::type_set<tag::can_be_null>,
detail::type_set<>>::type;
};
};

View File

@ -98,6 +98,13 @@ namespace sqlpp
no_value_t // if a required statement part is missing (e.g. columns in a select), then the statement cannot be used as a value
>::type;
using _can_be_null = detail::any_t<
can_be_null_t<_result_type_provider>::value,
detail::make_intersect_set_t<
required_tables_of<_result_type_provider>,
_all_provided_outer_tables
>::size::value != 0>;
using _traits = make_traits<_value_type, tag_if<tag::is_expression, not std::is_same<_value_type, no_value_t>::value>>;
struct _recursive_traits
@ -107,12 +114,9 @@ namespace sqlpp
using _provided_outer_tables = detail::type_set<>;
using _extra_tables = detail::type_set<>;
using _parameters = detail::make_parameter_tuple_t<parameters_of<Policies>...>;
using _can_be_null = detail::any_t<
can_be_null_t<_result_type_provider>::value,
detail::make_intersect_set_t<
required_tables_of<_result_type_provider>,
provided_outer_tables_of<statement_policies_t>
>::size::value != 0>;
using _tags = typename std::conditional<_can_be_null::value,
detail::type_set<tag::can_be_null>,
detail::type_set<>>::type;
};
};
}

View File

@ -37,7 +37,7 @@ namespace sqlpp
public alias_operators<sum_t<Flag, Expr>>
{
using _traits = make_traits<value_type_of<Expr>, tag::is_expression, tag::is_named_expression>;
using _recursive_traits = make_recursive_traits<Expr>;
using _recursive_traits = make_recursive_traits<Expr, aggregate_function>;
static_assert(is_noop<Flag>::value or std::is_same<distinct_t, Flag>::value, "sum() used with flag other than 'distinct'");
static_assert(is_numeric_t<Expr>::value, "sum() requires a numeric expression as argument");
@ -89,6 +89,7 @@ namespace sqlpp
template<typename T>
auto sum(T t) -> sum_t<noop, wrap_operand_t<T>>
{
static_assert(not contains_aggregate_function_t<wrap_operand_t<T>>::value, "sum() cannot be used on an aggregate function");
static_assert(is_numeric_t<wrap_operand_t<T>>::value, "sum() requires a numeric expression as argument");
return { t };
}
@ -96,6 +97,7 @@ namespace sqlpp
template<typename T>
auto sum(const distinct_t&, T t) -> sum_t<distinct_t, wrap_operand_t<T>>
{
static_assert(not contains_aggregate_function_t<wrap_operand_t<T>>::value, "sum() cannot be used on an aggregate function");
static_assert(is_numeric_t<wrap_operand_t<T>>::value, "sum() requires a numeric expression as argument");
return { t };
}

View File

@ -53,7 +53,7 @@ namespace sqlpp
using _provided_tables = detail::type_set<Table>;
using _provided_outer_tables = detail::type_set<>;
using _extra_tables = detail::type_set<>;
using _can_be_null = std::false_type;
using _tags = detail::type_set<>;
};
static_assert(sizeof...(ColumnSpec), "at least one column required per table");

View File

@ -49,7 +49,7 @@ namespace sqlpp
using _provided_tables = detail::type_set<AliasProvider>;
using _provided_outer_tables = detail::type_set<>;
using _extra_tables = detail::type_set<>;
using _can_be_null = std::false_type;
using _tags = detail::type_set<>;
};
static_assert(required_tables_of<Table>::size::value == 0, "table aliases must not depend on external tables");

View File

@ -32,28 +32,40 @@
namespace sqlpp
{
namespace tag
{
struct can_be_null{};
struct contains_aggregate_function{};
};
namespace detail
{
template<typename T, typename Enable = void>
struct can_be_null_impl { using type = std::false_type; };
template<typename T>
struct can_be_null_impl<T, typename std::enable_if<T::_recursive_traits::_can_be_null::value>::type> { using type = std::true_type; };
struct can_be_null_impl<T, typename std::enable_if<is_element_of<tag::can_be_null, typename T::_recursive_traits::_tags>::value>::type> { using type = std::true_type; };
}
template<typename T>
using can_be_null_t = typename detail::can_be_null_impl<T>::type;
namespace tag\
{\
struct can_be_null{};\
};\
namespace detail\
{\
template<typename T, typename Enable = void>\
struct column_spec_can_be_null_impl { using type = std::false_type; };\
template<typename T>\
struct column_spec_can_be_null_impl<T, typename std::enable_if<detail::is_element_of<tag::can_be_null, typename T::_traits::_tags>::value>::type> { using type = std::true_type; };\
}\
template<typename T>\
namespace detail
{
template<typename T, typename Enable = void>
struct contains_aggregate_function_impl { using type = std::false_type; };
template<typename T>
struct contains_aggregate_function_impl<T, typename std::enable_if<is_element_of<tag::contains_aggregate_function, typename T::_recursive_traits::_tags>::value>::type> { using type = std::true_type; };
}
template<typename T>
using contains_aggregate_function_t = typename detail::contains_aggregate_function_impl<T>::type;
namespace detail
{
template<typename T, typename Enable = void>
struct column_spec_can_be_null_impl { using type = std::false_type; };
template<typename T>
struct column_spec_can_be_null_impl<T, typename std::enable_if<detail::is_element_of<tag::can_be_null, typename T::_traits::_tags>::value>::type> { using type = std::true_type; };
}
template<typename T>
using column_spec_can_be_null_t = typename detail::column_spec_can_be_null_impl<T>::type;
#define SQLPP_VALUE_TRAIT_GENERATOR(name) \
@ -140,80 +152,35 @@ namespace sqlpp
namespace detail
{
template<typename T>
struct value_type_of_impl
{
using type = typename T::_traits::_value_type;
};
template<typename T>
struct required_table_of_impl
{
using type = typename T::_recursive_traits::_required_tables;
};
template<typename T>
struct provided_table_of_impl
{
using type = typename T::_recursive_traits::_provided_tables;
};
template<typename T>
struct provided_outer_table_of_impl
{
using type = typename T::_recursive_traits::_provided_outer_tables;
};
template<typename T>
struct extra_table_of_impl
{
using type = typename T::_recursive_traits::_extra_tables;
};
template<typename T>
struct parameters_of_impl
{
using type = typename T::_recursive_traits::_parameters;
};
template<typename T>
struct name_of_impl
{
using type = typename T::_name_t;
};
template<typename... T>
struct make_parameter_tuple_impl
{
using type = decltype(std::tuple_cat(std::declval<T>()...));
};
template<typename... T>
using make_parameter_tuple_t = typename make_parameter_tuple_impl<T...>::type;
using make_parameter_tuple_t = decltype(std::tuple_cat(std::declval<T>()...));
}
template<typename T>
using value_type_of = typename detail::value_type_of_impl<T>::type;
using value_type_of = typename T::_traits::_value_type;
template<typename T>
using cpp_value_type_of = typename value_type_of<T>::_cpp_value_type;
template<typename T>
using required_tables_of = typename detail::required_table_of_impl<T>::type;
using required_tables_of = typename T::_recursive_traits::_required_tables;
template<typename T>
using provided_tables_of = typename detail::provided_table_of_impl<T>::type;
using provided_tables_of = typename T::_recursive_traits::_provided_tables;
template<typename T>
using provided_outer_tables_of = typename detail::provided_outer_table_of_impl<T>::type;
using provided_outer_tables_of = typename T::_recursive_traits::_provided_outer_tables;
template<typename T>
using extra_tables_of = typename detail::extra_table_of_impl<T>::type;
using extra_tables_of = typename T::_recursive_traits::_extra_tables;
template<typename T>
using parameters_of = typename detail::parameters_of_impl<T>::type;
using parameters_of = typename T::_recursive_traits::_parameters;
template<typename T>
using name_of = typename detail::name_of_impl<T>::type;
using recursive_tags_of = typename T::_recursive_traits::_tags;
template<typename T>
using name_of = typename T::_name_t;
template<typename ValueType, typename... Tags>
struct make_traits
@ -221,6 +188,7 @@ namespace sqlpp
using _value_type = ValueType;
using _tags = detail::make_type_set_t<typename ValueType::_tag, Tags...>;
};
template<typename... Arguments>
struct make_recursive_traits
{
@ -229,7 +197,24 @@ namespace sqlpp
using _provided_outer_tables = detail::make_joined_set_t<provided_outer_tables_of<Arguments>...>;
using _extra_tables = detail::make_joined_set_t<extra_tables_of<Arguments>...>;
using _parameters = detail::make_parameter_tuple_t<parameters_of<Arguments>...>;
using _can_be_null = detail::any_t<can_be_null_t<Arguments>::value...>;
using _tags = detail::make_joined_set_t<recursive_tags_of<Arguments>...>;
};
template<typename... Tags>
struct recursive_tags
{
using _required_tables = detail::type_set<>;
using _provided_tables = detail::type_set<>;
using _provided_outer_tables = detail::type_set<>;
using _extra_tables = detail::type_set<>;
using _parameters = std::tuple<>;
using _tags = detail::type_set<Tags...>;
};
struct aggregate_function
{
struct _traits { using _value_type = void; using _tags = detail::type_set<>; };
using _recursive_traits = recursive_tags<tag::contains_aggregate_function>;
};
template<typename NameProvider, typename Member>

View File

@ -40,7 +40,7 @@ namespace sqlpp
using _traits = make_traits<ValueType, tag::is_expression>;
struct _recursive_traits : public make_recursive_traits<>
{
using _can_be_null = std::true_type; // since we do not know what's going on inside the verbatim, we assume it can be null
using _tags = detail::type_set<tag::can_be_null>; // since we do not know what's going on inside the verbatim, we assume it can be null
};
verbatim_t(std::string verbatim): _verbatim(verbatim) {}

View File

@ -24,6 +24,7 @@ function(test_constraint name pattern)
endfunction(test_constraint)
test_constraint(count_of_count "cannot be used on an aggregate function")
test_constraint(no_conversion_operator_if_null_not_trivial "int i = row.alpha")
test_constraint(require_insert "required column is missing")
test_constraint(must_not_insert "one assignment is prohibited")

View File

@ -0,0 +1,38 @@
/*
* Copyright (c) 2013-2014, 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 "Sample.h"
#include "MockDb.h"
#include <sqlpp11/functions.h>
#include <iostream>
MockDb db;
int main()
{
test::TabBar t;
count(count(t.alpha));
}