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
|
|
|
|
|
|
|
}
|
|
|
|
|
2010-05-09 02:10:26 +02:00
|
|
|
class Database : public QObject {
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
|
|
|
Database(QObject* parent = 0, const QString& database_name = QString());
|
|
|
|
|
|
|
|
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
|
|
|
|
2010-06-02 17:58:07 +02:00
|
|
|
void Stop() {}
|
|
|
|
|
2010-05-09 02:10:26 +02:00
|
|
|
QSqlDatabase Connect();
|
|
|
|
bool CheckErrors(const QSqlError& error);
|
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);
|
2010-07-04 01:00:07 +02:00
|
|
|
void ExecFromFile(const QString& filename, QSqlDatabase &db);
|
|
|
|
void ExecCommands(const QString& commands, QSqlDatabase &db);
|
|
|
|
|
2010-05-09 02:10:26 +02:00
|
|
|
signals:
|
|
|
|
void Error(const QString& message);
|
|
|
|
|
|
|
|
private:
|
|
|
|
void UpdateDatabaseSchema(int version, QSqlDatabase& db);
|
2010-10-17 19:50:20 +02:00
|
|
|
QStringList SongsTables(QSqlDatabase& db) const;
|
2010-05-09 02:10:26 +02:00
|
|
|
|
2010-11-27 17:14:09 +01:00
|
|
|
struct AttachedDatabase {
|
|
|
|
AttachedDatabase() {}
|
|
|
|
AttachedDatabase(const QString& filename, const QString& schema)
|
|
|
|
: filename_(filename), schema_(schema) {}
|
|
|
|
|
|
|
|
QString filename_;
|
|
|
|
QString schema_;
|
|
|
|
};
|
|
|
|
|
|
|
|
// 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_;
|
|
|
|
|
|
|
|
FRIEND_TEST(DatabaseTest, LikeWorksWithAllAscii);
|
|
|
|
FRIEND_TEST(DatabaseTest, LikeWorksWithUnicode);
|
|
|
|
FRIEND_TEST(DatabaseTest, LikeAsciiCaseInsensitive);
|
|
|
|
FRIEND_TEST(DatabaseTest, LikeUnicodeCaseInsensitive);
|
|
|
|
FRIEND_TEST(DatabaseTest, LikePerformance);
|
|
|
|
FRIEND_TEST(DatabaseTest, LikeCacheInvalidated);
|
|
|
|
FRIEND_TEST(DatabaseTest, LikeQuerySplit);
|
2010-06-18 14:54:11 +02:00
|
|
|
FRIEND_TEST(DatabaseTest, LikeDecomposes);
|
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();
|
|
|
|
|
|
|
|
// Custom LIKE() function for sqlite.
|
|
|
|
bool Like(const char* needle, const char* haystack);
|
|
|
|
static void SqliteLike(sqlite3_context* context, int argc, sqlite3_value** argv);
|
|
|
|
typedef int (*Sqlite3CreateFunc) (
|
|
|
|
sqlite3*, const char*, int, int, void*,
|
|
|
|
void (*) (sqlite3_context*, int, sqlite3_value**),
|
|
|
|
void (*) (sqlite3_context*, int, sqlite3_value**),
|
|
|
|
void (*) (sqlite3_context*));
|
|
|
|
|
|
|
|
// Sqlite3 functions. These will be loaded from the sqlite3 plugin.
|
|
|
|
static Sqlite3CreateFunc _sqlite3_create_function;
|
|
|
|
static int (*_sqlite3_value_type) (sqlite3_value*);
|
|
|
|
static sqlite_int64 (*_sqlite3_value_int64) (sqlite3_value*);
|
|
|
|
static const uchar* (*_sqlite3_value_text) (sqlite3_value*);
|
|
|
|
static void (*_sqlite3_result_int64) (sqlite3_context*, sqlite_int64);
|
|
|
|
static void* (*_sqlite3_user_data) (sqlite3_context*);
|
|
|
|
|
2010-06-20 21:46:51 +02:00
|
|
|
|
2010-05-09 02:10:26 +02:00
|
|
|
static bool sStaticInitDone;
|
|
|
|
static bool sLoadedSqliteSymbols;
|
2010-06-20 21:46:51 +02:00
|
|
|
|
|
|
|
static sqlite3_tokenizer_module* sFTSTokenizer;
|
|
|
|
|
|
|
|
static int FTSCreate(int argc, const char* const* argv, sqlite3_tokenizer** tokenizer);
|
|
|
|
static int FTSDestroy(sqlite3_tokenizer* tokenizer);
|
|
|
|
static int FTSOpen(sqlite3_tokenizer* tokenizer,
|
|
|
|
const char* input,
|
|
|
|
int bytes,
|
|
|
|
sqlite3_tokenizer_cursor** cursor);
|
|
|
|
static int FTSClose(sqlite3_tokenizer_cursor* cursor);
|
|
|
|
static int FTSNext(sqlite3_tokenizer_cursor* cursor,
|
|
|
|
const char** token,
|
|
|
|
int* bytes,
|
|
|
|
int* start_offset,
|
|
|
|
int* end_offset,
|
|
|
|
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:
|
|
|
|
MemoryDatabase(QObject* parent = 0) : Database(parent, ":memory:") {}
|
|
|
|
~MemoryDatabase() {
|
|
|
|
// Make sure Qt doesn't reuse the same database
|
|
|
|
QSqlDatabase::removeDatabase(Connect().connectionName());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // DATABASE_H
|