mirror of
https://github.com/rbock/sqlpp11.git
synced 2024-11-16 04:47:18 +08:00
Allow alias of single-column select to be used as select column (#542)
This commit is contained in:
parent
5c673603c3
commit
6cc2067ef5
@ -50,11 +50,31 @@ namespace sqlpp
|
|||||||
tag_if<tag::can_be_null, _can_be_null or _depends_on_outer_table>>;
|
tag_if<tag::can_be_null, _can_be_null or _depends_on_outer_table>>;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template<typename... NamedExpr>
|
||||||
|
struct select_expression_type {
|
||||||
|
using value_t = no_value_t;
|
||||||
|
static constexpr bool _is_expression = false;
|
||||||
|
static constexpr bool _can_be_null = false;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename NamedExpr>
|
||||||
|
struct select_expression_type<NamedExpr> {
|
||||||
|
using value_t = value_type_of<NamedExpr>;
|
||||||
|
static constexpr bool _is_expression = true;
|
||||||
|
static constexpr bool _can_be_null = can_be_null_t<NamedExpr>::value;
|
||||||
|
};
|
||||||
|
|
||||||
template <typename Select, typename... NamedExpr>
|
template <typename Select, typename... NamedExpr>
|
||||||
struct select_pseudo_table_t
|
struct select_pseudo_table_t
|
||||||
: public table_t<select_pseudo_table_t<Select, NamedExpr...>, select_column_spec_t<Select, NamedExpr>...>
|
: public table_t<select_pseudo_table_t<Select, NamedExpr...>, select_column_spec_t<Select, NamedExpr>...>
|
||||||
{
|
{
|
||||||
using _traits = make_traits<no_value_t,
|
using _expr_t = select_expression_type<NamedExpr...>;
|
||||||
|
using _traits = make_traits<
|
||||||
|
// Usage as named expression
|
||||||
|
typename _expr_t::value_t,
|
||||||
|
tag_if<tag::is_expression, _expr_t::_is_expression>,
|
||||||
|
tag_if<tag::can_be_null, _expr_t::_can_be_null>,
|
||||||
|
// Usage as table
|
||||||
tag::is_table,
|
tag::is_table,
|
||||||
tag::is_pseudo_table,
|
tag::is_pseudo_table,
|
||||||
tag_if<tag::requires_parens, requires_parens_t<Select>::value>>;
|
tag_if<tag::requires_parens, requires_parens_t<Select>::value>>;
|
||||||
|
@ -44,6 +44,7 @@ namespace sqlpp
|
|||||||
using _traits = make_traits<value_type_of<Table>,
|
using _traits = make_traits<value_type_of<Table>,
|
||||||
tag::is_table,
|
tag::is_table,
|
||||||
tag::is_alias,
|
tag::is_alias,
|
||||||
|
tag_if<tag::can_be_null, can_be_null_t<Table>::value>,
|
||||||
tag_if<tag::is_selectable, is_expression_t<Table>::value>>;
|
tag_if<tag::is_selectable, is_expression_t<Table>::value>>;
|
||||||
|
|
||||||
using _nodes = detail::type_vector<>;
|
using _nodes = detail::type_vector<>;
|
||||||
|
@ -43,6 +43,7 @@ set(test_files
|
|||||||
Min.cpp
|
Min.cpp
|
||||||
Operator.cpp
|
Operator.cpp
|
||||||
Over.cpp
|
Over.cpp
|
||||||
|
SelectAs.cpp
|
||||||
Some.cpp
|
Some.cpp
|
||||||
Sum.cpp
|
Sum.cpp
|
||||||
TableAlias.cpp
|
TableAlias.cpp
|
||||||
|
43
tests/core/serialize/SelectAs.cpp
Normal file
43
tests/core/serialize/SelectAs.cpp
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2023, 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 "compare.h"
|
||||||
|
#include "Sample.h"
|
||||||
|
#include <sqlpp11/sqlpp11.h>
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
SQLPP_ALIAS_PROVIDER(cheese)
|
||||||
|
|
||||||
|
int SelectAs(int, char*[])
|
||||||
|
{
|
||||||
|
const auto foo = test::TabFoo{};
|
||||||
|
const auto bar = test::TabBar{};
|
||||||
|
|
||||||
|
// SELECT...AS as selectable column
|
||||||
|
compare(__LINE__, select(foo.omega, select(count(bar.alpha)).from(bar).unconditionally().as(cheese)), "SELECT tab_foo.omega,(SELECT COUNT(tab_bar.alpha) AS count_ FROM tab_bar) AS cheese");
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
@ -61,6 +61,8 @@ void print_row(Row const& row)
|
|||||||
std::cout << a << ", " << b << std::endl;
|
std::cout << a << ", " << b << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SQLPP_ALIAS_PROVIDER(cheese)
|
||||||
|
|
||||||
int Select(int, char*[])
|
int Select(int, char*[])
|
||||||
{
|
{
|
||||||
MockDb db = {};
|
MockDb db = {};
|
||||||
@ -164,6 +166,7 @@ int Select(int, char*[])
|
|||||||
.dynamic_offset();
|
.dynamic_offset();
|
||||||
s.select_flags.add(sqlpp::distinct);
|
s.select_flags.add(sqlpp::distinct);
|
||||||
s.selected_columns.add(without_table_check(f.omega));
|
s.selected_columns.add(without_table_check(f.omega));
|
||||||
|
s.selected_columns.add(select(f.omega).from(f).unconditionally().as(f.delta));
|
||||||
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);
|
||||||
@ -210,5 +213,11 @@ int Select(int, char*[])
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for (const auto& row :
|
||||||
|
db(select(f.omega, select(count(t.alpha)).from(t).unconditionally().as(cheese)).from(f).unconditionally()))
|
||||||
|
{
|
||||||
|
std::cout << row.omega << " " << row.cheese << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -260,9 +260,9 @@ int SelectType(int, char*[])
|
|||||||
// Test an alias of a select of a single numeric table column
|
// Test an alias of a select of a single numeric table column
|
||||||
{
|
{
|
||||||
using T = decltype(select(t.alpha).from(t).as(alias::b));
|
using T = decltype(select(t.alpha).from(t).as(alias::b));
|
||||||
static_assert(not sqlpp::is_numeric_t<T>::value, "type requirement");
|
static_assert(sqlpp::is_numeric_t<T>::value, "type requirement");
|
||||||
static_assert(not sqlpp::is_expression_t<T>::value, "type requirement");
|
static_assert(not sqlpp::is_expression_t<T>::value, "type requirement");
|
||||||
static_assert(not sqlpp::is_selectable_t<T>::value, "type requirement");
|
static_assert(sqlpp::is_selectable_t<T>::value, "type requirement");
|
||||||
static_assert(not sqlpp::require_insert_t<T>::value, "type requirement");
|
static_assert(not sqlpp::require_insert_t<T>::value, "type requirement");
|
||||||
static_assert(not sqlpp::must_not_insert_t<T>::value, "type requirement");
|
static_assert(not sqlpp::must_not_insert_t<T>::value, "type requirement");
|
||||||
static_assert(not sqlpp::must_not_update_t<T>::value, "type requirement");
|
static_assert(not sqlpp::must_not_update_t<T>::value, "type requirement");
|
||||||
@ -276,9 +276,9 @@ int SelectType(int, char*[])
|
|||||||
// Test the column of an alias of a select of an alias of a single numeric table column
|
// Test the column of an alias of a select of an alias of a single numeric table column
|
||||||
{
|
{
|
||||||
using T = decltype(select(t.alpha.as(alias::a)).from(t).as(alias::b));
|
using T = decltype(select(t.alpha.as(alias::a)).from(t).as(alias::b));
|
||||||
static_assert(not sqlpp::is_numeric_t<T>::value, "type requirement");
|
static_assert(sqlpp::is_numeric_t<T>::value, "type requirement");
|
||||||
static_assert(not sqlpp::is_expression_t<T>::value, "type requirement");
|
static_assert(not sqlpp::is_expression_t<T>::value, "type requirement");
|
||||||
static_assert(not sqlpp::is_selectable_t<T>::value, "type requirement");
|
static_assert(sqlpp::is_selectable_t<T>::value, "type requirement");
|
||||||
static_assert(not sqlpp::require_insert_t<T>::value, "type requirement");
|
static_assert(not sqlpp::require_insert_t<T>::value, "type requirement");
|
||||||
static_assert(not sqlpp::must_not_insert_t<T>::value, "type requirement");
|
static_assert(not sqlpp::must_not_insert_t<T>::value, "type requirement");
|
||||||
static_assert(not sqlpp::must_not_update_t<T>::value, "type requirement");
|
static_assert(not sqlpp::must_not_update_t<T>::value, "type requirement");
|
||||||
|
Loading…
Reference in New Issue
Block a user