2010-05-09 02:10:26 +02:00
|
|
|
/* This file is part of Clementine.
|
2010-11-20 14:27:10 +01:00
|
|
|
Copyright 2010, David Sansome <me@davidsansome.com>
|
2010-05-09 02:10:26 +02:00
|
|
|
|
|
|
|
Clementine 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.
|
|
|
|
|
|
|
|
Clementine 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 Clementine. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef DATABASE_H
|
|
|
|
#define DATABASE_H
|
|
|
|
|
2010-11-27 17:14:09 +01:00
|
|
|
#include <QMap>
|
|
|
|
#include <QMutex>
|
2010-05-09 02:10:26 +02:00
|
|
|
#include <QObject>
|
|
|
|
#include <QSqlDatabase>
|
|
|
|
#include <QSqlError>
|
|
|
|
#include <QStringList>
|
|
|
|
|
|
|
|
#include <sqlite3.h>
|
|
|
|
|
|
|
|
#include "gtest/gtest_prod.h"
|
|
|
|
|
2010-06-20 21:46:51 +02:00
|
|
|
extern "C" {
|
|
|
|
|
|
|
|
struct sqlite3_tokenizer;
|
|
|
|
struct sqlite3_tokenizer_cursor;
|
2010-06-20 22:07:32 +02:00
|
|
|
struct sqlite3_tokenizer_module;
|
2010-06-20 21:46:51 +02:00
|
|
|
}
|
|
|
|
|
2012-02-12 14:41:50 +01:00
|
|
|
class Application;
|
|
|
|
|
2010-05-09 02:10:26 +02:00
|
|
|
class Database : public QObject {
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
2014-02-10 16:03:54 +01:00
|
|
|
Database(Application* app, QObject* parent = nullptr,
|
2012-02-12 14:41:50 +01:00
|
|
|
const QString& database_name = QString());
|
2010-05-09 02:10:26 +02:00
|
|
|
|
2013-08-01 18:13:14 +02:00
|
|
|
struct AttachedDatabase {
|
|
|
|
AttachedDatabase() {}
|
2014-02-07 16:34:20 +01:00
|
|
|
AttachedDatabase(const QString& filename, const QString& schema,
|
|
|
|
bool is_temporary)
|
|
|
|
: filename_(filename), schema_(schema), is_temporary_(is_temporary) {}
|
2013-08-01 18:13:14 +02:00
|
|
|
|
|
|
|
QString filename_;
|
|
|
|
QString schema_;
|
|
|
|
bool is_temporary_;
|
|
|
|
};
|
|
|
|
|
2010-05-09 02:10:26 +02:00
|
|
|
static const int kSchemaVersion;
|
|
|
|
static const char* kDatabaseFilename;
|
2010-10-17 19:50:20 +02:00
|
|
|
static const char* kMagicAllSongsTables;
|
2010-05-09 02:10:26 +02:00
|
|
|
|
|
|
|
QSqlDatabase Connect();
|
2011-02-05 14:43:04 +01:00
|
|
|
bool CheckErrors(const QSqlQuery& query);
|
2010-06-02 18:22:20 +02:00
|
|
|
QMutex* Mutex() { return &mutex_; }
|
2010-05-09 02:10:26 +02:00
|
|
|
|
2010-11-27 17:14:09 +01:00
|
|
|
void RecreateAttachedDb(const QString& database_name);
|
2014-02-07 16:34:20 +01:00
|
|
|
void ExecSchemaCommands(QSqlDatabase& db, const QString& schema,
|
|
|
|
int schema_version, bool in_transaction = false);
|
2010-07-04 01:00:07 +02:00
|
|
|
|
2011-02-25 21:10:41 +01:00
|
|
|
int startup_schema_version() const { return startup_schema_version_; }
|
|
|
|
int current_schema_version() const { return kSchemaVersion; }
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void AttachDatabase(const QString& database_name,
|
|
|
|
const AttachedDatabase& database);
|
2013-10-25 20:31:56 +02:00
|
|
|
void AttachDatabaseOnDbConnection(const QString& database_name,
|
|
|
|
const AttachedDatabase& database,
|
|
|
|
QSqlDatabase& db);
|
2013-08-01 18:13:14 +02:00
|
|
|
void DetachDatabase(const QString& database_name);
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
signals:
|
2010-05-09 02:10:26 +02:00
|
|
|
void Error(const QString& message);
|
|
|
|
|
2012-04-13 15:49:29 +02:00
|
|
|
public slots:
|
2012-03-13 15:32:44 +01:00
|
|
|
void DoBackup();
|
|
|
|
|
2010-05-09 02:10:26 +02:00
|
|
|
private:
|
2011-02-25 21:10:41 +01:00
|
|
|
void UpdateMainSchema(QSqlDatabase* db);
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void ExecSchemaCommandsFromFile(QSqlDatabase& db, const QString& filename,
|
2013-03-10 07:58:09 +01:00
|
|
|
int schema_version,
|
|
|
|
bool in_transaction = false);
|
2014-02-07 16:34:20 +01:00
|
|
|
void ExecSongTablesCommands(QSqlDatabase& db, const QStringList& song_tables,
|
2013-03-10 07:58:09 +01:00
|
|
|
const QStringList& commands);
|
2013-03-03 13:00:24 +01:00
|
|
|
|
2010-05-09 02:10:26 +02:00
|
|
|
void UpdateDatabaseSchema(int version, QSqlDatabase& db);
|
2011-06-14 16:46:02 +02:00
|
|
|
void UrlEncodeFilenameColumn(const QString& table, QSqlDatabase& db);
|
2011-04-27 00:06:58 +02:00
|
|
|
QStringList SongsTables(QSqlDatabase& db, int schema_version) const;
|
2012-03-13 15:32:44 +01:00
|
|
|
bool IntegrityCheck(QSqlDatabase db);
|
2012-03-16 14:50:20 +01:00
|
|
|
void BackupFile(const QString& filename);
|
|
|
|
bool OpenDatabase(const QString& filename, sqlite3** connection) const;
|
2010-05-09 02:10:26 +02:00
|
|
|
|
2012-02-12 14:41:50 +01:00
|
|
|
Application* app_;
|
|
|
|
|
2010-11-27 17:14:09 +01:00
|
|
|
// Alias -> filename
|
|
|
|
QMap<QString, AttachedDatabase> attached_databases_;
|
|
|
|
|
2010-05-09 02:10:26 +02:00
|
|
|
QString directory_;
|
|
|
|
QMutex connect_mutex_;
|
2010-06-02 18:22:20 +02:00
|
|
|
QMutex mutex_;
|
2010-05-09 02:10:26 +02:00
|
|
|
|
2010-06-26 14:41:18 +02:00
|
|
|
// This ID makes the QSqlDatabase name unique to the object as well as the
|
|
|
|
// thread
|
|
|
|
int connection_id_;
|
|
|
|
|
|
|
|
static QMutex sNextConnectionIdMutex;
|
|
|
|
static int sNextConnectionId;
|
|
|
|
|
2010-05-09 02:10:26 +02:00
|
|
|
// Used by tests
|
|
|
|
QString injected_database_name_;
|
|
|
|
|
|
|
|
uint query_hash_;
|
|
|
|
QStringList query_cache_;
|
|
|
|
|
2011-02-25 21:10:41 +01:00
|
|
|
// This is the schema version of Clementine's DB from the app's last run.
|
|
|
|
int startup_schema_version_;
|
|
|
|
|
2010-06-20 22:44:54 +02:00
|
|
|
FRIEND_TEST(DatabaseTest, FTSOpenParsesSimpleInput);
|
|
|
|
FRIEND_TEST(DatabaseTest, FTSOpenParsesUTF8Input);
|
2010-06-20 22:51:57 +02:00
|
|
|
FRIEND_TEST(DatabaseTest, FTSOpenParsesMultipleTokens);
|
2010-06-20 22:58:32 +02:00
|
|
|
FRIEND_TEST(DatabaseTest, FTSCursorWorks);
|
2010-06-21 14:07:56 +02:00
|
|
|
FRIEND_TEST(DatabaseTest, FTSOpenLeavesCyrillicQueries);
|
2010-05-09 02:10:26 +02:00
|
|
|
|
|
|
|
// Do static initialisation like loading sqlite functions.
|
|
|
|
static void StaticInit();
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
typedef int (*Sqlite3CreateFunc)(sqlite3*, const char*, int, int, void*,
|
|
|
|
void (*)(sqlite3_context*, int,
|
|
|
|
sqlite3_value**),
|
|
|
|
void (*)(sqlite3_context*, int,
|
|
|
|
sqlite3_value**),
|
|
|
|
void (*)(sqlite3_context*));
|
2010-05-09 02:10:26 +02:00
|
|
|
|
2010-06-20 21:46:51 +02:00
|
|
|
static sqlite3_tokenizer_module* sFTSTokenizer;
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
static int FTSCreate(int argc, const char* const* argv,
|
|
|
|
sqlite3_tokenizer** tokenizer);
|
2010-06-20 21:46:51 +02:00
|
|
|
static int FTSDestroy(sqlite3_tokenizer* tokenizer);
|
2014-02-07 16:34:20 +01:00
|
|
|
static int FTSOpen(sqlite3_tokenizer* tokenizer, const char* input, int bytes,
|
2010-06-20 21:46:51 +02:00
|
|
|
sqlite3_tokenizer_cursor** cursor);
|
|
|
|
static int FTSClose(sqlite3_tokenizer_cursor* cursor);
|
2014-02-07 16:34:20 +01:00
|
|
|
static int FTSNext(sqlite3_tokenizer_cursor* cursor, const char** token,
|
|
|
|
int* bytes, int* start_offset, int* end_offset,
|
2010-06-20 21:46:51 +02:00
|
|
|
int* position);
|
2010-06-20 22:44:54 +02:00
|
|
|
struct Token {
|
2010-06-20 22:51:57 +02:00
|
|
|
Token(const QString& token, int start, int end);
|
2010-06-20 22:44:54 +02:00
|
|
|
QString token;
|
|
|
|
int start_offset;
|
|
|
|
int end_offset;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Based on sqlite3_tokenizer.
|
|
|
|
struct UnicodeTokenizer {
|
|
|
|
const sqlite3_tokenizer_module* pModule;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct UnicodeTokenizerCursor {
|
|
|
|
const sqlite3_tokenizer* pTokenizer;
|
|
|
|
|
|
|
|
QList<Token> tokens;
|
|
|
|
int position;
|
|
|
|
QByteArray current_utf8;
|
|
|
|
};
|
2010-05-09 02:10:26 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
class MemoryDatabase : public Database {
|
|
|
|
public:
|
2014-02-10 16:03:54 +01:00
|
|
|
MemoryDatabase(Application* app, QObject* parent = nullptr)
|
2014-02-07 16:34:20 +01:00
|
|
|
: Database(app, parent, ":memory:") {}
|
2010-05-09 02:10:26 +02:00
|
|
|
~MemoryDatabase() {
|
|
|
|
// Make sure Qt doesn't reuse the same database
|
|
|
|
QSqlDatabase::removeDatabase(Connect().connectionName());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
#endif // DATABASE_H
|