1 #ifndef GIKOMONA_CORE_DATABASE_HPP
2 #define GIKOMONA_CORE_DATABASE_HPP
6 #include <boost/filesystem.hpp>
8 #include "GikoMona.hpp"
10 namespace monazilla { namespace GikoMona { namespace core {
12 class database final {
14 typedef database self_type;
16 database() : database("", boost::system::error_code()) {}
18 /// @brief すでに存在するデータベースファイルを開く。まだファイルが存在しない場合は、新たに作成する。
19 database(const boost::filesystem::path& db_path,
20 boost::system::error_code& ec) : db(db_path.c_str(), ec) {}
22 database(const self_type& other) = delete;
24 ~database() { db.close(); }
26 sqlite::connection& get_connection() { return db; }
28 /// @brief データベースの最適化を行う。
30 * @note 内部では以下のコマンドが実行され、データベース中の不要なデータの圧縮と再構築が行われる。
33 void optimize(boost::system::error_code& ec) noexcept {
34 sqlite::execute_statement<>(db, u8"VACUUM;", ec);
35 sqlite::execute_statement<>(db, u8"REINDEX;", ec);
38 void begin_sql_statement() noexcept {
39 sqlite::execute_statement<>(db, u8"BEGIN;", transaction_ec);
41 if(transaction_ec) { /* logger */ }
44 void end_sql_statement() noexcept {
46 sqlite::execute_statement<>(db, "END;");
53 sqlite::connection db;
54 boost::system::error_code transaction_ec;
57 struct sql_transaction final {
58 sql_transaction(database& db_) : db(db_) { db.begin_sql_statement(); }
59 ~sql_transaction() { db.end_sql_statement(); }