feat(db): init sql orm unique ptr

Former-commit-id: 41a4a96958a81482d2700a4bc3973d8d91d678c0
This commit is contained in:
Xu Peng 2019-04-17 20:28:16 +08:00
parent 2aa2720dbd
commit 0c8b4b04e8
2 changed files with 19 additions and 15 deletions

View File

@ -15,6 +15,19 @@ namespace meta {
using namespace sqlite_orm; using namespace sqlite_orm;
inline auto StoragePrototype(const std::string& path) {
return make_storage(path,
make_table("Groups",
make_column("id", &GroupSchema::id, primary_key()),
make_column("group_id", &GroupSchema::group_id, unique()),
make_column("dimension", &GroupSchema::dimension),
make_column("files_cnt", &GroupSchema::files_cnt, default_value(0))));
}
using ConnectorT = decltype(StoragePrototype(""));
static std::unique_ptr<ConnectorT> ConnectorPtr;
long GetFileSize(const std::string& filename) long GetFileSize(const std::string& filename)
{ {
struct stat stat_buf; struct stat stat_buf;
@ -32,14 +45,9 @@ Status DBMetaImpl::initialize() {
assert(boost::filesystem::create_directory(_options.path)); assert(boost::filesystem::create_directory(_options.path));
} }
auto db = make_storage(_options.path + "/meta.sqlite", ConnectorPtr = std::make_unique<ConnectorT>(StoragePrototype(_options.path+"/meta.sqlite"));
make_table("Groups",
make_column("id", &GroupSchema::id, primary_key()),
make_column("group_id", &GroupSchema::group_id, unique()),
make_column("dimension", &GroupSchema::dimension),
make_column("files_cnt", &GroupSchema::files_cnt, default_value(0))));
db.sync_schema(); ConnectorPtr->sync_schema();
return Status::OK(); return Status::OK();
} }
@ -47,19 +55,13 @@ Status DBMetaImpl::initialize() {
Status DBMetaImpl::add_group(const GroupOptions& options_, Status DBMetaImpl::add_group(const GroupOptions& options_,
const std::string& group_id_, const std::string& group_id_,
GroupSchema& group_info_) { GroupSchema& group_info_) {
auto db = make_storage(_options.path + "/meta.sqlite",
make_table("Groups",
make_column("id", &GroupSchema::id, primary_key()),
make_column("group_id", &GroupSchema::group_id, unique()),
make_column("dimension", &GroupSchema::dimension),
make_column("files_cnt", &GroupSchema::files_cnt, default_value(0))));
group_info_.dimension = options_.dimension; group_info_.dimension = options_.dimension;
group_info_.group_id = group_id_; group_info_.group_id = group_id_;
group_info_.files_cnt = 0; group_info_.files_cnt = 0;
group_info_.id = -1; group_info_.id = -1;
try { try {
auto id = db.insert(group_info_); auto id = ConnectorPtr->insert(group_info_);
std::cout << "id=" << id << std::endl; std::cout << "id=" << id << std::endl;
group_info_.id = id; group_info_.id = id;
} catch(std::system_error& e) { } catch(std::system_error& e) {
@ -69,7 +71,7 @@ Status DBMetaImpl::add_group(const GroupOptions& options_,
} }
Status DBMetaImpl::get_group(const std::string& group_id_, GroupSchema& group_info_) { Status DBMetaImpl::get_group(const std::string& group_id_, GroupSchema& group_info_) {
//PXU TODO
return Status::OK(); return Status::OK();
} }

View File

@ -9,6 +9,8 @@ namespace vecwise {
namespace engine { namespace engine {
namespace meta { namespace meta {
auto StoragePrototype(const std::string& path);
class DBMetaImpl : public Meta { class DBMetaImpl : public Meta {
public: public:
DBMetaImpl(const DBMetaOptions& options_); DBMetaImpl(const DBMetaOptions& options_);