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

Removed extra_tables (use without_table_check) instead

This commit is contained in:
rbock 2016-03-17 17:28:59 +01:00
parent f3bd2f01ab
commit 452175b514
9 changed files with 16 additions and 262 deletions

View File

@ -1,220 +0,0 @@
/*
* 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.
*/
#ifndef SQLPP_EXTRA_TABLES_H
#define SQLPP_EXTRA_TABLES_H
#include <sqlpp11/type_traits.h>
#include <sqlpp11/logic.h>
#include <sqlpp11/policy_update.h>
namespace sqlpp
{
template <typename... Tables>
struct extra_tables_data_t
{
extra_tables_data_t()
{
}
extra_tables_data_t(const extra_tables_data_t&) = default;
extra_tables_data_t(extra_tables_data_t&&) = default;
extra_tables_data_t& operator=(const extra_tables_data_t&) = default;
extra_tables_data_t& operator=(extra_tables_data_t&&) = default;
~extra_tables_data_t() = default;
};
// EXTRA_TABLES
template <typename... Tables>
struct extra_tables_t
{
using _traits = make_traits<no_value_t, tag::is_extra_tables>;
using _nodes = detail::type_vector<>;
using _required_ctes = detail::make_joined_set_t<required_ctes_of<Tables>...>;
using _extra_tables = detail::type_set<Tables...>;
// Data
using _data_t = extra_tables_data_t<Tables...>;
// Member implementation with data and methods
template <typename Policies>
struct _impl_t
{
// workaround for msvc bug https://connect.microsoft.com/VisualStudio/Feedback/Details/2091069
_impl_t() = default;
_impl_t(const _data_t& data) : _data(data)
{
}
_data_t _data;
};
// Base template to be inherited by the statement
template <typename Policies>
struct _base_t
{
using _data_t = extra_tables_data_t<Tables...>;
// workaround for msvc bug https://connect.microsoft.com/VisualStudio/Feedback/Details/2091069
template <typename... Args>
_base_t(Args&&... args)
: extra_tables{std::forward<Args>(args)...}
{
}
_impl_t<Policies> extra_tables;
_impl_t<Policies>& operator()()
{
return extra_tables;
}
const _impl_t<Policies>& operator()() const
{
return extra_tables;
}
template <typename T>
static auto _get_member(T t) -> decltype(t.extra_tables)
{
return t.extra_tables;
}
using _consistency_check = consistent_t;
};
};
// NO EXTRA TABLES YET
struct no_extra_tables_t
{
using _traits = make_traits<no_value_t, tag::is_noop>;
using _nodes = detail::type_vector<>;
// Data
using _data_t = no_data_t;
// Member implementation with data and methods
template <typename Policies>
struct _impl_t
{
// workaround for msvc bug https://connect.microsoft.com/VisualStudio/Feedback/Details/2091069
_impl_t() = default;
_impl_t(const _data_t& data) : _data(data)
{
}
_data_t _data;
};
// Base template to be inherited by the statement
template <typename Policies>
struct _base_t
{
using _data_t = no_data_t;
// workaround for msvc bug https://connect.microsoft.com/VisualStudio/Feedback/Details/2091069
template <typename... Args>
_base_t(Args&&... args)
: no_extra_tables{std::forward<Args>(args)...}
{
}
_impl_t<Policies> no_extra_tables;
_impl_t<Policies>& operator()()
{
return no_extra_tables;
}
const _impl_t<Policies>& operator()() const
{
return no_extra_tables;
}
template <typename T>
static auto _get_member(T t) -> decltype(t.no_extra_tables)
{
return t.no_extra_tables;
}
template <typename Check, typename T>
using _new_statement_t = new_statement_t<Check::value, Policies, no_extra_tables_t, T>;
// workaround for msvc bug https://connect.microsoft.com/VisualStudio/Feedback/Details/2173269
// template <typename... T>
// using _check = logic::all_t<is_table_t<T>::value...>;
template <typename... T>
struct _check : logic::all_t<is_table_t<T>::value...>
{
};
using _consistency_check = consistent_t;
template <typename... Tables>
auto extra_tables(Tables... tables) const -> _new_statement_t<_check<Tables...>, extra_tables_t<Tables...>>
{
static_assert(_check<Tables...>::value, "at least one argument is not a table or join in extra_tables()");
return _extra_tables_impl<void>(_check<Tables...>{}, tables...);
}
private:
template <typename Database, typename... Tables>
auto _extra_tables_impl(const std::false_type&, Tables... tables) const -> bad_statement;
template <typename Database, typename... Tables>
auto _extra_tables_impl(const std::true_type&, Tables...) const
-> _new_statement_t<std::true_type, extra_tables_t<Tables...>>
{
static_assert(required_tables_of<extra_tables_t<Tables...>>::size::value == 0,
"at least one table depends on another table in extra_tables()");
static constexpr std::size_t _number_of_tables = detail::sum(provided_tables_of<Tables>::size::value...);
using _unique_tables = detail::make_joined_set_t<provided_tables_of<Tables>...>;
// workaround for msvc bug https://connect.microsoft.com/VisualStudio/feedback/details/2173198
// using _unique_table_names = detail::transform_set_t<name_of, _unique_tables>;
using _unique_table_names = detail::make_name_of_set_t<_unique_tables>;
static_assert(_number_of_tables == _unique_tables::size::value,
"at least one duplicate table detected in extra_tables()");
static_assert(_number_of_tables == _unique_table_names::size::value,
"at least one duplicate table name detected in extra_tables()");
return {static_cast<const derived_statement_t<Policies>&>(*this), extra_tables_data_t<Tables...>{}};
}
};
};
// Interpreters
template <typename Context, typename Database, typename... Tables>
struct serializer_t<Context, extra_tables_data_t<Database, Tables...>>
{
using _serialize_check = serialize_check_of<Context, Tables...>;
using T = extra_tables_data_t<Database, Tables...>;
static Context& _(const T&, Context& context)
{
return context;
}
};
}
#endif

