diff options
Diffstat (limited to 'odb-tests/common/erase-query')
-rw-r--r-- | odb-tests/common/erase-query/buildfile | 41 | ||||
-rw-r--r-- | odb-tests/common/erase-query/driver.cxx | 181 | ||||
-rw-r--r-- | odb-tests/common/erase-query/test.hxx | 46 | ||||
-rw-r--r-- | odb-tests/common/erase-query/testscript | 33 |
4 files changed, 301 insertions, 0 deletions
diff --git a/odb-tests/common/erase-query/buildfile b/odb-tests/common/erase-query/buildfile new file mode 100644 index 0000000..d833b6e --- /dev/null +++ b/odb-tests/common/erase-query/buildfile @@ -0,0 +1,41 @@ +# file : common/erase-query/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 erase_query_ \ + --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/erase-query/driver.cxx b/odb-tests/common/erase-query/driver.cxx new file mode 100644 index 0000000..6c11957 --- /dev/null +++ b/odb-tests/common/erase-query/driver.cxx @@ -0,0 +1,181 @@ +// file : common/erase-query/driver.cxx +// license : GNU GPL v2; see accompanying LICENSE file + +// Test query-based erase. +// + +#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; + +void +persist (database& db) +{ + object o1 (1); + object o2 (2); + object o3 (3); + object o4 (4); + + transaction t (db.begin ()); + db.persist (o1); + db.persist (o2); + db.persist (o3); + db.persist (o4); + t.commit (); +} + +int +main (int argc, char* argv[]) +{ + try + { + typedef odb::query<object> query; + + unique_ptr<database> db (create_database (argc, argv)); + + // erase_query() + // + persist (*db); + + { + transaction t (db->begin ()); + assert (db->erase_query<object> () == 4); + t.commit (); + } + + // erase_query(const char*) + // + persist (*db); + + { + transaction t (db->begin ()); + + if (db->id () != odb::id_oracle) + assert (db->erase_query<object> ( + "erase_query_object.id < 3") == 2); + else + assert (db->erase_query<object> ( + "\"erase_query_object\".\"id\" < 3") == 2); + + db->erase_query<object> (); + t.commit (); + } + + // erase_query(query) + // + persist (*db); + + { + transaction t (db->begin ()); + assert (db->erase_query<object> (query::id == 2 || query::id == 4) == 2); + db->erase_query<object> (); + t.commit (); + } + + // Test predicates involving object pointers (DELETE JOIN). + // + /* + { + object o11 (1); + object o12 (2); + object o13 (3); + object2 o2; + + o11.o2 = &o2; + o2.num = 123; + + o12.o1 = &o13; + o13.num = 123; + + transaction t (db->begin ()); + db->persist (o2); + db->persist (o13); + db->persist (o12); + db->persist (o11); + t.commit (); + } + + { + transaction t (db->begin ()); + assert (db->erase_query<object> (query::o1::num == 123) == 1); + assert (db->erase_query<object> (query::o2::num == 123) == 1); + db->erase_query<object> (); + t.commit (); + } + */ + + // For now we can only do column-based tests, like is_null(). + // + { + object o11 (1); + object o12 (2); + object o13 (3); + object2 o2; + + o12.o2 = &o2; + + transaction t (db->begin ()); + db->persist (o2); + db->persist (o13); + db->persist (o12); + db->persist (o11); + t.commit (); + } + + { + transaction t (db->begin ()); + assert (db->erase_query<object> (query::o2.is_null ()) == 2); + db->erase_query<object> (); + t.commit (); + } + + // Make sure container data is deleted. + // + { + object o (1); + o.v.push_back (1); + o.v.push_back (2); + o.v.push_back (3); + + transaction t (db->begin ()); + db->persist (o); + t.commit (); + } + + { + transaction t (db->begin ()); + assert (db->erase_query<object> () == 1); + t.commit (); + } + + { + transaction t (db->begin ()); + + if (db->id () != odb::id_oracle) + assert (db->execute ("SELECT * FROM erase_query_object_v " + "WHERE object_id = 1") == 0); + else + assert (db->execute ("SELECT * FROM \"erase_query_object_v\" " + "WHERE \"object_id\" = 1") == 0); + t.commit (); + } + } + catch (const odb::exception& e) + { + cerr << e.what () << endl; + return 1; + } +} diff --git a/odb-tests/common/erase-query/test.hxx b/odb-tests/common/erase-query/test.hxx new file mode 100644 index 0000000..9e73f12 --- /dev/null +++ b/odb-tests/common/erase-query/test.hxx @@ -0,0 +1,46 @@ +// file : common/erase-query/test.hxx +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef TEST_HXX +#define TEST_HXX + +#include <vector> + +#include <odb/core.hxx> + +struct object2; + +#pragma db object +struct object +{ + object (unsigned long id) + : id_ (id), o1 (0), o2 (0) + { + } + + object () + : o1 (0), o2 (0) + { + } + + #pragma db id + unsigned long id_; + + std::vector<int> v; + + int num; + + object* o1; + object2* o2; +}; + +#pragma db object +struct object2 +{ + #pragma db id auto + unsigned long id_; + + int num; +}; + +#endif // TEST_HXX diff --git a/odb-tests/common/erase-query/testscript b/odb-tests/common/erase-query/testscript new file mode 100644 index 0000000..90862ab --- /dev/null +++ b/odb-tests/common/erase-query/testscript @@ -0,0 +1,33 @@ +# file : common/erase-query/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; + $* +} |