lib/sqlite/Database: wrapper for sqlite3*
This commit is contained in:
parent
77c9081f78
commit
a6dc1ab0a9
35
src/lib/sqlite/Database.cxx
Normal file
35
src/lib/sqlite/Database.cxx
Normal file
@ -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
|
58
src/lib/sqlite/Database.hxx
Normal file
58
src/lib/sqlite/Database.hxx
Normal file
@ -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 <sqlite3.h>
|
||||
|
||||
#include <utility>
|
||||
|
||||
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
|
@ -6,6 +6,7 @@ endif
|
||||
|
||||
sqlite = static_library(
|
||||
'sqlite',
|
||||
'Database.cxx',
|
||||
'Error.cxx',
|
||||
include_directories: inc,
|
||||
dependencies: [
|
||||
|
@ -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
|
||||
|
@ -43,6 +43,7 @@
|
||||
#define MPD_STICKER_DATABASE_HXX
|
||||
|
||||
#include "Match.hxx"
|
||||
#include "lib/sqlite/Database.hxx"
|
||||
#include "util/Compiler.h"
|
||||
|
||||
#include <sqlite3.h>
|
||||
@ -69,7 +70,7 @@ class StickerDatabase {
|
||||
SQL_COUNT
|
||||
};
|
||||
|
||||
sqlite3 *db;
|
||||
Sqlite::Database db;
|
||||
sqlite3_stmt *stmt[SQL_COUNT];
|
||||
|
||||
public:
|
||||
|
Loading…
Reference in New Issue
Block a user