|
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_sqlite.h" |
|
43 |
|
44 #include <qcoreapplication.h> |
|
45 #include <qvariant.h> |
|
46 #include <qsqlerror.h> |
|
47 #include <qsqlfield.h> |
|
48 #include <qsqlindex.h> |
|
49 #include <qsqlquery.h> |
|
50 #include <qstringlist.h> |
|
51 #include <qvector.h> |
|
52 #include <qdebug.h> |
|
53 |
|
54 #if defined Q_OS_WIN |
|
55 # include <qt_windows.h> |
|
56 #else |
|
57 # include <unistd.h> |
|
58 #endif |
|
59 |
|
60 #include <sqlite3.h> |
|
61 |
|
62 Q_DECLARE_METATYPE(sqlite3*) |
|
63 Q_DECLARE_METATYPE(sqlite3_stmt*) |
|
64 |
|
65 QT_BEGIN_NAMESPACE |
|
66 |
|
67 static QString _q_escapeIdentifier(const QString &identifier) |
|
68 { |
|
69 QString res = identifier; |
|
70 if(!identifier.isEmpty() && identifier.left(1) != QString(QLatin1Char('"')) && identifier.right(1) != QString(QLatin1Char('"')) ) { |
|
71 res.replace(QLatin1Char('"'), QLatin1String("\"\"")); |
|
72 res.prepend(QLatin1Char('"')).append(QLatin1Char('"')); |
|
73 res.replace(QLatin1Char('.'), QLatin1String("\".\"")); |
|
74 } |
|
75 return res; |
|
76 } |
|
77 |
|
78 static QVariant::Type qGetColumnType(const QString &tpName) |
|
79 { |
|
80 const QString typeName = tpName.toLower(); |
|
81 |
|
82 if (typeName == QLatin1String("integer") |
|
83 || typeName == QLatin1String("int")) |
|
84 return QVariant::Int; |
|
85 if (typeName == QLatin1String("double") |
|
86 || typeName == QLatin1String("float") |
|
87 || typeName.startsWith(QLatin1String("numeric"))) |
|
88 return QVariant::Double; |
|
89 if (typeName == QLatin1String("blob")) |
|
90 return QVariant::ByteArray; |
|
91 return QVariant::String; |
|
92 } |
|
93 |
|
94 static QSqlError qMakeError(sqlite3 *access, const QString &descr, QSqlError::ErrorType type, |
|
95 int errorCode = -1) |
|
96 { |
|
97 return QSqlError(descr, |
|
98 QString::fromUtf16(static_cast<const ushort *>(sqlite3_errmsg16(access))), |
|
99 type, errorCode); |
|
100 } |
|
101 |
|
102 class QSQLiteDriverPrivate |
|
103 { |
|
104 public: |
|
105 inline QSQLiteDriverPrivate() : access(0) {} |
|
106 sqlite3 *access; |
|
107 }; |
|
108 |
|
109 |
|
110 class QSQLiteResultPrivate |
|
111 { |
|
112 public: |
|
113 QSQLiteResultPrivate(QSQLiteResult *res); |
|
114 void cleanup(); |
|
115 bool fetchNext(QSqlCachedResult::ValueCache &values, int idx, bool initialFetch); |
|
116 // initializes the recordInfo and the cache |
|
117 void initColumns(bool emptyResultset); |
|
118 void finalize(); |
|
119 |
|
120 QSQLiteResult* q; |
|
121 sqlite3 *access; |
|
122 |
|
123 sqlite3_stmt *stmt; |
|
124 |
|
125 uint skippedStatus: 1; // the status of the fetchNext() that's skipped |
|
126 uint skipRow: 1; // skip the next fetchNext()? |
|
127 uint utf8: 1; |
|
128 QSqlRecord rInf; |
|
129 }; |
|
130 |
|
131 QSQLiteResultPrivate::QSQLiteResultPrivate(QSQLiteResult* res) : q(res), access(0), |
|
132 stmt(0), skippedStatus(false), skipRow(false), utf8(false) |
|
133 { |
|
134 } |
|
135 |
|
136 void QSQLiteResultPrivate::cleanup() |
|
137 { |
|
138 finalize(); |
|
139 rInf.clear(); |
|
140 skippedStatus = false; |
|
141 skipRow = false; |
|
142 q->setAt(QSql::BeforeFirstRow); |
|
143 q->setActive(false); |
|
144 q->cleanup(); |
|
145 } |
|
146 |
|
147 void QSQLiteResultPrivate::finalize() |
|
148 { |
|
149 if (!stmt) |
|
150 return; |
|
151 |
|
152 sqlite3_finalize(stmt); |
|
153 stmt = 0; |
|
154 } |
|
155 |
|
156 void QSQLiteResultPrivate::initColumns(bool emptyResultset) |
|
157 { |
|
158 int nCols = sqlite3_column_count(stmt); |
|
159 if (nCols <= 0) |
|
160 return; |
|
161 |
|
162 q->init(nCols); |
|
163 |
|
164 for (int i = 0; i < nCols; ++i) { |
|
165 QString colName = QString::fromUtf16( |
|
166 static_cast<const ushort *>(sqlite3_column_name16(stmt, i)) |
|
167 ).remove(QLatin1Char('"')); |
|
168 |
|
169 // must use typeName for resolving the type to match QSqliteDriver::record |
|
170 QString typeName = QString::fromUtf16( |
|
171 static_cast<const ushort *>(sqlite3_column_decltype16(stmt, i))); |
|
172 |
|
173 int dotIdx = colName.lastIndexOf(QLatin1Char('.')); |
|
174 QSqlField fld(colName.mid(dotIdx == -1 ? 0 : dotIdx + 1), qGetColumnType(typeName)); |
|
175 |
|
176 // sqlite3_column_type is documented to have undefined behavior if the result set is empty |
|
177 int stp = emptyResultset ? -1 : sqlite3_column_type(stmt, i); |
|
178 fld.setSqlType(stp); |
|
179 rInf.append(fld); |
|
180 } |
|
181 } |
|
182 |
|
183 bool QSQLiteResultPrivate::fetchNext(QSqlCachedResult::ValueCache &values, int idx, bool initialFetch) |
|
184 { |
|
185 int res; |
|
186 int i; |
|
187 |
|
188 if (skipRow) { |
|
189 // already fetched |
|
190 Q_ASSERT(!initialFetch); |
|
191 skipRow = false; |
|
192 return skippedStatus; |
|
193 } |
|
194 skipRow = initialFetch; |
|
195 |
|
196 if (!stmt) { |
|
197 q->setLastError(QSqlError(QCoreApplication::translate("QSQLiteResult", "Unable to fetch row"), |
|
198 QCoreApplication::translate("QSQLiteResult", "No query"), QSqlError::ConnectionError)); |
|
199 q->setAt(QSql::AfterLastRow); |
|
200 return false; |
|
201 } |
|
202 res = sqlite3_step(stmt); |
|
203 |
|
204 switch(res) { |
|
205 case SQLITE_ROW: |
|
206 // check to see if should fill out columns |
|
207 if (rInf.isEmpty()) |
|
208 // must be first call. |
|
209 initColumns(false); |
|
210 if (idx < 0 && !initialFetch) |
|
211 return true; |
|
212 for (i = 0; i < rInf.count(); ++i) { |
|
213 switch (sqlite3_column_type(stmt, i)) { |
|
214 case SQLITE_BLOB: |
|
215 values[i + idx] = QByteArray(static_cast<const char *>( |
|
216 sqlite3_column_blob(stmt, i)), |
|
217 sqlite3_column_bytes(stmt, i)); |
|
218 break; |
|
219 case SQLITE_INTEGER: |
|
220 values[i + idx] = sqlite3_column_int64(stmt, i); |
|
221 break; |
|
222 case SQLITE_FLOAT: |
|
223 switch(q->numericalPrecisionPolicy()) { |
|
224 case QSql::LowPrecisionInt32: |
|
225 values[i + idx] = sqlite3_column_int(stmt, i); |
|
226 break; |
|
227 case QSql::LowPrecisionInt64: |
|
228 values[i + idx] = sqlite3_column_int64(stmt, i); |
|
229 break; |
|
230 case QSql::LowPrecisionDouble: |
|
231 values[i + idx] = sqlite3_column_double(stmt, i); |
|
232 break; |
|
233 case QSql::HighPrecision: |
|
234 default: |
|
235 values[i + idx] = QString::fromUtf16(static_cast<const ushort *>( |
|
236 sqlite3_column_text16(stmt, i)), |
|
237 sqlite3_column_bytes16(stmt, i) / sizeof(ushort)); |
|
238 break; |
|
239 }; |
|
240 break; |
|
241 case SQLITE_NULL: |
|
242 values[i + idx] = QVariant(QVariant::String); |
|
243 break; |
|
244 default: |
|
245 values[i + idx] = QString::fromUtf16(static_cast<const ushort *>( |
|
246 sqlite3_column_text16(stmt, i)), |
|
247 sqlite3_column_bytes16(stmt, i) / sizeof(ushort)); |
|
248 break; |
|
249 } |
|
250 } |
|
251 return true; |
|
252 case SQLITE_DONE: |
|
253 if (rInf.isEmpty()) |
|
254 // must be first call. |
|
255 initColumns(true); |
|
256 q->setAt(QSql::AfterLastRow); |
|
257 sqlite3_reset(stmt); |
|
258 return false; |
|
259 case SQLITE_ERROR: |
|
260 // SQLITE_ERROR is a generic error code and we must call sqlite3_reset() |
|
261 // to get the specific error message. |
|
262 res = sqlite3_reset(stmt); |
|
263 q->setLastError(qMakeError(access, QCoreApplication::translate("QSQLiteResult", |
|
264 "Unable to fetch row"), QSqlError::ConnectionError, res)); |
|
265 q->setAt(QSql::AfterLastRow); |
|
266 return false; |
|
267 case SQLITE_MISUSE: |
|
268 case SQLITE_BUSY: |
|
269 default: |
|
270 // something wrong, don't get col info, but still return false |
|
271 q->setLastError(qMakeError(access, QCoreApplication::translate("QSQLiteResult", |
|
272 "Unable to fetch row"), QSqlError::ConnectionError, res)); |
|
273 sqlite3_reset(stmt); |
|
274 q->setAt(QSql::AfterLastRow); |
|
275 return false; |
|
276 } |
|
277 return false; |
|
278 } |
|
279 |
|
280 QSQLiteResult::QSQLiteResult(const QSQLiteDriver* db) |
|
281 : QSqlCachedResult(db) |
|
282 { |
|
283 d = new QSQLiteResultPrivate(this); |
|
284 d->access = db->d->access; |
|
285 } |
|
286 |
|
287 QSQLiteResult::~QSQLiteResult() |
|
288 { |
|
289 d->cleanup(); |
|
290 delete d; |
|
291 } |
|
292 |
|
293 void QSQLiteResult::virtual_hook(int id, void *data) |
|
294 { |
|
295 switch (id) { |
|
296 case QSqlResult::DetachFromResultSet: |
|
297 if (d->stmt) |
|
298 sqlite3_reset(d->stmt); |
|
299 break; |
|
300 default: |
|
301 QSqlCachedResult::virtual_hook(id, data); |
|
302 } |
|
303 } |
|
304 |
|
305 bool QSQLiteResult::reset(const QString &query) |
|
306 { |
|
307 if (!prepare(query)) |
|
308 return false; |
|
309 return exec(); |
|
310 } |
|
311 |
|
312 bool QSQLiteResult::prepare(const QString &query) |
|
313 { |
|
314 if (!driver() || !driver()->isOpen() || driver()->isOpenError()) |
|
315 return false; |
|
316 |
|
317 d->cleanup(); |
|
318 |
|
319 setSelect(false); |
|
320 |
|
321 #if (SQLITE_VERSION_NUMBER >= 3003011) |
|
322 int res = sqlite3_prepare16_v2(d->access, query.constData(), (query.size() + 1) * sizeof(QChar), |
|
323 &d->stmt, 0); |
|
324 #else |
|
325 int res = sqlite3_prepare16(d->access, query.constData(), (query.size() + 1) * sizeof(QChar), |
|
326 &d->stmt, 0); |
|
327 #endif |
|
328 |
|
329 if (res != SQLITE_OK) { |
|
330 setLastError(qMakeError(d->access, QCoreApplication::translate("QSQLiteResult", |
|
331 "Unable to execute statement"), QSqlError::StatementError, res)); |
|
332 d->finalize(); |
|
333 return false; |
|
334 } |
|
335 return true; |
|
336 } |
|
337 |
|
338 bool QSQLiteResult::exec() |
|
339 { |
|
340 const QVector<QVariant> values = boundValues(); |
|
341 |
|
342 d->skippedStatus = false; |
|
343 d->skipRow = false; |
|
344 d->rInf.clear(); |
|
345 clearValues(); |
|
346 setLastError(QSqlError()); |
|
347 |
|
348 int res = sqlite3_reset(d->stmt); |
|
349 if (res != SQLITE_OK) { |
|
350 setLastError(qMakeError(d->access, QCoreApplication::translate("QSQLiteResult", |
|
351 "Unable to reset statement"), QSqlError::StatementError, res)); |
|
352 d->finalize(); |
|
353 return false; |
|
354 } |
|
355 int paramCount = sqlite3_bind_parameter_count(d->stmt); |
|
356 if (paramCount == values.count()) { |
|
357 for (int i = 0; i < paramCount; ++i) { |
|
358 res = SQLITE_OK; |
|
359 const QVariant value = values.at(i); |
|
360 |
|
361 if (value.isNull()) { |
|
362 res = sqlite3_bind_null(d->stmt, i + 1); |
|
363 } else { |
|
364 switch (value.type()) { |
|
365 case QVariant::ByteArray: { |
|
366 const QByteArray *ba = static_cast<const QByteArray*>(value.constData()); |
|
367 res = sqlite3_bind_blob(d->stmt, i + 1, ba->constData(), |
|
368 ba->size(), SQLITE_STATIC); |
|
369 break; } |
|
370 case QVariant::Int: |
|
371 res = sqlite3_bind_int(d->stmt, i + 1, value.toInt()); |
|
372 break; |
|
373 case QVariant::Double: |
|
374 res = sqlite3_bind_double(d->stmt, i + 1, value.toDouble()); |
|
375 break; |
|
376 case QVariant::UInt: |
|
377 case QVariant::LongLong: |
|
378 res = sqlite3_bind_int64(d->stmt, i + 1, value.toLongLong()); |
|
379 break; |
|
380 case QVariant::String: { |
|
381 // lifetime of string == lifetime of its qvariant |
|
382 const QString *str = static_cast<const QString*>(value.constData()); |
|
383 res = sqlite3_bind_text16(d->stmt, i + 1, str->utf16(), |
|
384 (str->size()) * sizeof(QChar), SQLITE_STATIC); |
|
385 break; } |
|
386 default: { |
|
387 QString str = value.toString(); |
|
388 // SQLITE_TRANSIENT makes sure that sqlite buffers the data |
|
389 res = sqlite3_bind_text16(d->stmt, i + 1, str.utf16(), |
|
390 (str.size()) * sizeof(QChar), SQLITE_TRANSIENT); |
|
391 break; } |
|
392 } |
|
393 } |
|
394 if (res != SQLITE_OK) { |
|
395 setLastError(qMakeError(d->access, QCoreApplication::translate("QSQLiteResult", |
|
396 "Unable to bind parameters"), QSqlError::StatementError, res)); |
|
397 d->finalize(); |
|
398 return false; |
|
399 } |
|
400 } |
|
401 } else { |
|
402 setLastError(QSqlError(QCoreApplication::translate("QSQLiteResult", |
|
403 "Parameter count mismatch"), QString(), QSqlError::StatementError)); |
|
404 return false; |
|
405 } |
|
406 d->skippedStatus = d->fetchNext(cache(), 0, true); |
|
407 if (lastError().isValid()) { |
|
408 setSelect(false); |
|
409 setActive(false); |
|
410 return false; |
|
411 } |
|
412 setSelect(!d->rInf.isEmpty()); |
|
413 setActive(true); |
|
414 return true; |
|
415 } |
|
416 |
|
417 bool QSQLiteResult::gotoNext(QSqlCachedResult::ValueCache& row, int idx) |
|
418 { |
|
419 return d->fetchNext(row, idx, false); |
|
420 } |
|
421 |
|
422 int QSQLiteResult::size() |
|
423 { |
|
424 return -1; |
|
425 } |
|
426 |
|
427 int QSQLiteResult::numRowsAffected() |
|
428 { |
|
429 return sqlite3_changes(d->access); |
|
430 } |
|
431 |
|
432 QVariant QSQLiteResult::lastInsertId() const |
|
433 { |
|
434 if (isActive()) { |
|
435 qint64 id = sqlite3_last_insert_rowid(d->access); |
|
436 if (id) |
|
437 return id; |
|
438 } |
|
439 return QVariant(); |
|
440 } |
|
441 |
|
442 QSqlRecord QSQLiteResult::record() const |
|
443 { |
|
444 if (!isActive() || !isSelect()) |
|
445 return QSqlRecord(); |
|
446 return d->rInf; |
|
447 } |
|
448 |
|
449 QVariant QSQLiteResult::handle() const |
|
450 { |
|
451 return qVariantFromValue(d->stmt); |
|
452 } |
|
453 |
|
454 ///////////////////////////////////////////////////////// |
|
455 |
|
456 QSQLiteDriver::QSQLiteDriver(QObject * parent) |
|
457 : QSqlDriver(parent) |
|
458 { |
|
459 d = new QSQLiteDriverPrivate(); |
|
460 } |
|
461 |
|
462 QSQLiteDriver::QSQLiteDriver(sqlite3 *connection, QObject *parent) |
|
463 : QSqlDriver(parent) |
|
464 { |
|
465 d = new QSQLiteDriverPrivate(); |
|
466 d->access = connection; |
|
467 setOpen(true); |
|
468 setOpenError(false); |
|
469 } |
|
470 |
|
471 |
|
472 QSQLiteDriver::~QSQLiteDriver() |
|
473 { |
|
474 delete d; |
|
475 } |
|
476 |
|
477 bool QSQLiteDriver::hasFeature(DriverFeature f) const |
|
478 { |
|
479 switch (f) { |
|
480 case BLOB: |
|
481 case Transactions: |
|
482 case Unicode: |
|
483 case LastInsertId: |
|
484 case PreparedQueries: |
|
485 case PositionalPlaceholders: |
|
486 case SimpleLocking: |
|
487 case FinishQuery: |
|
488 case LowPrecisionNumbers: |
|
489 return true; |
|
490 case QuerySize: |
|
491 case NamedPlaceholders: |
|
492 case BatchOperations: |
|
493 case EventNotifications: |
|
494 case MultipleResultSets: |
|
495 return false; |
|
496 } |
|
497 return false; |
|
498 } |
|
499 |
|
500 static int qGetSqliteTimeout(QString opts) |
|
501 { |
|
502 enum { DefaultTimeout = 5000 }; |
|
503 |
|
504 opts.remove(QLatin1Char(' ')); |
|
505 foreach(QString option, opts.split(QLatin1Char(';'))) { |
|
506 if (option.startsWith(QLatin1String("QSQLITE_BUSY_TIMEOUT="))) { |
|
507 bool ok; |
|
508 int nt = option.mid(21).toInt(&ok); |
|
509 if (ok) |
|
510 return nt; |
|
511 } |
|
512 } |
|
513 return DefaultTimeout; |
|
514 } |
|
515 |
|
516 static int qGetSqliteOpenMode(QString opts) |
|
517 { |
|
518 opts.remove(QLatin1Char(' ')); |
|
519 foreach(QString option, opts.split(QLatin1Char(';'))) { |
|
520 if (option == QLatin1String("QSQLITE_OPEN_READONLY")) |
|
521 return SQLITE_OPEN_READONLY; |
|
522 } |
|
523 return SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE; |
|
524 } |
|
525 |
|
526 /* |
|
527 SQLite dbs have no user name, passwords, hosts or ports. |
|
528 just file names. |
|
529 */ |
|
530 bool QSQLiteDriver::open(const QString & db, const QString &, const QString &, const QString &, int, const QString &conOpts) |
|
531 { |
|
532 if (isOpen()) |
|
533 close(); |
|
534 |
|
535 if (db.isEmpty()) |
|
536 return false; |
|
537 |
|
538 if (sqlite3_open_v2(db.toUtf8().constData(), &d->access, qGetSqliteOpenMode(conOpts), NULL) == SQLITE_OK) { |
|
539 sqlite3_busy_timeout(d->access, qGetSqliteTimeout(conOpts)); |
|
540 setOpen(true); |
|
541 setOpenError(false); |
|
542 return true; |
|
543 } else { |
|
544 setLastError(qMakeError(d->access, tr("Error opening database"), |
|
545 QSqlError::ConnectionError)); |
|
546 setOpenError(true); |
|
547 return false; |
|
548 } |
|
549 } |
|
550 |
|
551 void QSQLiteDriver::close() |
|
552 { |
|
553 if (isOpen()) { |
|
554 if (sqlite3_close(d->access) != SQLITE_OK) |
|
555 setLastError(qMakeError(d->access, tr("Error closing database"), |
|
556 QSqlError::ConnectionError)); |
|
557 d->access = 0; |
|
558 setOpen(false); |
|
559 setOpenError(false); |
|
560 } |
|
561 } |
|
562 |
|
563 QSqlResult *QSQLiteDriver::createResult() const |
|
564 { |
|
565 return new QSQLiteResult(this); |
|
566 } |
|
567 |
|
568 bool QSQLiteDriver::beginTransaction() |
|
569 { |
|
570 if (!isOpen() || isOpenError()) |
|
571 return false; |
|
572 |
|
573 QSqlQuery q(createResult()); |
|
574 if (!q.exec(QLatin1String("BEGIN"))) { |
|
575 setLastError(QSqlError(tr("Unable to begin transaction"), |
|
576 q.lastError().databaseText(), QSqlError::TransactionError)); |
|
577 return false; |
|
578 } |
|
579 |
|
580 return true; |
|
581 } |
|
582 |
|
583 bool QSQLiteDriver::commitTransaction() |
|
584 { |
|
585 if (!isOpen() || isOpenError()) |
|
586 return false; |
|
587 |
|
588 QSqlQuery q(createResult()); |
|
589 if (!q.exec(QLatin1String("COMMIT"))) { |
|
590 setLastError(QSqlError(tr("Unable to commit transaction"), |
|
591 q.lastError().databaseText(), QSqlError::TransactionError)); |
|
592 return false; |
|
593 } |
|
594 |
|
595 return true; |
|
596 } |
|
597 |
|
598 bool QSQLiteDriver::rollbackTransaction() |
|
599 { |
|
600 if (!isOpen() || isOpenError()) |
|
601 return false; |
|
602 |
|
603 QSqlQuery q(createResult()); |
|
604 if (!q.exec(QLatin1String("ROLLBACK"))) { |
|
605 setLastError(QSqlError(tr("Unable to rollback transaction"), |
|
606 q.lastError().databaseText(), QSqlError::TransactionError)); |
|
607 return false; |
|
608 } |
|
609 |
|
610 return true; |
|
611 } |
|
612 |
|
613 QStringList QSQLiteDriver::tables(QSql::TableType type) const |
|
614 { |
|
615 QStringList res; |
|
616 if (!isOpen()) |
|
617 return res; |
|
618 |
|
619 QSqlQuery q(createResult()); |
|
620 q.setForwardOnly(true); |
|
621 |
|
622 QString sql = QLatin1String("SELECT name FROM sqlite_master WHERE %1 " |
|
623 "UNION ALL SELECT name FROM sqlite_temp_master WHERE %1"); |
|
624 if ((type & QSql::Tables) && (type & QSql::Views)) |
|
625 sql = sql.arg(QLatin1String("type='table' OR type='view'")); |
|
626 else if (type & QSql::Tables) |
|
627 sql = sql.arg(QLatin1String("type='table'")); |
|
628 else if (type & QSql::Views) |
|
629 sql = sql.arg(QLatin1String("type='view'")); |
|
630 else |
|
631 sql.clear(); |
|
632 |
|
633 if (!sql.isEmpty() && q.exec(sql)) { |
|
634 while(q.next()) |
|
635 res.append(q.value(0).toString()); |
|
636 } |
|
637 |
|
638 if (type & QSql::SystemTables) { |
|
639 // there are no internal tables beside this one: |
|
640 res.append(QLatin1String("sqlite_master")); |
|
641 } |
|
642 |
|
643 return res; |
|
644 } |
|
645 |
|
646 static QSqlIndex qGetTableInfo(QSqlQuery &q, const QString &tableName, bool onlyPIndex = false) |
|
647 { |
|
648 QString schema; |
|
649 QString table(tableName); |
|
650 int indexOfSeparator = tableName.indexOf(QLatin1Char('.')); |
|
651 if (indexOfSeparator > -1) { |
|
652 schema = tableName.left(indexOfSeparator).append(QLatin1Char('.')); |
|
653 table = tableName.mid(indexOfSeparator + 1); |
|
654 } |
|
655 q.exec(QLatin1String("PRAGMA ") + schema + QLatin1String("table_info (") + _q_escapeIdentifier(table) + QLatin1String(")")); |
|
656 |
|
657 QSqlIndex ind; |
|
658 while (q.next()) { |
|
659 bool isPk = q.value(5).toInt(); |
|
660 if (onlyPIndex && !isPk) |
|
661 continue; |
|
662 QString typeName = q.value(2).toString().toLower(); |
|
663 QSqlField fld(q.value(1).toString(), qGetColumnType(typeName)); |
|
664 if (isPk && (typeName == QLatin1String("integer"))) |
|
665 // INTEGER PRIMARY KEY fields are auto-generated in sqlite |
|
666 // INT PRIMARY KEY is not the same as INTEGER PRIMARY KEY! |
|
667 fld.setAutoValue(true); |
|
668 fld.setRequired(q.value(3).toInt() != 0); |
|
669 fld.setDefaultValue(q.value(4)); |
|
670 ind.append(fld); |
|
671 } |
|
672 return ind; |
|
673 } |
|
674 |
|
675 QSqlIndex QSQLiteDriver::primaryIndex(const QString &tblname) const |
|
676 { |
|
677 if (!isOpen()) |
|
678 return QSqlIndex(); |
|
679 |
|
680 QString table = tblname; |
|
681 if (isIdentifierEscaped(table, QSqlDriver::TableName)) |
|
682 table = stripDelimiters(table, QSqlDriver::TableName); |
|
683 |
|
684 QSqlQuery q(createResult()); |
|
685 q.setForwardOnly(true); |
|
686 return qGetTableInfo(q, table, true); |
|
687 } |
|
688 |
|
689 QSqlRecord QSQLiteDriver::record(const QString &tbl) const |
|
690 { |
|
691 if (!isOpen()) |
|
692 return QSqlRecord(); |
|
693 |
|
694 QString table = tbl; |
|
695 if (isIdentifierEscaped(table, QSqlDriver::TableName)) |
|
696 table = stripDelimiters(table, QSqlDriver::TableName); |
|
697 |
|
698 QSqlQuery q(createResult()); |
|
699 q.setForwardOnly(true); |
|
700 return qGetTableInfo(q, table); |
|
701 } |
|
702 |
|
703 QVariant QSQLiteDriver::handle() const |
|
704 { |
|
705 return qVariantFromValue(d->access); |
|
706 } |
|
707 |
|
708 QString QSQLiteDriver::escapeIdentifier(const QString &identifier, IdentifierType type) const |
|
709 { |
|
710 Q_UNUSED(type); |
|
711 return _q_escapeIdentifier(identifier); |
|
712 } |
|
713 |
|
714 QT_END_NAMESPACE |