summaryrefslogtreecommitdiff
path: root/odb-tests/common/query/array
diff options
context:
space:
mode:
Diffstat (limited to 'odb-tests/common/query/array')
-rw-r--r--odb-tests/common/query/array/buildfile43
-rw-r--r--odb-tests/common/query/array/driver.cxx220
-rw-r--r--odb-tests/common/query/array/test.hxx70
-rw-r--r--odb-tests/common/query/array/testscript33
4 files changed, 366 insertions, 0 deletions
diff --git a/odb-tests/common/query/array/buildfile b/odb-tests/common/query/array/buildfile
new file mode 100644
index 0000000..3beb6d0
--- /dev/null
+++ b/odb-tests/common/query/array/buildfile
@@ -0,0 +1,43 @@
+# file : common/query/array/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 t_query_array_ \
+ --generate-schema \
+ --generate-query \
+ --generate-prepared \
+ --sql-name-case oracle:upper
+
+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/query/array/driver.cxx b/odb-tests/common/query/array/driver.cxx
new file mode 100644
index 0000000..9327751
--- /dev/null
+++ b/odb-tests/common/query/array/driver.cxx
@@ -0,0 +1,220 @@
+// file : common/query/array/driver.cxx
+// license : GNU GPL v2; see accompanying LICENSE file
+
+// Test query support for C arrays.
+//
+
+#include <string>
+#include <memory> // std::unique_ptr
+#include <cstring> // std::memcpy
+#include <iostream>
+
+#include <odb/database.hxx>
+#include <odb/transaction.hxx>
+
+#include <libcommon/config.hxx> // DATABASE_*
+#include <libcommon/common.hxx>
+
+#include "test.hxx"
+#include "test-odb.hxx"
+
+#undef NDEBUG
+#include <cassert>
+
+using namespace std;
+using namespace odb::core;
+
+#ifndef MULTI_DATABASE
+# if defined(DATABASE_MYSQL)
+const odb::mysql::database_type_id bt = odb::mysql::id_blob;
+# elif defined(DATABASE_SQLITE)
+const odb::sqlite::database_type_id bt = odb::sqlite::id_blob;
+# elif defined(DATABASE_PGSQL)
+const odb::pgsql::database_type_id bt = odb::pgsql::id_bytea;
+# elif defined(DATABASE_ORACLE)
+const odb::oracle::database_type_id bt = odb::oracle::id_raw;
+# elif defined(DATABASE_MSSQL)
+const odb::mssql::database_type_id bt = odb::mssql::id_binary;
+# else
+# error unknown database
+# endif
+#endif
+
+int
+main (int argc, char* argv[])
+{
+ try
+ {
+ unique_ptr<database> db (create_database (argc, argv));
+
+ typedef odb::query<object> query;
+
+ const char buf[16] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6};
+
+ //
+ //
+ {
+ object o1 (1, "abc", buf);
+ object o2 (2, "bcd", buf);
+ object o3 (3, "cde", buf);
+
+ transaction t (db->begin ());
+ db->persist (o1);
+ db->persist (o2);
+ db->persist (o3);
+ t.commit ();
+ }
+
+ {
+ transaction t (db->begin ());
+
+ // string
+ //
+#ifndef MULTI_DATABASE
+ assert (size (db->query<object> (query::s == "abc")) == 1);
+ assert (size (db->query<object> (query::s == query::_val ("bcd"))) == 1);
+ assert (size (db->query<object> ("s = " + query::_val ("bcd"))) == 1);
+ assert (size (db->query<object> ("s = " + query::_ref ("bcd"))) == 1);
+#endif
+
+ {
+ char a[] = "bcd";
+ char* ra = a;
+#ifndef MULTI_DATABASE
+ assert (size (db->query<object> (query::s == a)) == 1);
+ assert (size (db->query<object> (query::s == query::_val (a))) == 1);
+#endif
+ assert (size (db->query<object> (query::s == query::_ref (ra))) == 1);
+#ifndef MULTI_DATABASE
+ assert (size (db->query<object> ("s = " + query::_val (a))) == 1);
+ assert (size (db->query<object> ("s = " + query::_ref (a))) == 1);
+#endif
+ }
+
+ {
+ const char a[] = "bcd";
+ const char* ra = a;
+#ifndef MULTI_DATABASE
+ assert (size (db->query<object> (query::s == a)) == 1);
+ assert (size (db->query<object> (query::s == query::_val (a))) == 1);
+#endif
+ assert (size (db->query<object> (query::s == query::_ref (ra))) == 1);
+#ifndef MULTI_DATABASE
+ assert (size (db->query<object> ("s = " + query::_val (a))) == 1);
+ assert (size (db->query<object> ("s = " + query::_ref (a))) == 1);
+#endif
+ }
+
+ {
+ const char* p = "cde";
+#ifndef MULTI_DATABASE
+ assert (size (db->query<object> (query::s == p)) == 1);
+ assert (size (db->query<object> (query::s == query::_val (p))) == 1);
+#endif
+ assert (size (db->query<object> (query::s == query::_ref (p))) == 1);
+#ifndef MULTI_DATABASE
+ assert (size (db->query<object> ("s = " + query::_val (p))) == 1);
+ assert (size (db->query<object> ("s = " + query::_ref (p))) == 1);
+#endif
+ }
+
+ {
+ char a[] = "cde";
+ char* p = a;
+#ifndef MULTI_DATABASE
+ assert (size (db->query<object> (query::s == p)) == 1);
+ assert (size (db->query<object> (query::s == query::_val (p))) == 1);
+#endif
+ assert (size (db->query<object> (query::s == query::_ref (p))) == 1);
+#ifndef MULTI_DATABASE
+ assert (size (db->query<object> ("s = " + query::_val (p))) == 1);
+ assert (size (db->query<object> ("s = " + query::_ref (p))) == 1);
+#endif
+ }
+
+#ifndef MULTI_DATABASE
+ string s ("abc");
+ //assert (size (db->query<object> (query::s == s)) == 1);
+ assert (size (db->query<object> (query::s == s.c_str ())) == 1);
+ //assert (size (db->query<object> (query::s == query::_val (s))) == 1);
+ assert (size (db->query<object> (query::s == query::_val (s.c_str ()))) == 1);
+
+ assert (size (db->query<object> ("s = " + query::_val (s))) == 1);
+ assert (size (db->query<object> ("s = " + query::_ref (s))) == 1);
+#endif
+
+ // @@ BUILD2 Ends up with the following warning, but strangely only in the
+ // multi-database mode:
+ //
+ // In file included from odb/odb-tests/common/query/array/test-odb.hxx:31,
+ // from odb/odb-tests/common/query/array/driver.cxx:20:
+ // odb/libodb/odb/query-dynamic.hxx: In instantiation of ‘odb::query_base odb::query_column<T>::operator==(const odb::query_column<T2>&) const [with T2 = char [17]; T = char [17]]’:
+ // odb/odb-tests/common/query/array/driver.cxx:144:7: required from here
+ // odb/libodb/odb/query-dynamic.hxx:895:43: error: comparison between two arrays is deprecated in C++20 [-Werror=array-compare]
+ // 895 | (void) (sizeof (type_instance<T> () == type_instance<T2> ()));
+ // | ~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
+ // odb/libodb/odb/query-dynamic.hxx:895:43: note: use unary ‘+’ which decays operands to pointers or ‘&‘indirect_ref’ not supported by dump_decl<declaration error>[0] == &‘indirect_ref’ not supported by dump_decl<declaration error>[0]’ to compare the addresses
+ //
+ // Looks like compile-time assertion. Doesn't make much sense for
+ // arrays since compares pointers to objects rather than objects.
+ // Should we somehow suppress the assertion for arrays or similar?
+ //
+ // Note: temporarily ifndef-ed.
+ //
+#ifndef MULTI_DATABASE
+ assert (size (db->query<object> (query::s == query::s1)) == 3);
+#endif
+
+ // std::array
+ //
+ array<char, 17> a;
+ memcpy (a.data (), "abc", 4); // VC++ strcpy deprecation.
+
+#ifndef MULTI_DATABASE
+ assert (size (db->query<object> (query::a == a)) == 1);
+ assert (size (db->query<object> (query::a == query::_val (a))) == 1);
+#endif
+ assert (size (db->query<object> (query::a == query::_ref (a))) == 1);
+#ifndef MULTI_DATABASE
+ assert (size (db->query<object> ("a = " + query::_val (a))) == 1);
+ assert (size (db->query<object> ("a = " + query::_ref (a))) == 1);
+#endif
+
+ // char
+ //
+ assert (size (db->query<object> (query::c == 'a')) == 1);
+
+ char c ('b');
+ assert (size (db->query<object> (query::c == query::_val (c))) == 1);
+ assert (size (db->query<object> (query::c == query::_ref (c))) == 1);
+
+#ifndef MULTI_DATABASE
+ assert (size (db->query<object> ("c = " + query::_val ('c'))) == 1);
+ assert (size (db->query<object> ("c = " + query::_ref (c))) == 1);
+#endif
+
+ assert (size (db->query<object> (query::c == query::c1)) == 3);
+
+ // buffer
+ //
+#ifndef MULTI_DATABASE
+ assert (size (db->query<object> (query::b == buf)) == 3);
+ assert (size (db->query<object> (query::b == query::_val (buf))) == 3);
+#endif
+
+ assert (size (db->query<object> (query::b == query::_ref (buf))) == 3);
+
+#ifndef MULTI_DATABASE
+ assert (size (db->query<object> ("b = " + query::_val<bt> (buf))) == 3);
+ assert (size (db->query<object> ("b = " + query::_ref<bt> (buf))) == 3);
+#endif
+
+ t.commit ();
+ }
+ }
+ catch (const odb::exception& e)
+ {
+ cerr << e.what () << endl;
+ return 1;
+ }
+}
diff --git a/odb-tests/common/query/array/test.hxx b/odb-tests/common/query/array/test.hxx
new file mode 100644
index 0000000..f0d5f3b
--- /dev/null
+++ b/odb-tests/common/query/array/test.hxx
@@ -0,0 +1,70 @@
+// file : common/query/array/test.hxx
+// license : GNU GPL v2; see accompanying LICENSE file
+
+#ifndef TEST_HXX
+#define TEST_HXX
+
+#include <array>
+#include <cstring> // std::memcpy, std::strlen
+
+#include <odb/core.hxx>
+
+#pragma db object
+struct object
+{
+ object () {}
+ object (unsigned long id, const char* s, const char* b)
+ : id_ (id)
+ {
+ std::memcpy (s_, s, std::strlen (s) + 1); // VC++ strncpy deprecation.
+ std::memcpy (s1_, s, std::strlen (s) + 1);
+ std::memcpy (a_.data (), s, std::strlen (s) + 1);
+ c_ = c1_ = *s;
+ std::memcpy (b_, b, sizeof (b_));
+ }
+
+ #pragma db id
+ unsigned long id_;
+
+ char s_[17];
+ char s1_[17];
+
+#ifdef ODB_COMPILER
+# if defined(ODB_DATABASE_MYSQL) || \
+ defined(ODB_DATABASE_PGSQL) || \
+ defined(ODB_DATABASE_ORACLE) || \
+ defined(ODB_DATABASE_MSSQL)
+# pragma db type("VARCHAR(16)")
+# elif defined(ODB_DATABASE_SQLITE)
+# pragma db type("TEXT")
+# elif defined(ODB_DATABASE_COMMON)
+# pragma db type("DYMMU") // Necessary to make it a value.
+# else
+# error unknown database
+# endif
+#endif
+ std::array<char, 17> a_;
+
+ char c_;
+ char c1_;
+
+#ifdef ODB_COMPILER
+# if defined(ODB_DATABASE_MYSQL)
+# pragma db type("BINARY(16)")
+# elif defined(ODB_DATABASE_SQLITE)
+# pragma db type("BLOB")
+# elif defined(ODB_DATABASE_PGSQL)
+# pragma db type("BYTEA")
+# elif defined(ODB_DATABASE_ORACLE)
+# pragma db type("RAW(16)")
+# elif defined(ODB_DATABASE_MSSQL)
+# pragma db type("BINARY(16)")
+# elif defined(ODB_DATABASE_COMMON)
+# else
+# error unknown database
+# endif
+#endif
+ char b_[16];
+};
+
+#endif // TEST_HXX
diff --git a/odb-tests/common/query/array/testscript b/odb-tests/common/query/array/testscript
new file mode 100644
index 0000000..631ae24
--- /dev/null
+++ b/odb-tests/common/query/array/testscript
@@ -0,0 +1,33 @@
+# file : common/query/array/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;
+ $*
+}