sql_utils: on-the-fly quoting via operator <<

This commit is contained in:
Mickey Rose 2017-02-02 19:33:37 +01:00
parent 8e34b323b5
commit 7b217133e2

View file

@ -39,6 +39,41 @@
namespace mapnik { namespace sql_utils { namespace mapnik { namespace sql_utils {
struct quoted_string
{
std::string const* operator-> () const { return &str; }
std::string const& str;
char const quot;
};
inline quoted_string identifier(std::string const& str)
{
return { str, '"' };
}
inline quoted_string literal(std::string const& str)
{
return { str, '\'' };
}
inline std::ostream& operator << (std::ostream& os, quoted_string qs)
{
std::size_t pos = 0, next;
os.put(qs.quot);
while ((next = qs->find(qs.quot, pos)) != std::string::npos)
{
os.write(qs->data() + pos, next - pos + 1);
os.put(qs.quot);
pos = next + 1;
}
if ((next = qs->size()) > pos)
{
os.write(qs->data() + pos, next - pos);
}
return os.put(qs.quot);
}
inline std::string unquote_double(std::string const& sql) inline std::string unquote_double(std::string const& sql)
{ {
std::string table_name = sql; std::string table_name = sql;