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

New tuple_to_sql_string

This commit is contained in:
Roland Bock 2024-08-17 21:11:52 +02:00
parent cc49ba1524
commit 55324d1f71
22 changed files with 99 additions and 86 deletions

View File

@ -26,7 +26,7 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <sqlpp11/core/interpret_tuple.h>
#include <sqlpp11/core/tuple_to_sql_string.h>
#include <sqlpp11/core/logic.h>
#include <sqlpp11/core/query/result_row.h>
#include <sqlpp11/core/clause/select_flags.h>

View File

@ -26,7 +26,7 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <sqlpp11/core/interpret_tuple.h>
#include <sqlpp11/core/tuple_to_sql_string.h>
#include <sqlpp11/core/logic.h>
#include <sqlpp11/core/no_data.h>
#include <sqlpp11/core/query/policy_update.h>

View File

@ -26,7 +26,7 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <sqlpp11/core/interpret_tuple.h>
#include <sqlpp11/core/tuple_to_sql_string.h>
#include <sqlpp11/core/logic.h>
#include <sqlpp11/core/query/policy_update.h>
#include <sqlpp11/core/type_traits.h>
@ -153,7 +153,7 @@ namespace sqlpp
template <typename Context, typename... Columns>
auto to_sql_string(Context& context, const group_by_data_t<Columns...>& t) -> std::string
{
return " GROUP BY " + interpret_tuple(t._columns, ',', context);
return " GROUP BY " + tuple_to_sql_string(context, t._columns, tuple_operand{", "});
}
template <typename... T>

View File

@ -26,7 +26,7 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <sqlpp11/core/interpret_tuple.h>
#include <sqlpp11/core/tuple_to_sql_string.h>
#include <sqlpp11/core/logic.h>
#include <sqlpp11/core/query/policy_update.h>
#include <sqlpp11/core/type_traits.h>

View File

@ -29,7 +29,7 @@
#include <sqlpp11/core/operator/assign_expression.h>
#include <sqlpp11/core/basic/column_fwd.h>
#include <sqlpp11/core/clause/insert_value.h>
#include <sqlpp11/core/interpret_tuple.h>
#include <sqlpp11/core/tuple_to_sql_string.h>
#include <sqlpp11/core/logic.h>
#include <sqlpp11/core/no_data.h>
#include <sqlpp11/core/query/policy_update.h>
@ -405,7 +405,7 @@ namespace sqlpp
auto to_sql_string(Context& context, const column_list_data_t<Columns...>& t) -> std::string
{
auto result = std::string{" ("};
result += interpret_tuple(t._columns, ",", context);
result += tuple_to_sql_string(context, t._columns, ",");
result += ")";
bool first = true;
for (const auto& row : t._insert_values)
@ -420,7 +420,7 @@ namespace sqlpp
result += ',';
}
result += '(';
result += interpret_tuple(row, ",", context);
result += tuple_to_sql_string(context, row, ",");
result += ')';
}
@ -431,9 +431,9 @@ namespace sqlpp
auto to_sql_string(Context& context, const insert_list_data_t<Assignments...>& t) -> std::string
{
auto result = std::string{" ("};
result += interpret_tuple(t._columns, ",", context);
result += tuple_to_sql_string(context, t._columns, tuple_operand{", "});
result += ") VALUES(";
result += interpret_tuple(t._values, ",", context);
result += tuple_to_sql_string(context, t._values, tuple_operand{", "});
result += ")";
return result;
}

View File

@ -27,7 +27,7 @@
*/
#include <sqlpp11/core/detail/type_set.h>
#include <sqlpp11/core/interpret_tuple.h>
#include <sqlpp11/core/tuple_to_sql_string.h>
#include <sqlpp11/core/logic.h>
#include <sqlpp11/core/query/policy_update.h>
#include <sqlpp11/core/type_traits.h>
@ -148,7 +148,7 @@ namespace sqlpp
template <typename Context, typename... Expressions>
auto to_sql_string(Context& context, const order_by_data_t<Expressions...>& t) -> std::string
{
return " ORDER BY " + interpret_tuple(t._expressions, ',', context);
return " ORDER BY " + tuple_to_sql_string(context, t._expressions, tuple_operand{", "});
}
template <typename... T>

View File

