mirror of
https://github.com/rbock/sqlpp11.git
synced 2024-11-16 04:47:18 +08:00
Add tests for schema-qualified tables
This commit is contained in:
parent
e7fcb03ca6
commit
c52a60cabd
@ -77,4 +77,10 @@ namespace sqlpp
|
|||||||
return ::sqlpp::cross_join(this->derived(), std::move(t));
|
return ::sqlpp::cross_join(this->derived(), std::move(t));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
struct has_enabled_join : public std::is_base_of<enable_join<T>, T>
|
||||||
|
{
|
||||||
|
};
|
||||||
|
|
||||||
} // namespace sqlpp
|
} // namespace sqlpp
|
||||||
|
@ -38,7 +38,12 @@ namespace sqlpp
|
|||||||
template <typename Context>
|
template <typename Context>
|
||||||
auto to_sql_string(Context& context, const schema_t& t) -> std::string
|
auto to_sql_string(Context& context, const schema_t& t) -> std::string
|
||||||
{
|
{
|
||||||
context << t._name;
|
return name_to_sql_string(context, t._name);
|
||||||
return context;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
auto schema(std::string name) -> schema_t
|
||||||
|
{
|
||||||
|
return schema_t{std::move(name)};
|
||||||
|
};
|
||||||
|
|
||||||
} // namespace sqlpp
|
} // namespace sqlpp
|
||||||
|
@ -37,52 +37,57 @@
|
|||||||
|
|
||||||
namespace sqlpp
|
namespace sqlpp
|
||||||
{
|
{
|
||||||
template <typename Table>
|
template <typename TableSpec, typename NameTag>
|
||||||
|
struct schema_qualified_table_as_t
|
||||||
|
: public TableSpec::_table_columns<schema_qualified_table_as_t<TableSpec, NameTag>>, public enable_join<schema_qualified_table_as_t<TableSpec, NameTag>>
|
||||||
|
{
|
||||||
|
schema_qualified_table_as_t(schema_t schema) : _schema(std::move(schema))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
schema_t _schema;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename TableSpec, typename NameTag>
|
||||||
|
struct is_table<schema_qualified_table_as_t<TableSpec, NameTag>> : public std::true_type {};
|
||||||
|
|
||||||
|
template <typename TableSpec, typename NameTag>
|
||||||
|
struct name_tag_of<schema_qualified_table_as_t<TableSpec, NameTag>> {
|
||||||
|
using type = NameTag;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename TableSpec, typename NameTag>
|
||||||
|
struct provided_tables_of<schema_qualified_table_as_t<TableSpec, NameTag>>
|
||||||
|
{
|
||||||
|
using type = detail::type_vector<schema_qualified_table_as_t<TableSpec, NameTag>>;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename Context, typename TableSpec, typename NameTag>
|
||||||
|
auto to_sql_string(Context& context, const schema_qualified_table_as_t<TableSpec, NameTag>& t) -> std::string
|
||||||
|
{
|
||||||
|
return to_sql_string(context, t._schema) + "." + name_to_sql_string(context, name_tag_of_t<TableSpec>::name) +
|
||||||
|
" AS " + name_to_sql_string(context, NameTag::name);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename TableSpec>
|
||||||
struct schema_qualified_table_t
|
struct schema_qualified_table_t
|
||||||
{
|
{
|
||||||
using _traits = make_traits<value_type_of_t<Table>>;
|
schema_qualified_table_t(schema_t schema) : _schema(std::move(schema))
|
||||||
|
|
||||||
using _nodes = detail::type_vector<>;
|
|
||||||
using _required_ctes = detail::type_set<>;
|
|
||||||
using _provided_tables = detail::type_set<>;
|
|
||||||
|
|
||||||
schema_qualified_table_t(schema_t schema, Table table) : _schema(std::move(schema)), _table(table)
|
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename NameTagProvider>
|
template <typename NameTagProvider>
|
||||||
typename Table::template _foreign_table_as_t<NameTagProvider, schema_qualified_table_t> as(
|
auto as(const NameTagProvider& /*unused*/) const->schema_qualified_table_as_t<TableSpec, name_tag_of_t<NameTagProvider>>
|
||||||
const NameTagProvider& /*unused*/) const
|
|
||||||
{
|
{
|
||||||
return {*this};
|
return {_schema};
|
||||||
}
|
}
|
||||||
|
|
||||||
schema_t _schema;
|
schema_t _schema;
|
||||||
Table _table;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename Table>
|
template <typename TableSpec>
|
||||||
struct is_table<schema_qualified_table_t<Table>> : public std::true_type {};
|
auto schema_qualified_table(schema_t schema, table_t<TableSpec>) -> schema_qualified_table_t<TableSpec>
|
||||||
|
|
||||||
template <typename Context, typename Table>
|
|
||||||
auto to_sql_string(Context& context, const schema_qualified_table_t<Table>& t) -> std::string
|
|
||||||
{
|
{
|
||||||
to_sql_string(context, t._schema);
|
return {schema};
|
||||||
context << '.';
|
|
||||||
to_sql_string(context, t._table);
|
|
||||||
return context;
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename Table>
|
|
||||||
auto schema_qualified_table(schema_t schema, Table table) -> schema_qualified_table_t<Table>
|
|
||||||
{
|
|
||||||
static_assert(required_tables_of_t<Table>::size::value == 0,
|
|
||||||
"schema qualified tables must not depend on other tables");
|
|
||||||
static_assert(required_ctes_of<Table>::size::value == 0,
|
|
||||||
"schema qualified tables must not depend on common table expressions");
|
|
||||||
static_assert(is_raw_table<Table>::value,
|
|
||||||
"table must be a raw table, i.e. not an alias or common table expression");
|
|
||||||
|
|
||||||
return {schema, table};
|
|
||||||
}
|
}
|
||||||
} // namespace sqlpp
|
} // namespace sqlpp
|
||||||
|
@ -39,13 +39,7 @@ namespace sqlpp
|
|||||||
template <typename TableSpec>
|
template <typename TableSpec>
|
||||||
struct table_t : public TableSpec::_table_columns<table_t<TableSpec>>, public enable_join<table_t<TableSpec>>
|
struct table_t : public TableSpec::_table_columns<table_t<TableSpec>>, public enable_join<table_t<TableSpec>>
|
||||||
{
|
{
|
||||||
using _traits = make_traits<no_value_t>;
|
|
||||||
|
|
||||||
using _required_insert_columns = typename TableSpec::_required_insert_columns;
|
using _required_insert_columns = typename TableSpec::_required_insert_columns;
|
||||||
#warning: Need to inherit?
|
|
||||||
//using _column_tuple_t = std::tuple<column_t<Table, ColumnSpec>...>;
|
|
||||||
template <typename NameTagProvider, typename T>
|
|
||||||
using _foreign_table_as_t = table_as_t<NameTagProvider, T>;
|
|
||||||
|
|
||||||
template <typename NameTagProvider>
|
template <typename NameTagProvider>
|
||||||
constexpr auto as(const NameTagProvider& /*unused*/) const -> table_as_t<TableSpec, name_tag_of_t<NameTagProvider>>
|
constexpr auto as(const NameTagProvider& /*unused*/) const -> table_as_t<TableSpec, name_tag_of_t<NameTagProvider>>
|
||||||
|
@ -42,9 +42,6 @@ namespace sqlpp
|
|||||||
using _nodes = detail::type_vector<>;
|
using _nodes = detail::type_vector<>;
|
||||||
|
|
||||||
static_assert(required_tables_of_t<TableSpec>::empty(), "table aliases must not depend on external tables");
|
static_assert(required_tables_of_t<TableSpec>::empty(), "table aliases must not depend on external tables");
|
||||||
|
|
||||||
#warning: need to inherit?
|
|
||||||
//using _column_tuple_t = std::tuple<column_t<NameTagProvider, ColumnSpec>...>;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename TableSpec, typename NameTag>
|
template<typename TableSpec, typename NameTag>
|
||||||
|
@ -266,7 +266,7 @@ namespace sqlpp
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <typename Context>
|
template <typename Context>
|
||||||
auto name_to_sql_string(Context& , const char* t) -> std::string
|
auto name_to_sql_string(Context& , const ::sqlpp::string_view& t) -> std::string
|
||||||
{
|
{
|
||||||
#warning: We used to have a version of SQLPP_ALIAS_PROVIDER that marked names as keywords
|
#warning: We used to have a version of SQLPP_ALIAS_PROVIDER that marked names as keywords
|
||||||
#warning: IIUC, the standard SQL way of handling keywords as names is to put them in square brackets, MySQL uses backticks, though
|
#warning: IIUC, the standard SQL way of handling keywords as names is to put them in square brackets, MySQL uses backticks, though
|
||||||
|
@ -30,4 +30,5 @@ function(create_test name)
|
|||||||
endfunction()
|
endfunction()
|
||||||
|
|
||||||
create_test(join)
|
create_test(join)
|
||||||
|
create_test(schema_qualified_table)
|
||||||
|
|
||||||
|
45
tests/core/serialize/basic/schema_qualified_table.cpp
Normal file
45
tests/core/serialize/basic/schema_qualified_table.cpp
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2024, 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 "../compare.h"
|
||||||
|
#include <sqlpp11/sqlpp11.h>
|
||||||
|
|
||||||
|
int main(int, char* [])
|
||||||
|
{
|
||||||
|
// A schema-qualified table cannot be used without AS.
|
||||||
|
auto major = schema_qualified_table(sqlpp::schema("major"), test::TabFoo{});
|
||||||
|
using M = decltype(major);
|
||||||
|
|
||||||
|
static_assert(not sqlpp::is_table<M>::value, "");
|
||||||
|
|
||||||
|
// A schema-qualified table can be used as table with AS:
|
||||||
|
auto major_foo = major.as(sqlpp::alias::a);
|
||||||
|
|
||||||
|
SQLPP_COMPARE(major_foo, "major.tab_foo AS a");
|
||||||
|
SQLPP_COMPARE(major_foo.id, "a.id");
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
@ -30,6 +30,7 @@ endfunction()
|
|||||||
|
|
||||||
test_compile(column)
|
test_compile(column)
|
||||||
test_compile(join)
|
test_compile(join)
|
||||||
|
test_compile(schema_qualified_table)
|
||||||
test_compile(table)
|
test_compile(table)
|
||||||
test_compile(table_as)
|
test_compile(table_as)
|
||||||
|
|
||||||
|
56
tests/core/types/basic/schema_qualified_table.cpp
Normal file
56
tests/core/types/basic/schema_qualified_table.cpp
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2024, 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 "../compare.h"
|
||||||
|
#include <sqlpp11/sqlpp11.h>
|
||||||
|
|
||||||
|
int main(int, char* [])
|
||||||
|
{
|
||||||
|
// A schema-qualified table cannot be used without AS.
|
||||||
|
auto major = schema_qualified_table(sqlpp::schema("major"), test::TabFoo{});
|
||||||
|
using M = decltype(major);
|
||||||
|
|
||||||
|
static_assert(not sqlpp::is_table<M>::value, "");
|
||||||
|
|
||||||
|
// A schema-qualified table can be used as table with AS:
|
||||||
|
auto major_foo = major.as(test::TabFoo{});
|
||||||
|
using MF = decltype(major_foo);
|
||||||
|
|
||||||
|
static_assert(sqlpp::is_table<MF>::value, "");
|
||||||
|
static_assert(sqlpp::has_enabled_join<MF>::value, "");
|
||||||
|
static_assert(std::is_same<sqlpp::provided_tables_of_t<MF>, sqlpp::detail::type_vector<MF>>::value, "");
|
||||||
|
static_assert(std::is_same<sqlpp::required_tables_of_t<MF>, sqlpp::detail::type_vector<>>::value, "");
|
||||||
|
|
||||||
|
// Columns of a schema-qualified table work just like columns of unqualified tables.
|
||||||
|
using MFI = decltype(major_foo.id);
|
||||||
|
static_assert(sqlpp::is_integral<MFI>::value, "");
|
||||||
|
static_assert(sqlpp::has_name_tag<MFI>::value, "");
|
||||||
|
|
||||||
|
static_assert(std::is_same<sqlpp::provided_tables_of_t<MFI>, sqlpp::detail::type_vector<>>::value, "");
|
||||||
|
static_assert(std::is_same<sqlpp::required_tables_of_t<MFI>, sqlpp::detail::type_vector<MF>>::value, "");
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user