author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Tue, 02 Feb 2010 00:43:10 +0200 | |
changeset 3 | 41300fa6a67c |
parent 0 | 1918ee327afb |
child 4 | 3b1da2848fc7 |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
3 |
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 |
** All rights reserved. |
|
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the QtSql module of the Qt Toolkit. |
|
8 |
** |
|
9 |
** $QT_BEGIN_LICENSE:LGPL$ |
|
10 |
** No Commercial Usage |
|
11 |
** This file contains pre-release code and may not be distributed. |
|
12 |
** You may use this file in accordance with the terms and conditions |
|
13 |
** contained in the Technology Preview License Agreement accompanying |
|
14 |
** this package. |
|
15 |
** |
|
16 |
** GNU Lesser General Public License Usage |
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 |
** General Public License version 2.1 as published by the Free Software |
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 |
** packaging of this file. Please review the following information to |
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 |
** |
|
24 |
** In addition, as a special exception, Nokia gives you certain additional |
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 |
** |
|
28 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
|
37 |
** |
|
38 |
** $QT_END_LICENSE$ |
|
39 |
** |
|
40 |
****************************************************************************/ |
|
41 |
||
42 |
#include "qsql_mysql.h" |
|
43 |
||
44 |
#include <qcoreapplication.h> |
|
45 |
#include <qvariant.h> |
|
46 |
#include <qdatetime.h> |
|
47 |
#include <qsqlerror.h> |
|
48 |
#include <qsqlfield.h> |
|
49 |
#include <qsqlindex.h> |
|
50 |
#include <qsqlquery.h> |
|
51 |
#include <qsqlrecord.h> |
|
52 |
#include <qstringlist.h> |
|
53 |
#include <qtextcodec.h> |
|
54 |
#include <qvector.h> |
|
55 |
||
56 |
#include <qdebug.h> |
|
57 |
||
58 |
#ifdef Q_OS_WIN32 |
|
59 |
// comment the next line out if you want to use MySQL/embedded on Win32 systems. |
|
60 |
// note that it will crash if you don't statically link to the mysql/e library! |
|
61 |
# define Q_NO_MYSQL_EMBEDDED |
|
62 |
#endif |
|
63 |
||
64 |
Q_DECLARE_METATYPE(MYSQL_RES*) |
|
65 |
Q_DECLARE_METATYPE(MYSQL*) |
|
66 |
||
67 |
#if MYSQL_VERSION_ID >= 40108 |
|
68 |
Q_DECLARE_METATYPE(MYSQL_STMT*) |
|
69 |
#endif |
|
70 |
||
71 |
#if MYSQL_VERSION_ID >= 40100 |
|
72 |
# define Q_CLIENT_MULTI_STATEMENTS CLIENT_MULTI_STATEMENTS |
|
73 |
#else |
|
74 |
# define Q_CLIENT_MULTI_STATEMENTS 0 |
|
75 |
#endif |
|
76 |
||
77 |
QT_BEGIN_NAMESPACE |
|
78 |
||
79 |
class QMYSQLDriverPrivate |
|
80 |
{ |
|
81 |
public: |
|
82 |
QMYSQLDriverPrivate() : mysql(0), |
|
83 |
#ifndef QT_NO_TEXTCODEC |
|
84 |
tc(QTextCodec::codecForLocale()), |
|
85 |
#else |
|
86 |
tc(0), |
|
87 |
#endif |
|
88 |
preparedQuerysEnabled(false) {} |
|
89 |
MYSQL *mysql; |
|
90 |
QTextCodec *tc; |
|
91 |
||
92 |
bool preparedQuerysEnabled; |
|
93 |
}; |
|
94 |
||
95 |
static inline QString toUnicode(QTextCodec *tc, const char *str) |
|
96 |
{ |
|
97 |
#ifdef QT_NO_TEXTCODEC |
|
98 |
Q_UNUSED(tc); |
|
99 |
return QString::fromLatin1(str); |
|
100 |
#else |
|
101 |
return tc->toUnicode(str); |
|
102 |
#endif |
|
103 |
} |
|
104 |
||
105 |
static inline QString toUnicode(QTextCodec *tc, const char *str, int length) |
|
106 |
{ |
|
107 |
#ifdef QT_NO_TEXTCODEC |
|
108 |
Q_UNUSED(tc); |
|
109 |
return QString::fromLatin1(str, length); |
|
110 |
#else |
|
111 |
return tc->toUnicode(str, length); |
|
112 |
#endif |
|
113 |
} |
|
114 |
||
115 |
static inline QByteArray fromUnicode(QTextCodec *tc, const QString &str) |
|
116 |
{ |
|
117 |
#ifdef QT_NO_TEXTCODEC |
|
118 |
Q_UNUSED(tc); |
|
119 |
return str.toLatin1(); |
|
120 |
#else |
|
121 |
return tc->fromUnicode(str); |
|
122 |
#endif |
|
123 |
} |
|
124 |
||
125 |
static inline QVariant qDateFromString(const QString &val) |
|
126 |
{ |
|
127 |
#ifdef QT_NO_DATESTRING |
|
128 |
Q_UNUSED(val); |
|
129 |
return QVariant(val); |
|
130 |
#else |
|
131 |
if (val.isEmpty()) |
|
132 |
return QVariant(QDate()); |
|
133 |
return QVariant(QDate::fromString(val, Qt::ISODate)); |
|
134 |
#endif |
|
135 |
} |
|
136 |
||
137 |
static inline QVariant qTimeFromString(const QString &val) |
|
138 |
{ |
|
139 |
#ifdef QT_NO_DATESTRING |
|
140 |
Q_UNUSED(val); |
|
141 |
return QVariant(val); |
|
142 |
#else |
|
143 |
if (val.isEmpty()) |
|
144 |
return QVariant(QTime()); |
|
145 |
return QVariant(QTime::fromString(val, Qt::ISODate)); |
|
146 |
#endif |
|
147 |
} |
|
148 |
||
149 |
static inline QVariant qDateTimeFromString(QString &val) |
|
150 |
{ |
|
151 |
#ifdef QT_NO_DATESTRING |
|
152 |
Q_UNUSED(val); |
|
153 |
return QVariant(val); |
|
154 |
#else |
|
155 |
if (val.isEmpty()) |
|
156 |
return QVariant(QDateTime()); |
|
157 |
if (val.length() == 14) |
|
158 |
// TIMESTAMPS have the format yyyyMMddhhmmss |
|
159 |
val.insert(4, QLatin1Char('-')).insert(7, QLatin1Char('-')).insert(10, |
|
160 |
QLatin1Char('T')).insert(13, QLatin1Char(':')).insert(16, QLatin1Char(':')); |
|
161 |
return QVariant(QDateTime::fromString(val, Qt::ISODate)); |
|
162 |
#endif |
|
163 |
} |
|
164 |
||
165 |
class QMYSQLResultPrivate : public QObject |
|
166 |
{ |
|
167 |
Q_OBJECT |
|
168 |
public: |
|
169 |
QMYSQLResultPrivate(const QMYSQLDriver* dp, const QMYSQLResult* d) : driver(dp), result(0), q(d), |
|
170 |
rowsAffected(0), hasBlobs(false) |
|
171 |
#if MYSQL_VERSION_ID >= 40108 |
|
172 |
, stmt(0), meta(0), inBinds(0), outBinds(0) |
|
173 |
#endif |
|
174 |
, preparedQuery(false) |
|
175 |
{ |
|
176 |
connect(dp, SIGNAL(destroyed()), this, SLOT(driverDestroyed())); |
|
177 |
} |
|
178 |
||
179 |
const QMYSQLDriver* driver; |
|
180 |
MYSQL_RES *result; |
|
181 |
MYSQL_ROW row; |
|
182 |
const QMYSQLResult* q; |
|
183 |
||
184 |
int rowsAffected; |
|
185 |
||
186 |
bool bindInValues(); |
|
187 |
void bindBlobs(); |
|
188 |
||
189 |
bool hasBlobs; |
|
190 |
struct QMyField |
|
191 |
{ |
|
192 |
QMyField() |
|
193 |
: outField(0), nullIndicator(false), bufLength(0ul), |
|
194 |
myField(0), type(QVariant::Invalid) |
|
195 |
{} |
|
196 |
char *outField; |
|
197 |
my_bool nullIndicator; |
|
198 |
ulong bufLength; |
|
199 |
MYSQL_FIELD *myField; |
|
200 |
QVariant::Type type; |
|
201 |
}; |
|
202 |
||
203 |
QVector<QMyField> fields; |
|
204 |
||
205 |
#if MYSQL_VERSION_ID >= 40108 |
|
206 |
MYSQL_STMT* stmt; |
|
207 |
MYSQL_RES* meta; |
|
208 |
||
209 |
MYSQL_BIND *inBinds; |
|
210 |
MYSQL_BIND *outBinds; |
|
211 |
#endif |
|
212 |
||
213 |
bool preparedQuery; |
|
214 |
||
215 |
private Q_SLOTS: |
|
216 |
void driverDestroyed() { driver = NULL; } |
|
217 |
}; |
|
218 |
||
219 |
#ifndef QT_NO_TEXTCODEC |
|
220 |
static QTextCodec* codec(MYSQL* mysql) |
|
221 |
{ |
|
222 |
#if MYSQL_VERSION_ID >= 32321 |
|
223 |
QTextCodec* heuristicCodec = QTextCodec::codecForName(mysql_character_set_name(mysql)); |
|
224 |
if (heuristicCodec) |
|
225 |
return heuristicCodec; |
|
226 |
#endif |
|
227 |
return QTextCodec::codecForLocale(); |
|
228 |
} |
|
229 |
#endif // QT_NO_TEXTCODEC |
|
230 |
||
231 |
static QSqlError qMakeError(const QString& err, QSqlError::ErrorType type, |
|
232 |
const QMYSQLDriverPrivate* p) |
|
233 |
{ |
|
234 |
const char *cerr = p->mysql ? mysql_error(p->mysql) : 0; |
|
235 |
return QSqlError(QLatin1String("QMYSQL: ") + err, |
|
236 |
p->tc ? toUnicode(p->tc, cerr) : QString::fromLatin1(cerr), |
|
237 |
type, mysql_errno(p->mysql)); |
|
238 |
} |
|
239 |
||
240 |
||
241 |
static QVariant::Type qDecodeMYSQLType(int mysqltype, uint flags) |
|
242 |
{ |
|
243 |
QVariant::Type type; |
|
244 |
switch (mysqltype) { |
|
245 |
case FIELD_TYPE_TINY : |
|
246 |
case FIELD_TYPE_SHORT : |
|
247 |
case FIELD_TYPE_LONG : |
|
248 |
case FIELD_TYPE_INT24 : |
|
249 |
type = (flags & UNSIGNED_FLAG) ? QVariant::UInt : QVariant::Int; |
|
250 |
break; |
|
251 |
case FIELD_TYPE_YEAR : |
|
252 |
type = QVariant::Int; |
|
253 |
break; |
|
254 |
case FIELD_TYPE_LONGLONG : |
|
255 |
type = (flags & UNSIGNED_FLAG) ? QVariant::ULongLong : QVariant::LongLong; |
|
256 |
break; |
|
257 |
case FIELD_TYPE_FLOAT : |
|
258 |
case FIELD_TYPE_DOUBLE : |
|
259 |
case FIELD_TYPE_DECIMAL : |
|
260 |
#if defined(FIELD_TYPE_NEWDECIMAL) |
|
261 |
case FIELD_TYPE_NEWDECIMAL: |
|
262 |
#endif |
|
263 |
type = QVariant::Double; |
|
264 |
break; |
|
265 |
case FIELD_TYPE_DATE : |
|
266 |
type = QVariant::Date; |
|
267 |
break; |
|
268 |
case FIELD_TYPE_TIME : |
|
269 |
type = QVariant::Time; |
|
270 |
break; |
|
271 |
case FIELD_TYPE_DATETIME : |
|
272 |
case FIELD_TYPE_TIMESTAMP : |
|
273 |
type = QVariant::DateTime; |
|
274 |
break; |
|
275 |
case FIELD_TYPE_STRING : |
|
276 |
case FIELD_TYPE_VAR_STRING : |
|
277 |
case FIELD_TYPE_BLOB : |
|
278 |
case FIELD_TYPE_TINY_BLOB : |
|
279 |
case FIELD_TYPE_MEDIUM_BLOB : |
|
280 |
case FIELD_TYPE_LONG_BLOB : |
|
281 |
type = (flags & BINARY_FLAG) ? QVariant::ByteArray : QVariant::String; |
|
282 |
break; |
|
283 |
default: |
|
284 |
case FIELD_TYPE_ENUM : |
|
285 |
case FIELD_TYPE_SET : |
|
286 |
type = QVariant::String; |
|
287 |
break; |
|
288 |
} |
|
289 |
return type; |
|
290 |
} |
|
291 |
||
292 |
static QSqlField qToField(MYSQL_FIELD *field, QTextCodec *tc) |
|
293 |
{ |
|
294 |
QSqlField f(toUnicode(tc, field->name), |
|
295 |
qDecodeMYSQLType(int(field->type), field->flags)); |
|
296 |
f.setRequired(IS_NOT_NULL(field->flags)); |
|
297 |
f.setLength(field->length); |
|
298 |
f.setPrecision(field->decimals); |
|
299 |
f.setSqlType(field->type); |
|
300 |
f.setAutoValue(field->flags & AUTO_INCREMENT_FLAG); |
|
301 |
return f; |
|
302 |
} |
|
303 |
||
304 |
#if MYSQL_VERSION_ID >= 40108 |
|
305 |
||
306 |
static QSqlError qMakeStmtError(const QString& err, QSqlError::ErrorType type, |
|
307 |
MYSQL_STMT* stmt) |
|
308 |
{ |
|
309 |
const char *cerr = mysql_stmt_error(stmt); |
|
310 |
return QSqlError(QLatin1String("QMYSQL3: ") + err, |
|
311 |
QString::fromLatin1(cerr), |
|
312 |
type, mysql_stmt_errno(stmt)); |
|
313 |
} |
|
314 |
||
315 |
static bool qIsBlob(int t) |
|
316 |
{ |
|
317 |
return t == MYSQL_TYPE_TINY_BLOB |
|
318 |
|| t == MYSQL_TYPE_BLOB |
|
319 |
|| t == MYSQL_TYPE_MEDIUM_BLOB |
|
320 |
|| t == MYSQL_TYPE_LONG_BLOB; |
|
321 |
} |
|
322 |
||
323 |
void QMYSQLResultPrivate::bindBlobs() |
|
324 |
{ |
|
325 |
int i; |
|
326 |
MYSQL_FIELD *fieldInfo; |
|
327 |
MYSQL_BIND *bind; |
|
328 |
||
329 |
for(i = 0; i < fields.count(); ++i) { |
|
330 |
fieldInfo = fields.at(i).myField; |
|
331 |
if (qIsBlob(inBinds[i].buffer_type) && meta && fieldInfo) { |
|
332 |
bind = &inBinds[i]; |
|
333 |
bind->buffer_length = fieldInfo->max_length; |
|
334 |
delete[] static_cast<char*>(bind->buffer); |
|
335 |
bind->buffer = new char[fieldInfo->max_length]; |
|
336 |
fields[i].outField = static_cast<char*>(bind->buffer); |
|
337 |
} |
|
338 |
} |
|
339 |
} |
|
340 |
||
341 |
bool QMYSQLResultPrivate::bindInValues() |
|
342 |
{ |
|
343 |
MYSQL_BIND *bind; |
|
344 |
char *field; |
|
345 |
int i = 0; |
|
346 |
||
347 |
if (!meta) |
|
348 |
meta = mysql_stmt_result_metadata(stmt); |
|
349 |
if (!meta) |
|
350 |
return false; |
|
351 |
||
352 |
fields.resize(mysql_num_fields(meta)); |
|
353 |
||
354 |
inBinds = new MYSQL_BIND[fields.size()]; |
|
355 |
memset(inBinds, 0, fields.size() * sizeof(MYSQL_BIND)); |
|
356 |
||
357 |
MYSQL_FIELD *fieldInfo; |
|
358 |
||
359 |
while((fieldInfo = mysql_fetch_field(meta))) { |
|
360 |
QMyField &f = fields[i]; |
|
361 |
f.myField = fieldInfo; |
|
362 |
||
363 |
f.type = qDecodeMYSQLType(fieldInfo->type, fieldInfo->flags); |
|
364 |
if (qIsBlob(fieldInfo->type)) { |
|
365 |
// the size of a blob-field is available as soon as we call |
|
366 |
// mysql_stmt_store_result() |
|
367 |
// after mysql_stmt_exec() in QMYSQLResult::exec() |
|
368 |
fieldInfo->length = 0; |
|
369 |
hasBlobs = true; |
|
370 |
} else { |
|
371 |
fieldInfo->type = MYSQL_TYPE_STRING; |
|
372 |
} |
|
373 |
bind = &inBinds[i]; |
|
374 |
field = new char[fieldInfo->length + 1]; |
|
375 |
memset(field, 0, fieldInfo->length + 1); |
|
376 |
||
377 |
bind->buffer_type = fieldInfo->type; |
|
378 |
bind->buffer = field; |
|
379 |
bind->buffer_length = f.bufLength = fieldInfo->length + 1; |
|
380 |
bind->is_null = &f.nullIndicator; |
|
381 |
bind->length = &f.bufLength; |
|
382 |
f.outField=field; |
|
383 |
||
384 |
++i; |
|
385 |
} |
|
386 |
return true; |
|
387 |
} |
|
388 |
#endif |
|
389 |
||
390 |
QMYSQLResult::QMYSQLResult(const QMYSQLDriver* db) |
|
391 |
: QSqlResult(db) |
|
392 |
{ |
|
393 |
d = new QMYSQLResultPrivate(db, this); |
|
394 |
} |
|
395 |
||
396 |
QMYSQLResult::~QMYSQLResult() |
|
397 |
{ |
|
398 |
cleanup(); |
|
399 |
delete d; |
|
400 |
} |
|
401 |
||
402 |
QVariant QMYSQLResult::handle() const |
|
403 |
{ |
|
404 |
#if MYSQL_VERSION_ID >= 40108 |
|
405 |
if(d->preparedQuery) |
|
406 |
return d->meta ? qVariantFromValue(d->meta) : qVariantFromValue(d->stmt); |
|
407 |
else |
|
408 |
#endif |
|
409 |
return qVariantFromValue(d->result); |
|
410 |
} |
|
411 |
||
412 |
void QMYSQLResult::cleanup() |
|
413 |
{ |
|
414 |
if (d->result) |
|
415 |
mysql_free_result(d->result); |
|
416 |
||
417 |
// must iterate trough leftover result sets from multi-selects or stored procedures |
|
418 |
// if this isn't done subsequent queries will fail with "Commands out of sync" |
|
419 |
#if MYSQL_VERSION_ID >= 40100 |
|
420 |
while (d->driver && d->driver->d->mysql && mysql_next_result(d->driver->d->mysql) == 0) { |
|
421 |
MYSQL_RES *res = mysql_store_result(d->driver->d->mysql); |
|
422 |
if (res) |
|
423 |
mysql_free_result(res); |
|
424 |
} |
|
425 |
#endif |
|
426 |
||
427 |
#if MYSQL_VERSION_ID >= 40108 |
|
428 |
if (d->stmt) { |
|
429 |
if (mysql_stmt_close(d->stmt)) |
|
430 |
qWarning("QMYSQLResult::cleanup: unable to free statement handle"); |
|
431 |
d->stmt = 0; |
|
432 |
} |
|
433 |
||
434 |
if (d->meta) { |
|
435 |
mysql_free_result(d->meta); |
|
436 |
d->meta = 0; |
|
437 |
} |
|
438 |
||
439 |
int i; |
|
440 |
for (i = 0; i < d->fields.count(); ++i) |
|
441 |
delete[] d->fields[i].outField; |
|
442 |
||
443 |
if (d->outBinds) { |
|
444 |
delete[] d->outBinds; |
|
445 |
d->outBinds = 0; |
|
446 |
} |
|
447 |
||
448 |
if (d->inBinds) { |
|
449 |
delete[] d->inBinds; |
|
450 |
d->inBinds = 0; |
|
451 |
} |
|
452 |
#endif |
|
453 |
||
454 |
d->hasBlobs = false; |
|
455 |
d->fields.clear(); |
|
456 |
d->result = NULL; |
|
457 |
d->row = NULL; |
|
458 |
setAt(-1); |
|
459 |
setActive(false); |
|
460 |
} |
|
461 |
||
462 |
bool QMYSQLResult::fetch(int i) |
|
463 |
{ |
|
464 |
if(!d->driver) |
|
465 |
return false; |
|
466 |
if (isForwardOnly()) { // fake a forward seek |
|
467 |
if (at() < i) { |
|
468 |
int x = i - at(); |
|
469 |
while (--x && fetchNext()) {}; |
|
470 |
return fetchNext(); |
|
471 |
} else { |
|
472 |
return false; |
|
473 |
} |
|
474 |
} |
|
475 |
if (at() == i) |
|
476 |
return true; |
|
477 |
if (d->preparedQuery) { |
|
478 |
#if MYSQL_VERSION_ID >= 40108 |
|
479 |
mysql_stmt_data_seek(d->stmt, i); |
|
480 |
||
481 |
int nRC = mysql_stmt_fetch(d->stmt); |
|
482 |
if (nRC) { |
|
483 |
#ifdef MYSQL_DATA_TRUNCATED |
|
484 |
if (nRC == 1 || nRC == MYSQL_DATA_TRUNCATED) |
|
485 |
#else |
|
486 |
if (nRC == 1) |
|
487 |
#endif |
|
488 |
setLastError(qMakeStmtError(QCoreApplication::translate("QMYSQLResult", |
|
489 |
"Unable to fetch data"), QSqlError::StatementError, d->stmt)); |
|
490 |
return false; |
|
491 |
} |
|
492 |
#else |
|
493 |
return false; |
|
494 |
#endif |
|
495 |
} else { |
|
496 |
mysql_data_seek(d->result, i); |
|
497 |
d->row = mysql_fetch_row(d->result); |
|
498 |
if (!d->row) |
|
499 |
return false; |
|
500 |
} |
|
501 |
||
502 |
setAt(i); |
|
503 |
return true; |
|
504 |
} |
|
505 |
||
506 |
bool QMYSQLResult::fetchNext() |
|
507 |
{ |
|
508 |
if(!d->driver) |
|
509 |
return false; |
|
510 |
if (d->preparedQuery) { |
|
511 |
#if MYSQL_VERSION_ID >= 40108 |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
512 |
int nRC = mysql_stmt_fetch(d->stmt); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
513 |
if (nRC) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
514 |
#ifdef MYSQL_DATA_TRUNCATED |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
515 |
if (nRC == 1 || nRC == MYSQL_DATA_TRUNCATED) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
516 |
#else |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
517 |
if (nRC == 1) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
518 |
#endif // MYSQL_DATA_TRUNCATED |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
519 |
setLastError(qMakeStmtError(QCoreApplication::translate("QMYSQLResult", |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
520 |
"Unable to fetch data"), QSqlError::StatementError, d->stmt)); |
0 | 521 |
return false; |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
522 |
} |
0 | 523 |
#else |
524 |
return false; |
|
525 |
#endif |
|
526 |
} else { |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
527 |
d->row = mysql_fetch_row(d->result); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
528 |
if (!d->row) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
529 |
return false; |
0 | 530 |
} |
531 |
setAt(at() + 1); |
|
532 |
return true; |
|
533 |
} |
|
534 |
||
535 |
bool QMYSQLResult::fetchLast() |
|
536 |
{ |
|
537 |
if(!d->driver) |
|
538 |
return false; |
|
539 |
if (isForwardOnly()) { // fake this since MySQL can't seek on forward only queries |
|
540 |
bool success = fetchNext(); // did we move at all? |
|
541 |
while (fetchNext()) {}; |
|
542 |
return success; |
|
543 |
} |
|
544 |
||
545 |
my_ulonglong numRows; |
|
546 |
if (d->preparedQuery) { |
|
547 |
#if MYSQL_VERSION_ID >= 40108 |
|
548 |
numRows = mysql_stmt_num_rows(d->stmt); |
|
549 |
#else |
|
550 |
numRows = 0; |
|
551 |
#endif |
|
552 |
} else { |
|
553 |
numRows = mysql_num_rows(d->result); |
|
554 |
} |
|
555 |
if (at() == int(numRows)) |
|
556 |
return true; |
|
557 |
if (!numRows) |
|
558 |
return false; |
|
559 |
return fetch(numRows - 1); |
|
560 |
} |
|
561 |
||
562 |
bool QMYSQLResult::fetchFirst() |
|
563 |
{ |
|
564 |
if (at() == 0) |
|
565 |
return true; |
|
566 |
||
567 |
if (isForwardOnly()) |
|
568 |
return (at() == QSql::BeforeFirstRow) ? fetchNext() : false; |
|
569 |
return fetch(0); |
|
570 |
} |
|
571 |
||
572 |
QVariant QMYSQLResult::data(int field) |
|
573 |
{ |
|
574 |
||
575 |
if (!isSelect() || field >= d->fields.count()) { |
|
576 |
qWarning("QMYSQLResult::data: column %d out of range", field); |
|
577 |
return QVariant(); |
|
578 |
} |
|
579 |
||
580 |
if (!d->driver) |
|
581 |
return QVariant(); |
|
582 |
||
583 |
int fieldLength = 0; |
|
584 |
const QMYSQLResultPrivate::QMyField &f = d->fields.at(field); |
|
585 |
QString val; |
|
586 |
if (d->preparedQuery) { |
|
587 |
if (f.nullIndicator) |
|
588 |
return QVariant(f.type); |
|
589 |
||
590 |
if (f.type != QVariant::ByteArray) |
|
591 |
val = toUnicode(d->driver->d->tc, f.outField, f.bufLength); |
|
592 |
} else { |
|
593 |
if (d->row[field] == NULL) { |
|
594 |
// NULL value |
|
595 |
return QVariant(f.type); |
|
596 |
} |
|
597 |
fieldLength = mysql_fetch_lengths(d->result)[field]; |
|
598 |
if (f.type != QVariant::ByteArray) |
|
599 |
val = toUnicode(d->driver->d->tc, d->row[field], fieldLength); |
|
600 |
} |
|
601 |
||
602 |
switch(f.type) { |
|
603 |
case QVariant::LongLong: |
|
604 |
return QVariant(val.toLongLong()); |
|
605 |
case QVariant::ULongLong: |
|
606 |
return QVariant(val.toULongLong()); |
|
607 |
case QVariant::Int: |
|
608 |
return QVariant(val.toInt()); |
|
609 |
case QVariant::UInt: |
|
610 |
return QVariant(val.toUInt()); |
|
611 |
case QVariant::Double: { |
|
612 |
QVariant v; |
|
613 |
bool ok=false; |
|
614 |
double dbl = val.toDouble(&ok); |
|
615 |
switch(numericalPrecisionPolicy()) { |
|
616 |
case QSql::LowPrecisionInt32: |
|
617 |
v=QVariant(dbl).toInt(); |
|
618 |
break; |
|
619 |
case QSql::LowPrecisionInt64: |
|
620 |
v = QVariant(dbl).toLongLong(); |
|
621 |
break; |
|
622 |
case QSql::LowPrecisionDouble: |
|
623 |
v = QVariant(dbl); |
|
624 |
break; |
|
625 |
case QSql::HighPrecision: |
|
626 |
default: |
|
627 |
v = val; |
|
628 |
ok = true; |
|
629 |
break; |
|
630 |
} |
|
631 |
if(ok) |
|
632 |
return v; |
|
633 |
else |
|
634 |
return QVariant(); |
|
635 |
} |
|
636 |
return QVariant(val.toDouble()); |
|
637 |
case QVariant::Date: |
|
638 |
return qDateFromString(val); |
|
639 |
case QVariant::Time: |
|
640 |
return qTimeFromString(val); |
|
641 |
case QVariant::DateTime: |
|
642 |
return qDateTimeFromString(val); |
|
643 |
case QVariant::ByteArray: { |
|
644 |
||
645 |
QByteArray ba; |
|
646 |
if (d->preparedQuery) { |
|
647 |
ba = QByteArray(f.outField, f.bufLength); |
|
648 |
} else { |
|
649 |
ba = QByteArray(d->row[field], fieldLength); |
|
650 |
} |
|
651 |
return QVariant(ba); |
|
652 |
} |
|
653 |
default: |
|
654 |
case QVariant::String: |
|
655 |
return QVariant(val); |
|
656 |
} |
|
657 |
qWarning("QMYSQLResult::data: unknown data type"); |
|
658 |
return QVariant(); |
|
659 |
} |
|
660 |
||
661 |
bool QMYSQLResult::isNull(int field) |
|
662 |
{ |
|
663 |
if (d->preparedQuery) |
|
664 |
return d->fields.at(field).nullIndicator; |
|
665 |
else |
|
666 |
return d->row[field] == NULL; |
|
667 |
} |
|
668 |
||
669 |
bool QMYSQLResult::reset (const QString& query) |
|
670 |
{ |
|
671 |
if (!driver() || !driver()->isOpen() || driver()->isOpenError() || !d->driver) |
|
672 |
return false; |
|
673 |
||
674 |
d->preparedQuery = false; |
|
675 |
||
676 |
cleanup(); |
|
677 |
||
678 |
const QByteArray encQuery(fromUnicode(d->driver->d->tc, query)); |
|
679 |
if (mysql_real_query(d->driver->d->mysql, encQuery.data(), encQuery.length())) { |
|
680 |
setLastError(qMakeError(QCoreApplication::translate("QMYSQLResult", "Unable to execute query"), |
|
681 |
QSqlError::StatementError, d->driver->d)); |
|
682 |
return false; |
|
683 |
} |
|
684 |
d->result = mysql_store_result(d->driver->d->mysql); |
|
685 |
if (!d->result && mysql_field_count(d->driver->d->mysql) > 0) { |
|
686 |
setLastError(qMakeError(QCoreApplication::translate("QMYSQLResult", "Unable to store result"), |
|
687 |
QSqlError::StatementError, d->driver->d)); |
|
688 |
return false; |
|
689 |
} |
|
690 |
int numFields = mysql_field_count(d->driver->d->mysql); |
|
691 |
setSelect(numFields != 0); |
|
692 |
d->fields.resize(numFields); |
|
693 |
d->rowsAffected = mysql_affected_rows(d->driver->d->mysql); |
|
694 |
||
695 |
if (isSelect()) { |
|
696 |
for(int i = 0; i < numFields; i++) { |
|
697 |
MYSQL_FIELD* field = mysql_fetch_field_direct(d->result, i); |
|
698 |
d->fields[i].type = qDecodeMYSQLType(field->type, field->flags); |
|
699 |
} |
|
700 |
setAt(QSql::BeforeFirstRow); |
|
701 |
} |
|
702 |
setActive(true); |
|
703 |
return isActive(); |
|
704 |
} |
|
705 |
||
706 |
int QMYSQLResult::size() |
|
707 |
{ |
|
708 |
if (d->driver && isSelect()) |
|
709 |
if (d->preparedQuery) |
|
710 |
#if MYSQL_VERSION_ID >= 40108 |
|
711 |
return mysql_stmt_num_rows(d->stmt); |
|
712 |
#else |
|
713 |
return -1; |
|
714 |
#endif |
|
715 |
else |
|
716 |
return int(mysql_num_rows(d->result)); |
|
717 |
else |
|
718 |
return -1; |
|
719 |
} |
|
720 |
||
721 |
int QMYSQLResult::numRowsAffected() |
|
722 |
{ |
|
723 |
return d->rowsAffected; |
|
724 |
} |
|
725 |
||
726 |
QVariant QMYSQLResult::lastInsertId() const |
|
727 |
{ |
|
728 |
if (!isActive() || !d->driver) |
|
729 |
return QVariant(); |
|
730 |
||
731 |
if (d->preparedQuery) { |
|
732 |
#if MYSQL_VERSION_ID >= 40108 |
|
733 |
quint64 id = mysql_stmt_insert_id(d->stmt); |
|
734 |
if (id) |
|
735 |
return QVariant(id); |
|
736 |
#endif |
|
737 |
} else { |
|
738 |
quint64 id = mysql_insert_id(d->driver->d->mysql); |
|
739 |
if (id) |
|
740 |
return QVariant(id); |
|
741 |
} |
|
742 |
return QVariant(); |
|
743 |
} |
|
744 |
||
745 |
QSqlRecord QMYSQLResult::record() const |
|
746 |
{ |
|
747 |
QSqlRecord info; |
|
748 |
MYSQL_RES *res; |
|
749 |
if (!isActive() || !isSelect() || !d->driver) |
|
750 |
return info; |
|
751 |
||
752 |
#if MYSQL_VERSION_ID >= 40108 |
|
753 |
res = d->preparedQuery ? d->meta : d->result; |
|
754 |
#else |
|
755 |
res = d->result; |
|
756 |
#endif |
|
757 |
||
758 |
if (!mysql_errno(d->driver->d->mysql)) { |
|
759 |
mysql_field_seek(res, 0); |
|
760 |
MYSQL_FIELD* field = mysql_fetch_field(res); |
|
761 |
while(field) { |
|
762 |
info.append(qToField(field, d->driver->d->tc)); |
|
763 |
field = mysql_fetch_field(res); |
|
764 |
} |
|
765 |
} |
|
766 |
mysql_field_seek(res, 0); |
|
767 |
return info; |
|
768 |
} |
|
769 |
||
770 |
bool QMYSQLResult::nextResult() |
|
771 |
{ |
|
772 |
if(!d->driver) |
|
773 |
return false; |
|
774 |
#if MYSQL_VERSION_ID >= 40100 |
|
775 |
setAt(-1); |
|
776 |
setActive(false); |
|
777 |
||
778 |
if (d->result && isSelect()) |
|
779 |
mysql_free_result(d->result); |
|
780 |
d->result = 0; |
|
781 |
setSelect(false); |
|
782 |
||
783 |
for (int i = 0; i < d->fields.count(); ++i) |
|
784 |
delete[] d->fields[i].outField; |
|
785 |
d->fields.clear(); |
|
786 |
||
787 |
int status = mysql_next_result(d->driver->d->mysql); |
|
788 |
if (status > 0) { |
|
789 |
setLastError(qMakeError(QCoreApplication::translate("QMYSQLResult", "Unable to execute next query"), |
|
790 |
QSqlError::StatementError, d->driver->d)); |
|
791 |
return false; |
|
792 |
} else if (status == -1) { |
|
793 |
return false; // No more result sets |
|
794 |
} |
|
795 |
||
796 |
d->result = mysql_store_result(d->driver->d->mysql); |
|
797 |
int numFields = mysql_field_count(d->driver->d->mysql); |
|
798 |
if (!d->result && numFields > 0) { |
|
799 |
setLastError(qMakeError(QCoreApplication::translate("QMYSQLResult", "Unable to store next result"), |
|
800 |
QSqlError::StatementError, d->driver->d)); |
|
801 |
return false; |
|
802 |
} |
|
803 |
||
804 |
setSelect(numFields > 0); |
|
805 |
d->fields.resize(numFields); |
|
806 |
d->rowsAffected = mysql_affected_rows(d->driver->d->mysql); |
|
807 |
||
808 |
if (isSelect()) { |
|
809 |
for (int i = 0; i < numFields; i++) { |
|
810 |
MYSQL_FIELD* field = mysql_fetch_field_direct(d->result, i); |
|
811 |
d->fields[i].type = qDecodeMYSQLType(field->type, field->flags); |
|
812 |
} |
|
813 |
} |
|
814 |
||
815 |
setActive(true); |
|
816 |
return true; |
|
817 |
#else |
|
818 |
return false; |
|
819 |
#endif |
|
820 |
} |
|
821 |
||
822 |
void QMYSQLResult::virtual_hook(int id, void *data) |
|
823 |
{ |
|
824 |
switch (id) { |
|
825 |
case QSqlResult::NextResult: |
|
826 |
Q_ASSERT(data); |
|
827 |
*static_cast<bool*>(data) = nextResult(); |
|
828 |
break; |
|
829 |
default: |
|
830 |
QSqlResult::virtual_hook(id, data); |
|
831 |
} |
|
832 |
} |
|
833 |
||
834 |
||
835 |
#if MYSQL_VERSION_ID >= 40108 |
|
836 |
||
837 |
static MYSQL_TIME *toMySqlDate(QDate date, QTime time, QVariant::Type type) |
|
838 |
{ |
|
839 |
Q_ASSERT(type == QVariant::Time || type == QVariant::Date |
|
840 |
|| type == QVariant::DateTime); |
|
841 |
||
842 |
MYSQL_TIME *myTime = new MYSQL_TIME; |
|
843 |
memset(myTime, 0, sizeof(MYSQL_TIME)); |
|
844 |
||
845 |
if (type == QVariant::Time || type == QVariant::DateTime) { |
|
846 |
myTime->hour = time.hour(); |
|
847 |
myTime->minute = time.minute(); |
|
848 |
myTime->second = time.second(); |
|
849 |
myTime->second_part = time.msec(); |
|
850 |
} |
|
851 |
if (type == QVariant::Date || type == QVariant::DateTime) { |
|
852 |
myTime->year = date.year(); |
|
853 |
myTime->month = date.month(); |
|
854 |
myTime->day = date.day(); |
|
855 |
} |
|
856 |
||
857 |
return myTime; |
|
858 |
} |
|
859 |
||
860 |
bool QMYSQLResult::prepare(const QString& query) |
|
861 |
{ |
|
862 |
if(!d->driver) |
|
863 |
return false; |
|
864 |
#if MYSQL_VERSION_ID >= 40108 |
|
865 |
cleanup(); |
|
866 |
if (!d->driver->d->preparedQuerysEnabled) |
|
867 |
return QSqlResult::prepare(query); |
|
868 |
||
869 |
int r; |
|
870 |
||
871 |
if (query.isEmpty()) |
|
872 |
return false; |
|
873 |
||
874 |
if (!d->stmt) |
|
875 |
d->stmt = mysql_stmt_init(d->driver->d->mysql); |
|
876 |
if (!d->stmt) { |
|
877 |
setLastError(qMakeError(QCoreApplication::translate("QMYSQLResult", "Unable to prepare statement"), |
|
878 |
QSqlError::StatementError, d->driver->d)); |
|
879 |
return false; |
|
880 |
} |
|
881 |
||
882 |
const QByteArray encQuery(fromUnicode(d->driver->d->tc, query)); |
|
883 |
r = mysql_stmt_prepare(d->stmt, encQuery.constData(), encQuery.length()); |
|
884 |
if (r != 0) { |
|
885 |
setLastError(qMakeStmtError(QCoreApplication::translate("QMYSQLResult", |
|
886 |
"Unable to prepare statement"), QSqlError::StatementError, d->stmt)); |
|
887 |
cleanup(); |
|
888 |
return false; |
|
889 |
} |
|
890 |
||
891 |
if (mysql_stmt_param_count(d->stmt) > 0) {// allocate memory for outvalues |
|
892 |
d->outBinds = new MYSQL_BIND[mysql_stmt_param_count(d->stmt)]; |
|
893 |
} |
|
894 |
||
895 |
setSelect(d->bindInValues()); |
|
896 |
d->preparedQuery = true; |
|
897 |
return true; |
|
898 |
#else |
|
899 |
return false; |
|
900 |
#endif |
|
901 |
} |
|
902 |
||
903 |
bool QMYSQLResult::exec() |
|
904 |
{ |
|
905 |
if (!d->driver) |
|
906 |
return false; |
|
907 |
if (!d->preparedQuery) |
|
908 |
return QSqlResult::exec(); |
|
909 |
if (!d->stmt) |
|
910 |
return false; |
|
911 |
||
912 |
int r = 0; |
|
913 |
MYSQL_BIND* currBind; |
|
914 |
QVector<MYSQL_TIME *> timeVector; |
|
915 |
QVector<QByteArray> stringVector; |
|
916 |
QVector<my_bool> nullVector; |
|
917 |
||
918 |
const QVector<QVariant> values = boundValues(); |
|
919 |
||
920 |
r = mysql_stmt_reset(d->stmt); |
|
921 |
if (r != 0) { |
|
922 |
setLastError(qMakeStmtError(QCoreApplication::translate("QMYSQLResult", |
|
923 |
"Unable to reset statement"), QSqlError::StatementError, d->stmt)); |
|
924 |
return false; |
|
925 |
} |
|
926 |
||
927 |
if (mysql_stmt_param_count(d->stmt) > 0 && |
|
928 |
mysql_stmt_param_count(d->stmt) == (uint)values.count()) { |
|
929 |
||
930 |
nullVector.resize(values.count()); |
|
931 |
for (int i = 0; i < values.count(); ++i) { |
|
932 |
const QVariant &val = boundValues().at(i); |
|
933 |
void *data = const_cast<void *>(val.constData()); |
|
934 |
||
935 |
currBind = &d->outBinds[i]; |
|
936 |
||
937 |
nullVector[i] = static_cast<my_bool>(val.isNull()); |
|
938 |
currBind->is_null = &nullVector[i]; |
|
939 |
currBind->length = 0; |
|
940 |
currBind->is_unsigned = 0; |
|
941 |
||
942 |
switch (val.type()) { |
|
943 |
case QVariant::ByteArray: |
|
944 |
currBind->buffer_type = MYSQL_TYPE_BLOB; |
|
945 |
currBind->buffer = const_cast<char *>(val.toByteArray().constData()); |
|
946 |
currBind->buffer_length = val.toByteArray().size(); |
|
947 |
break; |
|
948 |
||
949 |
case QVariant::Time: |
|
950 |
case QVariant::Date: |
|
951 |
case QVariant::DateTime: { |
|
952 |
MYSQL_TIME *myTime = toMySqlDate(val.toDate(), val.toTime(), val.type()); |
|
953 |
timeVector.append(myTime); |
|
954 |
||
955 |
currBind->buffer = myTime; |
|
956 |
switch(val.type()) { |
|
957 |
case QVariant::Time: |
|
958 |
currBind->buffer_type = MYSQL_TYPE_TIME; |
|
959 |
myTime->time_type = MYSQL_TIMESTAMP_TIME; |
|
960 |
break; |
|
961 |
case QVariant::Date: |
|
962 |
currBind->buffer_type = MYSQL_TYPE_DATE; |
|
963 |
myTime->time_type = MYSQL_TIMESTAMP_DATE; |
|
964 |
break; |
|
965 |
case QVariant::DateTime: |
|
966 |
currBind->buffer_type = MYSQL_TYPE_DATETIME; |
|
967 |
myTime->time_type = MYSQL_TIMESTAMP_DATETIME; |
|
968 |
break; |
|
969 |
default: |
|
970 |
break; |
|
971 |
} |
|
972 |
currBind->buffer_length = sizeof(MYSQL_TIME); |
|
973 |
currBind->length = 0; |
|
974 |
break; } |
|
975 |
case QVariant::UInt: |
|
976 |
case QVariant::Int: |
|
977 |
case QVariant::Bool: |
|
978 |
currBind->buffer_type = MYSQL_TYPE_LONG; |
|
979 |
currBind->buffer = data; |
|
980 |
currBind->buffer_length = sizeof(int); |
|
981 |
currBind->is_unsigned = (val.type() != QVariant::Int); |
|
982 |
break; |
|
983 |
case QVariant::Double: |
|
984 |
currBind->buffer_type = MYSQL_TYPE_DOUBLE; |
|
985 |
currBind->buffer = data; |
|
986 |
currBind->buffer_length = sizeof(double); |
|
987 |
break; |
|
988 |
case QVariant::LongLong: |
|
989 |
case QVariant::ULongLong: |
|
990 |
currBind->buffer_type = MYSQL_TYPE_LONGLONG; |
|
991 |
currBind->buffer = data; |
|
992 |
currBind->buffer_length = sizeof(qint64); |
|
993 |
currBind->is_unsigned = (val.type() == QVariant::ULongLong); |
|
994 |
break; |
|
995 |
case QVariant::String: |
|
996 |
default: { |
|
997 |
QByteArray ba = fromUnicode(d->driver->d->tc, val.toString()); |
|
998 |
stringVector.append(ba); |
|
999 |
currBind->buffer_type = MYSQL_TYPE_STRING; |
|
1000 |
currBind->buffer = const_cast<char *>(ba.constData()); |
|
1001 |
currBind->buffer_length = ba.length(); |
|
1002 |
break; } |
|
1003 |
} |
|
1004 |
} |
|
1005 |
||
1006 |
r = mysql_stmt_bind_param(d->stmt, d->outBinds); |
|
1007 |
if (r != 0) { |
|
1008 |
setLastError(qMakeStmtError(QCoreApplication::translate("QMYSQLResult", |
|
1009 |
"Unable to bind value"), QSqlError::StatementError, d->stmt)); |
|
1010 |
qDeleteAll(timeVector); |
|
1011 |
return false; |
|
1012 |
} |
|
1013 |
} |
|
1014 |
r = mysql_stmt_execute(d->stmt); |
|
1015 |
||
1016 |
qDeleteAll(timeVector); |
|
1017 |
||
1018 |
if (r != 0) { |
|
1019 |
setLastError(qMakeStmtError(QCoreApplication::translate("QMYSQLResult", |
|
1020 |
"Unable to execute statement"), QSqlError::StatementError, d->stmt)); |
|
1021 |
return false; |
|
1022 |
} |
|
1023 |
//if there is meta-data there is also data |
|
1024 |
setSelect(d->meta); |
|
1025 |
||
1026 |
d->rowsAffected = mysql_stmt_affected_rows(d->stmt); |
|
1027 |
||
1028 |
if (isSelect()) { |
|
1029 |
my_bool update_max_length = true; |
|
1030 |
||
1031 |
r = mysql_stmt_bind_result(d->stmt, d->inBinds); |
|
1032 |
if (r != 0) { |
|
1033 |
setLastError(qMakeStmtError(QCoreApplication::translate("QMYSQLResult", |
|
1034 |
"Unable to bind outvalues"), QSqlError::StatementError, d->stmt)); |
|
1035 |
return false; |
|
1036 |
} |
|
1037 |
if (d->hasBlobs) |
|
1038 |
mysql_stmt_attr_set(d->stmt, STMT_ATTR_UPDATE_MAX_LENGTH, &update_max_length); |
|
1039 |
||
1040 |
r = mysql_stmt_store_result(d->stmt); |
|
1041 |
if (r != 0) { |
|
1042 |
setLastError(qMakeStmtError(QCoreApplication::translate("QMYSQLResult", |
|
1043 |
"Unable to store statement results"), QSqlError::StatementError, d->stmt)); |
|
1044 |
return false; |
|
1045 |
} |
|
1046 |
||
1047 |
if (d->hasBlobs) { |
|
1048 |
// mysql_stmt_store_result() with STMT_ATTR_UPDATE_MAX_LENGTH set to true crashes |
|
1049 |
// when called without a preceding call to mysql_stmt_bind_result() |
|
1050 |
// in versions < 4.1.8 |
|
1051 |
d->bindBlobs(); |
|
1052 |
r = mysql_stmt_bind_result(d->stmt, d->inBinds); |
|
1053 |
if (r != 0) { |
|
1054 |
setLastError(qMakeStmtError(QCoreApplication::translate("QMYSQLResult", |
|
1055 |
"Unable to bind outvalues"), QSqlError::StatementError, d->stmt)); |
|
1056 |
return false; |
|
1057 |
} |
|
1058 |
} |
|
1059 |
setAt(QSql::BeforeFirstRow); |
|
1060 |
} |
|
1061 |
setActive(true); |
|
1062 |
return true; |
|
1063 |
} |
|
1064 |
#endif |
|
1065 |
///////////////////////////////////////////////////////// |
|
1066 |
||
1067 |
static int qMySqlConnectionCount = 0; |
|
1068 |
static bool qMySqlInitHandledByUser = false; |
|
1069 |
||
1070 |
static void qLibraryInit() |
|
1071 |
{ |
|
1072 |
#ifndef Q_NO_MYSQL_EMBEDDED |
|
1073 |
# if MYSQL_VERSION_ID >= 40000 |
|
1074 |
if (qMySqlInitHandledByUser || qMySqlConnectionCount > 1) |
|
1075 |
return; |
|
1076 |
||
1077 |
# if (MYSQL_VERSION_ID >= 40110 && MYSQL_VERSION_ID < 50000) || MYSQL_VERSION_ID >= 50003 |
|
1078 |
if (mysql_library_init(0, 0, 0)) { |
|
1079 |
# else |
|
1080 |
if (mysql_server_init(0, 0, 0)) { |
|
1081 |
# endif |
|
1082 |
qWarning("QMYSQLDriver::qServerInit: unable to start server."); |
|
1083 |
} |
|
1084 |
# endif // MYSQL_VERSION_ID |
|
1085 |
#endif // Q_NO_MYSQL_EMBEDDED |
|
1086 |
} |
|
1087 |
||
1088 |
static void qLibraryEnd() |
|
1089 |
{ |
|
1090 |
#ifndef Q_NO_MYSQL_EMBEDDED |
|
1091 |
# if MYSQL_VERSION_ID > 40000 |
|
1092 |
# if (MYSQL_VERSION_ID >= 40110 && MYSQL_VERSION_ID < 50000) || MYSQL_VERSION_ID >= 50003 |
|
1093 |
mysql_library_end(); |
|
1094 |
# else |
|
1095 |
mysql_server_end(); |
|
1096 |
# endif |
|
1097 |
# endif |
|
1098 |
#endif |
|
1099 |
} |
|
1100 |
||
1101 |
QMYSQLDriver::QMYSQLDriver(QObject * parent) |
|
1102 |
: QSqlDriver(parent) |
|
1103 |
{ |
|
1104 |
init(); |
|
1105 |
qLibraryInit(); |
|
1106 |
} |
|
1107 |
||
1108 |
/*! |
|
1109 |
Create a driver instance with the open connection handle, \a con. |
|
1110 |
The instance's parent (owner) is \a parent. |
|
1111 |
*/ |
|
1112 |
||
1113 |
QMYSQLDriver::QMYSQLDriver(MYSQL * con, QObject * parent) |
|
1114 |
: QSqlDriver(parent) |
|
1115 |
{ |
|
1116 |
init(); |
|
1117 |
if (con) { |
|
1118 |
d->mysql = (MYSQL *) con; |
|
1119 |
#ifndef QT_NO_TEXTCODEC |
|
1120 |
d->tc = codec(con); |
|
1121 |
#endif |
|
1122 |
setOpen(true); |
|
1123 |
setOpenError(false); |
|
1124 |
if (qMySqlConnectionCount == 1) |
|
1125 |
qMySqlInitHandledByUser = true; |
|
1126 |
} else { |
|
1127 |
qLibraryInit(); |
|
1128 |
} |
|
1129 |
} |
|
1130 |
||
1131 |
void QMYSQLDriver::init() |
|
1132 |
{ |
|
1133 |
d = new QMYSQLDriverPrivate(); |
|
1134 |
d->mysql = 0; |
|
1135 |
qMySqlConnectionCount++; |
|
1136 |
} |
|
1137 |
||
1138 |
QMYSQLDriver::~QMYSQLDriver() |
|
1139 |
{ |
|
1140 |
qMySqlConnectionCount--; |
|
1141 |
if (qMySqlConnectionCount == 0 && !qMySqlInitHandledByUser) |
|
1142 |
qLibraryEnd(); |
|
1143 |
delete d; |
|
1144 |
} |
|
1145 |
||
1146 |
bool QMYSQLDriver::hasFeature(DriverFeature f) const |
|
1147 |
{ |
|
1148 |
switch (f) { |
|
1149 |
case Transactions: |
|
1150 |
// CLIENT_TRANSACTION should be defined in all recent mysql client libs > 3.23.34 |
|
1151 |
#ifdef CLIENT_TRANSACTIONS |
|
1152 |
if (d->mysql) { |
|
1153 |
if ((d->mysql->server_capabilities & CLIENT_TRANSACTIONS) == CLIENT_TRANSACTIONS) |
|
1154 |
return true; |
|
1155 |
} |
|
1156 |
#endif |
|
1157 |
return false; |
|
1158 |
case NamedPlaceholders: |
|
1159 |
case BatchOperations: |
|
1160 |
case SimpleLocking: |
|
1161 |
case EventNotifications: |
|
1162 |
case FinishQuery: |
|
1163 |
return false; |
|
1164 |
case QuerySize: |
|
1165 |
case BLOB: |
|
1166 |
case LastInsertId: |
|
1167 |
case Unicode: |
|
1168 |
case LowPrecisionNumbers: |
|
1169 |
return true; |
|
1170 |
case PreparedQueries: |
|
1171 |
case PositionalPlaceholders: |
|
1172 |
#if MYSQL_VERSION_ID >= 40108 |
|
1173 |
return d->preparedQuerysEnabled; |
|
1174 |
#else |
|
1175 |
return false; |
|
1176 |
#endif |
|
1177 |
case MultipleResultSets: |
|
1178 |
#if MYSQL_VERSION_ID >= 40100 |
|
1179 |
return true; |
|
1180 |
#else |
|
1181 |
return false; |
|
1182 |
#endif |
|
1183 |
} |
|
1184 |
return false; |
|
1185 |
} |
|
1186 |
||
1187 |
static void setOptionFlag(uint &optionFlags, const QString &opt) |
|
1188 |
{ |
|
1189 |
if (opt == QLatin1String("CLIENT_COMPRESS")) |
|
1190 |
optionFlags |= CLIENT_COMPRESS; |
|
1191 |
else if (opt == QLatin1String("CLIENT_FOUND_ROWS")) |
|
1192 |
optionFlags |= CLIENT_FOUND_ROWS; |
|
1193 |
else if (opt == QLatin1String("CLIENT_IGNORE_SPACE")) |
|
1194 |
optionFlags |= CLIENT_IGNORE_SPACE; |
|
1195 |
else if (opt == QLatin1String("CLIENT_INTERACTIVE")) |
|
1196 |
optionFlags |= CLIENT_INTERACTIVE; |
|
1197 |
else if (opt == QLatin1String("CLIENT_NO_SCHEMA")) |
|
1198 |
optionFlags |= CLIENT_NO_SCHEMA; |
|
1199 |
else if (opt == QLatin1String("CLIENT_ODBC")) |
|
1200 |
optionFlags |= CLIENT_ODBC; |
|
1201 |
else if (opt == QLatin1String("CLIENT_SSL")) |
|
1202 |
optionFlags |= CLIENT_SSL; |
|
1203 |
else |
|
1204 |
qWarning("QMYSQLDriver::open: Unknown connect option '%s'", opt.toLocal8Bit().constData()); |
|
1205 |
} |
|
1206 |
||
1207 |
bool QMYSQLDriver::open(const QString& db, |
|
1208 |
const QString& user, |
|
1209 |
const QString& password, |
|
1210 |
const QString& host, |
|
1211 |
int port, |
|
1212 |
const QString& connOpts) |
|
1213 |
{ |
|
1214 |
if (isOpen()) |
|
1215 |
close(); |
|
1216 |
||
1217 |
/* This is a hack to get MySQL's stored procedure support working. |
|
1218 |
Since a stored procedure _may_ return multiple result sets, |
|
1219 |
we have to enable CLIEN_MULTI_STATEMENTS here, otherwise _any_ |
|
1220 |
stored procedure call will fail. |
|
1221 |
*/ |
|
1222 |
unsigned int optionFlags = Q_CLIENT_MULTI_STATEMENTS; |
|
1223 |
const QStringList opts(connOpts.split(QLatin1Char(';'), QString::SkipEmptyParts)); |
|
1224 |
QString unixSocket; |
|
1225 |
#if MYSQL_VERSION_ID >= 50000 |
|
1226 |
my_bool reconnect=false; |
|
1227 |
#endif |
|
1228 |
||
1229 |
// extract the real options from the string |
|
1230 |
for (int i = 0; i < opts.count(); ++i) { |
|
1231 |
QString tmp(opts.at(i).simplified()); |
|
1232 |
int idx; |
|
1233 |
if ((idx = tmp.indexOf(QLatin1Char('='))) != -1) { |
|
1234 |
QString val = tmp.mid(idx + 1).simplified(); |
|
1235 |
QString opt = tmp.left(idx).simplified(); |
|
1236 |
if (opt == QLatin1String("UNIX_SOCKET")) |
|
1237 |
unixSocket = val; |
|
1238 |
#if MYSQL_VERSION_ID >= 50000 |
|
1239 |
else if (opt == QLatin1String("MYSQL_OPT_RECONNECT")) { |
|
1240 |
if (val == QLatin1String("TRUE") || val == QLatin1String("1") || val.isEmpty()) |
|
1241 |
reconnect = true; |
|
1242 |
} |
|
1243 |
#endif |
|
1244 |
else if (val == QLatin1String("TRUE") || val == QLatin1String("1")) |
|
1245 |
setOptionFlag(optionFlags, tmp.left(idx).simplified()); |
|
1246 |
else |
|
1247 |
qWarning("QMYSQLDriver::open: Illegal connect option value '%s'", |
|
1248 |
tmp.toLocal8Bit().constData()); |
|
1249 |
} else { |
|
1250 |
setOptionFlag(optionFlags, tmp); |
|
1251 |
} |
|
1252 |
} |
|
1253 |
||
1254 |
if ((d->mysql = mysql_init((MYSQL*) 0)) && |
|
1255 |
mysql_real_connect(d->mysql, |
|
1256 |
host.isNull() ? static_cast<const char *>(0) |
|
1257 |
: host.toLocal8Bit().constData(), |
|
1258 |
user.isNull() ? static_cast<const char *>(0) |
|
1259 |
: user.toLocal8Bit().constData(), |
|
1260 |
password.isNull() ? static_cast<const char *>(0) |
|
1261 |
: password.toLocal8Bit().constData(), |
|
1262 |
db.isNull() ? static_cast<const char *>(0) |
|
1263 |
: db.toLocal8Bit().constData(), |
|
1264 |
(port > -1) ? port : 0, |
|
1265 |
unixSocket.isNull() ? static_cast<const char *>(0) |
|
1266 |
: unixSocket.toLocal8Bit().constData(), |
|
1267 |
optionFlags)) |
|
1268 |
{ |
|
1269 |
if (!db.isEmpty() && mysql_select_db(d->mysql, db.toLocal8Bit().constData())) { |
|
1270 |
setLastError(qMakeError(tr("Unable to open database '") + db + |
|
1271 |
QLatin1Char('\''), QSqlError::ConnectionError, d)); |
|
1272 |
mysql_close(d->mysql); |
|
1273 |
setOpenError(true); |
|
1274 |
return false; |
|
1275 |
} |
|
1276 |
#if MYSQL_VERSION_ID >= 50000 |
|
1277 |
if(reconnect) |
|
1278 |
mysql_options(d->mysql, MYSQL_OPT_RECONNECT, &reconnect); |
|
1279 |
#endif |
|
1280 |
} else { |
|
1281 |
setLastError(qMakeError(tr("Unable to connect"), |
|
1282 |
QSqlError::ConnectionError, d)); |
|
1283 |
mysql_close(d->mysql); |
|
1284 |
d->mysql = NULL; |
|
1285 |
setOpenError(true); |
|
1286 |
return false; |
|
1287 |
} |
|
1288 |
||
1289 |
#if (MYSQL_VERSION_ID >= 40113 && MYSQL_VERSION_ID < 50000) || MYSQL_VERSION_ID >= 50007 |
|
1290 |
// force the communication to be utf8 |
|
1291 |
mysql_set_character_set(d->mysql, "utf8"); |
|
1292 |
#endif |
|
1293 |
#ifndef QT_NO_TEXTCODEC |
|
1294 |
d->tc = codec(d->mysql); |
|
1295 |
#endif |
|
1296 |
||
1297 |
#if MYSQL_VERSION_ID >= 40108 |
|
1298 |
d->preparedQuerysEnabled = mysql_get_client_version() >= 40108 |
|
1299 |
&& mysql_get_server_version(d->mysql) >= 40100; |
|
1300 |
#else |
|
1301 |
d->preparedQuerysEnabled = false; |
|
1302 |
#endif |
|
1303 |
||
1304 |
#ifndef QT_NO_THREAD |
|
1305 |
mysql_thread_init(); |
|
1306 |
#endif |
|
1307 |
||
1308 |
||
1309 |
setOpen(true); |
|
1310 |
setOpenError(false); |
|
1311 |
return true; |
|
1312 |
} |
|
1313 |
||
1314 |
void QMYSQLDriver::close() |
|
1315 |
{ |
|
1316 |
if (isOpen()) { |
|
1317 |
#ifndef QT_NO_THREAD |
|
1318 |
mysql_thread_end(); |
|
1319 |
#endif |
|
1320 |
mysql_close(d->mysql); |
|
1321 |
d->mysql = NULL; |
|
1322 |
setOpen(false); |
|
1323 |
setOpenError(false); |
|
1324 |
} |
|
1325 |
} |
|
1326 |
||
1327 |
QSqlResult *QMYSQLDriver::createResult() const |
|
1328 |
{ |
|
1329 |
return new QMYSQLResult(this); |
|
1330 |
} |
|
1331 |
||
1332 |
QStringList QMYSQLDriver::tables(QSql::TableType type) const |
|
1333 |
{ |
|
1334 |
QStringList tl; |
|
1335 |
#if MYSQL_VERSION_ID >= 40100 |
|
1336 |
if( mysql_get_server_version(d->mysql) < 50000) |
|
1337 |
{ |
|
1338 |
#endif |
|
1339 |
if (!isOpen()) |
|
1340 |
return tl; |
|
1341 |
if (!(type & QSql::Tables)) |
|
1342 |
return tl; |
|
1343 |
||
1344 |
MYSQL_RES* tableRes = mysql_list_tables(d->mysql, NULL); |
|
1345 |
MYSQL_ROW row; |
|
1346 |
int i = 0; |
|
1347 |
while (tableRes) { |
|
1348 |
mysql_data_seek(tableRes, i); |
|
1349 |
row = mysql_fetch_row(tableRes); |
|
1350 |
if (!row) |
|
1351 |
break; |
|
1352 |
tl.append(toUnicode(d->tc, row[0])); |
|
1353 |
i++; |
|
1354 |
} |
|
1355 |
mysql_free_result(tableRes); |
|
1356 |
#if MYSQL_VERSION_ID >= 40100 |
|
1357 |
} else { |
|
1358 |
QSqlQuery q(createResult()); |
|
1359 |
if(type & QSql::Tables) { |
|
1360 |
q.exec(QLatin1String("select table_name from information_schema.tables where table_type = 'BASE TABLE'")); |
|
1361 |
while(q.next()) |
|
1362 |
tl.append(q.value(0).toString()); |
|
1363 |
} |
|
1364 |
if(type & QSql::Views) { |
|
1365 |
q.exec(QLatin1String("select table_name from information_schema.tables where table_type = 'VIEW'")); |
|
1366 |
while(q.next()) |
|
1367 |
tl.append(q.value(0).toString()); |
|
1368 |
} |
|
1369 |
} |
|
1370 |
#endif |
|
1371 |
return tl; |
|
1372 |
} |
|
1373 |
||
1374 |
QSqlIndex QMYSQLDriver::primaryIndex(const QString& tablename) const |
|
1375 |
{ |
|
1376 |
QSqlIndex idx; |
|
1377 |
if (!isOpen()) |
|
1378 |
return idx; |
|
1379 |
||
1380 |
QSqlQuery i(createResult()); |
|
1381 |
QString stmt(QLatin1String("show index from %1;")); |
|
1382 |
QSqlRecord fil = record(tablename); |
|
1383 |
i.exec(stmt.arg(tablename)); |
|
1384 |
while (i.isActive() && i.next()) { |
|
1385 |
if (i.value(2).toString() == QLatin1String("PRIMARY")) { |
|
1386 |
idx.append(fil.field(i.value(4).toString())); |
|
1387 |
idx.setCursorName(i.value(0).toString()); |
|
1388 |
idx.setName(i.value(2).toString()); |
|
1389 |
} |
|
1390 |
} |
|
1391 |
||
1392 |
return idx; |
|
1393 |
} |
|
1394 |
||
1395 |
QSqlRecord QMYSQLDriver::record(const QString& tablename) const |
|
1396 |
{ |
|
1397 |
QString table=tablename; |
|
1398 |
if(isIdentifierEscaped(table, QSqlDriver::TableName)) |
|
1399 |
table = stripDelimiters(table, QSqlDriver::TableName); |
|
1400 |
||
1401 |
QSqlRecord info; |
|
1402 |
if (!isOpen()) |
|
1403 |
return info; |
|
1404 |
MYSQL_RES* r = mysql_list_fields(d->mysql, table.toLocal8Bit().constData(), 0); |
|
1405 |
if (!r) { |
|
1406 |
return info; |
|
1407 |
} |
|
1408 |
MYSQL_FIELD* field; |
|
1409 |
||
1410 |
while ((field = mysql_fetch_field(r))) |
|
1411 |
info.append(qToField(field, d->tc)); |
|
1412 |
mysql_free_result(r); |
|
1413 |
return info; |
|
1414 |
} |
|
1415 |
||
1416 |
QVariant QMYSQLDriver::handle() const |
|
1417 |
{ |
|
1418 |
return qVariantFromValue(d->mysql); |
|
1419 |
} |
|
1420 |
||
1421 |
bool QMYSQLDriver::beginTransaction() |
|
1422 |
{ |
|
1423 |
#ifndef CLIENT_TRANSACTIONS |
|
1424 |
return false; |
|
1425 |
#endif |
|
1426 |
if (!isOpen()) { |
|
1427 |
qWarning("QMYSQLDriver::beginTransaction: Database not open"); |
|
1428 |
return false; |
|
1429 |
} |
|
1430 |
if (mysql_query(d->mysql, "BEGIN WORK")) { |
|
1431 |
setLastError(qMakeError(tr("Unable to begin transaction"), |
|
1432 |
QSqlError::StatementError, d)); |
|
1433 |
return false; |
|
1434 |
} |
|
1435 |
return true; |
|
1436 |
} |
|
1437 |
||
1438 |
bool QMYSQLDriver::commitTransaction() |
|
1439 |
{ |
|
1440 |
#ifndef CLIENT_TRANSACTIONS |
|
1441 |
return false; |
|
1442 |
#endif |
|
1443 |
if (!isOpen()) { |
|
1444 |
qWarning("QMYSQLDriver::commitTransaction: Database not open"); |
|
1445 |
return false; |
|
1446 |
} |
|
1447 |
if (mysql_query(d->mysql, "COMMIT")) { |
|
1448 |
setLastError(qMakeError(tr("Unable to commit transaction"), |
|
1449 |
QSqlError::StatementError, d)); |
|
1450 |
return false; |
|
1451 |
} |
|
1452 |
return true; |
|
1453 |
} |
|
1454 |
||
1455 |
bool QMYSQLDriver::rollbackTransaction() |
|
1456 |
{ |
|
1457 |
#ifndef CLIENT_TRANSACTIONS |
|
1458 |
return false; |
|
1459 |
#endif |
|
1460 |
if (!isOpen()) { |
|
1461 |
qWarning("QMYSQLDriver::rollbackTransaction: Database not open"); |
|
1462 |
return false; |
|
1463 |
} |
|
1464 |
if (mysql_query(d->mysql, "ROLLBACK")) { |
|
1465 |
setLastError(qMakeError(tr("Unable to rollback transaction"), |
|
1466 |
QSqlError::StatementError, d)); |
|
1467 |
return false; |
|
1468 |
} |
|
1469 |
return true; |
|
1470 |
} |
|
1471 |
||
1472 |
QString QMYSQLDriver::formatValue(const QSqlField &field, bool trimStrings) const |
|
1473 |
{ |
|
1474 |
QString r; |
|
1475 |
if (field.isNull()) { |
|
1476 |
r = QLatin1String("NULL"); |
|
1477 |
} else { |
|
1478 |
switch(field.type()) { |
|
1479 |
case QVariant::String: |
|
1480 |
// Escape '\' characters |
|
1481 |
r = QSqlDriver::formatValue(field, trimStrings); |
|
1482 |
r.replace(QLatin1String("\\"), QLatin1String("\\\\")); |
|
1483 |
break; |
|
1484 |
case QVariant::ByteArray: |
|
1485 |
if (isOpen()) { |
|
1486 |
const QByteArray ba = field.value().toByteArray(); |
|
1487 |
// buffer has to be at least length*2+1 bytes |
|
1488 |
char* buffer = new char[ba.size() * 2 + 1]; |
|
1489 |
int escapedSize = int(mysql_real_escape_string(d->mysql, buffer, |
|
1490 |
ba.data(), ba.size())); |
|
1491 |
r.reserve(escapedSize + 3); |
|
1492 |
r.append(QLatin1Char('\'')).append(toUnicode(d->tc, buffer)).append(QLatin1Char('\'')); |
|
1493 |
delete[] buffer; |
|
1494 |
break; |
|
1495 |
} else { |
|
1496 |
qWarning("QMYSQLDriver::formatValue: Database not open"); |
|
1497 |
} |
|
1498 |
// fall through |
|
1499 |
default: |
|
1500 |
r = QSqlDriver::formatValue(field, trimStrings); |
|
1501 |
} |
|
1502 |
} |
|
1503 |
return r; |
|
1504 |
} |
|
1505 |
||
1506 |
QString QMYSQLDriver::escapeIdentifier(const QString &identifier, IdentifierType) const |
|
1507 |
{ |
|
1508 |
QString res = identifier; |
|
1509 |
if(!identifier.isEmpty() && !identifier.startsWith(QLatin1Char('`')) && !identifier.endsWith(QLatin1Char('`')) ) { |
|
1510 |
res.prepend(QLatin1Char('`')).append(QLatin1Char('`')); |
|
1511 |
res.replace(QLatin1Char('.'), QLatin1String("`.`")); |
|
1512 |
} |
|
1513 |
return res; |
|
1514 |
} |
|
1515 |
||
1516 |
bool QMYSQLDriver::isIdentifierEscapedImplementation(const QString &identifier, IdentifierType type) const |
|
1517 |
{ |
|
1518 |
Q_UNUSED(type); |
|
1519 |
return identifier.size() > 2 |
|
1520 |
&& identifier.startsWith(QLatin1Char('`')) //left delimited |
|
1521 |
&& identifier.endsWith(QLatin1Char('`')); //right delimited |
|
1522 |
} |
|
1523 |
||
1524 |
QT_END_NAMESPACE |
|
1525 |
||
1526 |
#include "qsql_mysql.moc" |