aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2013-02-21 11:07:25 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2013-02-21 11:09:52 +0200
commit90786906ee3bce15c83445c0ba25a69a6b7e115d (patch)
tree28b5051153b11b9d9f662ca4e7afce47504b4b28
parent9a8e7d08c12b51407b8aa710fd4ca8cdf8842669 (diff)
Add support for pattern matching (SQL LIKE)
-rw-r--r--odb/pgsql/query-dynamic.cxx16
-rw-r--r--odb/pgsql/query.hxx41
-rw-r--r--odb/pgsql/query.txx51
3 files changed, 108 insertions, 0 deletions
diff --git a/odb/pgsql/query-dynamic.cxx b/odb/pgsql/query-dynamic.cxx
index ba38758..bd2c949 100644
--- a/odb/pgsql/query-dynamic.cxx
+++ b/odb/pgsql/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/pgsql/query.hxx b/odb/pgsql/query.hxx
index 266e733..acfede0 100644
--- a/odb/pgsql/query.hxx
+++ b/odb/pgsql/query.hxx
@@ -730,6 +730,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<T> (pattern));
+ }
+
+ query_base
+ like (val_bind<T> pattern) const;
+
+ template <typename T2>
+ query_base
+ like (val_bind<T2> pattern) const
+ {
+ return like (val_bind<T> (decayed_type (pattern.val)));
+ }
+
+ query_base
+ like (ref_bind<T> pattern) const;
+
+ query_base
+ like (decayed_type pattern, decayed_type escape) const
+ {
+ return like (val_bind<T> (pattern), escape);
+ }
+
+ query_base
+ like (val_bind<T> pattern, decayed_type escape) const;
+
+ template <typename T2>
+ query_base
+ like (val_bind<T2> pattern, decayed_type escape) const
+ {
+ return like (val_bind<T> (decayed_type (pattern.val)), escape);
+ }
+
+ query_base
+ like (ref_bind<T> pattern, decayed_type escape) const;
+
// =
//
public:
diff --git a/odb/pgsql/query.txx b/odb/pgsql/query.txx
index 7a16ec5..6ce5907 100644
--- a/odb/pgsql/query.txx
+++ b/odb/pgsql/query.txx
@@ -6,6 +6,7 @@ namespace odb
{
namespace pgsql
{
+ //
// query_base
//
@@ -22,8 +23,12 @@ namespace odb
append<bool, ID> (val_bind<bool> (true), c.conversion ());
}
+ //
// query_column
//
+
+ // in
+ //
template <typename T, database_type_id ID>
query_base query_column<T, ID>::
in (decayed_type v1, decayed_type v2) const
@@ -109,5 +114,51 @@ namespace odb
q += ")";
return q;
}
+
+ // like
+ //
+ template <typename T, database_type_id ID>
+ query_base query_column<T, ID>::
+ like (val_bind<T> p) const
+ {
+ query_base q (table_, column_);
+ q += "LIKE";
+ q.append<T, ID> (p, conversion_);
+ return q;
+ }
+
+ template <typename T, database_type_id ID>
+ query_base query_column<T, ID>::
+ like (ref_bind<T> p) const
+ {
+ query_base q (table_, column_);
+ q += "LIKE";
+ q.append<T, ID> (p, conversion_);
+ return q;
+ }
+
+ template <typename T, database_type_id ID>
+ query_base query_column<T, ID>::
+ like (val_bind<T> p, decayed_type e) const
+ {
+ query_base q (table_, column_);
+ q += "LIKE";
+ q.append<T, ID> (p, conversion_);
+ q += "ESCAPE";
+ q.append<T, ID> (val_bind<T> (e), conversion_);
+ return q;
+ }
+
+ template <typename T, database_type_id ID>
+ query_base query_column<T, ID>::
+ like (ref_bind<T> p, decayed_type e) const
+ {
+ query_base q (table_, column_);
+ q += "LIKE";
+ q.append<T, ID> (p, conversion_);
+ q += "ESCAPE";
+ q.append<T, ID> (val_bind<T> (e), conversion_);
+ return q;
+ }
}
}