mirror of
https://github.com/rbock/sqlpp11.git
synced 2024-11-16 04:47:18 +08:00
Simplified syntax for multi_columns a lot
Instead of multi_column(alias, columns...) it is now multi_column(columns...).as(alias) Also, you can now write: all_of(table).as(alias)
This commit is contained in:
parent
3e9eab8773
commit
61a4797d0b
71
include/sqlpp11/all_of.h
Normal file
71
include/sqlpp11/all_of.h
Normal file
@ -0,0 +1,71 @@
|
||||
/*
|
||||
* Copyright (c) 2013, 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_ALL_OF_H
|
||||
#define SQLPP_ALL_OF_H
|
||||
|
||||
#include <sqlpp11/interpret.h>
|
||||
#include <sqlpp11/alias.h>
|
||||
#include <sqlpp11/multi_column.h>
|
||||
|
||||
namespace sqlpp
|
||||
{
|
||||
template<typename Table>
|
||||
struct all_of_t
|
||||
{
|
||||
using _column_tuple_t = typename Table::_column_tuple_t;
|
||||
|
||||
template<typename AliasProvider>
|
||||
detail::copy_tuple_args_t<multi_column_alias_t, AliasProvider, _column_tuple_t> as(const AliasProvider& alias)
|
||||
{
|
||||
return ::sqlpp::multi_column(_column_tuple_t{}).as(alias);
|
||||
}
|
||||
};
|
||||
|
||||
template<typename Table>
|
||||
auto all_of(Table t) -> all_of_t<Table>
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
||||
namespace vendor
|
||||
{
|
||||
template<typename Context, typename Table>
|
||||
struct interpreter_t<Context, all_of_t<Table>>
|
||||
{
|
||||
using T = all_of_t<Table>;
|
||||
|
||||
static Context& _(const T& t, const Context&)
|
||||
{
|
||||
static_assert(wrong_t<T>::value, "all_of(table) does not seem to be used in select");
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
|
@ -31,12 +31,21 @@
|
||||
|
||||
namespace sqlpp
|
||||
{
|
||||
template<typename Table>
|
||||
struct all_of_t;
|
||||
|
||||
namespace detail
|
||||
{
|
||||
template<typename T>
|
||||
struct as_tuple
|
||||
{
|
||||
static std::tuple<T> _(T t) { return std::tuple<T>{ t }; };
|
||||
static std::tuple<T> _(T t) { return std::tuple<T>{ t }; }
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
struct as_tuple<::sqlpp::all_of_t<T>>
|
||||
{
|
||||
static typename ::sqlpp::all_of_t<T>::_column_tuple_t _(::sqlpp::all_of_t<T>) { return { }; }
|
||||
};
|
||||
|
||||
template<typename... Args>
|
||||
|
@ -35,12 +35,13 @@
|
||||
namespace sqlpp
|
||||
{
|
||||
template<typename AliasProvider, typename... Columns>
|
||||
struct multi_column_alias_t;
|
||||
|
||||
template<typename Unused, typename... Columns>
|
||||
struct multi_column_t
|
||||
{
|
||||
static_assert(detail::and_t<is_named_expression_t, Columns...>::value, "multi_column parameters need to be named expressions");
|
||||
|
||||
using _name_t = typename AliasProvider::_name_t;
|
||||
|
||||
multi_column_t(std::tuple<Columns...> columns):
|
||||
_columns(columns)
|
||||
{}
|
||||
@ -55,6 +56,43 @@ namespace sqlpp
|
||||
multi_column_t& operator=(multi_column_t&&) = default;
|
||||
~multi_column_t() = default;
|
||||
|
||||
template<typename AliasProvider>
|
||||
multi_column_alias_t<AliasProvider, Columns...> as(const AliasProvider&)
|
||||
{
|
||||
return { *this };
|
||||
}
|
||||
|
||||
|
||||
using _value_type = no_value_t;
|
||||
using _is_multi_column = std::true_type;
|
||||
|
||||
std::tuple<Columns...> _columns;
|
||||
};
|
||||
|
||||
template<typename AliasProvider, typename... Columns>
|
||||
struct multi_column_alias_t
|
||||
{
|
||||
static_assert(detail::and_t<is_named_expression_t, Columns...>::value, "multi_column parameters need to be named expressions");
|
||||
|
||||
using _name_t = typename AliasProvider::_name_t;
|
||||
|
||||
multi_column_alias_t(multi_column_t<void, Columns...> multi_column):
|
||||
_columns(multi_column._columns)
|
||||
{}
|
||||
|
||||
multi_column_alias_t(std::tuple<Columns...> columns):
|
||||
_columns(columns)
|
||||
{}
|
||||
|
||||
multi_column_alias_t(Columns... columns):
|
||||
_columns(columns...)
|
||||
{}
|
||||
|
||||
multi_column_alias_t(const multi_column_alias_t&) = default;
|
||||
multi_column_alias_t(multi_column_alias_t&&) = default;
|
||||
multi_column_alias_t& operator=(const multi_column_alias_t&) = default;
|
||||
multi_column_alias_t& operator=(multi_column_alias_t&&) = default;
|
||||
~multi_column_alias_t() = default;
|
||||
|
||||
struct _value_type: public no_value_t
|
||||
{
|
||||
@ -67,10 +105,21 @@ namespace sqlpp
|
||||
|
||||
namespace vendor
|
||||
{
|
||||
template<typename Context, typename AliasProvider, typename... Columns>
|
||||
struct interpreter_t<Context, multi_column_t<AliasProvider, Columns...>>
|
||||
template<typename Context, typename... Columns>
|
||||
struct interpreter_t<Context, multi_column_t<void, Columns...>>
|
||||
{
|
||||
using T = multi_column_t<AliasProvider, Columns...>;
|
||||
using T = multi_column_t<void, Columns...>;
|
||||
|
||||
static void _(const T& t, Context& context)
|
||||
{
|
||||
static_assert(wrong_t<Columns...>::value, "multi_column must be used with an alias");
|
||||
}
|
||||
};
|
||||
|
||||
template<typename Context, typename AliasProvider, typename... Columns>
|
||||
struct interpreter_t<Context, multi_column_alias_t<AliasProvider, Columns...>>
|
||||
{
|
||||
using T = multi_column_alias_t<AliasProvider, Columns...>;
|
||||
|
||||
static Context& _(const T& t, Context& context)
|
||||
{
|
||||
@ -82,17 +131,17 @@ namespace sqlpp
|
||||
|
||||
namespace detail
|
||||
{
|
||||
template<typename AliasProvider, typename... Columns>
|
||||
template<typename... Columns>
|
||||
using make_multi_column_t =
|
||||
detail::copy_tuple_args_t<multi_column_t, AliasProvider,
|
||||
detail::copy_tuple_args_t<multi_column_t, void,
|
||||
decltype(std::tuple_cat(detail::as_tuple<Columns>::_(std::declval<Columns>())...))>;
|
||||
}
|
||||
|
||||
template<typename AliasProvider, typename... Columns>
|
||||
auto multi_column(const AliasProvider&, Columns... columns)
|
||||
-> detail::make_multi_column_t<AliasProvider, Columns...>
|
||||
template<typename... Columns>
|
||||
auto multi_column(Columns... columns)
|
||||
-> detail::make_multi_column_t<Columns...>
|
||||
{
|
||||
return detail::make_multi_column_t<AliasProvider, Columns...>(std::tuple_cat(detail::as_tuple<Columns>::_(columns)...));
|
||||
return detail::make_multi_column_t<Columns...>(std::tuple_cat(detail::as_tuple<Columns>::_(columns)...));
|
||||
}
|
||||
|
||||
|
||||
|
@ -29,6 +29,7 @@
|
||||
|
||||
#include <sqlpp11/type_traits.h>
|
||||
#include <sqlpp11/table_alias.h>
|
||||
#include <sqlpp11/all_of.h>
|
||||
#include <sqlpp11/column.h>
|
||||
#include <sqlpp11/detail/type_set.h>
|
||||
#include <sqlpp11/join.h>
|
||||
@ -42,10 +43,9 @@ namespace sqlpp
|
||||
struct table_t: public table_base_t, public ColumnSpec::_name_t::template _member_t<column_t<Table, ColumnSpec>>...
|
||||
{
|
||||
using _table_set = detail::type_set<Table>; // Hint need a type_set here to be similar to a join (which always represents more than one table)
|
||||
using _all_columns = typename detail::make_set<column_t<Table, ColumnSpec>...>::type;
|
||||
static_assert(_all_columns::size::value, "at least one column required per table");
|
||||
static_assert(sizeof...(ColumnSpec), "at least one column required per table");
|
||||
using _required_insert_columns = typename detail::make_set_if<require_insert_t, column_t<Table, ColumnSpec>...>::type;
|
||||
using _all_of_t = std::tuple<column_t<Table, ColumnSpec>...>;
|
||||
using _column_tuple_t = std::tuple<column_t<Table, ColumnSpec>...>;
|
||||
template<typename AliasProvider>
|
||||
using _alias_t = table_alias_t<AliasProvider, Table, ColumnSpec...>;
|
||||
|
||||
@ -93,12 +93,6 @@ namespace sqlpp
|
||||
}
|
||||
};
|
||||
|
||||
template<typename Table>
|
||||
auto all_of(Table t) -> typename Table::_all_of_t
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
||||
namespace vendor
|
||||
{
|
||||
template<typename Context, typename X>
|
||||
|
@ -52,7 +52,7 @@ namespace sqlpp
|
||||
};
|
||||
|
||||
using _name_t = typename AliasProvider::_name_t;
|
||||
using _all_of_t = std::tuple<column_t<AliasProvider, ColumnSpec>...>;
|
||||
using _column_tuple_t = std::tuple<column_t<Table, ColumnSpec>...>;
|
||||
|
||||
table_alias_t(Table table):
|
||||
_table(table)
|
||||
|
2
include/sqlpp11/vendor/field.h
vendored
2
include/sqlpp11/vendor/field.h
vendored
@ -54,7 +54,7 @@ namespace sqlpp
|
||||
};
|
||||
|
||||
template<typename AliasProvider, typename... NamedExpr>
|
||||
struct make_field_t_impl<multi_column_t<AliasProvider, NamedExpr...>>
|
||||
struct make_field_t_impl<multi_column_alias_t<AliasProvider, NamedExpr...>>
|
||||
{
|
||||
using type = multi_field_t<AliasProvider, std::tuple<typename make_field_t_impl<NamedExpr>::type...>>;
|
||||
};
|
||||
|
@ -127,8 +127,9 @@ int main()
|
||||
interpret(t.inner_join(t.as(t.alpha)).on(t.beta == t.as(t.alpha).beta), printer).flush();
|
||||
|
||||
// multi_column
|
||||
interpret(multi_column(t.alpha, t.alpha, (t.beta + "cake").as(t.gamma)), printer).flush();
|
||||
interpret(multi_column(t, all_of(t)), printer).flush();
|
||||
interpret(multi_column(t.alpha, (t.beta + "cake").as(t.gamma)).as(t.alpha), printer).flush();
|
||||
interpret(multi_column(all_of(t)).as(t), printer).flush();
|
||||
interpret(all_of(t).as(t), printer).flush();
|
||||
|
||||
// dynamic select
|
||||
{
|
||||
|
@ -281,21 +281,14 @@ int main()
|
||||
|
||||
// Test that select(all_of(tab)) is expanded in multi_column
|
||||
{
|
||||
auto a = multi_column(alias::a, all_of(t));
|
||||
auto b = multi_column(alias::a, t.alpha, t.beta, t.gamma, t.delta);
|
||||
auto a = multi_column(all_of(t)).as(alias::a);
|
||||
auto b = multi_column(t.alpha, t.beta, t.gamma, t.delta).as(alias::a);
|
||||
static_assert(std::is_same<decltype(a), decltype(b)>::value, "all_of(t) has to be expanded by multi_column");
|
||||
}
|
||||
|
||||
// Test that select(tab) is expanded in multi_column
|
||||
{
|
||||
auto a = multi_column(alias::a, all_of(t));
|
||||
auto b = multi_column(alias::a, t.alpha, t.beta, t.gamma, t.delta);
|
||||
static_assert(std::is_same<decltype(a), decltype(b)>::value, "t has to be expanded by multi_column");
|
||||
}
|
||||
|
||||
// Test that a multicolumn is not a value
|
||||
{
|
||||
auto m = multi_column(alias::a, t.alpha, t.beta);
|
||||
auto m = multi_column(t.alpha, t.beta).as(alias::a);
|
||||
auto a = select(m).from(t).as(alias::b).a;
|
||||
static_assert(not sqlpp::is_value_t<decltype(a)>::value, "a multi_column is not a value");
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user