0
0
mirror of https://github.com/rbock/sqlpp11.git synced 2024-11-15 20:31:16 +08:00

Add quoting for table and column names.

More reserved words have to be added to ddl2cpp
This commit is contained in:
rbock 2017-11-26 19:19:26 +01:00
parent 8ad1bb5db2
commit 109b0af880
13 changed files with 68 additions and 14 deletions

View File

@ -66,7 +66,7 @@ namespace sqlpp
{ {
serialize_operand(t._expression, context); serialize_operand(t._expression, context);
context << " AS "; context << " AS ";
context << name_of<T>::char_ptr(); context << name_of<T>::template char_ptr<Context>();
return context; return context;
} }
}; };

View File

@ -54,6 +54,30 @@
}; \ }; \
constexpr name##_t name = {}; 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<sizeof(_literal), _literal>; \
template <typename T> \
struct _member_t \
{ \
T name; \
T& operator()() \
{ \
return name; \
} \
const T& operator()() const \
{ \
return name; \
} \
}; \
}; \
}; \
constexpr name##_t name = {};
namespace sqlpp namespace sqlpp
{ {
template <typename T, typename Enable = void> template <typename T, typename Enable = void>

View File

@ -34,11 +34,24 @@ namespace sqlpp
template <char... Cs> template <char... Cs>
struct char_sequence struct char_sequence
{ {
template <typename Context>
static const char* char_ptr() static const char* char_ptr()
{ {
static char s[] = {Cs...}; static char s[] = {Cs..., '\0'};
return s; return s;
}; }
};
template <char... Cs>
struct char_sequence<'!', Cs...>
{
template <typename Context>
static const char* char_ptr()
{
static char s[] = {decltype(get_quote_left(std::declval<Context>()))::value, Cs...,
decltype(get_quote_right(std::declval<Context>()))::value, '\0'};
return s;
}
}; };
template <std::size_t N, const char (&s)[N], typename T> template <std::size_t N, const char (&s)[N], typename T>
@ -52,7 +65,7 @@ namespace sqlpp
template <std::size_t N, const char (&Input)[N]> template <std::size_t N, const char (&Input)[N]>
using make_char_sequence = using make_char_sequence =
typename make_char_sequence_impl<sizeof(Input), Input, sqlpp::detail::make_index_sequence<sizeof(Input)>>::type; typename make_char_sequence_impl<N, Input, sqlpp::detail::make_index_sequence<N - 1>>::type;
} // namespace sqlpp } // namespace sqlpp
#endif #endif

View File

@ -125,7 +125,8 @@ namespace sqlpp
static Context& _(const T& /*unused*/, Context& context) static Context& _(const T& /*unused*/, Context& context)
{ {
context << name_of<typename T::_table>::char_ptr() << '.' << name_of<T>::char_ptr(); context << name_of<typename T::_table>::template char_ptr<Context>() << '.'
<< name_of<T>::template char_ptr<Context>();
return context; return context;
} }
}; };

View File

@ -235,7 +235,7 @@ namespace sqlpp
static Context& _(const T& t, Context& context) static Context& _(const T& t, Context& context)
{ {
context << name_of<T>::char_ptr() << " AS ("; context << name_of<T>::template char_ptr<Context>() << " AS (";
serialize(t._statement, context); serialize(t._statement, context);
context << ")"; context << ")";
return context; return context;
@ -279,7 +279,7 @@ namespace sqlpp
static Context& _(const T& /*unused*/, Context& context) static Context& _(const T& /*unused*/, Context& context)
{ {
context << name_of<T>::char_ptr(); context << name_of<T>::template char_ptr<Context>();
return context; return context;
} }
}; };

View File

@ -44,7 +44,7 @@ namespace sqlpp
template <typename Expr> template <typename Expr>
void emplace_back(Expr expr) void emplace_back(Expr expr)
{ {
_dynamic_expression_names.push_back(name_of<Expr>::char_ptr()); _dynamic_expression_names.push_back(name_of<Expr>::template char_ptr<typename Db::_serializer_context_t>());
_dynamic_columns.emplace_back(expr); _dynamic_columns.emplace_back(expr);
} }

View File

@ -134,7 +134,7 @@ namespace sqlpp
template <typename T> template <typename T>
constexpr const char* get_sql_name(const T& /*unused*/) constexpr const char* get_sql_name(const T& /*unused*/)
{ {
return name_of<T>::char_ptr(); return name_of<T>::template char_ptr<void>();
} }
} // namespace sqlpp } // namespace sqlpp

View File

@ -116,7 +116,7 @@ namespace sqlpp
std::string _get_name() const std::string _get_name() const
{ {
return name_of<T>::char_ptr(); return name_of<T>::template char_ptr<_interpreter_context_t>();
} }
T _t; T _t;

View File

@ -34,6 +34,12 @@ namespace sqlpp
{ {
SQLPP_PORTABLE_STATIC_ASSERT(assert_serializer_specialization_t, "missing serializer specialization"); SQLPP_PORTABLE_STATIC_ASSERT(assert_serializer_specialization_t, "missing serializer specialization");
template <typename Context>
std::integral_constant<char, '"'> get_quote_left(const Context&);
template <typename Context>
std::integral_constant<char, '"'> get_quote_right(const Context&);
template <typename Context, typename T, typename Enable = void> template <typename Context, typename T, typename Enable = void>
struct serializer_t struct serializer_t
{ {

View File

@ -56,7 +56,7 @@ namespace sqlpp
static Context& _(const T& /*unused*/, Context& context) static Context& _(const T& /*unused*/, Context& context)
{ {
context << name_of<typename T::_column_t>::char_ptr(); context << name_of<typename T::_column_t>::template char_ptr<Context>();
return context; return context;
} }
}; };

View File

@ -120,7 +120,7 @@ namespace sqlpp
static Context& _(const T& /*unused*/, Context& context) static Context& _(const T& /*unused*/, Context& context)
{ {
context << name_of<T>::char_ptr(); context << name_of<T>::template char_ptr<Context>();
return context; return context;
} }
}; };

View File

@ -116,7 +116,7 @@ namespace sqlpp
{ {
context << ")"; context << ")";
} }
context << " AS " << name_of<T>::char_ptr(); context << " AS " << name_of<T>::template char_ptr<Context>();
return context; return context;
} }
}; };

View File

@ -249,6 +249,16 @@ if warnOnParse:
nsList = namespace.split('::') 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 # PROCESS DDL
tableCreations = ddl.parseFile(pathToDdl) tableCreations = ddl.parseFile(pathToDdl)
@ -292,7 +302,7 @@ for create in tableCreations:
print(' {', file=header) print(' {', file=header)
print(' struct _alias_t', file=header) print(' struct _alias_t', file=header)
print(' {', 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<sizeof(_literal), _literal>;', file=header) print(' using _name_t = sqlpp::make_char_sequence<sizeof(_literal), _literal>;', file=header)
print(' template<typename T>', file=header) print(' template<typename T>', file=header)
print(' struct _member_t', file=header) print(' struct _member_t', file=header)