1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
|
#include <QSqlDriver>
#include <QSqlError>
#ifdef __SQLITE3__
#include <sqlite3.h>
#endif // __SQLITE3__
#include "util/db/dbconnection.h"
#include "util/db/sqllikewildcards.h"
#include "util/memory.h"
#include "util/logger.h"
#include "util/assert.h"
// Originally from public domain code:
// http://www.archivum.info/qt-interest@trolltech.com/2008-12/00584/Re-%28Qt-interest%29-Qt-Sqlite-UserDefinedFunction.html
namespace mixxx {
namespace {
const mixxx::Logger kLogger("DbConnection");
QSqlDatabase createDatabase(
const DbConnection::Params& params,
const QString connectionName) {
kLogger.info()
<< "Available drivers for database connections:"
<< QSqlDatabase::drivers();
QSqlDatabase database =
QSqlDatabase::addDatabase(params.type, connectionName);
database.setHostName(params.hostName);
database.setDatabaseName(params.filePath);
database.setUserName(params.userName);
database.setPassword(params.password);
return database;
}
QSqlDatabase cloneDatabase(
const QSqlDatabase& database,
const QString connectionName) {
DEBUG_ASSERT(!database.isOpen());
return QSqlDatabase::cloneDatabase(database, connectionName);
}
void removeDatabase(
QSqlDatabase* pDatabase) {
DEBUG_ASSERT(pDatabase);
DEBUG_ASSERT(!pDatabase->isOpen());
// pDatabase must be the last reference to the implicitly shared
// QSqlDatabase object
QString connectionName = pDatabase->connectionName();
// Drop the last reference before actually removing the database
// to avoid the following warning:
// "Warning [Main]: QSqlDatabasePrivate::removeDatabase: connection
// '...' is still in use, all queries will cease to work."
*pDatabase = QSqlDatabase();
// After all references have been dropped we can safely remove the
// connection. If still some of the afore mentioned warnings appear
// in the log than a component is misbehaving and still holding an
// invalid copy of the QSqlDatabase object that it shouldn't have!!
QSqlDatabase::removeDatabase(connectionName);
}
// The default comparison of strings for sorting.
inline int compareLocaleAwareCaseInsensitive(
const QString& first, const QString& second) {
return QString::localeAwareCompare(first.toLower(), second.toLower());
}
void makeLatinLow(QChar* c, int count) {
for (int i = 0; i < count; ++i) {
if (c[i].decompositionTag() != QChar::NoDecomposition) {
QString decomposition = c[i].decomposition();
if (!decomposition[0].isSpace()) {
// here we remove the decoration brom all characters.
// We want "o" matching "ó" and all other variants but we
// do not decompose decoration only characters like "˚" where
// the base character is a space
c[i] = c[i].decomposition()[0];
}
}
if (c[i].isUpper()) {
c[i] = c[i].toLower();
}
}
}
const QChar kSqlLikeEscapeDefault = '\0';
// Compare two strings for equality where the first string is
// a "LIKE" expression. Return true (1) if they are the same and
// false (0) if they are different.
// This is the original sqlite3 icuLikeCompare rewritten for QChar
int likeCompareInner(
const QChar* pattern, // LIKE pattern
int patternSize,
const QChar* string, // The string to compare against
int stringSize,
const QChar esc) { // The escape character
int iPattern = 0; // Current index in pattern
int iString = 0; // Current index in string
bool prevEscape = false; // True if the previous character was uEsc
while (iPattern < patternSize) {
// Read (and consume) the next character from the input pattern.
QChar uPattern = pattern[iPattern++];
// There are now 4 possibilities:
// 1. uPattern is an unescaped match-all character "%",
// 2. uPattern is an unescaped match-one character "_",
// 3. uPattern is an unescaped escape character, or
// 4. uPattern is to be handled as an ordinary character
if (!prevEscape && uPattern == kSqlLikeMatchAll) {
// Case 1.
QChar c;
// Skip any kSqlLikeMatchAll or kSqlLikeMatchOne characters that follow a
// kSqlLikeMatchAll. For each kSqlLikeMatchOne, skip one character in the
// test string.
if (iPattern >= patternSize) {
// Tailing %
return 1;
}
while ((c = pattern[iPattern]) == kSqlLikeMatchAll || c == kSqlLikeMatchOne) {
if (c == kSqlLikeMatchOne) {
if (++iString == stringSize) {
return 0;
}
}
if (++iPattern == patternSize) {
// Two or more tailing %
return 1;
}
}
while (iString < stringSize) {
if (likeCompareInner(&pattern[iPattern], patternSize - iPattern,
&string[iString], stringSize - iString, esc)) {
return 1;
}
iString++;
}
return 0;
} else if (!prevEscape && uPattern == kSqlLikeMatchOne) {
// Case 2.
if (++iString == stringSize) {
return 0;
}
} else if (!prevEscape && uPattern == esc) {
// Case 3.
prevEscape = 1;
} else {
// Case 4.
if (iString == stringSize) {
return 0;
}
QChar uString = string[iString++];
if (uString != uPattern) {
return 0;
}
prevEscape = false;
}
}
return iString == stringSize;
}
#ifdef __SQLITE3__
// The collating function callback is invoked with a copy of the pArg
// application data pointer and with two strings in the encoding specified
// by the eTextRep argument.
// The collating function must return an integer that is negative, zero,
// or positive if the first string is less than, equal to, or greater
// than the second, respectively.
int sqliteStringCompareUTF16(void* pArg,
int len1, const void* data1,
int len2, const void* data2) {
Q_UNUSED(pArg);
// Construct a QString without copy
QString string1 = QString::fromRawData(reinterpret_cast<const QChar*>(data1),
len1 / sizeof(QChar));
QString string2 = QString::fromRawData(reinterpret_cast<const QChar*>(data2),
len2 / sizeof(QChar));
return compareLocaleAwareCaseInsensitive(string1, string2);
}
const char* const kLexicographicalCollationFunc = "mixxxLexicographicalCollationFunc";
// This implements the like() SQL function. This is used by the LIKE operator.
// The SQL statement 'A LIKE B' is implemented as 'like(B, A)', and if there is
// an escape character, say E, it is implemented as 'like(B, A, E)'
//static
void sqliteLike(sqlite3_context *context,
int aArgc,
sqlite3_value **aArgv) {
VERIFY_OR_DEBUG_ASSERT(aArgc == 2 || aArgc == 3) {
return;
}
const char* b = reinterpret_cast<const char*>(
sqlite3_value_text(aArgv[0]));
const char* a = reinterpret_cast<const char*>(
sqlite3_value_text(aArgv[1]));
if (!a || !b) {
return;
}
QString stringB = QString::fromUtf8(b); // Like String
QString stringA = QString::fromUtf8(a);
QChar esc = kSqlLikeEscapeDefault;
if (aArgc == 3) {
const char* e = reinterpret_cast<const char*>(
sqlite3_value_text(aArgv[2]));
if (e) {
QString stringE = QString::fromUtf8(e);
if (!stringE.isEmpty()) {
esc = stringE.data()[0];
}
}
}
int ret = DbConnection::likeCompareLatinLow(&stringB, &stringA, esc);
sqlite3_result_int64(context, ret);
return;
}
#endif // __SQLITE3__
bool initDatabase(QSqlDatabase database) {
DEBUG_ASSERT(database.isOpen());
#ifdef __SQLITE3__
QVariant v = database.driver()->handle();
VERIFY_OR_DEBUG_ASSERT(v.isValid()) {
kLogger.warning() << "Driver handle is invalid";
return false; // abort
}
if (strcmp(v.typeName(), "sqlite3*") != 0) {
kLogger.warning()
<< "Unsupported database driver:"
<< v.typeName();
return false; // abort
}
// v.data() returns a pointer to the handle
sq
|