diff options
Diffstat (limited to 'odb-tests/qt/common')
-rw-r--r-- | odb-tests/qt/common/basic/buildfile | 51 | ||||
-rw-r--r-- | odb-tests/qt/common/basic/driver.cxx | 68 | ||||
-rw-r--r-- | odb-tests/qt/common/basic/test.hxx | 35 | ||||
-rw-r--r-- | odb-tests/qt/common/basic/testscript | 53 | ||||
-rw-r--r-- | odb-tests/qt/common/containers/basics/buildfile | 51 | ||||
-rw-r--r-- | odb-tests/qt/common/containers/basics/driver.cxx | 523 | ||||
-rw-r--r-- | odb-tests/qt/common/containers/basics/test.hxx | 209 | ||||
-rw-r--r-- | odb-tests/qt/common/containers/basics/testscript | 53 | ||||
-rw-r--r-- | odb-tests/qt/common/containers/change-tracking/buildfile | 51 | ||||
-rw-r--r-- | odb-tests/qt/common/containers/change-tracking/driver.cxx | 631 | ||||
-rw-r--r-- | odb-tests/qt/common/containers/change-tracking/test.hxx | 38 | ||||
-rw-r--r-- | odb-tests/qt/common/containers/change-tracking/testscript | 53 | ||||
-rw-r--r-- | odb-tests/qt/common/smart-ptr/buildfile | 51 | ||||
-rw-r--r-- | odb-tests/qt/common/smart-ptr/driver.cxx | 250 | ||||
-rw-r--r-- | odb-tests/qt/common/smart-ptr/test.hxx | 77 | ||||
-rw-r--r-- | odb-tests/qt/common/smart-ptr/testscript | 53 |
16 files changed, 2247 insertions, 0 deletions
diff --git a/odb-tests/qt/common/basic/buildfile b/odb-tests/qt/common/basic/buildfile new file mode 100644 index 0000000..cb06233 --- /dev/null +++ b/odb-tests/qt/common/basic/buildfile @@ -0,0 +1,51 @@ +# file : qt/common/basic/buildfile +# license : GNU GPL v2; see accompanying LICENSE file + +if ($build.meta_operation != 'dist') +{ + assert ($qt) \ + "Qt version should be configured for this test via config.odb_tests.qt variable" +} + +import meta_libs = libodb%lib{odb} +import meta_libs += libodb-qt%lib{odb-qt} +import meta_libs += "libQt$(qt_ver)Core"%lib{"Qt$(qt_ver)Core"} + +libs = + +for db: $databases + import libs += libodb-$db%lib{odb-$db} + +import libs += lib{common} + +exe{driver}: {hxx cxx}{* -test-odb -test-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}: $meta_libs + +<{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 = --std ($qt_ver == 5 ? c++11 : c++17) \ + --table-prefix qt_basic_ \ + --profile qt/basic \ + --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/qt/common/basic/driver.cxx b/odb-tests/qt/common/basic/driver.cxx new file mode 100644 index 0000000..90c7d20 --- /dev/null +++ b/odb-tests/qt/common/basic/driver.cxx @@ -0,0 +1,68 @@ +// file : qt/common/basic/driver.cxx +// license : GNU GPL v2; see accompanying LICENSE file + +// Test Qt basic type persistence (common part). +// + +#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 + { + unique_ptr<database> db (create_database (argc, argv)); + + object o (1); + o.uuid_ = QUuid::createUuid (); + o.null_ = QUuid (); + o.zero_ = QUuid (); + + { + transaction t (db->begin ()); + db->persist (o); + t.commit (); + } + + { + transaction t (db->begin ()); + unique_ptr<object> p (db->load<object> (o.id_)); + t.commit (); + + assert (*p == o); + } + + { + typedef odb::query<object> query; + typedef odb::result<object> result; + + transaction t (db->begin ()); + result r (db->query<object> (query::uuid == o.uuid_)); + result::iterator i (r.begin ()); + assert (i != r.end () && i->id_ == o.id_); + assert (++i == r.end ()); + t.commit (); + } + + } + catch (const odb::exception& e) + { + cerr << e.what () << endl; + return 1; + } +} diff --git a/odb-tests/qt/common/basic/test.hxx b/odb-tests/qt/common/basic/test.hxx new file mode 100644 index 0000000..e67302e --- /dev/null +++ b/odb-tests/qt/common/basic/test.hxx @@ -0,0 +1,35 @@ +// file : qt/common/basic/test.hxx +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef TEST_HXX +#define TEST_HXX + +#include <QtCore/QUuid> + +#include <odb/core.hxx> + +#pragma db object +struct object +{ + object () {} + object (unsigned long id): id_ (id) {} + + #pragma db id + unsigned long id_; + + QUuid uuid_; + QUuid null_; + + #pragma db not_null + QUuid zero_; + + bool operator== (const object& x) const + { + return id_ == x.id_ && + uuid_ == x.uuid_ && + null_ == x.null_ && + zero_ == x.zero_; + } +}; + +#endif // TEST_HXX diff --git a/odb-tests/qt/common/basic/testscript b/odb-tests/qt/common/basic/testscript new file mode 100644 index 0000000..02be561 --- /dev/null +++ b/odb-tests/qt/common/basic/testscript @@ -0,0 +1,53 @@ +# file : qt/common/basic/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; + $* +} diff --git a/odb-tests/qt/common/containers/basics/buildfile b/odb-tests/qt/common/containers/basics/buildfile new file mode 100644 index 0000000..2c1b8f3 --- /dev/null +++ b/odb-tests/qt/common/containers/basics/buildfile @@ -0,0 +1,51 @@ +# file : qt/common/containers/basics/buildfile +# license : GNU GPL v2; see accompanying LICENSE file + +if ($build.meta_operation != 'dist') +{ + assert ($qt) \ + "Qt version should be configured for this test via config.odb_tests.qt variable" +} + +import meta_libs = libodb%lib{odb} +import meta_libs += libodb-qt%lib{odb-qt} +import meta_libs += "libQt$(qt_ver)Core"%lib{"Qt$(qt_ver)Core"} + +libs = + +for db: $databases + import libs += libodb-$db%lib{odb-$db} + +import libs += lib{common} + +exe{driver}: {hxx cxx}{* -test-odb -test-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}: $meta_libs + +<{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 = --std ($qt_ver == 5 ? c++11 : c++17) \ + --table-prefix qt_cont_bs_ \ + --profile qt/containers \ + --profile qt/basic \ + --generate-schema + +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/qt/common/containers/basics/driver.cxx b/odb-tests/qt/common/containers/basics/driver.cxx new file mode 100644 index 0000000..8d59796 --- /dev/null +++ b/odb-tests/qt/common/containers/basics/driver.cxx @@ -0,0 +1,523 @@ +// file : qt/common/containers/basics/driver.cxx +// license : GNU GPL v2; see accompanying LICENSE file + +// Test basic Qt containers persistence. +// + +#include <memory> // std::unique_ptr +#include <cassert> +#include <iostream> + +#include <QtCore/QCoreApplication> + +#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[]) +{ + QCoreApplication app (argc, argv); + + try + { + unique_ptr<database> db (create_database (argc, argv)); + + for (unsigned short i (0); i < 2; ++i) + { + object empty ("empty"), med ("medium"), full ("full"); + + // + // empty + // + + empty.num = 0; + empty.str = ""; + + // + // med + // + + med.num = 999; + med.str = "xxx"; + + // vector + // + med.nv.push_back (123); + med.nv.push_back (234); + + med.sv.push_back ("aaa"); + med.sv.push_back ("bbbb"); + + med.cv.push_back (comp (123, "aaa")); + med.cv.push_back (comp (234, "bbbb")); + + med.uv.push_back (123); + med.uv.push_back (234); + + // list + // + med.sl.push_back ("aaa"); + med.sl.push_back ("bbbb"); + + med.nl.push_back (123); + med.nl.push_back (234); + + med.cl.push_back (comp (123, "aaa")); + med.cl.push_back (comp (234, "bbbb")); + + // set + // + med.ns.insert (123); + med.ns.insert (234); + + med.ss.insert ("aaa"); + med.ss.insert ("bbbb"); + + // map + // + med.nsm[123] = "aaa"; + med.nsm[234] = "bbbb"; + + med.snm["aaa"] = 123; + med.snm["bbbb"] = 234; + + med.ncm[123] = comp (123, "aaa"); + med.ncm[234] = comp (234, "bbbb"); + + med.csm[comp (123, "aaa")] = "aaa"; + med.csm[comp (234, "bbbb")] = "bbbb"; + + // multimap + // + med.nsmm.insert (123, "aaa"); + med.nsmm.insert (123, "bbbb"); + med.nsmm.insert (234, "ccccc"); + + med.snmm.insert ("aaa", 123); + med.snmm.insert ("aaa", 234); + med.snmm.insert ("bbb", 345); + + med.ncmm.insert (123, comp (123, "aaa")); + med.ncmm.insert (123, comp (234, "bbbb")); + med.ncmm.insert (234, comp (345, "ccccc")); + + // hash + // + med.nsh[123] = "aaa"; + med.nsh[234] = "bbbb"; + + med.snh["aaa"] = 123; + med.snh["bbb"] = 234; + + med.sch["iii"] = comp (123, "aaa"); + med.sch["jjj"] = comp (234, "bbbb"); + + // multihash + // + med.nsmh.insert (123, "aaa"); + med.nsmh.insert (123, "bbbb"); + med.nsmh.insert (234, "ccccc"); + + med.snmh.insert ("aaa", 123); + med.snmh.insert ("aaa", 234); + med.snmh.insert ("bbb", 345); + + med.ncmh.insert (123, comp (123, "aaa")); + med.ncmh.insert (123, comp (234, "bbbb")); + med.ncmh.insert (234, comp (345, "ccccc")); + + // + // full + // + + full.num = 9999; + full.str = "xxxx"; + + // vector + // + full.nv.push_back (1234); + full.nv.push_back (2345); + full.nv.push_back (3456); + + full.sv.push_back ("aaaa"); + full.sv.push_back ("bbbbb"); + full.sv.push_back ("cccccc"); + + full.cv.push_back (comp (1234, "aaaa")); + full.cv.push_back (comp (2345, "bbbbb")); + full.cv.push_back (comp (3456, "cccccc")); + + full.uv.push_back (1234); + full.uv.push_back (2345); + full.uv.push_back (3456); + + // list + // + full.sl.push_back ("aaaa"); + full.sl.push_back ("bbbbb"); + full.sl.push_back ("cccccc"); + + full.nl.push_back (1234); + full.nl.push_back (2345); + full.nl.push_back (3456); + + full.cl.push_back (comp (1234, "aaaa")); + full.cl.push_back (comp (2345, "bbbbb")); + full.cl.push_back (comp (3456, "cccccc")); + + // set + // + full.ns.insert (1234); + full.ns.insert (2345); + full.ns.insert (3456); + + full.ss.insert ("aaaa"); + full.ss.insert ("bbbbb"); + full.ss.insert ("cccccc"); + + // map + // + full.nsm[1234] = "aaaa"; + full.nsm[2345] = "bbbbb"; + full.nsm[3456] = "cccccc"; + + full.snm["aaaa"] = 1234; + full.snm["bbbb"] = 2345; + full.snm["cccc"] = 3456; + + full.ncm[1234] = comp (1234, "aaaa"); + full.ncm[2345] = comp (2345, "bbbbb"); + full.ncm[3456] = comp (3456, "cccccc"); + + full.csm[comp (1234, "aaaa")] = "aaaa"; + full.csm[comp (2345, "bbbb")] = "bbbbb"; + full.csm[comp (3456, "cccc")] = "cccccc"; + + // multimap + // + full.nsmm.insert (1234, "aaaa"); + full.nsmm.insert (1234, "bbbbb"); + full.nsmm.insert (2345, "cccccc"); + full.nsmm.insert (2345, "ddddddd"); + + full.snmm.insert ("aaaa", 1234); + full.snmm.insert ("aaaa", 2345); + full.snmm.insert ("bbbb", 3456); + full.snmm.insert ("bbbb", 4567); + + full.ncmm.insert (1234, comp (1234, "aaaa")); + full.ncmm.insert (1234, comp (2345, "bbbbb")); + full.ncmm.insert (2345, comp (3456, "cccccc")); + full.ncmm.insert (2345, comp (4567, "ddddddd")); + + // hash + // + full.nsh[1234] = "aaaa"; + full.nsh[2345] = "bbbbb"; + full.nsh[3456] = "cccccc"; + + full.snh["aaaa"] = 1234; + full.snh["bbbb"] = 2345; + full.snh["cccc"] = 3456; + + full.sch["iiii"] = comp (1234, "aaaa"); + full.sch["jjjj"] = comp (2345, "bbbbb"); + full.sch["kkkk"] = comp (3456, "cccccc"); + + // multihash + // + full.nsmh.insert (1234, "aaaa"); + full.nsmh.insert (1234, "bbbbb"); + full.nsmh.insert (2345, "cccccc"); + full.nsmh.insert (2345, "ddddddd"); + + full.snmh.insert ("aaaa", 1234); + full.snmh.insert ("aaaa", 2345); + full.snmh.insert ("bbbb", 3456); + full.snmh.insert ("bbbb", 4567); + + full.ncmh.insert (1234, comp (1234, "aaaa")); + full.ncmh.insert (1234, comp (2345, "bbbbb")); + full.ncmh.insert (2345, comp (3456, "cccccc")); + full.ncmh.insert (2345, comp (4567, "ddddddd")); + + // persist + // + { + transaction t (db->begin ()); + db->persist (empty); + db->persist (med); + db->persist (full); + t.commit (); + } + + // load & check + // + { + transaction t (db->begin ()); + unique_ptr<object> e (db->load<object> ("empty")); + unique_ptr<object> m (db->load<object> ("medium")); + unique_ptr<object> f (db->load<object> ("full")); + t.commit (); + + assert (empty == *e); + assert (med == *m); + assert (full == *f); + } + + // + // empty + // + + empty.num = 99; + empty.str = "xx"; + + // vector + // + empty.nv.push_back (12); + empty.sv.push_back ("aa"); + empty.cv.push_back (comp (12, "aa")); + empty.uv.push_back (12); + + // list + // + empty.sl.push_back ("aa"); + empty.nl.push_back (12); + empty.cl.push_back (comp (12, "aa")); + + // set + // + empty.ns.insert (12); + empty.ss.insert ("aa"); + + // map + // + empty.nsm[12] = "aa"; + empty.snm["aa"] = 12; + empty.ncm[12] = comp (12, "aa"); + empty.csm[comp (12, "aa")] = "aa"; + + // multimap + // + empty.nsmm.insert (12, "aa"); + empty.nsmm.insert (12, "bbb"); + empty.nsmm.insert (23, "cccc"); + empty.snmm.insert ("aa", 12); + empty.snmm.insert ("aa", 23); + empty.snmm.insert ("bb", 34); + empty.ncmm.insert (12, comp (12, "aa")); + empty.ncmm.insert (12, comp (23, "bb")); + empty.ncmm.insert (23, comp (34, "cc")); + + // hash + // + empty.nsh[12] = "aa"; + empty.snh["aa"] = 12; + empty.sch["ii"] = comp (12, "aa"); + + // multihash + // + empty.nsmh.insert (12, "aa"); + empty.nsmh.insert (12, "bbb"); + empty.nsmh.insert (23, "cccc"); + empty.snmh.insert ("aa", 12); + empty.snmh.insert ("aa", 23); + empty.snmh.insert ("bb", 34); + empty.ncmh.insert (12, comp (12, "aa")); + empty.ncmh.insert (12, comp (23, "bb")); + empty.ncmh.insert (23, comp (34, "cc")); + + // + // med + // + + med.num = 0; + med.str = ""; + + // vector + // + med.nv.clear (); + med.sv.clear (); + med.cv.clear (); + med.uv.clear (); + + // list + // + med.sl.clear (); + med.nl.clear (); + med.cl.clear (); + + // set + // + med.ns.clear (); + med.ss.clear (); + + // map + // + med.nsm.clear (); + med.snm.clear (); + med.ncm.clear (); + med.csm.clear (); + + // multimap + // + med.nsmm.clear (); + med.snmm.clear (); + med.ncmm.clear (); + + // hash + // + med.nsh.clear (); + med.snh.clear (); + med.sch.clear (); + + // multihash + // + med.nsmh.clear (); + med.snmh.clear (); + med.ncmh.clear (); + + + // + // full + // + + full.num++; + full.str += "x"; + + // vector + // + full.nv.back ()++; + full.nv.push_back (4567); + + full.sv.back () += "c"; + full.sv.push_back ("ddddddd"); + + full.cv.back ().num++; + full.cv.back ().str += "c"; + full.cv.push_back (comp (4567, "ddddddd")); + + full.uv.back ()++; + full.uv.push_back (4567); + + // list + // + full.sl.back () += "c"; + full.sl.push_back ("ddddddd"); + + full.nl.back ()++; + full.nl.push_back (4567); + + full.cl.back ().num++; + full.cl.back ().str += "c"; + full.cl.push_back (comp (4567, "ddddddd")); + + // set + // + full.ns.insert (4567); + full.ss.insert ("ddddddd"); + + // map + // + full.nsm[3456] += "c"; + full.nsm[4567] = "ddddddd"; + + full.snm["cccc"]++; + full.snm["dddd"] = 4567; + + full.ncm[3456].num++; + full.ncm[3456].str += "c"; + full.ncm[4567] = comp (4567, "ddddddd"); + + full.csm[comp (3456, "cccc")] += "c"; + full.csm[comp (4567, "dddd")] = "ddddddd"; + + // multimap + // + full.nsmm.find (2345).value () += "d"; + full.nsmm.insert (3456, "eeeeeeee"); + + full.snmm.find ("bbbb").value ()++; + full.snmm.insert ("cccc", 5678); + + full.ncmm.find (1234).value ().num++; + full.ncmm.find (2345).value ().str += "d"; + full.ncmm.insert (3456, comp (5678, "eeeeeeee")); + + // hash + // + full.nsh[3456] += "c"; + full.nsh[4567] = "ddddddd"; + + full.snh["cccc"]++; + full.snh["dddd"] = 4567; + + full.sch["iiii"].num++; + full.sch["jjjj"].str += "b"; + full.sch["kkkk"] = comp (4567, "dddddddd"); + + // multihash + // + full.nsmh.find (2345).value () += "d"; + full.nsmh.insert (3456, "eeeeeeee"); + + full.snmh.find ("bbbb").value ()++; + full.snmh.insert ("cccc", 5678); + + full.ncmh.find (1234).value ().num++; + full.ncmh.find (2345).value ().str += "d"; + full.ncmh.insert (3456, comp (5678, "eeeeeeee")); + + // update + // + { + transaction t (db->begin ()); + db->update (empty); + db->update (med); + db->update (full); + t.commit (); + } + + // load & check + // + { + transaction t (db->begin ()); + unique_ptr<object> e (db->load<object> ("empty")); + unique_ptr<object> m (db->load<object> ("medium")); + unique_ptr<object> f (db->load<object> ("full")); + t.commit (); + + assert (empty == *e); + assert (med == *m); + assert (full == *f); + } + + // erase + // + if (i == 0) + { + transaction t (db->begin ()); + db->erase<object> ("empty"); + db->erase<object> ("medium"); + db->erase<object> ("full"); + t.commit (); + } + } + } + catch (const odb::exception& e) + { + cerr << e.what () << endl; + return 1; + } +} diff --git a/odb-tests/qt/common/containers/basics/test.hxx b/odb-tests/qt/common/containers/basics/test.hxx new file mode 100644 index 0000000..6a51a5b --- /dev/null +++ b/odb-tests/qt/common/containers/basics/test.hxx @@ -0,0 +1,209 @@ +// file : qt/common/containers/basics/test.hxx +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef TEST_HXX +#define TEST_HXX + +#include <QtCore/QString> +#include <QtCore/QVector> +#include <QtCore/QList> +#include <QtCore/QSet> +#include <QtCore/QMap> +#include <QtCore/QMultiMap> +#include <QtCore/QHash> +#include <QtCore/QMultiHash> + +#include <odb/core.hxx> + +#pragma db value +struct comp +{ + comp (): num (0) {} + comp (int n, const QString& s) : num (n), str (s) {} + + #pragma db column("number") + int num; + QString str; +}; + +inline bool +operator== (const comp& x, const comp& y) +{ + return x.num == y.num && x.str == y.str; +} + +inline bool +operator< (const comp& x, const comp& y) +{ + return x.num != y.num ? x.num < y.num : x.str < y.str; +} + +typedef QVector<int> num_vector; +typedef QVector<QString> str_vector; +typedef QVector<comp> comp_vector; + +typedef QList<int> num_list; +typedef QList<QString> str_list; +typedef QList<comp> comp_list; + +typedef QSet<int> num_set; +typedef QSet<QString> str_set; + +typedef QMap<int, QString> num_str_map; +typedef QMap<QString, int> str_num_map; +typedef QMap<int, comp> num_comp_map; +typedef QMap<comp, QString> comp_str_map; + +typedef QMultiMap<int, QString> num_str_multimap; +typedef QMultiMap<QString, int> str_num_multimap; +typedef QMultiMap<int, comp> num_comp_multimap; + +typedef QHash<int, QString> num_str_hash; +typedef QHash<QString, int> str_num_hash; +typedef QHash<QString, comp> str_comp_hash; + +typedef QMultiHash<int, QString> num_str_multihash; +typedef QMultiHash<QString, int> str_num_multihash; +typedef QMultiHash<int, comp> num_comp_multihash; + +#pragma db value +struct cont_comp1 +{ + // This composite value does not have any columns. + // + num_vector sv; // Have the name "conflict" with the one in the object. +}; + +#pragma db value +struct cont_comp2 +{ + cont_comp2 (): num (777), str ("ggg") {} + + int num; + str_list sl; + QString str; +}; + +#pragma db object +struct object +{ + object (): nv (comp1_.sv), sl (comp2_.sl) {} + object (const QString& id) : id_ (id), nv (comp1_.sv), sl (comp2_.sl) {} + + #pragma db id + QString id_; + + int num; + + cont_comp1 comp1_; + cont_comp2 comp2_; + + // vector + // + #pragma db transient + num_vector& nv; + + #pragma db table("object_strings") id_column ("obj_id") + str_vector sv; + + #pragma db value_column("") + comp_vector cv; + + #pragma db unordered + num_vector uv; + + // list + // + #pragma db transient + str_list& sl; + + num_list nl; + comp_list cl; + + // set + // + num_set ns; + str_set ss; + + // map + // + num_str_map nsm; + str_num_map snm; + num_comp_map ncm; + comp_str_map csm; + + // multimap + // + num_str_multimap nsmm; + str_num_multimap snmm; + num_comp_multimap ncmm; + + // hash + // + num_str_hash nsh; + str_num_hash snh; + str_comp_hash sch; + + // multihash + // + num_str_multihash nsmh; + str_num_multihash snmh; + num_comp_multihash ncmh; + + QString str; +}; + +inline bool +operator== (const object& x, const object& y) +{ + if (x.uv.size () != y.uv.size ()) + return false; + + int xs (0), ys (0); + + for (num_vector::size_type i (0); i < x.uv.size (); ++i) + { + xs += x.uv[i]; + ys += y.uv[i]; + } + + return + x.id_ == y.id_ && + x.num == y.num && + + x.comp2_.num == y.comp2_.num && + x.comp2_.str == y.comp2_.str && + + x.nv == y.nv && + x.sv == y.sv && + x.cv == y.cv && + xs == ys && + + x.sl == y.sl && + x.nl == y.nl && + x.cl == y.cl && + + x.ns == y.ns && + x.ss == y.ss && + + x.nsm == y.nsm && + x.snm == y.snm && + x.ncm == y.ncm && + x.csm == y.csm && + + x.nsmm.uniqueKeys () == y.nsmm.uniqueKeys () && + x.snmm.uniqueKeys () == y.snmm.uniqueKeys () && + x.ncmm.uniqueKeys () == y.ncmm.uniqueKeys () && + + x.nsh == y.nsh && + x.snh == y.snh && + x.sch == y.sch && + + x.nsmh.uniqueKeys () == y.nsmh.uniqueKeys () && + x.snmh.uniqueKeys () == y.snmh.uniqueKeys () && + x.ncmh.uniqueKeys () == y.ncmh.uniqueKeys () && + + x.str == y.str; +} + +#endif // TEST_HXX diff --git a/odb-tests/qt/common/containers/basics/testscript b/odb-tests/qt/common/containers/basics/testscript new file mode 100644 index 0000000..de65f68 --- /dev/null +++ b/odb-tests/qt/common/containers/basics/testscript @@ -0,0 +1,53 @@ +# file : qt/common/containers/basics/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; + $* +} diff --git a/odb-tests/qt/common/containers/change-tracking/buildfile b/odb-tests/qt/common/containers/change-tracking/buildfile new file mode 100644 index 0000000..92cc878 --- /dev/null +++ b/odb-tests/qt/common/containers/change-tracking/buildfile @@ -0,0 +1,51 @@ +# file : qt/common/containers/change-tracking/buildfile +# license : GNU GPL v2; see accompanying LICENSE file + +if ($build.meta_operation != 'dist') +{ + assert ($qt) \ + "Qt version should be configured for this test via config.odb_tests.qt variable" +} + +import meta_libs = libodb%lib{odb} +import meta_libs += libodb-qt%lib{odb-qt} +import meta_libs += "libQt$(qt_ver)Core"%lib{"Qt$(qt_ver)Core"} + +libs = + +for db: $databases + import libs += libodb-$db%lib{odb-$db} + +import libs += lib{common} + +exe{driver}: {hxx cxx}{* -test-odb -test-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}: $meta_libs + +<{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 = --std ($qt_ver == 5 ? c++11 : c++17) \ + --table-prefix qt_cont_ct_ \ + --profile qt/containers \ + --profile qt/basic \ + --generate-schema + +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/qt/common/containers/change-tracking/driver.cxx b/odb-tests/qt/common/containers/change-tracking/driver.cxx new file mode 100644 index 0000000..f2e4179 --- /dev/null +++ b/odb-tests/qt/common/containers/change-tracking/driver.cxx @@ -0,0 +1,631 @@ +// file : qt/common/containers/change-tracking/driver.cxx +// license : GNU GPL v2; see accompanying LICENSE file + +// Test change-tracking Qt containers. +// + +#include <memory> // std::unique_ptr +#include <utility> // std::move +#include <cassert> +#include <iostream> + + +#include <QtCore/QtGlobal> // QT_VERSION, Q_FOREACH +#include <QtCore/QCoreApplication> + +#include <odb/tracer.hxx> +#include <odb/database.hxx> +#include <odb/transaction.hxx> + +#include <odb/qt/list-iterator.hxx> +#include <odb/qt/mutable-list-iterator.hxx> + +#include <libcommon/common.hxx> + +#include "test.hxx" +#include "test-odb.hxx" + +#undef NDEBUG +#include <cassert> + +using namespace std; +using namespace odb::core; + +struct counting_tracer: odb::tracer +{ + void + reset (transaction& tr) {u = i = d = s = t = 0; tr.tracer (*this);} + + virtual void + execute (odb::connection&, const char* stmt) + { + string p (stmt, 6); + if (p == "UPDATE") + u++; + else if (p == "INSERT") + i++; + else if (p == "DELETE") + d++; + else if (p == "SELECT") + s++; + t++; + } + + size_t u, i, d, s, t; +}; + +static counting_tracer tr; + +// Compilation test: instantiate all the functions. Only do this if we +// have a fairly recent version of Qt, otherwise some underlying +// functions will be missing. +// +#if QT_VERSION >= 0x050000 +template class QOdbList<short>; +template class QOdbListIteratorImpl<QOdbList<short> >; +template class QOdbListIterator<short>; +template class QMutableOdbListIterator<short>; +#endif + +void +f (const QList<int>&) {} + +int +main (int argc, char* argv[]) +{ + QCoreApplication app (argc, argv); + + try + { + // Test extended interface. + // + { + typedef QOdbList<int> list; + + list ol; + QList<int> ql; + f (ol); // Implicit conversion to QList. + list ol1 (ql); // Initialization from QList. + ol = ql; // Assignement from QList. + + // Container comparison. + // + if (ol != ol1 || + ol != ql || + ql != ol1) + ol.clear (); + + // Container operators. + // + ol += ol1; + ol += ql; + ol = ol1 + ql; + ol = ql + ol1; + + // Iterator comparison/conversion. + // +#ifndef QT_STRICT_ITERATORS + list::const_iterator i (ol.begin ()); + if (i != ol.end ()) + i = ol.end (); +#endif + + Q_FOREACH (const int& i, ol) + cerr << i; + } + + unique_ptr<database> db (create_database (argc, argv)); + + // Test traits logic. + // + { + object o ("1"); + o.i = 123; + o.s.push_back ("a"); + + assert (!o.s._tracking ()); + + // persist + // + { + transaction t (db->begin ()); + db->persist (o); + t.commit (); + } + + assert (o.s._tracking ()); + + // load + // + { + transaction t (db->begin ()); + unique_ptr<object> p (db->load<object> ("1")); + assert (p->s._tracking ()); + t.commit (); + } + + // update + // + { + transaction t (db->begin ()); + db->update (o); + t.commit (); + } + + assert (o.s._tracking ()); + + // erase + // + { + transaction t (db->begin ()); + db->erase (o); + t.commit (); + } + + assert (!o.s._tracking ()); + } + + // Test change tracking. + // + object o ("1"); + o.i = 123; + o.s.push_back ("a"); + + { + transaction t (db->begin ()); + db->persist (o); + t.commit (); + } + + // push_back/pop_back + // + { + o.s.push_back ("b"); // insert + + transaction t (db->begin ()); + tr.reset (t); + db->update (o); + assert (tr.u == 1 && tr.i == 1 && tr.t == 2); + assert (*db->load<object> ("1") == o); + t.commit (); + } + + { + o.s.pop_back (); + o.s.push_back ("c"); // update + + transaction t (db->begin ()); + tr.reset (t); + db->update (o); + assert (tr.u == 2 && tr.t == 2); + assert (*db->load<object> ("1") == o); + t.commit (); + } + + { + o.s.pop_back (); // delete + + transaction t (db->begin ()); + tr.reset (t); + db->update (o); + assert (tr.u == 1 && tr.d == 1 && tr.t == 2); + assert (*db->load<object> ("1") == o); + t.commit (); + } + + { + o.s.push_back ("b"); + o.s.pop_back (); // no-op + + transaction t (db->begin ()); + tr.reset (t); + db->update (o); + assert (tr.u == 1 && tr.t == 1); + assert (*db->load<object> ("1") == o); + t.commit (); + } + + // insert + // + { + o.s.clear (); + o.s.push_back ("a"); + o.s.push_back ("b"); + + transaction t (db->begin ()); + tr.reset (t); + db->update (o); + assert (*db->load<object> ("1") == o); + t.commit (); + } + + { + o.s.insert (o.s.begin (), "a1"); // insert front + + transaction t (db->begin ()); + tr.reset (t); + db->update (o); + assert (tr.u == 3 && tr.i == 1 && tr.t == 4); + assert (*db->load<object> ("1") == o); + t.commit (); + } + + { + o.s.insert (o.s.begin () + 1, "a2"); // insert middle + + transaction t (db->begin ()); + tr.reset (t); + db->update (o); + assert (tr.u == 3 && tr.i == 1 && tr.t == 4); + assert (*db->load<object> ("1") == o); + t.commit (); + } + + { + o.s.insert (o.s.end (), "b1"); // insert back + + transaction t (db->begin ()); + tr.reset (t); + db->update (o); + assert (tr.u == 1 && tr.i == 1 && tr.t == 2); + assert (*db->load<object> ("1") == o); + t.commit (); + } + + // erase + // + { + o.s.clear (); + o.s.push_back ("a"); + o.s.push_back ("b"); + o.s.push_back ("c"); + o.s.push_back ("d"); + + transaction t (db->begin ()); + tr.reset (t); + db->update (o); + assert (*db->load<object> ("1") == o); + t.commit (); + } + + { + o.s.erase (o.s.begin ()); // erase front + + transaction t (db->begin ()); + tr.reset (t); + db->update (o); + assert (tr.u == 4 && tr.d == 1 && tr.t == 5); + assert (*db->load<object> ("1") == o); + t.commit (); + } + + { + o.s.erase (o.s.begin () + 1); // erase middle + + transaction t (db->begin ()); + tr.reset (t); + db->update (o); + assert (tr.u == 2 && tr.d == 1 && tr.t == 3); + assert (*db->load<object> ("1") == o); + t.commit (); + } + + { + o.s.erase (o.s.end () - 1); // erase back + + transaction t (db->begin ()); + tr.reset (t); + db->update (o); + assert (tr.u == 1 && tr.d == 1 && tr.t == 2); + assert (*db->load<object> ("1") == o); + t.commit (); + } + + // modify + // + { + o.s.clear (); + o.s.push_back ("a"); + o.s.push_back ("b"); + o.s.push_back ("c"); + o.s.push_back ("d"); + + transaction t (db->begin ()); + tr.reset (t); + db->update (o); + assert (*db->load<object> ("1") == o); + t.commit (); + } + + { + o.s.modify (1) += 'b'; + o.s.modify_back () += 'd'; + + transaction t (db->begin ()); + tr.reset (t); + db->update (o); + assert (tr.u == 3 && tr.t == 3); + assert (*db->load<object> ("1") == o); + t.commit (); + } + + { + o.s.modify_front () += 'a'; + o.s.modify_back () += 'd'; + + transaction t (db->begin ()); + tr.reset (t); + db->update (o); + assert (tr.u == 3 && tr.t == 3); + assert (*db->load<object> ("1") == o); + t.commit (); + } + + { + o.s.begin ().modify () += 'a'; + + transaction t (db->begin ()); + tr.reset (t); + db->update (o); + assert (tr.u == 2 && tr.t == 2); + assert (*db->load<object> ("1") == o); + t.commit (); + } + + { + o.s.mbegin (); + + transaction t (db->begin ()); + tr.reset (t); + db->update (o); + assert (tr.u == 5 && tr.t == 5); + assert (*db->load<object> ("1") == o); + t.commit (); + } + + // clear + // + { + o.s.clear (); + o.s.push_back ("a"); + o.s.push_back ("b"); + o.s.push_back ("c"); + + transaction t (db->begin ()); + tr.reset (t); + db->update (o); + assert (*db->load<object> ("1") == o); + t.commit (); + } + + { + o.s.clear (); + + transaction t (db->begin ()); + tr.reset (t); + db->update (o); + assert (tr.u == 1 && tr.d == 1 && tr.t == 2); + assert (*db->load<object> ("1") == o); + t.commit (); + } + + // assign + // + { + o.s.clear (); + o.s.push_back ("a"); + o.s.push_back ("b"); + o.s.push_back ("c"); + + transaction t (db->begin ()); + tr.reset (t); + db->update (o); + assert (*db->load<object> ("1") == o); + t.commit (); + } + + { + QList<QString> v; + v.push_back ("1"); + v.push_back ("2"); + v.push_back ("3"); + v.push_back ("4"); + o.s = v; + + transaction t (db->begin ()); + tr.reset (t); + db->update (o); + assert (tr.u == 4 && tr.i == 1 && tr.t == 5); + assert (*db->load<object> ("1") == o); + t.commit (); + } + + // removeOne/removeAll + // + { + o.s.clear (); + o.s.push_back ("a"); + o.s.push_back ("a"); + o.s.push_back ("b"); + o.s.push_back ("c"); + o.s.push_back ("a"); + o.s.push_back ("d"); + + transaction t (db->begin ()); + tr.reset (t); + db->update (o); + assert (*db->load<object> ("1") == o); + t.commit (); + } + + { + o.s.removeOne ("c"); + assert (o.s.size () == 5 && o.s[3] == "a"); + + transaction t (db->begin ()); + tr.reset (t); + db->update (o); + assert (tr.u == 3 && tr.d == 1 && tr.t == 4); + assert (*db->load<object> ("1") == o); + t.commit (); + } + + { + o.s.removeAll ("a"); + assert (o.s.size () == 2 && o.s[0] == "b" && o.s[1] == "d"); + + transaction t (db->begin ()); + tr.reset (t); + db->update (o); + assert (tr.u == 3 && tr.d == 1 && tr.t == 4); + assert (*db->load<object> ("1") == o); + t.commit (); + } + + // Transaction rollback. + // + { + o.s.clear (); + o.s.push_back ("a"); + o.s.push_back ("b"); + o.s.push_back ("c"); + + transaction t (db->begin ()); + tr.reset (t); + db->update (o); + assert (*db->load<object> ("1") == o); + t.commit (); + } + + { + { + o.s.push_back ("d"); + + transaction t (db->begin ()); + db->update (o); + t.rollback (); + } + + { + transaction t (db->begin ()); + tr.reset (t); + db->update (o); + assert (tr.u == 1 && tr.i == 4 && tr.d == 1 && tr.t == 6); + t.commit (); + } + + { + transaction t (db->begin ()); + tr.reset (t); + db->update (o); + assert (tr.u == 1 && tr.t == 1); + t.commit (); + } + } + + // Armed copy. + // + { + + unique_ptr<object> c; + + { + o.s.pop_back (); + + transaction t (db->begin ()); + db->update (o); + c.reset (new object (o)); + t.rollback (); + } + + { + transaction t (db->begin ()); + tr.reset (t); + db->update (c); + assert (tr.u == 1 && tr.i == 3 && tr.d == 1 && tr.t == 5); + t.commit (); + } + + { + transaction t (db->begin ()); + tr.reset (t); + db->update (c); + assert (tr.u == 1 && tr.t == 1); + t.commit (); + } + } + + // Armed swap. + // +#if QT_VERSION >= 0x040800 + { + object c (o); + + { + o.s.push_back ("d"); + + transaction t (db->begin ()); + db->update (o); + assert (o.s._tracking () && !c.s._tracking ()); + c.s.swap (o.s); + assert (!o.s._tracking () && c.s._tracking ()); + t.rollback (); + } + + { + transaction t (db->begin ()); + tr.reset (t); + db->update (c); + assert (tr.u == 1 && tr.i == 4 && tr.d == 1 && tr.t == 6); + t.commit (); + } + + { + transaction t (db->begin ()); + tr.reset (t); + db->update (c); + assert (tr.u == 1 && tr.t == 1); + t.commit (); + } + } +#endif + + // Armed move. + // + { + unique_ptr<object> c; + + { + o.s.pop_back (); + + transaction t (db->begin ()); + db->update (o); + assert (o.s._tracking ()); + c.reset (new object (std::move (o))); + assert (!o.s._tracking () && c->s._tracking ()); + t.rollback (); + } + + { + transaction t (db->begin ()); + tr.reset (t); + db->update (c); + assert (tr.u == 1 && tr.i == 2 && tr.d == 1 && tr.t == 4); + t.commit (); + } + + { + transaction t (db->begin ()); + tr.reset (t); + db->update (c); + assert (tr.u == 1 && tr.t == 1); + t.commit (); + } + } + } + catch (const odb::exception& e) + { + cerr << e.what () << endl; + return 1; + } +} diff --git a/odb-tests/qt/common/containers/change-tracking/test.hxx b/odb-tests/qt/common/containers/change-tracking/test.hxx new file mode 100644 index 0000000..1548dcc --- /dev/null +++ b/odb-tests/qt/common/containers/change-tracking/test.hxx @@ -0,0 +1,38 @@ +// file : qt/common/containers/change-tracking/test.hxx +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef TEST_HXX +#define TEST_HXX + +#include <memory> +#include <utility> // std::move + +#include <QtCore/QString> + +#include <odb/core.hxx> +#include <odb/qt/list.hxx> + +#pragma db object pointer(std::unique_ptr) +struct object +{ + object () {} + object (const QString& id): id_ (id) {} + + object (const object& x): id_ (x.id_), i (x.i), s (x.s) {} + object (object&& x): id_ (std::move (x.id_)), i (x.i), s (std::move (x.s)) {} + + #pragma db id + QString id_; + + unsigned int i; + + QOdbList<QString> s; + + inline bool + operator== (const object& o) const + { + return id_ == o.id_ && i == o.i && s == o.s; + } +}; + +#endif // TEST_HXX diff --git a/odb-tests/qt/common/containers/change-tracking/testscript b/odb-tests/qt/common/containers/change-tracking/testscript new file mode 100644 index 0000000..da193a9 --- /dev/null +++ b/odb-tests/qt/common/containers/change-tracking/testscript @@ -0,0 +1,53 @@ +# file : qt/common/containers/change-tracking/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; + $* +} diff --git a/odb-tests/qt/common/smart-ptr/buildfile b/odb-tests/qt/common/smart-ptr/buildfile new file mode 100644 index 0000000..11234a2 --- /dev/null +++ b/odb-tests/qt/common/smart-ptr/buildfile @@ -0,0 +1,51 @@ +# file : qt/common/smart-ptr/buildfile +# license : GNU GPL v2; see accompanying LICENSE file + +if ($build.meta_operation != 'dist') +{ + assert ($qt) \ + "Qt version should be configured for this test via config.odb_tests.qt variable" +} + +import meta_libs = libodb%lib{odb} +import meta_libs += libodb-qt%lib{odb-qt} +import meta_libs += "libQt$(qt_ver)Core"%lib{"Qt$(qt_ver)Core"} + +libs = + +for db: $databases + import libs += libodb-$db%lib{odb-$db} + +import libs += lib{common} + +exe{driver}: {hxx cxx}{* -test-odb -test-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}: $meta_libs + +<{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 = --std ($qt_ver == 5 ? c++11 : c++17) \ + --table-prefix qt_smart_ptr_ \ + --profile qt/smart-ptr \ + --generate-schema \ + --generate-session + +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/qt/common/smart-ptr/driver.cxx b/odb-tests/qt/common/smart-ptr/driver.cxx new file mode 100644 index 0000000..3c7419c --- /dev/null +++ b/odb-tests/qt/common/smart-ptr/driver.cxx @@ -0,0 +1,250 @@ +// file : qt/common/smart-ptr/driver.cxx +// license : GNU GPL v2; see accompanying LICENSE file + +// Test Qt smart pointers. +// + +#include <memory> // std::unique_ptr +#include <iostream> + +#include <QtCore/QSharedPointer> +#include <QtCore/QCoreApplication> + +#include <odb/database.hxx> +#include <odb/session.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; + +// Force instantiation of all QLazySharedPointer and QLazyWeakPointer +// class template members. +// +template class QLazySharedPointer<cont>; +template class QLazyWeakPointer<cont>; + +int +main (int argc, char* argv[]) +{ + QCoreApplication app (argc, argv); + + try + { + unique_ptr<database> db (create_database (argc, argv)); + + QSharedPointer<cont> c1 (new cont (1)); + QSharedPointer<cont> c2 (new cont (2)); + + // Test boolean conversion operator. + // + { + assert (!QLazySharedPointer<cont> ()); + assert (!QLazyWeakPointer<cont> ()); + assert (QLazySharedPointer<cont> (c1)); + assert (QLazySharedPointer<cont> (*db, 1)); + assert (QLazyWeakPointer<cont> (c1)); + assert (QLazyWeakPointer<cont> (*db, 1)); + } + + // Test loaded () implementation. + // + { + assert (QLazySharedPointer<cont> ().loaded ()); + assert (!QLazySharedPointer<cont> (c1).loaded ()); + assert (!QLazySharedPointer<cont> (*db, 1).loaded ()); + assert (QLazySharedPointer<cont> (*db, c1).loaded ()); + assert (QLazyWeakPointer<cont> ().loaded ()); + assert (!QLazyWeakPointer<cont> (c1).loaded ()); + assert (!QLazyWeakPointer<cont> (*db, 1).loaded ()); + assert (QLazyWeakPointer<cont> (*db, c1).loaded ()); + } + + // Test comparison operators. + // + { + // Transient QLazySharedPointer. + // + assert (QLazySharedPointer<cont> () == QLazySharedPointer<cont> ()); + assert (QLazySharedPointer<cont> () != QLazySharedPointer<cont> (c1)); + assert (QLazySharedPointer<cont> (c1) != QLazySharedPointer<cont> (c2)); + assert (QLazySharedPointer<cont> (c2) == QLazySharedPointer<cont> (c2)); + + // Persistent QLazySharedPointer. + // + QLazySharedPointer<cont> ls1 (*db, 1), ls2 (*db, c2); + assert (ls1 != QLazySharedPointer<cont> ()); + assert (ls1 != QLazySharedPointer<cont> (c1)); + assert (ls1 == QLazySharedPointer<cont> (*db, c1)); + assert (ls1 != ls2); + assert (ls2 == QLazySharedPointer<cont> (c2)); + + // Transient QLazyWeakPointer. + // + assert (QLazyWeakPointer<cont> () == QLazyWeakPointer<cont> ()); + assert (QLazyWeakPointer<cont> () != QLazyWeakPointer<cont> (c1)); + assert (QLazyWeakPointer<cont> (c1) != QLazyWeakPointer<cont> (c2)); + assert (QLazyWeakPointer<cont> (c2) == QLazyWeakPointer<cont> (c2)); + assert (QLazyWeakPointer<cont> () == QLazySharedPointer<cont> ()); + assert (QLazyWeakPointer<cont> () != QLazySharedPointer<cont> (c1)); + assert (QLazyWeakPointer<cont> (c1) != QLazySharedPointer<cont> (c2)); + assert (QLazyWeakPointer<cont> (c2) == QLazySharedPointer<cont> (c2)); + + // Persistent QLazyWeakPointer. + // + QLazyWeakPointer<cont> lw1 (*db, 1), lw2 (*db, c2); + assert (lw1 != QLazyWeakPointer<cont> ()); + assert (lw1 != QLazyWeakPointer<cont> (c1)); + assert (lw1 == QLazyWeakPointer<cont> (*db, c1)); + assert (lw1 != lw2); + assert (lw2 == QLazyWeakPointer<cont> (c2)); + assert (ls1 == lw1); + assert (ls1 != QLazyWeakPointer<cont> (c1)); + assert (ls1 == QLazyWeakPointer<cont> (*db, c1)); + assert (ls1 != lw2); + assert (ls2 == QLazyWeakPointer<cont> (c2)); + } + + // Test swap. + // + { + QLazySharedPointer<cont> lx (*db, 1), ly; + swap (lx, ly); + + assert (lx.isNull ()); + assert (ly == QLazySharedPointer<cont> (*db, c1)); + } + + // Persist. + // + QSharedPointer<obj> o1 (new obj (1)); + QSharedPointer<obj> o2 (new obj (2)); + QSharedPointer<obj> o3 (new obj (3)); + QSharedPointer<obj> o4 (new obj (4)); + + o1->c = c1; + o2->c = c1; + o3->c = c2; + o4->c = c2; + + { + transaction t (db->begin ()); + + db->persist (c1); + + db->persist (o1); + db->persist (o2); + db->persist (o3); + db->persist (o4); + + db->persist (c2); + + t.commit (); + } + + // Load. + // + { + session s; + transaction t (db->begin ()); + + QSharedPointer<cont> c (db->load<cont> (1)); + QSharedPointer<obj> o (db->load<obj> (1)); + + // Ensure that lazy pointers are present but not loaded. + // + assert (c->o.size () == 2); + assert (!c->o[0].loaded ()); + assert (!c->o[1].loaded ()); + assert (!o->c.loaded ()); + + // Ensure that the correct object IDs were loaded. + // + assert (c->o[0].objectId<obj> () == 1); + assert (c->o[1].objectId<obj> () == 2); + assert (o->c.objectId<obj> () == 1); + + // Load the lazy pointer targets ensuring that the loaded + // targets correspond to the cached session objects. + // + QSharedPointer<cont> cl (o->c.load ()); + QSharedPointer<obj> ol (c->o[0].load ()); + + assert (c->o[0].loaded ()); + assert (o->c.loaded ()); + + assert (cl == c); + assert (ol == o); + + t.commit (); + } + + // Test lazy weak locking and reloading. + // + { + // No session. + // + transaction t (db->begin ()); + QSharedPointer<cont> c (db->load<cont> (1)); + + // Lock. + // + assert (!c->o[1].loaded ()); + QLazySharedPointer<obj> l (c->o[1].toStrongRef ()); + assert (!l.loaded ()); + assert (l.objectId<obj> () == c->o[1].objectId<obj> ()); + + // Reload. + // + assert (!c->o[1].loaded ()); + + QSharedPointer<obj> ol (c->o[1].load ()); + assert (c->o[1].loaded ()); + + ol.clear (); + assert (!c->o[1].loaded ()); + + ol = c->o[1].load (); + assert (c->o[1].loaded ()); + + t.commit (); + } + + // + // Test QSharedPointer as a value wrapper. + // + + { + obj2 o1 (1); + obj2 o2 (2); + o2.num = QSharedPointer<unsigned long> (new unsigned long (123)); + + transaction t (db->begin ()); + db->persist (o1); + db->persist (o2); + t.commit (); + } + + { + transaction t (db->begin ()); + QSharedPointer<obj2> o1 (db->load<obj2> (1)); + QSharedPointer<obj2> o2 (db->load<obj2> (2)); + t.commit (); + + assert (!o1->num); + assert (o2->num && *o2->num == 123); + } + } + catch (const odb::exception& e) + { + cerr << e.what () << endl; + return 1; + } +} diff --git a/odb-tests/qt/common/smart-ptr/test.hxx b/odb-tests/qt/common/smart-ptr/test.hxx new file mode 100644 index 0000000..2c8bf36 --- /dev/null +++ b/odb-tests/qt/common/smart-ptr/test.hxx @@ -0,0 +1,77 @@ +// file : qt/common/smart-ptr/test.hxx +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef TEST_HXX +#define TEST_HXX + +#include <vector> + +#include <QtCore/QSharedPointer> + +#include <odb/core.hxx> +#include <odb/qt/lazy-ptr.hxx> + +struct obj; + +#pragma db object +struct cont +{ + cont () + { + } + + cont (unsigned long id) + : id (id) + { + } + + #pragma db id + unsigned long id; + + typedef std::vector<QLazyWeakPointer<obj> > obj_list; + + #pragma db inverse(c) value_not_null + obj_list o; +}; + +#pragma db object +struct obj +{ + obj () + { + } + + obj (unsigned long id) + : id (id) + { + } + + #pragma db id + unsigned long id; + + #pragma db not_null + QLazySharedPointer<cont> c; +}; + +// Test QSharedPointer as a value wrapper. +// +#pragma db object +struct obj2 +{ + obj2 () + { + } + + obj2 (unsigned long id) + : id (id) + { + } + + #pragma db id + unsigned long id; + + #pragma db null + QSharedPointer<unsigned long> num; +}; + +#endif // TEST_HXX diff --git a/odb-tests/qt/common/smart-ptr/testscript b/odb-tests/qt/common/smart-ptr/testscript new file mode 100644 index 0000000..d78ccb0 --- /dev/null +++ b/odb-tests/qt/common/smart-ptr/testscript @@ -0,0 +1,53 @@ +# file : qt/common/smart-ptr/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; + $* +} |