View File

@ -34,7 +34,6 @@
#include <sqlpp11/prepared_remove.h> #include <sqlpp11/prepared_remove.h>
#include <sqlpp11/noop.h> #include <sqlpp11/noop.h>
#include <sqlpp11/from.h> #include <sqlpp11/from.h>
#include <sqlpp11/extra_tables.h>
#include <sqlpp11/using.h> #include <sqlpp11/using.h>
#include <sqlpp11/where.h> #include <sqlpp11/where.h>
@ -103,7 +102,7 @@ namespace sqlpp
}; };
template <typename Database> template <typename Database>
using blank_remove_t = statement_t<Database, remove_t, no_from_t, no_using_t, no_extra_tables_t, no_where_t<true>>; using blank_remove_t = statement_t<Database, remove_t, no_from_t, no_using_t, no_where_t<true>>;
inline auto remove() -> blank_remove_t<void> inline auto remove() -> blank_remove_t<void>
{ {

View File

@ -35,7 +35,6 @@
#include <sqlpp11/select_flag_list.h> #include <sqlpp11/select_flag_list.h>
#include <sqlpp11/select_column_list.h> #include <sqlpp11/select_column_list.h>
#include <sqlpp11/from.h> #include <sqlpp11/from.h>
#include <sqlpp11/extra_tables.h>
#include <sqlpp11/where.h> #include <sqlpp11/where.h>
#include <sqlpp11/group_by.h> #include <sqlpp11/group_by.h>
#include <sqlpp11/having.h> #include <sqlpp11/having.h>
@ -77,7 +76,6 @@ namespace sqlpp
no_select_flag_list_t, no_select_flag_list_t,
no_select_column_list_t, no_select_column_list_t,
no_from_t, no_from_t,
no_extra_tables_t,
no_where_t<true>, no_where_t<true>,
no_group_by_t, no_group_by_t,
no_having_t, no_having_t,

View File

@ -76,13 +76,10 @@ namespace sqlpp
using _all_required_tables = detail::make_joined_set_t<required_tables_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_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>...>; using _all_provided_outer_tables = detail::make_joined_set_t<provided_outer_tables_of<Policies>...>;
using _all_extra_tables = detail::make_joined_set_t<extra_tables_of<Policies>...>;
using _all_provided_aggregates = detail::make_joined_set_t<provided_aggregates_of<Policies>...>; using _all_provided_aggregates = detail::make_joined_set_t<provided_aggregates_of<Policies>...>;
using _known_tables = detail::make_joined_set_t<_all_provided_tables, _all_extra_tables>;
template <typename Expression> template <typename Expression>
using _no_unknown_tables = detail::is_subset_of<required_tables_of<Expression>, _known_tables>; using _no_unknown_tables = detail::is_subset_of<required_tables_of<Expression>, _all_provided_tables>;
// workaround for msvc bug https://connect.microsoft.com/VisualStudio/Feedback/Details/2086629 // workaround for msvc bug https://connect.microsoft.com/VisualStudio/Feedback/Details/2086629
// template <typename... Expressions> // template <typename... Expressions>
@ -100,11 +97,7 @@ namespace sqlpp
using any_t = logic::any_t<Predicate<Policies>::value...>; using any_t = logic::any_t<Predicate<Policies>::value...>;
// The tables not covered by the from. // The tables not covered by the from.
using _required_tables = using _required_tables = detail::make_difference_set_t<_all_required_tables, _all_provided_tables>;
detail::make_difference_set_t<_all_required_tables,
_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. // The common table expressions not covered by the with.
using _required_ctes = detail::make_difference_set_t<_all_required_ctes, _all_provided_ctes>; using _required_ctes = detail::make_difference_set_t<_all_required_ctes, _all_provided_ctes>;

View File

@ -181,7 +181,6 @@ namespace sqlpp
SQLPP_VALUE_TRAIT_GENERATOR(is_from) SQLPP_VALUE_TRAIT_GENERATOR(is_from)
SQLPP_VALUE_TRAIT_GENERATOR(is_single_table) SQLPP_VALUE_TRAIT_GENERATOR(is_single_table)
SQLPP_VALUE_TRAIT_GENERATOR(is_into) SQLPP_VALUE_TRAIT_GENERATOR(is_into)
SQLPP_VALUE_TRAIT_GENERATOR(is_extra_tables)
SQLPP_VALUE_TRAIT_GENERATOR(is_on) SQLPP_VALUE_TRAIT_GENERATOR(is_on)
SQLPP_VALUE_TRAIT_GENERATOR(is_where) SQLPP_VALUE_TRAIT_GENERATOR(is_where)
SQLPP_VALUE_TRAIT_GENERATOR(is_group_by) SQLPP_VALUE_TRAIT_GENERATOR(is_group_by)
@ -259,7 +258,6 @@ namespace sqlpp
SQLPP_RECURSIVE_TRAIT_SET_GENERATOR(required_tables) SQLPP_RECURSIVE_TRAIT_SET_GENERATOR(required_tables)
SQLPP_RECURSIVE_TRAIT_SET_GENERATOR(provided_tables) SQLPP_RECURSIVE_TRAIT_SET_GENERATOR(provided_tables)
SQLPP_RECURSIVE_TRAIT_SET_GENERATOR(provided_outer_tables) SQLPP_RECURSIVE_TRAIT_SET_GENERATOR(provided_outer_tables)
SQLPP_RECURSIVE_TRAIT_SET_GENERATOR(extra_tables)
SQLPP_RECURSIVE_TRAIT_SET_GENERATOR(provided_aggregates) SQLPP_RECURSIVE_TRAIT_SET_GENERATOR(provided_aggregates)
#define SQLPP_RECURSIVE_TRAIT_GENERATOR(trait) \ #define SQLPP_RECURSIVE_TRAIT_GENERATOR(trait) \

View File

@ -33,23 +33,13 @@
namespace sqlpp namespace sqlpp
{ {
template <typename Expression> template <typename Expression>
struct without_table_check_t struct without_table_check_t : Expression
{ {
using _traits = typename Expression::_traits;
using _nodes = detail::type_vector<Expression>;
using _required_tables = detail::type_set<>; using _required_tables = detail::type_set<>;
without_table_check_t(Expression expression) : _expression(expression) without_table_check_t(Expression expression) : Expression(expression)
{ {
} }
without_table_check_t(const without_table_check_t&) = default;
without_table_check_t(without_table_check_t&&) = default;
without_table_check_t& operator=(const without_table_check_t&) = default;
without_table_check_t& operator=(without_table_check_t&&) = default;
~without_table_check_t() = default;
Expression _expression;
}; };
template <typename Context, typename Expression> template <typename Context, typename Expression>
@ -60,7 +50,7 @@ namespace sqlpp
static Context& _(const T& t, Context& context) static Context& _(const T& t, Context& context)
{ {
serialize(t._expression, context); serialize<Expression>(t, context);
return context; return context;
} }
}; };

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2013-2015, Roland Bock * Copyright (c) 2013-2016, Roland Bock
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without modification, * Redistribution and use in source and binary forms, with or without modification,
@ -163,13 +163,11 @@ int Interpret(int, char* [])
serialize(s, printer).str(); serialize(s, printer).str();
} }
{ {
auto s = dynamic_select(db) // Behold, dynamically constructed queries might compile but be illegal SQL
.dynamic_flags(sqlpp::distinct) auto s = dynamic_select(db).dynamic_flags(sqlpp::distinct).dynamic_columns(t.alpha);
.dynamic_columns(t.alpha)
.extra_tables(t); // Would fail to run()
s.select_flags.add(sqlpp::all); s.select_flags.add(sqlpp::all);
s.selected_columns.add(t.beta); s.selected_columns.add(without_table_check(t.beta));
s.selected_columns.add(t.gamma); s.selected_columns.add(without_table_check(t.gamma));
serialize(s, printer).str(); serialize(s, printer).str();
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2013-2015, Roland Bock * Copyright (c) 2013-2016, Roland Bock
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without modification, * Redistribution and use in source and binary forms, with or without modification,
@ -31,6 +31,7 @@
#include <sqlpp11/select.h> #include <sqlpp11/select.h>
#include <sqlpp11/functions.h> #include <sqlpp11/functions.h>
#include <sqlpp11/connection.h> #include <sqlpp11/connection.h>
#include <sqlpp11/without_table_check.h>
template <typename Db, typename Column> template <typename Db, typename Column>
int64_t getColumn(Db&& db, const Column& column) int64_t getColumn(Db&& db, const Column& column)
@ -108,7 +109,6 @@ int Select(int, char* [])
.columns(all_of(t)) .columns(all_of(t))
.flags(sqlpp::all) .flags(sqlpp::all)
.from(t) .from(t)
.extra_tables(f, t)
.where(t.alpha > 0) .where(t.alpha > 0)
.group_by(t.alpha) .group_by(t.alpha)
.order_by(t.gamma.asc()) .order_by(t.gamma.asc())
@ -122,7 +122,6 @@ int Select(int, char* [])
.columns(all_of(t)) .columns(all_of(t))
.flags(sqlpp::all) .flags(sqlpp::all)
.from(t) .from(t)
.extra_tables(f, t)
.where(t.alpha > 0) .where(t.alpha > 0)
.group_by(t.alpha) .group_by(t.alpha)
.order_by(t.gamma.asc()) .order_by(t.gamma.asc())
@ -137,7 +136,6 @@ int Select(int, char* [])
.dynamic_columns(all_of(t)) .dynamic_columns(all_of(t))
.dynamic_flags() .dynamic_flags()
.dynamic_from(t) .dynamic_from(t)
.extra_tables(f, t)
.dynamic_where() .dynamic_where()
.dynamic_group_by(t.alpha) .dynamic_group_by(t.alpha)
.dynamic_order_by() .dynamic_order_by()
@ -145,7 +143,7 @@ int Select(int, char* [])
.dynamic_limit() .dynamic_limit()
.dynamic_offset(); .dynamic_offset();
s.select_flags.add(sqlpp::distinct); s.select_flags.add(sqlpp::distinct);
s.selected_columns.add(f.omega); s.selected_columns.add(without_table_check(f.omega));
s.from.add(dynamic_cross_join(f)); s.from.add(dynamic_cross_join(f));
s.where.add(t.alpha > 7); s.where.add(t.alpha > 7);
s.having.add(t.alpha > 7); s.having.add(t.alpha > 7);

View File

@ -340,8 +340,8 @@ int SelectType(int, char* [])
// Test that select can be called with zero columns if it is used with dynamic columns. // Test that select can be called with zero columns if it is used with dynamic columns.
{ {
auto s = dynamic_select(db).dynamic_columns().extra_tables(t); auto s = dynamic_select(db).dynamic_columns();
s.selected_columns.add(t.alpha); s.selected_columns.add(without_table_check(t.alpha));
serialize(s, printer).str(); serialize(s, printer).str();
} }