📄 q3sqlcursor.cpp
字号:
cur.update(); } \endcode In the above example, a cursor is created on the 'prices' table and is positioned on the record to be updated. Then a pointer to the cursor's edit buffer is acquired using primeUpdate(). A new value is calculated and placed into the edit buffer with the setValue() call. Finally, an update() call is made on the cursor which uses the tables's primary index to update the record in the database with the contents of the cursor's edit buffer. Remember: all edit operations (insert(), update() and delete()) operate on the contents of the cursor edit buffer and not on the contents of the cursor itself. Note that if the primary index does not uniquely distinguish records the database may be changed into an inconsistent state. \sa setMode() lastError()*/int Q3SqlCursor::update(bool invalidate){ if (d->editIndex.isEmpty()) return 0; return update(d->editIndex, invalidate);}/*! \overload Updates the database with the current contents of the cursor edit buffer using the specified \a filter. Returns the number of records which were updated. For error information, use lastError(). Only records which meet the filter criteria are updated, otherwise all records in the table are updated. If \a invalidate is true (the default), the cursor can no longer be navigated. A new select() call must be made before you can move to a valid record. \sa primeUpdate() setMode() lastError()*/int Q3SqlCursor::update(const QString & filter, bool invalidate){ if ((d->md & Update) != Update) { return false; } int k = count(); if (k == 0) { return 0; } // use a prepared query if the driver supports it if (driver()->hasFeature(QSqlDriver::PreparedQueries)) { QString fList; bool comma = false; int cnt = 0; bool oraStyle = driver()->hasFeature(QSqlDriver::NamedPlaceholders); for(int j = 0; j < k; ++j) { QSqlField f = d->editBuffer.field(j); if (d->editBuffer.isGenerated(j)) { if (comma) { fList += QLatin1Char(','); } fList += f.name() + QLatin1String(" = ") + (oraStyle == true ? QLatin1String(":f") + QString::number(cnt) : QString(QLatin1Char('?'))); cnt++; comma = true; } } if (!comma) { return 0; } QString str(QLatin1String("update ") + name() + QLatin1String(" set ") + fList); if (filter.length()) { str+= QLatin1String(" where ") + filter; } return applyPrepared(str, invalidate); } else { QString str = QLatin1String("update ") + name(); str += QLatin1String(" set ") + toString(&d->editBuffer, QString(), QString(QLatin1Char('=')), QString(QLatin1Char(','))); if (filter.length()) { str+= QLatin1String(" where ") + filter; } return apply(str, invalidate); }}/*! Deletes a record from the database using the cursor's primary index and the contents of the cursor edit buffer. Returns the number of records which were deleted. For error information, use lastError(). Only records which meet the filter criteria specified by the cursor's primary index are deleted. If the cursor does not contain a primary index, no delete is performed and 0 is returned. If \a invalidate is true (the default), the current cursor can no longer be navigated. A new select() call must be made before you can move to a valid record. For example: \code Q3SqlCursor cur("prices"); cur.select("id=999"); if (cur.next()) { cur.primeDelete(); cur.del(); } \endcode In the above example, a cursor is created on the 'prices' table and positioned to the record to be deleted. First primeDelete() is called to populate the edit buffer with the current cursor values, e.g. with an id of 999, and then del() is called to actually delete the record from the database. Remember: all edit operations (insert(), update() and delete()) operate on the contents of the cursor edit buffer and not on the contents of the cursor itself. \sa primeDelete() setMode() lastError()*/int Q3SqlCursor::del(bool invalidate){ QSqlIndex idx = primaryIndex(false); if (idx.isEmpty()) return del(qWhereClause(&d->editBuffer, d->nm, QLatin1String("and"), driver()), invalidate); return del(toString(primaryIndex(), &d->editBuffer, d->nm, QString(QLatin1Char('=')), QLatin1String("and")), invalidate);}/*! \overload Deletes the current cursor record from the database using the filter \a filter. Only records which meet the filter criteria are deleted. Returns the number of records which were deleted. If \a invalidate is true (the default), the current cursor can no longer be navigated. A new select() call must be made before you can move to a valid record. For error information, use lastError(). The \a filter is an SQL \c WHERE clause, e.g. \c{id=500}. \sa setMode() lastError()*/int Q3SqlCursor::del(const QString & filter, bool invalidate){ if ((d->md & Delete) != Delete) return 0; int k = count(); if(k == 0) return 0; QString str = QLatin1String("delete from ") + name(); if (filter.length()) str+= QLatin1String(" where ") + filter; return apply(str, invalidate);}/* \internal*/int Q3SqlCursor::apply(const QString& q, bool invalidate){ int ar = 0; if (invalidate) { if (exec(q)) ar = numRowsAffected(); } else if (driver()) { QSqlQuery* sql = d->query(); if (sql && sql->exec(q)) ar = sql->numRowsAffected(); } return ar;}/* \internal*/int Q3SqlCursor::applyPrepared(const QString& q, bool invalidate){ int ar = 0; QSqlQuery* sql = 0; if (invalidate) { sql = (QSqlQuery*)this; d->lastAt = QSql::BeforeFirst; } else { sql = d->query(); } if (!sql) return 0; if (invalidate || sql->lastQuery() != q) { if (!sql->prepare(q)) return 0; } int cnt = 0; int fieldCount = (int)count(); for (int j = 0; j < fieldCount; ++j) { const QSqlField f = d->editBuffer.field(j); if (d->editBuffer.isGenerated(j)) { if (f.type() == QVariant::ByteArray) sql->bindValue(cnt, f.value(), QSql::In | QSql::Binary); else sql->bindValue(cnt, f.value()); cnt++; } } if (sql->exec()) { ar = sql->numRowsAffected(); } return ar;}/*! Executes the SQL query \a sql. Returns true of the cursor is active, otherwise returns false.*/bool Q3SqlCursor::exec(const QString & sql){ d->lastAt = QSql::BeforeFirst; QSqlQuery::exec(sql); return isActive();}/*! Protected virtual function which is called whenever a field needs to be calculated. If calculated fields are being used, derived classes must reimplement this function and return the appropriate value for field \a name. The default implementation returns an invalid QVariant. \sa setCalculated()*/QVariant Q3SqlCursor::calculateField(const QString&){ return QVariant();}/*! \internal Ensure fieldlist is synced with query.*/static QString qTrim(const QString& s){ QString result = s; int end = result.length() - 1; while (end >= 0 && result[end].isSpace()) // skip white space from end end--; result.truncate(end + 1); return result;}/*! \internal */void Q3SqlCursor::sync(){ if (isActive() && isValid() && d->lastAt != at()) { d->lastAt = at(); int i = 0; int j = 0; bool haveCalculatedFields = false; for (; i < count(); ++i) { if (!haveCalculatedFields && d->infoBuffer[i].isCalculated()) { haveCalculatedFields = true; } if (QSqlRecord::isGenerated(i)) { QVariant v = QSqlQuery::value(j); if ((v.type() == QVariant::String) && d->infoBuffer[i].isTrim()) { v = qTrim(v.toString()); } QSqlRecord::setValue(i, v); if (QSqlQuery::isNull(j)) QSqlRecord::field(i).clear(); j++; } } if (haveCalculatedFields) { for (i = 0; i < count(); ++i) { if (d->infoBuffer[i].isCalculated()) QSqlRecord::setValue(i, calculateField(fieldName(i))); } } }}/*! Returns the value of field number \a i.*/QVariant Q3SqlCursor::value(int i) const{ const_cast<Q3SqlCursor *>(this)->sync(); return QSqlRecord::value(i);}/*! \internal cursors should be filled with Q3SqlFieldInfos...*/void Q3SqlCursor::append(const QSqlField& field){ append(Q3SqlFieldInfo(field));}/*! Returns true if the field \a i is NULL or if there is no field at position \a i; otherwise returns false. This is the same as calling QSqlRecord::isNull(\a i)*/bool Q3SqlCursor::isNull(int i) const{ const_cast<Q3SqlCursor *>(this)->sync(); return QSqlRecord::isNull(i);}/*! \overload Returns true if the field called \a name is NULL or if there is no field called \a name; otherwise returns false. This is the same as calling QSqlRecord::isNull(\a name)*/bool Q3SqlCursor::isNull(const QString& name) const{ const_cast<Q3SqlCursor *>(this)->sync(); return QSqlRecord::isNull(name);}/*! \internal */void Q3SqlCursor::setValue(int i, const QVariant& val){ sync();#ifdef QT_DEBUG qDebug("Q3SqlCursor::setValue(): This will not affect actual database values. Use primeInsert(), primeUpdate() or primeDelete().");#endif QSqlRecord::setValue(i, val);}/*! \internal */bool Q3SqlCursor::seek(int i, bool relative){ bool res = QSqlQuery::seek(i, relative); sync(); return res;}/*! \internal */bool Q3SqlCursor::next(){ bool res = QSqlQuery::next(); sync(); return res;}/*! \fn Q3SqlCursor::previous() \internal*//*! \internal */bool Q3SqlCursor::prev(){ bool res = QSqlQuery::previous(); sync(); return res;}/*! \internal */bool Q3SqlCursor::first(){ bool res = QSqlQuery::first(); sync(); return res;}/*! \internal */bool Q3SqlCursor::last(){ bool res = QSqlQuery::last(); sync(); return res;}#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -