// 创建表结构
inline auto initstorage(const std::string &path) {
return make_storage(
path,make_index("id", &entity::id),
make_table("entity",
make_column("primary_id", &entity::primary_id, primary_key(),
autoincrement(), default_value("-1")),
make_column("id", &entity::id),
make_column("type", &entity::type),
make_column("context", &entity::context),
make_column("start_at", &entity::start_at),
make_column("update_at", &entity::update_at)));
}
创建表时要指明存储的文件名,如“db.sqlite" 和表名 如 “users”,“user_types”,若要存储在内存里,文件名为":memory:"或者为空
可为列添加的属性,如自增autoincrement(),主键 primary_key(),初始值default_value(“0”)
查找:
auto templ_entity = this->dbptr_->select(
columns(&entity::primary_id, &entity::id, &entity::type,
&entity::context, &entity::start_at,
&entity::update_at),
where(is_equal(&entity::id, id)));
entity_gather = this->dbptr_->get_all<entity>();
新增:
this->dbptr_->transaction([&] {
auto id_entity = this->dbptr_->insert(
entity{tpl.primary_id, tpl.id, tpl.type, tpl.context,
tpl.start_at, tpl.update_at});
insert_entity.id = id_entity;
return true;});
this->dbptr_->transaction([&] {
this->dbptr_->update(insert_entity);
return true;
});
删除:
this->dbptr_->transaction([&] {
this->dbptr_->remove_all<entity>(
where(is_equal(&entity::id, id)));
return true;
});