src/sql/drivers/sqlite/qsql_sqlite.cpp
changeset 7 f7bc934e204c
parent 3 41300fa6a67c
child 23 89e065397ea6
equal deleted inserted replaced
3:41300fa6a67c 7:f7bc934e204c
     1 /****************************************************************************
     1 /****************************************************************************
     2 **
     2 **
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
     3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
     4 ** All rights reserved.
     4 ** All rights reserved.
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
     6 **
     6 **
     7 ** This file is part of the QtSql module of the Qt Toolkit.
     7 ** This file is part of the QtSql module of the Qt Toolkit.
     8 **
     8 **
   243                 break;
   243                 break;
   244             case SQLITE_NULL:
   244             case SQLITE_NULL:
   245                 values[i + idx] = QVariant(QVariant::String);
   245                 values[i + idx] = QVariant(QVariant::String);
   246                 break;
   246                 break;
   247             default:
   247             default:
   248                 values[i + idx] = QString::fromUtf16(static_cast<const ushort *>(
   248                 values[i + idx] = QString(reinterpret_cast<const QChar *>(
   249                             sqlite3_column_text16(stmt, i)),
   249                             sqlite3_column_text16(stmt, i)),
   250                             sqlite3_column_bytes16(stmt, i) / sizeof(ushort));
   250                             sqlite3_column_bytes16(stmt, i) / sizeof(QChar));
   251                 break;
   251                 break;
   252             }
   252             }
   253         }
   253         }
   254         return true;
   254         return true;
   255     case SQLITE_DONE:
   255     case SQLITE_DONE:
   498         return false;
   498         return false;
   499     }
   499     }
   500     return false;
   500     return false;
   501 }
   501 }
   502 
   502 
   503 static int qGetSqliteTimeout(QString opts)
       
   504 {
       
   505     enum { DefaultTimeout = 5000 };
       
   506 
       
   507     opts.remove(QLatin1Char(' '));
       
   508     foreach(QString option, opts.split(QLatin1Char(';'))) {
       
   509         if (option.startsWith(QLatin1String("QSQLITE_BUSY_TIMEOUT="))) {
       
   510             bool ok;
       
   511             int nt = option.mid(21).toInt(&ok);
       
   512             if (ok)
       
   513                 return nt;
       
   514         }
       
   515     }
       
   516     return DefaultTimeout;
       
   517 }
       
   518 
       
   519 static int qGetSqliteOpenMode(QString opts)
       
   520 {
       
   521     opts.remove(QLatin1Char(' '));
       
   522     foreach(QString option, opts.split(QLatin1Char(';'))) {
       
   523         if (option == QLatin1String("QSQLITE_OPEN_READONLY"))
       
   524                 return SQLITE_OPEN_READONLY;
       
   525     }
       
   526     return SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE;
       
   527 }
       
   528 
       
   529 /*
   503 /*
   530    SQLite dbs have no user name, passwords, hosts or ports.
   504    SQLite dbs have no user name, passwords, hosts or ports.
   531    just file names.
   505    just file names.
   532 */
   506 */
   533 bool QSQLiteDriver::open(const QString & db, const QString &, const QString &, const QString &, int, const QString &conOpts)
   507 bool QSQLiteDriver::open(const QString & db, const QString &, const QString &, const QString &, int, const QString &conOpts)
   535     if (isOpen())
   509     if (isOpen())
   536         close();
   510         close();
   537 
   511 
   538     if (db.isEmpty())
   512     if (db.isEmpty())
   539         return false;
   513         return false;
   540 
   514     bool sharedCache = false;
   541     if (sqlite3_open_v2(db.toUtf8().constData(), &d->access, qGetSqliteOpenMode(conOpts), NULL) == SQLITE_OK) {
   515     int openMode = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, timeOut=5000;
   542         sqlite3_busy_timeout(d->access, qGetSqliteTimeout(conOpts));
   516     QStringList opts=QString(conOpts).remove(QLatin1Char(' ')).split(QLatin1Char(';'));
       
   517     foreach(const QString &option, opts) {
       
   518         if (option.startsWith(QLatin1String("QSQLITE_BUSY_TIMEOUT="))) {
       
   519             bool ok;
       
   520             int nt = option.mid(21).toInt(&ok);
       
   521             if (ok)
       
   522                 timeOut = nt;
       
   523         }
       
   524         if (option == QLatin1String("QSQLITE_OPEN_READONLY"))
       
   525             openMode = SQLITE_OPEN_READONLY;
       
   526         if (option == QLatin1String("QSQLITE_ENABLE_SHARED_CACHE"))
       
   527             sharedCache = true;
       
   528     }
       
   529 
       
   530     sqlite3_enable_shared_cache(sharedCache);
       
   531 
       
   532     if (sqlite3_open_v2(db.toUtf8().constData(), &d->access, openMode, NULL) == SQLITE_OK) {
       
   533         sqlite3_busy_timeout(d->access, timeOut);
   543         setOpen(true);
   534         setOpen(true);
   544         setOpenError(false);
   535         setOpenError(false);
   545         return true;
   536         return true;
   546     } else {
   537     } else {
   547         setLastError(qMakeError(d->access, tr("Error opening database"),
   538         setLastError(qMakeError(d->access, tr("Error opening database"),