?? qsql_sqlite.cpp
字號(hào):
break; case QVariant::LongLong: res = sqlite3_bind_int64(d->stmt, i + 1, value.toLongLong()); break; case QVariant::String: { // lifetime of string == lifetime of its qvariant const QString *str = static_cast<const QString*>(value.constData()); res = sqlite3_bind_text16(d->stmt, i + 1, str->utf16(), (str->size()) * sizeof(QChar), SQLITE_STATIC); break; } default: { QString str = value.toString(); // SQLITE_TRANSIENT makes sure that sqlite buffers the data res = sqlite3_bind_text16(d->stmt, i + 1, str.utf16(), (str.size()) * sizeof(QChar), SQLITE_TRANSIENT); break; } } } if (res != SQLITE_OK) { setLastError(qMakeError(d->access, QCoreApplication::translate("QSQLiteResult", "Unable to bind parameters"), QSqlError::StatementError, res)); d->finalize(); return false; } } } else { setLastError(QSqlError(QCoreApplication::translate("QSQLiteResult", "Parameter count mismatch"), QString(), QSqlError::StatementError)); return false; } d->skippedStatus = d->fetchNext(cache(), 0, true); if (lastError().isValid()) { setSelect(false); setActive(false); return false; } setSelect(!d->rInf.isEmpty()); setActive(true); return true;}bool QSQLiteResult::gotoNext(QSqlCachedResult::ValueCache& row, int idx){ return d->fetchNext(row, idx, false);}int QSQLiteResult::size(){ return -1;}int QSQLiteResult::numRowsAffected(){ return sqlite3_changes(d->access);}QVariant QSQLiteResult::lastInsertId() const{ if (isActive()) { qint64 id = sqlite3_last_insert_rowid(d->access); if (id) return id; } return QVariant();}QSqlRecord QSQLiteResult::record() const{ if (!isActive() || !isSelect()) return QSqlRecord(); return d->rInf;}QVariant QSQLiteResult::handle() const{ return qVariantFromValue(d->stmt);}/////////////////////////////////////////////////////////QSQLiteDriver::QSQLiteDriver(QObject * parent) : QSqlDriver(parent){ d = new QSQLiteDriverPrivate();}QSQLiteDriver::QSQLiteDriver(sqlite3 *connection, QObject *parent) : QSqlDriver(parent){ d = new QSQLiteDriverPrivate(); d->access = connection; setOpen(true); setOpenError(false);}QSQLiteDriver::~QSQLiteDriver(){ delete d;}bool QSQLiteDriver::hasFeature(DriverFeature f) const{ switch (f) { case BLOB: case Transactions: case Unicode: case LastInsertId: case PreparedQueries: case PositionalPlaceholders: return true; case QuerySize: case NamedPlaceholders: case BatchOperations: return false; } return false;}static int qGetSqliteTimeout(QString opts){ enum { DefaultTimeout = 5000 }; opts.remove(QLatin1Char(' ')); if (opts.startsWith(QLatin1String("QSQLITE_BUSY_TIMEOUT="))) { bool ok; int nt = opts.mid(21).toInt(&ok); if (ok) return nt; } return DefaultTimeout;}/* SQLite dbs have no user name, passwords, hosts or ports. just file names.*/bool QSQLiteDriver::open(const QString & db, const QString &, const QString &, const QString &, int, const QString &conOpts){ if (isOpen()) close(); if (db.isEmpty()) return false; if (sqlite3_open16(db.constData(), &d->access) == SQLITE_OK) { sqlite3_busy_timeout(d->access, qGetSqliteTimeout(conOpts)); setOpen(true); setOpenError(false); return true; } else { setLastError(qMakeError(d->access, tr("Error opening database"), QSqlError::ConnectionError)); setOpenError(true); return false; }}void QSQLiteDriver::close(){ if (isOpen()) { if (sqlite3_close(d->access) != SQLITE_OK) setLastError(qMakeError(d->access, tr("Error closing database"), QSqlError::ConnectionError)); d->access = 0; setOpen(false); setOpenError(false); }}QSqlResult *QSQLiteDriver::createResult() const{ return new QSQLiteResult(this);}bool QSQLiteDriver::beginTransaction(){ if (!isOpen() || isOpenError()) return false; QSqlQuery q(createResult()); if (!q.exec(QLatin1String("BEGIN"))) { setLastError(QSqlError(tr("Unable to begin transaction"), q.lastError().databaseText(), QSqlError::TransactionError)); return false; } return true;}bool QSQLiteDriver::commitTransaction(){ if (!isOpen() || isOpenError()) return false; QSqlQuery q(createResult()); if (!q.exec(QLatin1String("COMMIT"))) { setLastError(QSqlError(tr("Unable to commit transaction"), q.lastError().databaseText(), QSqlError::TransactionError)); return false; } return true;}bool QSQLiteDriver::rollbackTransaction(){ if (!isOpen() || isOpenError()) return false; QSqlQuery q(createResult()); if (!q.exec(QLatin1String("ROLLBACK"))) { setLastError(QSqlError(tr("Unable to roll back transaction"), q.lastError().databaseText(), QSqlError::TransactionError)); return false; } return true;}QStringList QSQLiteDriver::tables(QSql::TableType type) const{ QStringList res; if (!isOpen()) return res; QSqlQuery q(createResult()); q.setForwardOnly(true); QString sql = QLatin1String("SELECT name FROM sqlite_master WHERE %1 " "UNION ALL SELECT name FROM sqlite_temp_master WHERE %1"); if ((type & QSql::Tables) && (type & QSql::Views)) sql = sql.arg(QLatin1String("type='table' OR type='view'")); else if (type & QSql::Tables) sql = sql.arg(QLatin1String("type='table'")); else if (type & QSql::Views) sql = sql.arg(QLatin1String("type='view'")); else sql.clear(); if (!sql.isEmpty() && q.exec(sql)) { while(q.next()) res.append(q.value(0).toString()); } if (type & QSql::SystemTables) { // there are no internal tables beside this one: res.append(QLatin1String("sqlite_master")); } return res;}static QSqlIndex qGetTableInfo(QSqlQuery &q, const QString &tableName, bool onlyPIndex = false){ QSqlIndex ind; q.exec(QLatin1String("PRAGMA table_info ('") + tableName + QLatin1String("')")); while (q.next()) { bool isPk = q.value(5).toInt(); if (onlyPIndex && !isPk) continue; QString typeName = q.value(2).toString().toLower(); QSqlField fld(q.value(1).toString(), qGetColumnType(typeName)); if (isPk && typeName == QLatin1String("integer")) // integer primary key fields are auto-generated in sqlite fld.setAutoValue(true); fld.setRequired(q.value(3).toInt() != 0); fld.setDefaultValue(q.value(4)); ind.append(fld); } return ind;}QSqlIndex QSQLiteDriver::primaryIndex(const QString &tblname) const{ if (!isOpen()) return QSqlIndex(); QSqlQuery q(createResult()); q.setForwardOnly(true); return qGetTableInfo(q, tblname, true);}QSqlRecord QSQLiteDriver::record(const QString &tbl) const{ if (!isOpen()) return QSqlRecord(); QSqlQuery q(createResult()); q.setForwardOnly(true); return qGetTableInfo(q, tbl);}QVariant QSQLiteDriver::handle() const{ return qVariantFromValue(d->access);}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -