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

Started to add common table expressions and 'with'

This commit is contained in:
rbock 2015-01-13 21:53:29 +01:00
parent 8488dae0a7
commit 92e670118a
16 changed files with 329 additions and 13 deletions

View File

@ -54,11 +54,13 @@ namespace sqlpp
struct _recursive_traits
{
using _parameters = std::tuple<>;
using _required_ctes = detail::type_set<>;
using _provided_ctes = detail::type_set<>;
using _required_tables = detail::type_set<Table>;
using _provided_tables = detail::type_set<>;
using _provided_outer_tables = detail::type_set<>;
using _required_tables = detail::type_set<Table>;
using _extra_tables = detail::type_set<>;
using _parameters = std::tuple<>;
using _tags = typename std::conditional<column_spec_can_be_null_t<ColumnSpec>::value,
detail::type_set<tag::can_be_null>,
detail::type_set<>>::type;

View File

@ -41,6 +41,8 @@ namespace sqlpp
using _traits = make_traits<integral, tag::is_expression, tag::is_selectable>;
struct _recursive_traits
{
using _required_ctes = required_ctes_of<Expr>;
using _provided_ctes = detail::type_set<>;
using _required_tables = required_tables_of<Expr>;
using _provided_tables = provided_tables_of<Expr>;
using _provided_outer_tables = provided_outer_tables_of<Expr>;

61
include/sqlpp11/cte.h Normal file
View File

@ -0,0 +1,61 @@
/*
* 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.
*/
#ifndef SQLPP_CTE_H
#define SQLPP_CTE_H
#include <sqlpp11/statement_fwd.h>
#include <sqlpp11/type_traits.h>
#include <sqlpp11/parameter_list.h>
#include <sqlpp11/expression.h>
#include <sqlpp11/interpret_tuple.h>
#include <sqlpp11/interpretable_list.h>
#include <sqlpp11/logic.h>
namespace sqlpp
{
template<typename AliasProvider>
struct pre_cte_t
{
template<typename Statement>
auto as(Statement statement)
-> cte<AliasProvider, Statement>
{
// FIXME: Need to check stuff here.
return { statement };
}
};
template<typename AliasProvider>
auto cte(const AliasProvider&)
-> pre_cte_t<AliasProvider>
{
return {};
}
}
#endif

View File

@ -54,11 +54,13 @@ namespace sqlpp
using _traits = make_traits<no_value_t, tag::is_extra_tables>;
struct _recursive_traits
{
using _parameters = std::tuple<>;
using _required_ctes = detail::make_joined_set_t<required_ctes_of<Tables>...>;
using _provided_ctes = detail::type_set<>;
using _required_tables = detail::type_set<>;
using _provided_outer_tables = detail::type_set<>;
using _provided_tables = detail::type_set<>;
using _extra_tables = detail::type_set<Tables...>;
using _parameters = std::tuple<>;
using _tags = detail::type_set<>;
};

View File

@ -68,6 +68,8 @@ namespace sqlpp
using _traits = make_traits<no_value_t, tag::is_table, tag::is_join>;
struct _recursive_traits
{
using _required_ctes = detail::make_joined_set_t<required_ctes_of<Lhs>, required_ctes_of<Rhs>>;
using _provided_ctes = detail::type_set<>;
using _required_tables = detail::make_joined_set_t<required_tables_of<Lhs>, required_tables_of<Rhs>>;
using _provided_tables = detail::make_joined_set_t<provided_tables_of<Lhs>, provided_tables_of<Rhs>>;
using _provided_outer_tables = typename JoinType::template _provided_outer_tables<Lhs, Rhs>;

View File

@ -40,11 +40,13 @@ namespace sqlpp
using _traits = make_traits<ValueType, tag::is_parameter, tag::is_expression>;
struct _recursive_traits
{
using _parameters = std::tuple<parameter_t>;
using _required_ctes = detail::type_set<>;
using _provided_ctes = detail::type_set<>;
using _required_tables = detail::type_set<>;
using _provided_tables = detail::type_set<>;
using _provided_outer_tables = detail::type_set<>;
using _required_tables = detail::type_set<>;
using _extra_tables = detail::type_set<>;
using _parameters = std::tuple<parameter_t>;
using _tags = detail::type_set<tag::can_be_null>;
};

View File

@ -83,11 +83,13 @@ namespace sqlpp
struct _recursive_traits
{
using _parameters = std::tuple<>;
using _required_ctes = detail::type_set<>;
using _provided_ctes = detail::type_set<>;
using _required_tables = detail::type_set<>;
using _provided_tables = detail::type_set<>;
using _provided_outer_tables = detail::type_set<>;
using _required_tables = detail::type_set<>;
using _extra_tables = detail::type_set<>;
using _parameters = std::tuple<>;
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

@ -31,6 +31,7 @@
#include <sqlpp11/noop.h>
#include <sqlpp11/connection.h>
#include <sqlpp11/with.h>
#include <sqlpp11/select_flag_list.h>
#include <sqlpp11/select_column_list.h>
#include <sqlpp11/from.h>
@ -69,6 +70,7 @@ namespace sqlpp
template<typename Database>
using blank_select_t = statement_t<Database,
no_with_t,
select_t,
no_select_flag_list_t,
no_select_column_list_t,

View File

@ -44,6 +44,17 @@ namespace sqlpp
template<typename Db, typename... Policies>
struct statement_t;
struct assert_no_unknown_ctes_t
{
using type = std::false_type;
template<typename T = void>
static void _()
{
static_assert(wrong_t<T>::value, "one clause requires common table expressions which are otherwise not known in the statement");
}
};
struct assert_no_unknown_tables_t
{
using type = std::false_type;
@ -84,6 +95,8 @@ namespace sqlpp
template<typename Needle, typename Replacement>
using _new_statement_t = typename _policies_update_t<Needle, Replacement>::type;
using _all_required_ctes = detail::make_joined_set_t<required_ctes_of<Policies>...>;
using _all_provided_ctes = detail::make_joined_set_t<provided_ctes_of<Policies>...>;
using _all_required_tables = detail::make_joined_set_t<required_tables_of<Policies>...>;
using _all_provided_tables = detail::make_joined_set_t<provided_tables_of<Policies>...>;
using _all_provided_outer_tables = detail::make_joined_set_t<provided_outer_tables_of<Policies>...>;
@ -100,6 +113,12 @@ namespace sqlpp
_all_provided_tables // Hint: extra_tables are not used here because they are just a helper for dynamic .add_*()
>;
// The common table expressions not covered by the with.
using _required_ctes = detail::make_difference_set_t<
_all_required_ctes,
_all_provided_ctes
>;
using _result_type_provider = detail::get_last_if<is_return_value_t, noop, Policies...>;
struct _result_methods_t: public _result_type_provider::template _result_methods_t<_statement_t>
@ -111,7 +130,9 @@ namespace sqlpp
// - the select is complete (leaks no tables)
static constexpr bool _can_be_used_as_table()
{
return is_select_column_list_t<_result_type_provider>::value and _required_tables::size::value == 0
return is_select_column_list_t<_result_type_provider>::value
and _required_tables::size::value == 0
and _required_ctes::size::value == 0
? true
: false;
}
@ -133,6 +154,8 @@ namespace sqlpp
struct _recursive_traits
{
using _required_ctes = statement_policies_t::_required_ctes;
using _provided_ctes = detail::type_set<>;
using _required_tables = statement_policies_t::_required_tables;
using _provided_tables = detail::type_set<>;
using _provided_outer_tables = detail::type_set<>;
@ -143,6 +166,8 @@ namespace sqlpp
detail::type_set<>>::type;
};
using _cte_check = typename std::conditional<_required_ctes::size::value == 0,
consistent_t, assert_no_unknown_ctes_t>::type;
using _table_check = typename std::conditional<_required_tables::size::value == 0,
consistent_t, assert_no_unknown_tables_t>::type;
using _parameter_check = typename std::conditional<std::tuple_size<typename _recursive_traits::_parameters>::value == 0,
@ -161,10 +186,12 @@ namespace sqlpp
using _run_check = detail::get_first_if<is_inconsistent_t, consistent_t,
typename _policies_t::_parameter_check,
typename Policies::template _base_t<_policies_t>::_consistency_check...,
typename _policies_t::_table_check>;
typename _policies_t::_table_check,
typename _policies_t::_cte_check>;
using _prepare_check = detail::get_first_if<is_inconsistent_t, consistent_t,
typename Policies::template _base_t<_policies_t>::_consistency_check...,
typename _policies_t::_table_check>;
typename _policies_t::_table_check,
typename _policies_t::_cte_check>;
using _result_type_provider = typename _policies_t::_result_type_provider;
template<typename Composite>

View File

@ -48,11 +48,13 @@ namespace sqlpp
struct _recursive_traits
{
using _parameters = std::tuple<>;
using _required_ctes = detail::type_set<>;
using _provided_ctes = detail::type_set<>;
using _required_tables = detail::type_set<>;
using _provided_tables = detail::type_set<Table>;
using _provided_outer_tables = detail::type_set<>;
using _extra_tables = detail::type_set<>;
using _parameters = std::tuple<>;
using _tags = detail::type_set<>;
};

View File

@ -44,11 +44,13 @@ namespace sqlpp
struct _recursive_traits
{
using _parameters = std::tuple<>;
using _required_ctes = required_ctes_of<Table>;
using _provided_ctes = detail::type_set<>;
using _required_tables = detail::type_set<>;
using _provided_tables = detail::type_set<AliasProvider>;
using _provided_outer_tables = detail::type_set<>;
using _extra_tables = detail::type_set<>;
using _parameters = std::tuple<>;
using _tags = detail::type_set<>;
};

View File

@ -183,6 +183,12 @@ namespace sqlpp
template<typename T>
using cpp_value_type_of = typename value_type_of<T>::_cpp_value_type;
template<typename T>
using required_ctes_of = typename T::_recursive_traits::_required_ctes;
template<typename T>
using provided_ctes_of = typename T::_recursive_traits::_provided_ctes;
template<typename T>
using required_tables_of = typename T::_recursive_traits::_required_tables;
@ -217,6 +223,8 @@ namespace sqlpp
template<typename... Arguments>
struct make_recursive_traits
{
using _required_ctes = detail::make_joined_set_t<required_ctes_of<Arguments>...>;
using _provided_ctes = detail::make_joined_set_t<required_ctes_of<Arguments>...>;
using _required_tables = detail::make_joined_set_t<required_tables_of<Arguments>...>;
using _provided_tables = detail::make_joined_set_t<provided_tables_of<Arguments>...>;
using _provided_outer_tables = detail::make_joined_set_t<provided_outer_tables_of<Arguments>...>;
@ -228,6 +236,8 @@ namespace sqlpp
template<typename... Tags>
struct recursive_tags
{
using _required_ctes = detail::type_set<>;
using _provided_ctes = detail::type_set<>;
using _required_tables = detail::type_set<>;
using _provided_tables = detail::type_set<>;
using _provided_outer_tables = detail::type_set<>;

View File

@ -83,7 +83,7 @@ namespace sqlpp
template<bool, typename Union>
struct union_statement_impl
{
using type = statement_t<void, Union>;
using type = statement_t<void, Union, no_union_t>;
};
template<typename Union>
@ -208,6 +208,7 @@ namespace sqlpp
auto union_distinct(Rhs rhs) const
-> _new_statement_t<_check<Rhs>, union_t<void, distinct_t, derived_statement_t<Policies>, Rhs>>
{
#warning: make sure that Rhs is a select, not a uninon
static_assert(is_statement_t<Rhs>::value, "argument of union call has to be a statement");
static_assert(has_result_row_t<Rhs>::value, "argument of a union has to be a (complete) select statement");
static_assert(has_result_row_t<derived_statement_t<Policies>>::value, "left hand side argument of a union has to be a (complete) select statement");

158
include/sqlpp11/with.h Normal file
View File

@ -0,0 +1,158 @@
/*
* 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.
*/
#ifndef SQLPP_WITH_H
#define SQLPP_WITH_H
#include <sqlpp11/statement_fwd.h>
#include <sqlpp11/type_traits.h>
#include <sqlpp11/no_data.h>
#include <sqlpp11/policy_update.h>
#include <sqlpp11/parameter_list.h>
#include <sqlpp11/expression.h>
#include <sqlpp11/interpret_tuple.h>
#include <sqlpp11/interpretable_list.h>
#include <sqlpp11/logic.h>
namespace sqlpp
{
template<typename Database, typename... Expressions>
struct with_data_t
{
with_data_t(Expressions... expressions):
_expressions(expressions...)
{}
with_data_t(const with_data_t&) = default;
with_data_t(with_data_t&&) = default;
with_data_t& operator=(const with_data_t&) = default;
with_data_t& operator=(with_data_t&&) = default;
~with_data_t() = default;
std::tuple<Expressions...> _expressions;
interpretable_list_t<Database> _dynamic_expressions;
};
template<typename Database, typename... Expressions>
struct with_t
{
using _traits = make_traits<no_value_t, tag::is_where>;
using _recursive_traits = make_recursive_traits<Expressions...>;
using _is_dynamic = is_database<Database>;
// Data
using _data_t = with_data_t<Database, Expressions...>;
// Member implementation with data and methods
template <typename Policies>
struct _impl_t
{
public:
_data_t _data;
};
// Base template to be inherited by the statement
template<typename Policies>
struct _base_t
{
using _data_t = with_data_t<Database, Expressions...>;
_impl_t<Policies> where;
_impl_t<Policies>& operator()() { return where; }
const _impl_t<Policies>& operator()() const { return where; }
template<typename T>
static auto _get_member(T t) -> decltype(t.where)
{
return t.where;
}
#warning: Need real checks here
using _consistency_check = consistent_t;
};
};
struct no_with_t
{
using _traits = make_traits<no_value_t, tag::is_where>;
using _recursive_traits = make_recursive_traits<>;
// Data
using _data_t = no_data_t;
// Member implementation with data and methods
template<typename Policies>
struct _impl_t
{
_data_t _data;
};
// Base template to be inherited by the statement
template<typename Policies>
struct _base_t
{
using _data_t = no_data_t;
_impl_t<Policies> no_with;
_impl_t<Policies>& operator()() { return no_with; }
const _impl_t<Policies>& operator()() const { return no_with; }
template<typename T>
static auto _get_member(T t) -> decltype(t.no_with)
{
return t.no_with;
}
using _consistency_check = consistent_t;
};
};
template<typename Database, typename... Expressions>
struct blank_with_t
{
with_data_t<Database, Expressions...> _data;
template<typename Statement>
auto operator()(Statement statement)
-> new_statement_t<true, Statement, no_with_t, with_t<Expressions...>>
{
// FIXME need checks here, e.g. if there is recursion
return { statement, _data };
}
};
template<typename... Expressions>
auto with(Expressions... cte)
-> blank_with_t<void, Expressions...>
{
return { {cte...} };
}
}
#endif

View File

@ -19,6 +19,7 @@ endmacro ()
#build_and_run(Minimalistic)
#build_and_run(ResultTest)
build_and_run(UnionTest)
build_and_run(WithTest)
# if you want to use the generator, you can do something like this:
#find_package(PythonInterp REQUIRED)

40
tests/WithTest.cpp Normal file
View File

@ -0,0 +1,40 @@
/*
* 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/select.h>
#include <sqlpp11/alias_provider.h>
#include <iostream>
MockDb db;
MockDb::_serializer_context_t printer;
int main()
{
// to be done
return 0;
}