From a6dc1ab0a9224a4c1dd6b8837a66081df4ce6868 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Thu, 25 Apr 2019 12:10:12 +0200 Subject: [PATCH] lib/sqlite/Database: wrapper for `sqlite3*` --- src/lib/sqlite/Database.cxx | 35 ++++++++++++++++++++++ src/lib/sqlite/Database.hxx | 58 +++++++++++++++++++++++++++++++++++++ src/lib/sqlite/meson.build | 1 + src/sticker/Database.cxx | 13 +-------- src/sticker/Database.hxx | 3 +- 5 files changed, 97 insertions(+), 13 deletions(-) create mode 100644 src/lib/sqlite/Database.cxx create mode 100644 src/lib/sqlite/Database.hxx diff --git a/src/lib/sqlite/Database.cxx b/src/lib/sqlite/Database.cxx new file mode 100644 index 000000000..cff7b2341 --- /dev/null +++ b/src/lib/sqlite/Database.cxx @@ -0,0 +1,35 @@ +/* + * Copyright 2003-2019 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "Database.hxx" +#include "Error.hxx" +#include "util/StringFormat.hxx" + +namespace Sqlite { + +Database::Database(const char *path) +{ + int result = sqlite3_open(path, &db); + if (result != SQLITE_OK) + throw SqliteError(db, result, + StringFormat<1024>("Failed to open sqlite database '%s'", + path)); +} + +} // namespace Sqlite diff --git a/src/lib/sqlite/Database.hxx b/src/lib/sqlite/Database.hxx new file mode 100644 index 000000000..5e293f1e5 --- /dev/null +++ b/src/lib/sqlite/Database.hxx @@ -0,0 +1,58 @@ +/* + * Copyright 2003-2019 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef MPD_SQLITE_DATABASE_HXX +#define MPD_SQLITE_DATABASE_HXX + +#include + +#include + +namespace Sqlite { + +class Database { + sqlite3 *db = nullptr; + +public: + Database() = default; + + explicit Database(const char *path); + + ~Database() noexcept { + if (db != nullptr) + sqlite3_close(db); + } + + Database(Database &&src) noexcept + :db(std::exchange(src.db, nullptr)) {} + + Database &operator=(Database &&src) noexcept { + using std::swap; + swap(db, src.db); + return *this; + } + + operator sqlite3 *() const noexcept { + return db; + } +}; + +} // namespace Sqlite + +#endif diff --git a/src/lib/sqlite/meson.build b/src/lib/sqlite/meson.build index dcaf9e267..77a5f19ff 100644 --- a/src/lib/sqlite/meson.build +++ b/src/lib/sqlite/meson.build @@ -6,6 +6,7 @@ endif sqlite = static_library( 'sqlite', + 'Database.cxx', 'Error.cxx', include_directories: inc, dependencies: [ diff --git a/src/sticker/Database.cxx b/src/sticker/Database.cxx index 302ba3e3f..c90cbdcf7 100644 --- a/src/sticker/Database.cxx +++ b/src/sticker/Database.cxx @@ -82,21 +82,12 @@ static const char sticker_sql_create[] = ""; StickerDatabase::StickerDatabase(Path path) + :db(path.c_str()) { assert(!path.IsNull()); int ret; - /* open/create the sqlite database */ - - ret = sqlite3_open(path.c_str(), &db); - if (ret != SQLITE_OK) { - const std::string utf8 = path.ToUTF8(); - throw SqliteError(db, ret, - ("Failed to open sqlite database '" + - utf8 + "'").c_str()); - } - /* create the table and index */ ret = sqlite3_exec(db, sticker_sql_create, @@ -123,8 +114,6 @@ StickerDatabase::~StickerDatabase() noexcept sqlite3_finalize(stmt[i]); } - - sqlite3_close(db); } std::string diff --git a/src/sticker/Database.hxx b/src/sticker/Database.hxx index efc94b3ca..61a908d81 100644 --- a/src/sticker/Database.hxx +++ b/src/sticker/Database.hxx @@ -43,6 +43,7 @@ #define MPD_STICKER_DATABASE_HXX #include "Match.hxx" +#include "lib/sqlite/Database.hxx" #include "util/Compiler.h" #include @@ -69,7 +70,7 @@ class StickerDatabase { SQL_COUNT }; - sqlite3 *db; + Sqlite::Database db; sqlite3_stmt *stmt[SQL_COUNT]; public: