-
-
Notifications
You must be signed in to change notification settings - Fork 319
storage_t::get
template<class O, class ...Ids>
O get(Ids ...ids)
Select * by id routine. This is a basic function for CRUD get by id.
std::system_error(orm_error_code::not_found, orm_error_category)
if object not found with given id. If you don't like catching exceptions in that case please use get_no_throw function instead.
std::system_error
with orm_error_category
in case of db error
class O is a mapped type you expect to return. Must be specified explicitly.
class ...Ids primary key classes. Sqlite expects int as id but you can use string or any other class that can be used as a column type if you like. Pack is used for composite key. Pass a single id if you have only one primary key. You have not to specify this template arguments explicitly cause they are deduced from function arguments
(1) ids
id(s) of object you expect to return. Note that column may have any name (not "id") but it must have PRIMARY KEY
option in the storage and in the db (or composite key in case more than 1 primary keys). It is enough to specify primary_key in make_column and call sync_schema to meet this condition.
Value with specified id(s).
struct Tweet {
int id;
std::string text;
std::string date;
};
using namespace sqlite_orm;
auto storage = make_storage("tweets.sqlite",
make_table("tweets",
make_column("id", &Tweet::id, primary_key()),
make_column("text", &Tweet::text),
make_column("date", &Tweet::date)));
storage.sync_schema();
// remove all old tweet if they do exist
storage.remove_all<Tweet>();
auto insertedId = storage.insert(Tweet{
-1,
"I love sqlite_orm!",
storage.current_timestamp(),
});
try {
auto justInsertedTweet = storage.get<Tweet>(insertedId);
cout << storage.dump(justInsertedTweet) << endl;
auto notExistingTweet = storage.get<Tweet>(insertedId + 1);
cout << "This line will never be printed out" << endl;
assert(0);
}catch(sqlite_orm::not_found_exception e) {
cout << e.what() << endl;
}