aboutsummaryrefslogtreecommitdiff
path: root/common/query/driver.cxx
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2010-08-10 11:19:14 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2010-08-10 11:19:14 +0200
commit3076df2628f514b50065912a72269f2dc717e164 (patch)
tree07549856302db4ddd19421ecb02029391fa30383 /common/query/driver.cxx
parentb9161e6e332cb0279ef8616dbbce4c90b60bce15 (diff)
Query test
Diffstat (limited to 'common/query/driver.cxx')
-rw-r--r--common/query/driver.cxx162
1 files changed, 162 insertions, 0 deletions
diff --git a/common/query/driver.cxx b/common/query/driver.cxx
new file mode 100644
index 0000000..214dd7b
--- /dev/null
+++ b/common/query/driver.cxx
@@ -0,0 +1,162 @@
+// file : common/query/driver.cxx
+// author : Boris Kolpackov <boris@codesynthesis.com>
+// copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC
+// license : GNU GPL v2; see accompanying LICENSE file
+
+// Test query support.
+//
+
+#include <memory> // std::auto_ptr
+#include <cassert>
+#include <iostream>
+
+#include <odb/database.hxx>
+#include <odb/transaction.hxx>
+
+#include <common.hxx>
+
+#include "test.hxx"
+#include "test-odb.hxx"
+
+using namespace std;
+using namespace odb;
+
+using odb::shared_ptr;
+
+void
+print (result<person>& r)
+{
+ for (result<person>::iterator i (r.begin ()); i != r.end (); ++i)
+ {
+ auto_ptr<person> o (*i);
+ cout << *o << endl;
+ }
+}
+
+int
+main (int argc, char* argv[])
+{
+ try
+ {
+ auto_ptr<database> db (create_database (argc, argv));
+
+ //
+ //
+ {
+ person p1 (1, "John", "Doe", 30);
+ person p2 (2, "Jane", "Doe", 29);
+ person p3 (3, "Joe", "Dirt", 31);
+
+ transaction t (db->begin_transaction ());
+ db->persist (p1);
+ db->persist (p2);
+ db->persist (p3);
+ t.commit ();
+ }
+
+ // Compilation tests.
+ //
+ if (false)
+ {
+ typedef odb::query<person> query;
+
+ string name;
+ unsigned short age;
+
+ db->query<person> ("age = " + query::_ref (age));
+ db->query<person> ("age = " + query::_val (age));
+
+ query age_q (query::_ref (age) + " = age");
+ query name_q ("first_name = " + query::_val (name));
+ query q (age_q + "AND" + name_q);
+
+ db->query (q);
+ db->query<person> (age_q + "OR" +
+ name_q + "OR" +
+ "age < " + query::_ref (age));
+ }
+
+ // Select-all query.
+ //
+ cout << "test 001" << endl;
+ {
+ transaction t (db->begin_transaction ());
+ result<person> r (db->query<person> ());
+ print (r);
+ t.commit ();
+ }
+
+ // Select-all query with order by.
+ //
+ cout << "test 002" << endl;
+ {
+ transaction t (db->begin_transaction ());
+ result<person> r (db->query<person> ("ORDER BY age"));
+ print (r);
+ t.commit ();
+ }
+
+ // String query.
+ //
+ cout << "test 003" << endl;
+ {
+ transaction t (db->begin_transaction ());
+ result<person> r (db->query<person> ("age >= 30 AND last_name = 'Doe'"));
+ print (r);
+ t.commit ();
+ }
+
+ typedef odb::query<person> query;
+
+ // Value binding.
+ //
+ cout << "test 004" << endl;
+ {
+ transaction t (db->begin_transaction ());
+
+ const char* name = "Doe";
+
+ result<person> r (
+ db->query<person> (
+ "age >= " + query::_ref (30) + "AND" +
+ "last_name = " + query::_val (name)));
+
+ print (r);
+ t.commit ();
+ }
+
+ // Reference binding.
+ //
+ cout << "test 005" << endl;
+ {
+ transaction t (db->begin_transaction ());
+
+ string name;
+ unsigned short age;
+
+ query q ("age >= " + query::_ref (age) + "AND" +
+ "last_name = " + query::_ref (name));
+
+ {
+ name = "Doe";
+ age = 30;
+ result<person> r (db->query<person> (q));
+ print (r);
+ }
+
+ {
+ name = "Dirt";
+ age = 31;
+ result<person> r (db->query<person> (q));
+ print (r);
+ }
+
+ t.commit ();
+ }
+ }
+ catch (const odb::exception& e)
+ {
+ cerr << e.what () << endl;
+ return 1;
+ }
+}