Skip to content

storage_t::remove_all

Yevgeniy Zakharov edited this page Jun 15, 2017 · 4 revisions
template<class O, class ...Args>
void remove_all(Args ...args);

Performs DELETE * FROM table WHERE conditions query. You can also use this function to remove a row by PRIMARY KEY but there is another function created especially for deleting by PRIMARY KEY remove.

Parameters

class O is a mapped type. Storage deduces table by provided O argument. Must be provided explicitly.

(1) args...

Conditions or other query options. If empty then DELETE * FROM table query will be performed.

Return value

None

Example

struct Human {
    int company;
    std::string name;
    std::shared_ptr<std::string> job;
};
using namespace sqlite_orm;
auto storage = make_storage("jobs.sqlite",
                            make_table("jobs",
                                       make_column("company",
                                                   &Human::company),
                                       make_column("name",
                                                   &Human::name),
                                       make_column("job",
                                                   &Human::job)));
storage.sync_schema();

storage.insert(Human{ 1, "Peter", std::make_shared<std::string>("Manager") });
storage.insert(Human{ 1, "Jim", nullptr });
storage.insert(Human{ 1, "John", std::make_shared<std::string>("CEO") });
storage.insert(Human{ 1, "Alex", std::make_shared<std::string>("Developer") });
storage.insert(Human{ 1, "Lisa", nullptr });

//  first lets see what 'jobs' table contains
cout << "humans count = " << storage.count<Human>() << endl;
for(auto &human : storage.get_all<Human>()) {
    cout << "human = " << storage.dump(human) << endl;
}

//  now let's remove all humans with null job
storage.remove_all<Human>(where(is_null(&Human::job)));

cout << "humans after null job removal count = " << storage.count<Human>() << endl;

//  remove all rows from 'jobs' table
storage.remove_all<Human>();

//  now we are sure that `jobs` table is empty
assert(!storage.count<Human>());