Fall back on sqlite's default non-unicode aware LIKE when we couldn't load the sqlite symbols. Fixes problems for slackware users (one of their problems anyway...).

This commit is contained in:
David Sansome 2010-03-24 16:02:06 +00:00
parent ba8f57fcbd
commit 70b6ba8c39
2 changed files with 37 additions and 30 deletions

View File

@ -44,11 +44,15 @@ sqlite_int64 (*LibraryBackend::_sqlite3_value_int64) (sqlite3_value*) = NULL;
const uchar* (*LibraryBackend::_sqlite3_value_text) (sqlite3_value*) = NULL;
void (*LibraryBackend::_sqlite3_result_int64) (sqlite3_context*, sqlite_int64) = NULL;
bool LibraryBackend::sStaticInitDone = false;
bool LibraryBackend::sLoadedSqliteSymbols = false;
bool LibraryBackend::StaticInit() {
if (_sqlite3_create_function) {
return true;
void LibraryBackend::StaticInit() {
if (sStaticInitDone) {
return;
}
sStaticInitDone = true;
#ifdef Q_OS_WIN32
// We statically link libqsqlite.dll on windows so these symbols are already
@ -65,7 +69,7 @@ bool LibraryBackend::StaticInit() {
QLibrary library(plugin_path);
if (!library.load()) {
qDebug() << "QLibrary::load() failed for " << plugin_path;
return false;
return;
}
_sqlite3_create_function = reinterpret_cast<Sqlite3CreateFunc>(
@ -79,15 +83,16 @@ bool LibraryBackend::StaticInit() {
_sqlite3_result_int64 = reinterpret_cast<void (*) (sqlite3_context*, sqlite_int64)>(
library.resolve("sqlite3_result_int64"));
if (_sqlite3_create_function &&
_sqlite3_value_type &&
_sqlite3_value_int64 &&
_sqlite3_value_text &&
_sqlite3_result_int64) {
return true;
if (!_sqlite3_create_function ||
!_sqlite3_value_type ||
!_sqlite3_value_int64 ||
!_sqlite3_value_text ||
!_sqlite3_result_int64) {
qDebug() << "Couldn't resolve sqlite symbols";
sLoadedSqliteSymbols = false;
} else {
sLoadedSqliteSymbols = true;
}
qDebug() << "Couldn't resolve sqlite symbols";
return false;
#endif
}
@ -169,24 +174,23 @@ QSqlDatabase LibraryBackend::Connect() {
}
// Find Sqlite3 functions in the Qt plugin.
if (!StaticInit()) {
emit Error("LibraryBackend: StaticInit() failed.");
return db;
}
StaticInit();
// 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().
&LibraryBackend::SqliteLike, // Our function :-)
NULL, NULL);
// We want Unicode aware LIKE clauses if possible
if (sLoadedSqliteSymbols) {
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().
&LibraryBackend::SqliteLike, // Our function :-)
NULL, NULL);
}
}
}

View File

@ -189,7 +189,7 @@ class LibraryBackend : public LibraryBackendInterface {
FRIEND_TEST(LibraryBackendTest, LikeUnicodeCaseInsensitive);
// Do static initialisation like loading sqlite functions.
static bool StaticInit();
static void StaticInit();
// Custom LIKE() function for sqlite.
static bool Like(const char* needle, const char* haystack);
static void SqliteLike(sqlite3_context* context, int argc, sqlite3_value** argv);
@ -205,6 +205,9 @@ class LibraryBackend : public LibraryBackendInterface {
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 bool sStaticInitDone;
static bool sLoadedSqliteSymbols;
};
#endif // LIBRARYBACKEND_H