From 4962329eecb716bc2b99810ec1ac4214606fc1e8 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Thu, 21 Feb 2013 11:07:24 +0200 Subject: Add support for pattern matching (SQL LIKE) --- odb/query-dynamic.cxx | 2 ++ odb/query-dynamic.hxx | 43 +++++++++++++++++++++++++++++++++++++++++++ odb/query-dynamic.txx | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 95 insertions(+) diff --git a/odb/query-dynamic.cxx b/odb/query-dynamic.cxx index 4050c90..81959ea 100644 --- a/odb/query-dynamic.cxx +++ b/odb/query-dynamic.cxx @@ -100,6 +100,8 @@ namespace odb case clause_part::op_null: case clause_part::op_not_null: case clause_part::op_in: + case clause_part::op_like: + case clause_part::op_like_escape: break; } } diff --git a/odb/query-dynamic.hxx b/odb/query-dynamic.hxx index a2779b1..132e0c0 100644 --- a/odb/query-dynamic.hxx +++ b/odb/query-dynamic.hxx @@ -135,6 +135,8 @@ namespace odb op_not_null, // is_not_null () op_in, // in(), data is the number of arguments + op_like, // like(pattern) + op_like_escape, // like(pattern, escape) op_eq, // == op_ne, // != @@ -415,6 +417,47 @@ namespace odb query_base in_range (I begin, I end) const; + // like + // + public: + query_base + like (const T& 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 (T (pattern.val))); + } + + query_base + like (ref_bind pattern) const; + + query_base + like (const T& pattern, const T& escape) const + { + return like (val_bind (pattern), escape); + } + + query_base + like (val_bind pattern, const T& escape) const; + + template + query_base + like (val_bind pattern, const T& escape) const + { + return like (val_bind (T (pattern.val)), escape); + } + + query_base + like (ref_bind pattern, const T& escape) const; + // == // public: diff --git a/odb/query-dynamic.txx b/odb/query-dynamic.txx index 521ab86..95bddf3 100644 --- a/odb/query-dynamic.txx +++ b/odb/query-dynamic.txx @@ -4,8 +4,10 @@ namespace odb { + // // query_base // + template void query_base:: append_val (const T& val, const native_column_info* c) @@ -21,8 +23,12 @@ namespace odb p.data = reinterpret_cast (qp); } + // // query_column // + + // in + // template query_base query_column:: in (const T& v1, const T& v2) const @@ -87,4 +93,48 @@ namespace odb q.append (query_base::clause_part::op_in, n); return q; } + + // like + // + template + query_base query_column:: + like (val_bind p) const + { + query_base q (native_info); + q.append_val (p.val, native_info); + q.append (query_base::clause_part::op_like, 0); + return q; + } + + template + query_base query_column:: + like (ref_bind p) const + { + query_base q (native_info); + q.append_ref (p.ptr (), native_info); + q.append (query_base::clause_part::op_like, 0); + return q; + } + + template + query_base query_column:: + like (val_bind p, const T& e) const + { + query_base q (native_info); + q.append_val (p.val, native_info); + q.append_val (e, native_info); + q.append (query_base::clause_part::op_like_escape, 0); + return q; + } + + template + query_base query_column:: + like (ref_bind p, const T& e) const + { + query_base q (native_info); + q.append_ref (p.ptr (), native_info); + q.append_val (e, native_info); + q.append (query_base::clause_part::op_like_escape, 0); + return q; + } } -- cgit v1.1