@ -30,7 +30,7 @@
#include <sqlpp11/core/operator/as_expression.h>
#include <sqlpp11/core/query/dynamic.h>
#include <sqlpp11/core/field_spec.h>
#include <sqlpp11/core/interpret_tuple.h>
#include <sqlpp11/core/tuple_to_sql_string.h>
#include <sqlpp11/core/query/policy_update.h>
#include <sqlpp11/core/query/result_row.h>
#include <sqlpp11/core/clause/select_as.h>
@ -330,7 +330,7 @@ namespace sqlpp
template <typename Context, typename... Columns>
auto to_sql_string(Context& context, const std::tuple<Columns...>& t) -> std::string
{
return interpret_tuple(t, ",", context);
return tuple_to_sql_string(context, t, tuple_operand{", "});
}
template <typename... T>

View File

@ -27,7 +27,7 @@
*/
#include <sqlpp11/core/detail/type_set.h>
#include <sqlpp11/core/interpret_tuple.h>
#include <sqlpp11/core/tuple_to_sql_string.h>
#include <sqlpp11/core/no_data.h>
#include <sqlpp11/core/query/policy_update.h>
#include <sqlpp11/core/clause/select_flags.h>
@ -137,7 +137,7 @@ namespace sqlpp
template <typename Context, typename... Flags>
auto to_sql_string(Context& context, const select_flag_list_data_t<Flags...>& t) -> std::string
{
return interpret_tuple(t._flags, "", context);
return tuple_to_sql_string(context, t._flags, tuple_operand{" "});
}
template <typename T>

View File

@ -28,7 +28,7 @@
#include <sqlpp11/core/type_traits.h>
#include <sqlpp11/core/detail/type_set.h>
#include <sqlpp11/core/interpret_tuple.h>
#include <sqlpp11/core/tuple_to_sql_string.h>
#include <tuple>
namespace sqlpp

View File

@ -26,7 +26,7 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <sqlpp11/core/interpret_tuple.h>
#include <sqlpp11/core/tuple_to_sql_string.h>
#include <sqlpp11/core/logic.h>
#include <sqlpp11/core/query/result_row.h>
#include <sqlpp11/core/query/statement_fwd.h>

View File

@ -28,7 +28,7 @@
#include <sqlpp11/core/type_traits.h>
#include <sqlpp11/core/detail/type_set.h>
#include <sqlpp11/core/interpret_tuple.h>
#include <sqlpp11/core/tuple_to_sql_string.h>
#include <tuple>
namespace sqlpp

View File

@ -27,7 +27,7 @@
*/
#include <sqlpp11/core/detail/type_set.h>
#include <sqlpp11/core/interpret_tuple.h>
#include <sqlpp11/core/tuple_to_sql_string.h>
#include <sqlpp11/core/type_traits.h>
namespace sqlpp
@ -167,6 +167,6 @@ namespace sqlpp
template <typename Context, typename... Assignments>
auto to_sql_string(Context& context, const update_list_data_t<Assignments...>& t) -> std::string
{
return " SET " + interpret_tuple(t._assignments, ",", context);
return " SET " + tuple_to_sql_string(context, t._assignments, tuple_operand{", "});
}
} // namespace sqlpp

View File

@ -27,7 +27,7 @@
*/
#include <sqlpp11/core/detail/type_set.h>
#include <sqlpp11/core/interpret_tuple.h>
#include <sqlpp11/core/tuple_to_sql_string.h>
#include <sqlpp11/core/query/policy_update.h>
#include <sqlpp11/core/type_traits.h>
@ -138,8 +138,6 @@ namespace sqlpp
template <typename Context, typename... Tables>
auto to_sql_string(Context& context, const using_data_t<Tables...>& t) -> std::string
{
context << " USING ";
interpret_tuple(t._tables, ',', context);
return context;
return " USING " + tuple_to_sql_string(context, t._tables, tuple_operand{", "});
}
} // namespace sqlpp

View File

@ -26,7 +26,7 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <sqlpp11/core/interpret_tuple.h>
#include <sqlpp11/core/tuple_to_sql_string.h>
#include <sqlpp11/core/logic.h>
#include <sqlpp11/core/query/statement_fwd.h>
#include <sqlpp11/core/type_traits.h>

View File

@ -28,7 +28,7 @@
#include <sqlpp11/core/operator/assign_expression.h>
#include <sqlpp11/core/basic/column_fwd.h>
#include <sqlpp11/core/interpret_tuple.h>
#include <sqlpp11/core/tuple_to_sql_string.h>
#include <sqlpp11/core/logic.h>
#include <sqlpp11/core/no_data.h>
#include <sqlpp11/core/query/policy_update.h>
@ -132,7 +132,7 @@ namespace sqlpp
{
context << "RECURSIVE ";
}
interpret_tuple(t._expressions, ',', context);
tuple_to_sql_string(context, t._expressions, tuple_operand{", "});
context << ' ';
return context;
}

