From 34d177d03f5020ca0ec4bf9b77e20951ed17ff29 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Thu, 21 Feb 2013 11:07:26 +0200 Subject: Add support for pattern matching (SQL LIKE) --- common/query/basics/driver.cxx | 32 ++++++++++++++++++++++++++++++++ common/query/basics/test.std | 10 ++++++++++ 2 files changed, 42 insertions(+) diff --git a/common/query/basics/driver.cxx b/common/query/basics/driver.cxx index a87a0b0..ee8e3b1 100644 --- a/common/query/basics/driver.cxx +++ b/common/query/basics/driver.cxx @@ -247,6 +247,9 @@ main (int argc, char* argv[]) db->query (query::first_name.in ("John", "Jane")); db->query (query::first_name.in_range (names, names_end)); + db->query (query::first_name.like ("J%")); + db->query (query::first_name.like ("J%!%", "!")); + // Query operators. // db->query (query::age == 30 && query::last_name == "Doe"); @@ -598,6 +601,35 @@ main (int argc, char* argv[]) } } #endif + + // Test like. + // + cout << "test 020" << endl; + { + transaction t (db->begin ()); + + result r (db->query (query::first_name.like ("Jo%"))); + print (r); + + r = db->query (!query::first_name.like ("Jo%")); + print (r); + + r = db->query (query::first_name.like ("Jo!%", "!")); + print (r); + + // In Oracle one can only escape special characters (% and _). + // +#if defined(DATABASE_ORACLE) + string v ("Ja%"); +#else + string v ("!Ja%"); +#endif + + r = db->query (query::first_name.like (query::_ref (v), "!")); + print (r); + + t.commit (); + } } catch (const odb::exception& e) { diff --git a/common/query/basics/test.std b/common/query/basics/test.std index 0548968..d420dc4 100644 --- a/common/query/basics/test.std +++ b/common/query/basics/test.std @@ -100,3 +100,13 @@ test 016 test 017 test 018 test 019 +test 020 +John Doe 30 married +Joe Squeaky Dirt 31 single +Johansen J Johansen 32 single + +Jane Doe 29 married + + +Jane Doe 29 married + -- cgit v1.1