1
0
mirror of https://github.com/clementine-player/Clementine synced 2024-12-15 18:58:55 +01:00

Support unicode-aware comparisons in Sqlite3.

Fixes issue #71
This commit is contained in:
John Maguire 2010-03-20 23:41:59 +00:00
parent e1c44cdc77
commit 758f99be49
3 changed files with 42 additions and 0 deletions

View File

@ -23,6 +23,8 @@ endif(WIN32)
find_package(OpenGL REQUIRED)
find_package(Boost REQUIRED)
find_library(SQLITE3_LIBRARIES sqlite3)
if(WIN32)
find_library(TAGLIB_LIBRARIES tag)
else(WIN32)

View File

@ -208,6 +208,7 @@ target_link_libraries(clementine_lib
${LIBNOTIFY_LIBRARIES}
${TAGLIB_LIBRARIES}
${QT_LIBRARIES}
${SQLITE3_LIBRARIES}
)
if (APPLE)
target_link_libraries(clementine_lib

View File

@ -7,13 +7,37 @@
#include <QSettings>
#include <QtDebug>
#include <QSqlDatabase>
#include <QSqlDriver>
#include <QSqlQuery>
#include <QCoreApplication>
#include <QThread>
#include <sqlite3.h>
const char* LibraryBackend::kDatabaseName = "clementine.db";
const int LibraryBackend::kSchemaVersion = 3;
// Custom LIKE(X, Y) function for sqlite3 that supports case insensitive unicode matching.
void SqliteLike(sqlite3_context* context, int argc, sqlite3_value** argv) {
Q_ASSERT(argc == 2 || argc == 3);
Q_ASSERT(sqlite3_value_type(argv[0]) == sqlite3_value_type(argv[1]));
switch (sqlite3_value_type(argv[0])) {
case SQLITE_INTEGER: {
qint64 result = sqlite3_value_int64(argv[0]) - sqlite3_value_int64(argv[1]);
sqlite3_result_int64(context, result ? 0 : 1);
break;
}
case SQLITE_TEXT: {
const uchar* data_a = sqlite3_value_text(argv[0]);
const uchar* data_b = sqlite3_value_text(argv[1]);
QString a = QString::fromUtf8(reinterpret_cast<const char*>(data_a)).section('%', 1, 1);
QString b = QString::fromUtf8(reinterpret_cast<const char*>(data_b));
sqlite3_result_int64(context, b.contains(a, Qt::CaseInsensitive) ? 1 : 0);
break;
}
}
}
LibraryBackend::LibraryBackend(QObject* parent, const QString& database_name)
: QObject(parent),
injected_database_name_(database_name)
@ -60,6 +84,21 @@ QSqlDatabase LibraryBackend::Connect() {
emit Error("LibraryBackend: " + db.lastError().text());
return db;
}
// We want Unicode aware LIKE clauses.
QVariant v = db.driver()->handle();
if (v.isValid() && qstrcmp(v.typeName(), "sqlite3*") == 0) {
sqlite3* handle = *static_cast<sqlite3**>(v.data());
if (handle) {
sqlite3_create_function(
handle, // Sqlite3 handle.
"LIKE", // Function name (either override or new).
2, // Number of args.
SQLITE_ANY, // What types this function accepts.
NULL, // Custom data available via sqlite3_user_data().
&SqliteLike, // Our function :-)
NULL, NULL);
}
}
if (db.tables().count() == 0) {
// Set up initial schema