strawberry-audio-player-win.../src/core/database.h

159 lines
4.5 KiB
C
Raw Normal View History

2018-02-27 18:06:05 +01:00
/*
* Strawberry Music Player
* This file was part of Clementine.
* Copyright 2010, David Sansome <me@davidsansome.com>
2021-03-20 21:14:47 +01:00
* Copyright 2018-2021, Jonas Kvinge <jonas@jkvinge.net>
2018-02-27 18:06:05 +01:00
*
* Strawberry 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 3 of the License, or
* (at your option) any later version.
*
* Strawberry 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 Strawberry. If not, see <http://www.gnu.org/licenses/>.
2018-08-09 18:39:44 +02:00
*
2018-02-27 18:06:05 +01:00
*/
#ifndef DATABASE_H
#define DATABASE_H
#include "config.h"
#include <sqlite3.h>
#include <QtGlobal>
2018-02-27 18:06:05 +01:00
#include <QObject>
#include <QMutex>
#include <QMap>
2018-02-27 18:06:05 +01:00
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QString>
2018-02-27 18:06:05 +01:00
#include <QStringList>
2020-09-22 18:58:44 +02:00
#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
# include <QRecursiveMutex>
#endif
2018-02-27 18:06:05 +01:00
#include "sqlquery.h"
2019-07-24 23:29:09 +02:00
class QThread;
2018-02-27 18:06:05 +01:00
class Application;
class Database : public QObject {
Q_OBJECT
public:
2020-04-07 16:49:15 +02:00
explicit Database(Application *app, QObject *parent = nullptr, const QString &database_name = QString());
2020-06-15 21:55:05 +02:00
~Database() override;
2018-02-27 18:06:05 +01:00
struct AttachedDatabase {
AttachedDatabase() {}
AttachedDatabase(const QString &filename, const QString &schema, bool is_temporary)
: filename_(filename), schema_(schema), is_temporary_(is_temporary) {}
QString filename_;
QString schema_;
bool is_temporary_;
};
static const int kSchemaVersion;
2022-07-10 18:57:00 +02:00
static const int kMinSupportedSchemaVersion;
2018-02-27 18:06:05 +01:00
static const char *kDatabaseFilename;
static const char *kMagicAllSongsTables;
2019-07-24 23:29:09 +02:00
void ExitAsync();
2018-02-27 18:06:05 +01:00
QSqlDatabase Connect();
void Close();
void ReportErrors(const SqlQuery &query);
2020-09-22 18:58:44 +02:00
#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
QRecursiveMutex *Mutex() { return &mutex_; }
#else
2018-02-27 18:06:05 +01:00
QMutex *Mutex() { return &mutex_; }
2020-09-22 18:58:44 +02:00
#endif
2018-02-27 18:06:05 +01:00
void RecreateAttachedDb(const QString &database_name);
void ExecSchemaCommands(QSqlDatabase &db, const QString &schema, int schema_version, bool in_transaction = false);
int startup_schema_version() const { return startup_schema_version_; }
int current_schema_version() const { return kSchemaVersion; }
void AttachDatabase(const QString &database_name, const AttachedDatabase &database);
void AttachDatabaseOnDbConnection(const QString &database_name, const AttachedDatabase &database, QSqlDatabase &db);
void DetachDatabase(const QString &database_name);
2019-07-24 23:29:09 +02:00
signals:
void ExitFinished();
void Error(QString error);
void Errors(QStringList errors);
2018-02-27 18:06:05 +01:00
2019-07-24 23:29:09 +02:00
private slots:
void Exit();
2018-02-27 18:06:05 +01:00
public slots:
void DoBackup();
private:
2021-06-22 13:41:38 +02:00
static int SchemaVersion(QSqlDatabase *db);
2018-02-27 18:06:05 +01:00
void UpdateMainSchema(QSqlDatabase *db);
void ExecSchemaCommandsFromFile(QSqlDatabase &db, const QString &filename, int schema_version, bool in_transaction = false);
void ExecSongTablesCommands(QSqlDatabase &db, const QStringList &song_tables, const QStringList &commands);
2018-02-27 18:06:05 +01:00
void UpdateDatabaseSchema(int version, QSqlDatabase &db);
void UrlEncodeFilenameColumn(const QString &table, QSqlDatabase &db);
QStringList SongsTables(QSqlDatabase &db, const int schema_version);
2021-06-20 19:04:08 +02:00
bool IntegrityCheck(const QSqlDatabase &db);
2018-02-27 18:06:05 +01:00
void BackupFile(const QString &filename);
2021-07-11 09:49:38 +02:00
static bool OpenDatabase(const QString &filename, sqlite3 **connection);
2018-02-27 18:06:05 +01:00
Application *app_;
// Alias -> filename
QMap<QString, AttachedDatabase> attached_databases_;
QString directory_;
QMutex connect_mutex_;
2020-09-22 18:58:44 +02:00
#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
QRecursiveMutex mutex_;
#else
2018-02-27 18:06:05 +01:00
QMutex mutex_;
2020-09-22 18:58:44 +02:00
#endif
2018-02-27 18:06:05 +01:00
2018-03-31 19:43:28 +02:00
// This ID makes the QSqlDatabase name unique to the object as well as the thread
2018-02-27 18:06:05 +01:00
int connection_id_;
static QMutex sNextConnectionIdMutex;
static int sNextConnectionId;
// Used by tests
QString injected_database_name_;
uint query_hash_;
QStringList query_cache_;
// This is the schema version of Strawberry's DB from the app's last run.
int startup_schema_version_;
2019-07-24 23:29:09 +02:00
QThread *original_thread_;
2018-02-27 18:06:05 +01:00
};
class MemoryDatabase : public Database {
2021-06-20 19:04:08 +02:00
Q_OBJECT
2018-02-27 18:06:05 +01:00
public:
2020-06-26 22:41:38 +02:00
explicit MemoryDatabase(Application *app, QObject *parent = nullptr)
2018-02-27 18:06:05 +01:00
: Database(app, parent, ":memory:") {}
2020-06-15 21:55:05 +02:00
~MemoryDatabase() override {
2018-02-27 18:06:05 +01:00
// Make sure Qt doesn't reuse the same database
QSqlDatabase::removeDatabase(Connect().connectionName());
}
};
#endif // DATABASE_H