From 109b0af88031a2f35843a41c4ff90a0996e6b7db Mon Sep 17 00:00:00 2001 From: rbock Date: Sun, 26 Nov 2017 19:19:26 +0100 Subject: [PATCH] Add quoting for table and column names. More reserved words have to be added to ddl2cpp --- include/sqlpp11/alias.h | 2 +- include/sqlpp11/alias_provider.h | 24 ++++++++++++++++++++ include/sqlpp11/char_sequence.h | 19 +++++++++++++--- include/sqlpp11/column.h | 3 ++- include/sqlpp11/cte.h | 4 ++-- include/sqlpp11/dynamic_select_column_list.h | 2 +- include/sqlpp11/functions.h | 2 +- include/sqlpp11/named_interpretable.h | 2 +- include/sqlpp11/serializer.h | 6 +++++ include/sqlpp11/simple_column.h | 2 +- include/sqlpp11/table.h | 2 +- include/sqlpp11/table_alias.h | 2 +- scripts/ddl2cpp | 12 +++++++++- 13 files changed, 68 insertions(+), 14 deletions(-) diff --git a/include/sqlpp11/alias.h b/include/sqlpp11/alias.h index 197862cf..ea2c28ee 100644 --- a/include/sqlpp11/alias.h +++ b/include/sqlpp11/alias.h @@ -66,7 +66,7 @@ namespace sqlpp { serialize_operand(t._expression, context); context << " AS "; - context << name_of::char_ptr(); + context << name_of::template char_ptr(); return context; } }; diff --git a/include/sqlpp11/alias_provider.h b/include/sqlpp11/alias_provider.h index b77a9e20..7982193a 100644 --- a/include/sqlpp11/alias_provider.h +++ b/include/sqlpp11/alias_provider.h @@ -54,6 +54,30 @@ }; \ constexpr name##_t name = {}; +#define SQLPP_QUOTED_ALIAS_PROVIDER(name) \ + struct name##_t \ + { \ + struct _alias_t \ + { \ + static constexpr const char _literal[] = "!" #name; \ + using _name_t = sqlpp::make_char_sequence; \ + template \ + struct _member_t \ + { \ + T name; \ + T& operator()() \ + { \ + return name; \ + } \ + const T& operator()() const \ + { \ + return name; \ + } \ + }; \ + }; \ + }; \ + constexpr name##_t name = {}; + namespace sqlpp { template diff --git a/include/sqlpp11/char_sequence.h b/include/sqlpp11/char_sequence.h index 58d294dd..57595258 100644 --- a/include/sqlpp11/char_sequence.h +++ b/include/sqlpp11/char_sequence.h @@ -34,11 +34,24 @@ namespace sqlpp template struct char_sequence { + template static const char* char_ptr() { - static char s[] = {Cs...}; + static char s[] = {Cs..., '\0'}; return s; - }; + } + }; + + template + struct char_sequence<'!', Cs...> + { + template + static const char* char_ptr() + { + static char s[] = {decltype(get_quote_left(std::declval()))::value, Cs..., + decltype(get_quote_right(std::declval()))::value, '\0'}; + return s; + } }; template @@ -52,7 +65,7 @@ namespace sqlpp template using make_char_sequence = - typename make_char_sequence_impl>::type; + typename make_char_sequence_impl>::type; } // namespace sqlpp #endif diff --git a/include/sqlpp11/column.h b/include/sqlpp11/column.h index 3b8497dd..7920b1f4 100644 --- a/include/sqlpp11/column.h +++ b/include/sqlpp11/column.h @@ -125,7 +125,8 @@ namespace sqlpp static Context& _(const T& /*unused*/, Context& context) { - context << name_of::char_ptr() << '.' << name_of::char_ptr(); + context << name_of::template char_ptr() << '.' + << name_of::template char_ptr(); return context; } }; diff --git a/include/sqlpp11/cte.h b/include/sqlpp11/cte.h index cd1b5f11..9b3d40d4 100644 --- a/include/sqlpp11/cte.h +++ b/include/sqlpp11/cte.h @@ -235,7 +235,7 @@ namespace sqlpp static Context& _(const T& t, Context& context) { - context << name_of::char_ptr() << " AS ("; + context << name_of::template char_ptr() << " AS ("; serialize(t._statement, context); context << ")"; return context; @@ -279,7 +279,7 @@ namespace sqlpp static Context& _(const T& /*unused*/, Context& context) { - context << name_of::char_ptr(); + context << name_of::template char_ptr(); return context; } }; diff --git a/include/sqlpp11/dynamic_select_column_list.h b/include/sqlpp11/dynamic_select_column_list.h index a7ec8727..4491c07d 100644 --- a/include/sqlpp11/dynamic_select_column_list.h +++ b/include/sqlpp11/dynamic_select_column_list.h @@ -44,7 +44,7 @@ namespace sqlpp template void emplace_back(Expr expr) { - _dynamic_expression_names.push_back(name_of::char_ptr()); + _dynamic_expression_names.push_back(name_of::template char_ptr()); _dynamic_columns.emplace_back(expr); } diff --git a/include/sqlpp11/functions.h b/include/sqlpp11/functions.h index e867b656..3c348eef 100644 --- a/include/sqlpp11/functions.h +++ b/include/sqlpp11/functions.h @@ -134,7 +134,7 @@ namespace sqlpp template constexpr const char* get_sql_name(const T& /*unused*/) { - return name_of::char_ptr(); + return name_of::template char_ptr(); } } // namespace sqlpp diff --git a/include/sqlpp11/named_interpretable.h b/include/sqlpp11/named_interpretable.h index 108dbaf4..f75cc8c2 100644 --- a/include/sqlpp11/named_interpretable.h +++ b/include/sqlpp11/named_interpretable.h @@ -116,7 +116,7 @@ namespace sqlpp std::string _get_name() const { - return name_of::char_ptr(); + return name_of::template char_ptr<_interpreter_context_t>(); } T _t; diff --git a/include/sqlpp11/serializer.h b/include/sqlpp11/serializer.h index 2adcfb98..305cd8ff 100644 --- a/include/sqlpp11/serializer.h +++ b/include/sqlpp11/serializer.h @@ -34,6 +34,12 @@ namespace sqlpp { SQLPP_PORTABLE_STATIC_ASSERT(assert_serializer_specialization_t, "missing serializer specialization"); + template + std::integral_constant get_quote_left(const Context&); + + template + std::integral_constant get_quote_right(const Context&); + template struct serializer_t { diff --git a/include/sqlpp11/simple_column.h b/include/sqlpp11/simple_column.h index a413d327..547da05c 100644 --- a/include/sqlpp11/simple_column.h +++ b/include/sqlpp11/simple_column.h @@ -56,7 +56,7 @@ namespace sqlpp static Context& _(const T& /*unused*/, Context& context) { - context << name_of::char_ptr(); + context << name_of::template char_ptr(); return context; } }; diff --git a/include/sqlpp11/table.h b/include/sqlpp11/table.h index e655c29a..17a80836 100644 --- a/include/sqlpp11/table.h +++ b/include/sqlpp11/table.h @@ -120,7 +120,7 @@ namespace sqlpp static Context& _(const T& /*unused*/, Context& context) { - context << name_of::char_ptr(); + context << name_of::template char_ptr(); return context; } }; diff --git a/include/sqlpp11/table_alias.h b/include/sqlpp11/table_alias.h index 633fda79..a73ee89d 100644 --- a/include/sqlpp11/table_alias.h +++ b/include/sqlpp11/table_alias.h @@ -116,7 +116,7 @@ namespace sqlpp { context << ")"; } - context << " AS " << name_of::char_ptr(); + context << " AS " << name_of::template char_ptr(); return context; } }; diff --git a/scripts/ddl2cpp b/scripts/ddl2cpp index 1bae60fc..8291b84d 100755 --- a/scripts/ddl2cpp +++ b/scripts/ddl2cpp @@ -249,6 +249,16 @@ if warnOnParse: nsList = namespace.split('::') +def escape_if_reserved(name): + reserved_names = [ + 'GROUP', + 'ORDER', + ] + if name.upper() in reserved_names: + return '!{}'.format(name) + return name + + # PROCESS DDL tableCreations = ddl.parseFile(pathToDdl) @@ -292,7 +302,7 @@ for create in tableCreations: print(' {', file=header) print(' struct _alias_t', file=header) print(' {', file=header) - print(' static constexpr const char _literal[] = "' + sqlColumnName + '";', file=header) + print(' static constexpr const char _literal[] = "' + escape_if_reserved(sqlColumnName) + '";', file=header) print(' using _name_t = sqlpp::make_char_sequence;', file=header) print(' template', file=header) print(' struct _member_t', file=header)