-
-
Notifications
You must be signed in to change notification settings - Fork 321
storage_t::transaction
Yevgeniy Zakharov edited this page Mar 9, 2017
·
4 revisions
bool transaction(std::function<bool()> f);
Begins transaction, executes lambda f
passed as first argument and commits or rollbacks transaction depending on lambda return result.
(1) f
Function to be executed within a transaction. Lambda expression or function pointer can be passed. Function passed must take zero arguments and must return bool
value which indicates whether storage should COMMIT
or ROLLBACK
afterwards.
The result received from f
. It is just passed outside if you need to know whether transaction committed. Just ignore it if you don't need it.
auto storage = make_storage("db.sqlite",
make_table("users",
make_column("id",
&User::id,
autoincrement(),
primary_key()),
make_column("first_name",
&User::firstName),
make_column("last_name",
&User::lastName),
make_column("birth_date",
&User::birthDate),
make_column("image_url",
&User::imageUrl),
make_column("type_id",
&User::typeId)),
make_table("user_types",
make_column("id",
&UserType::id,
autoincrement(),
primary_key()),
make_column("name",
&UserType::name,
default_value("name_placeholder"))));
storage.sync_schema();
storage.transaction([&] () mutable {
storage.remove_all<User>(where(lesser_than(&User::id, 100)));
auto usersRemoved = storage.changes();
cout << "usersRemoved = " << usersRemoved << endl;
return true;
});
As you can see here we call changes member function to get removed rows amount. Without a transaction changed
returns 0 (in case not-RAM storage).