diff --git a/database.cpp b/database.cpp index 8904bf7..fc114dc 100644 --- a/database.cpp +++ b/database.cpp @@ -51,16 +51,7 @@ void DataBase::open() { if (!opened) { mdb_env_create(&environment); - QString path(QStandardPaths::writableLocation(QStandardPaths::CacheLocation)); - path += "/" + getName(); - QDir cache(path); - - if (!cache.exists()) { - bool res = cache.mkpath(path); - if (!res) { - throw Directory(path.toStdString()); - } - } + QString path = createDirectory(); mdb_env_set_maxdbs(environment, tables.size()); mdb_env_set_mapsize(environment, size * 1024UL * 1024UL); @@ -98,6 +89,27 @@ bool DataBase::removeDirectory() } } +QString DataBase::createDirectory() +{ + if (opened) { + throw Opened(name, "create database directory"); + } + + QString path(QStandardPaths::writableLocation(QStandardPaths::CacheLocation)); + path += "/" + getName(); + QDir cache(path); + + if (!cache.exists()) { + bool res = cache.mkpath(path); + if (!res) { + throw Directory(path.toStdString()); + } + } + + return path; +} + + QString DataBase::getName() const { return QString::fromStdString(name); diff --git a/database.h b/database.h index a56a5a4..b1903b5 100644 --- a/database.h +++ b/database.h @@ -44,9 +44,11 @@ public: void close(); bool ready() const; bool removeDirectory(); + QString createDirectory(); QString getName() const; void drop(); + template Table* addTable(const std::string& name);