src/sql/drivers/sqlite/qsql_sqlite.cpp
branchRCL_3
changeset 8 3f74d0d4af4c
parent 5 d3bac044e0f0
child 14 c0432d11811c
equal deleted inserted replaced
6:dee5afe5301f 8:3f74d0d4af4c
   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"),