From 794b3767df6f39e9b195e3a3735edf665f660c21 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Thu, 21 Jul 2011 15:24:46 +0200 Subject: Add support for SQL string quoting --- odb/relational/context.cxx | 19 +++++++++++++++++++ odb/relational/context.hxx | 14 ++++++++++++++ odb/relational/context.ixx | 6 ++++++ 3 files changed, 39 insertions(+) diff --git a/odb/relational/context.cxx b/odb/relational/context.cxx index fcf3ac2..e8be832 100644 --- a/odb/relational/context.cxx +++ b/odb/relational/context.cxx @@ -59,6 +59,25 @@ namespace relational } string context:: + quote_string_impl (string const& s) const + { + string r; + r.reserve (s.size ()); + r += '\''; + + for (string::size_type i (0), n (s.size ()); i < n; ++i) + { + if (s[i] == '\'') + r += "''"; + else + r += s[i]; + } + + r += '\''; + return r; + } + + string context:: quote_id_impl (string const& id) const { string r; diff --git a/odb/relational/context.hxx b/odb/relational/context.hxx index 08f8b81..e654aee 100644 --- a/odb/relational/context.hxx +++ b/odb/relational/context.hxx @@ -28,6 +28,11 @@ namespace relational grow (semantics::data_member&, semantics::type&, string const& key_prefix); public: + // Quote SQL string. + // + string + quote_string (string const&) const; + // Quote SQL identifier. // string @@ -75,6 +80,15 @@ namespace relational semantics::type&, string const&); + // The default implementation uses the ISO quoting ('') and + // escapes singe quotes inside the string by double-quoting + // (' -> ''). Some (most?) database systems support escape + // sequences. We may want to provide utilize that to support + // things like \n, \t, etc. + // + virtual string + quote_string_impl (string const&) const; + // The default implementation uses the ISO quoting (""). // virtual string diff --git a/odb/relational/context.ixx b/odb/relational/context.ixx index da24d39..8bc10fa 100644 --- a/odb/relational/context.ixx +++ b/odb/relational/context.ixx @@ -24,6 +24,12 @@ namespace relational } inline context::string context:: + quote_string (string const& str) const + { + return current ().quote_string_impl (str); + } + + inline context::string context:: quote_id (string const& id) const { return current ().quote_id_impl (id); -- cgit v1.1