From b69ec982bc3b417f026e4828e0b16b37b64b5841 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Thu, 21 Feb 2013 11:07:25 +0200 Subject: Add support for pattern matching (SQL LIKE) --- odb/mysql/query-dynamic.cxx | 16 ++++++++++++++ odb/mysql/query.hxx | 41 ++++++++++++++++++++++++++++++++++++ odb/mysql/query.txx | 51 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 108 insertions(+) diff --git a/odb/mysql/query-dynamic.cxx b/odb/mysql/query-dynamic.cxx index 6377464..8461fb4 100644 --- a/odb/mysql/query-dynamic.cxx +++ b/odb/mysql/query-dynamic.cxx @@ -110,6 +110,22 @@ namespace odb q += ")"; break; } + case part::op_like: + { + translate (q, s, p - 2); // column + q += "LIKE"; + translate (q, s, p - 1); // pattern + break; + } + case part::op_like_escape: + { + translate (q, s, p - 3); // column + q += "LIKE"; + translate (q, s, p - 2); // pattern + q += "ESCAPE"; + translate (q, s, p - 1); // escape + break; + } case part::op_eq: case part::op_ne: case part::op_lt: diff --git a/odb/mysql/query.hxx b/odb/mysql/query.hxx index f8f2056..de10d00 100644 --- a/odb/mysql/query.hxx +++ b/odb/mysql/query.hxx @@ -704,6 +704,47 @@ namespace odb query_base in_range (I begin, I end) const; + // like + // + public: + query_base + like (decayed_type pattern) const + { + return like (val_bind (pattern)); + } + + query_base + like (val_bind pattern) const; + + template + query_base + like (val_bind pattern) const + { + return like (val_bind (decayed_type (pattern.val))); + } + + query_base + like (ref_bind pattern) const; + + query_base + like (decayed_type pattern, decayed_type escape) const + { + return like (val_bind (pattern), escape); + } + + query_base + like (val_bind pattern, decayed_type escape) const; + + template + query_base + like (val_bind pattern, decayed_type escape) const + { + return like (val_bind (decayed_type (pattern.val)), escape); + } + + query_base + like (ref_bind pattern, decayed_type escape) const; + // = // public: diff --git a/odb/mysql/query.txx b/odb/mysql/query.txx index 2aeaf8c..8c1665f 100644 --- a/odb/mysql/query.txx +++ b/odb/mysql/query.txx @@ -6,6 +6,7 @@ namespace odb { namespace mysql { + // // query_base // @@ -22,8 +23,12 @@ namespace odb append (val_bind (true), c.conversion ()); } + // // query_column // + + // in + // template query_base query_column:: in (decayed_type v1, decayed_type v2) const @@ -109,5 +114,51 @@ namespace odb q += ")"; return q; } + + // like + // + template + query_base query_column:: + like (val_bind p) const + { + query_base q (table_, column_); + q += "LIKE"; + q.append (p, conversion_); + return q; + } + + template + query_base query_column:: + like (ref_bind p) const + { + query_base q (table_, column_); + q += "LIKE"; + q.append (p, conversion_); + return q; + } + + template + query_base query_column:: + like (val_bind p, decayed_type e) const + { + query_base q (table_, column_); + q += "LIKE"; + q.append (p, conversion_); + q += "ESCAPE"; + q.append (val_bind (e), conversion_); + return q; + } + + template + query_base query_column:: + like (ref_bind p, decayed_type e) const + { + query_base q (table_, column_); + q += "LIKE"; + q.append (p, conversion_); + q += "ESCAPE"; + q.append (val_bind (e), conversion_); + return q; + } } } -- cgit v1.1