summaryrefslogtreecommitdiff
path: root/odb-tests/common/enum
diff options
context:
space:
mode:
Diffstat (limited to 'odb-tests/common/enum')
-rw-r--r--odb-tests/common/enum/buildfile41
-rw-r--r--odb-tests/common/enum/driver.cxx84
-rw-r--r--odb-tests/common/enum/test.hxx47
-rw-r--r--odb-tests/common/enum/testscript53
4 files changed, 225 insertions, 0 deletions
diff --git a/odb-tests/common/enum/buildfile b/odb-tests/common/enum/buildfile
new file mode 100644
index 0000000..eb3a29a
--- /dev/null
+++ b/odb-tests/common/enum/buildfile
@@ -0,0 +1,41 @@
+# file : common/enum/buildfile
+# license : GNU GPL v2; see accompanying LICENSE file
+
+import libodb = libodb%lib{odb}
+
+libs =
+
+for db: $databases
+ import libs += libodb-$db%lib{odb-$db}
+
+import libs += lib{common}
+
+exe{driver}: {hxx cxx}{* -*-odb -*-odb-*} {hxx ixx cxx}{test-odb} testscript
+
+# Introduce the metadata library target to make sure the libodb library is
+# resolved for the odb_compile ad hoc rule (see build/root.build for details).
+#
+libue{test-meta}: $libodb
+
+<{hxx ixx cxx}{test-odb}>: hxx{test} libue{test-meta}
+
+for db: $databases
+{
+ exe{driver}: {hxx ixx cxx}{test-odb-$db}: include = $multi
+ <{hxx ixx cxx}{test-odb-$db}>: hxx{test} libue{test-meta}
+}
+
+exe{driver}: libue{test-meta} $libs
+
+# Specify the ODB custom options to be used by the odb_compile ad hoc rule
+# (see build/root.build for details).
+#
+odb_options = --table-prefix enum_ \
+ --generate-schema \
+ --generate-query
+
+cxx.poptions =+ "-I$out_base" "-I$src_base"
+
+# Testscript's run-time prerequisites.
+#
+exe{driver}: ../../alias{database-client}: include = adhoc
diff --git a/odb-tests/common/enum/driver.cxx b/odb-tests/common/enum/driver.cxx
new file mode 100644
index 0000000..ed3eb59
--- /dev/null
+++ b/odb-tests/common/enum/driver.cxx
@@ -0,0 +1,84 @@
+// file : common/enum/driver.cxx
+// license : GNU GPL v2; see accompanying LICENSE file
+
+// Test automatic C++ enum mapping.
+//
+
+#include <memory> // std::unique_ptr
+#include <iostream>
+
+#include <odb/database.hxx>
+#include <odb/transaction.hxx>
+
+#include <libcommon/common.hxx>
+
+#include "test.hxx"
+#include "test-odb.hxx"
+
+#undef NDEBUG
+#include <cassert>
+
+using namespace std;
+using namespace odb::core;
+
+int
+main (int argc, char* argv[])
+{
+ try
+ {
+ typedef odb::query<object> query;
+ typedef odb::result<object> result;
+
+ unique_ptr<database> db (create_database (argc, argv));
+
+ object o;
+ o.color_ = green;
+ o.taste_ = object::sweet;
+ o.position_ = object::left;
+
+ o.gender_ = object::gender::female;
+ o.scale_ = object::scale::ten;
+ o.yesno_ = object::yesno::yes;
+
+ {
+ transaction t (db->begin ());
+ db->persist (o);
+ t.commit ();
+ }
+
+ {
+ transaction t (db->begin ());
+ unique_ptr<object> o1 (db->load<object> (o.id_));
+ t.commit ();
+
+ assert (o == *o1);
+ }
+
+ {
+ transaction t (db->begin ());
+
+ result r1 (db->query<object> (query::color == blue));
+ result r2 (db->query<object> (query::taste == object::sweet));
+ result r3 (db->query<object> (query::position == object::left));
+
+ assert (r1.empty ());
+ assert (!r2.empty ());
+ assert (!r3.empty ());
+
+ result r4 (db->query<object> (query::gender == object::gender::female));
+ result r5 (db->query<object> (query::scale == object::scale::ten));
+ result r6 (db->query<object> (query::yesno == object::yesno::yes));
+
+ assert (!r4.empty ());
+ assert (!r5.empty ());
+ assert (!r6.empty ());
+
+ t.commit ();
+ }
+ }
+ catch (const odb::exception& e)
+ {
+ cerr << e.what () << endl;
+ return 1;
+ }
+}
diff --git a/odb-tests/common/enum/test.hxx b/odb-tests/common/enum/test.hxx
new file mode 100644
index 0000000..a279112
--- /dev/null
+++ b/odb-tests/common/enum/test.hxx
@@ -0,0 +1,47 @@
+// file : common/enum/test.hxx
+// license : GNU GPL v2; see accompanying LICENSE file
+
+#ifndef TEST_HXX
+#define TEST_HXX
+
+#include <odb/core.hxx>
+
+enum color {red, green, blue};
+
+#pragma db object
+struct object
+{
+ #pragma db id auto
+ unsigned long id_;
+
+ color color_;
+ enum taste {bitter, sweet, sour};
+ taste taste_;
+
+ enum position {left = -1, center = 0, right = 1};
+ position position_;
+
+
+ enum class gender {male, female};
+ enum class scale: unsigned char {one = 1, ten = 10, hundred = 100};
+ enum class yesno: bool {no, yes};
+
+ gender gender_;
+ scale scale_;
+ yesno yesno_;
+};
+
+inline bool
+operator == (const object& x, const object& y)
+{
+ return
+ x.id_ == y.id_
+ && x.color_ == y.color_
+ && x.taste_ == y.taste_
+ && x.position_ == y.position_
+ && x.gender_ == y.gender_
+ && x.scale_ == y.scale_
+ && x.yesno_ == y.yesno_;
+}
+
+#endif // TEST_HXX
diff --git a/odb-tests/common/enum/testscript b/odb-tests/common/enum/testscript
new file mode 100644
index 0000000..fbf1a99
--- /dev/null
+++ b/odb-tests/common/enum/testscript
@@ -0,0 +1,53 @@
+# file : common/enum/testscript
+# license : GNU GPL v2; see accompanying LICENSE file
+
+.include ../../database-options.testscript
+
+: mysql
+:
+if $mysql
+{
+ .include ../../mysql.testscript
+
+ $create_schema;
+ $*
+}
+
+: sqlite
+:
+if $sqlite
+{
+ .include ../../sqlite.testscript
+
+ $*
+}
+
+: pgsql
+:
+if $pgsql
+{
+ .include ../../pgsql.testscript
+
+ $create_schema;
+ $*
+}
+
+: oracle
+:
+if $oracle
+{
+ .include ../../oracle.testscript
+
+ $create_schema;
+ $*
+}
+
+: mssql
+:
+if $mssql
+{
+ .include ../../mssql.testscript
+
+ $create_schema;
+ $*
+}