View File

@ -59,7 +59,7 @@ namespace sqlpp
template <typename Target, size_t... Is>
void _bind_impl(Target& target, const ::sqlpp::index_sequence<Is...>& /*unused*/) const
{
using swallow = int[]; // see core/interpret_tuple.h
using swallow = int[]; // see coretuple_to_sql_string.h
(void)swallow{0, (std::tuple_element<Is, _member_tuple_t>::type::operator()()._bind(target, Is), 0)...};
}
};

View File

@ -32,7 +32,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <sqlpp11/core/operator/enable_as.h>
#include <sqlpp11/core/type_traits.h>
#include <sqlpp11/core/logic.h>
#include <sqlpp11/core/interpret_tuple.h>
#include <sqlpp11/core/tuple_to_sql_string.h>
namespace sqlpp
{
@ -127,8 +127,7 @@ namespace sqlpp
}
else
{
#warning: interpret_tuple arguments should take Context first, too
result += interpret_tuple(t._r, ", ", context);
result += tuple_to_sql_string(context, t._r, tuple_operand{", "});
}
result += ")";
return result;

View File

@ -29,7 +29,7 @@
#include <sqlpp11/core/database/connection.h>
#include <sqlpp11/core/detail/get_first.h>
#include <sqlpp11/core/hidden.h>
#include <sqlpp11/core/interpret_tuple.h>
#include <sqlpp11/core/tuple_to_sql_string.h>
#include <sqlpp11/core/query/statement.h>
namespace sqlpp
@ -125,7 +125,7 @@ namespace sqlpp
template <typename Context, typename... Parts>
auto to_sql_string(Context& context, const custom_query_t<Parts...>& t) -> std::string
{
return interpret_tuple_without_braces(t._parts, " ", context);
return tuple_to_sql_string(context, t._parts, tuple_clause{" "});
}
template <typename... Parts>

View File

@ -1,7 +1,7 @@
#pragma once
/*
* Copyright (c) 2013-2015, Roland Bock
* Copyright (c) 2024, Roland Bock
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
@ -33,53 +33,80 @@
namespace sqlpp
{
template <typename Element, typename Separator, typename Context, typename UseBraces>
static auto interpret_tuple_element(
const Element& element, const Separator& separator, Context& context, const UseBraces& /*unused*/, size_t index) -> std::string
struct tuple_operand
{
const auto prefix = index ? std::string{separator} : std::string{};
if (UseBraces::value)
template <typename Context, typename T>
auto operator()(Context& context, const T& t, size_t index) const -> std::string
{
return prefix + operand_to_sql_string(context, element);
const auto prefix = index ? std::string{separator} : std::string{};
return prefix + operand_to_sql_string(context, t);
}
else
{
return prefix + to_sql_string(context, element);
}
}
template <typename Tuple, typename Separator, typename Context, typename UseBraces, size_t... Is>
auto interpret_tuple_impl(const Tuple& t,
const Separator& separator,
Context& context,
const UseBraces& useBraces,
const ::sqlpp::index_sequence<Is...> &
/*unused*/) -> std::string
sqlpp::string_view separator;
};
#warning: to be used by group_by and update
struct tuple_operand_no_dynamic
{
template <typename Context, typename T>
auto operator()(Context& context, const T& t, size_t ) const -> std::string
{
const auto prefix = need_prefix ? std::string{separator} : std::string{};
need_prefix = true;
return prefix + operand_to_sql_string(context, t);
}
template <typename Context, typename T>
auto operator()(Context& context, const sqlpp::dynamic_t<T>& t, size_t index) const -> std::string
{
if (t._condition)
{
return operator()(context, t._expr, index);
}
return "";
}
sqlpp::string_view separator;
bool need_prefix = false;
};
struct tuple_clause
{
template <typename Context, typename T>
auto operator()(Context& context, const T& t, size_t index) const -> std::string
{
const auto prefix = index ? std::string{separator} : std::string{};
return prefix + to_sql_string(context, t);
}
sqlpp::string_view separator;
};
template <typename Context, typename Tuple, typename Strategy, size_t... Is>
auto tuple_to_sql_string_impl(Context& context,
const Tuple& t,
const Strategy& strategy,
const ::sqlpp::index_sequence<Is...>&
/*unused*/) -> std::string
{
// Note: A braced-init-list does guarantee the order of evaluation according to 12.6.1 [class.explicit.init]
// paragraph 2 and 8.5.4 [dcl.init.list] paragraph 4.
// See for example: "http://en.cppreference.com/w/cpp/utility/integer_sequence"
// See also: "http://stackoverflow.com/questions/6245735/pretty-print-stdtuple/6245777#6245777"
// Beware of gcc-bug: "http://gcc.gnu.org/bugzilla/show_bug.cgi?id=51253", otherwise an empty swallow struct could
// be used
// be used.
auto result = std::string{};
using swallow = int[];
(void)swallow{0, // workaround against -Wpedantic GCC warning "zero-size array 'int [0]'"
(result += interpret_tuple_element(std::get<Is>(t), separator, context, useBraces, Is), 0)...};
(result += strategy(context, std::get<Is>(t), Is), 0)...};
return result;
}
template <typename Tuple, typename Separator, typename Context>
auto interpret_tuple(const Tuple& t, const Separator& separator, Context& context) -> std::string
template <typename Context, typename Tuple, typename Strategy>
auto tuple_to_sql_string(Context& context, const Tuple& t, const Strategy& strategy) -> std::string
{
return interpret_tuple_impl(t, separator, context, std::true_type{},
return tuple_to_sql_string_impl(context, t, strategy,
::sqlpp::make_index_sequence<std::tuple_size<Tuple>::value>{});
}
template <typename Tuple, typename Separator, typename Context>
auto interpret_tuple_without_braces(const Tuple& t, const Separator& separator, Context& context) -> std::string
{
return interpret_tuple_impl(t, separator, context, std::false_type{},
::sqlpp::make_index_sequence<std::tuple_size<Tuple>::value>{});
}
} // namespace sqlpp

