From b79afa6dbea19905b2a035365274616dd1dae2b1 Mon Sep 17 00:00:00 2001
From: Boris Kolpackov To delete a persistent object's state from the database we use the
- 3.9 Deleting Persistent Objects
database::erase()
function template. If the application
- still has an instance of the erased object, this instance becomes
- transient. The erase()
function has the following
- overloaded versions:database::erase()
or database::erase_query()
+ function templates. If the application still has an instance of the
+ erased object, this instance becomes transient. The erase()
+ function has the following overloaded versions:
template <typename T> @@ -2709,6 +2709,43 @@ db.erase<person> (joe_id); t.commit ();+
The erase_query()
function allows us to delete
+ the state of multiple objects matching certain criteria. It uses
+ the query expression of the database::query()
function
+ (Chapter 4, "Querying the Database") and,
+ because the ODB query facility is optional, it is only available
+ if the --generate-query
ODB compiler option was
+ specified. The erase_query()
function has the
+ following overloaded versions:
+ template <typename T> + unsigned long long + erase_query (); + + template <typename T> + unsigned long long + erase_query (const odb::query<T>&); ++ +
The first erase_query()
function is used to delete
+ the state of all the persistent objects of a given type stored
+ in the database. The second function uses the passed query instance
+ to only delete the state of objects matching the query criteria.
+ Both functions return the number of objects erased. When calling
+ the erase_query()
function, we have to explicitly
+ specify the object type we are erasing. For example:
+typedef odb::query<person> query; + +transaction t (db.begin ()); + +db.erase_query<person> (query::last == "Doe" && query::are < 30); + +t.commit (); ++
In some situations we may need to execute native SQL statements @@ -8926,7 +8963,8 @@ class person
If foreign key constraints checking is disabled or not available,
then inconsistencies in object relationships will not be detected.
- Furthermore, using the erase_query()
function (@@ ref)
+ Furthermore, using the erase_query()
function
+ (Section 3.9, "Deleting Persistent Objects")
to delete persistent objects that contain containers will not work
correctly. Container data for such objects will not be deleted.