lib/sqlite/Database: wrapper for sqlite3*

This commit is contained in:
Max Kellermann
2019-04-25 12:10:12 +02:00
parent 77c9081f78
commit a6dc1ab0a9
5 changed files with 97 additions and 13 deletions

View 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

View 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

View File

@@ -6,6 +6,7 @@ endif
sqlite = static_library(
'sqlite',
'Database.cxx',
'Error.cxx',
include_directories: inc,
dependencies: [