View File

@ -32,17 +32,13 @@
namespace sqlpp
{
template <typename First, typename... Args>
mysql::context_t& to_sql_string(const concat_t<First, Args...>& t, mysql::context_t& ctx)
auto to_sql_string(mysql::context_t& ctx, const concat_t<First, Args...>& t, mysql::context_t& ctx) -> std::string
{
ctx << "CONCAT(";
interpret_tuple(t._args, ',', ctx);
ctx << ')';
return ctx;
return "CONCAT(" + tuple_to_sql_string(context, t._args, tuple_operand(", ")) + ")";
}
inline mysql::context_t& to_sql_string(const insert_default_values_data_t&, mysql::context_t& ctx)
inline auto to_sql_string(mysql::context_t&, const insert_default_values_data_t&) -> std::string
{
ctx << " () VALUES()";
return ctx;
return " () VALUES()";
}
}

View File

@ -28,7 +28,7 @@
*/
#include <sqlpp11/core/detail/type_set.h>
#include <sqlpp11/core/interpret_tuple.h>
#include <sqlpp11/core/tuple_to_sql_string.h>
#include <sqlpp11/core/type_traits.h>
#include <sqlpp11/core/clause/where.h>
@ -139,14 +139,10 @@ namespace sqlpp
};
template <typename ConflictTarget, typename... Assignments>
postgresql::context_t& to_sql_string(
const postgresql::on_conflict_do_update_data_t<ConflictTarget, Assignments...>& o,
postgresql::context_t& context)
auto to_sql_string(postgresql::context_t& context,
const postgresql::on_conflict_do_update_data_t<ConflictTarget, Assignments...>& o) -> std::string
{
to_sql_string(context, o._conflict_target);
context << "DO UPDATE SET ";
interpret_tuple(o._assignments, ",", context);
return context;
return to_sql_string(context, o._conflict_target) + "DO UPDATE SET " + tuple_to_sql_string(context, o._assignments, tuple_operand{", "});
}
template <typename ConflictTarget, typename Expression, typename... Assignments>

View File

@ -32,7 +32,7 @@
#include <sqlpp11/core/detail/type_set.h>
#include <sqlpp11/expression_fwd.h>
#include <sqlpp11/core/field_spec.h>
#include <sqlpp11/core/interpret_tuple.h>
#include <sqlpp11/core/tuple_to_sql_string.h>
#include <sqlpp11/core/query/policy_update.h>
#include <sqlpp11/core/query/result_row.h>
#include <sqlpp11/select_pseudo_table.h>
@ -293,12 +293,9 @@ namespace sqlpp
// Serialization
template <typename... Columns>
postgresql::context_t& to_sql_string(const postgresql::returning_column_list_data_t<Columns...>& t,
postgresql::context_t& context)
auto to_sql_string(postgresql::context_t& context, const postgresql::returning_column_list_data_t<Columns...>& t) -> std::string
{
context << " RETURNING ";
interpret_tuple(t._columns, ',', context);
return context;
return " RETURNING " + tuple_to_sql_string(context, t._columns, tuple_operand{", "});
}
}
} // namespace sqlpp