📄 q3sqlcursor.cpp
字号:
fields do not appear in generated SQL statements sent to the database. \sa calculateField()*/void Q3SqlCursor::setCalculated(const QString& name, bool calculated){ int pos = indexOf(name); if (pos < 0) return; d->infoBuffer[pos].setCalculated(calculated); if (calculated) setGenerated(pos, false);}/*! Returns true if the field \a name exists and is calculated; otherwise returns false. \sa setCalculated()*/bool Q3SqlCursor::isCalculated(const QString& name) const{ int pos = indexOf(name); if (pos < 0) return false; return d->infoBuffer[pos].isCalculated();}/*! Sets field \a{name}'s trimmed status to \a trim. If the field \a name does not exist, nothing happens. When a trimmed field of type string is read from the database any trailing (right-most) spaces are removed. \sa isTrimmed() QVariant*/void Q3SqlCursor::setTrimmed(const QString& name, bool trim){ int pos = indexOf(name); if (pos < 0) return; d->infoBuffer[pos].setTrim(trim);}/*! Returns true if the field \a name exists and is trimmed; otherwise returns false. When a trimmed field of type string or cstring is read from the database any trailing (right-most) spaces are removed. \sa setTrimmed()*/bool Q3SqlCursor::isTrimmed(const QString& name) const{ int pos = indexOf(name); if (pos < 0) return false; return d->infoBuffer[pos].isTrim();}/*! Returns true if the cursor is read-only; otherwise returns false. The default is false. Read-only cursors cannot be edited using insert(), update() or del(). \sa setMode()*/bool Q3SqlCursor::isReadOnly() const{ return d->md == 0;}/*! Returns true if the cursor will perform inserts; otherwise returns false. \sa setMode()*/bool Q3SqlCursor::canInsert() const{ return ((d->md & Insert) == Insert) ;}/*! Returns true if the cursor will perform updates; otherwise returns false. \sa setMode()*/bool Q3SqlCursor::canUpdate() const{ return ((d->md & Update) == Update) ;}/*! Returns true if the cursor will perform deletes; otherwise returns false. \sa setMode()*/bool Q3SqlCursor::canDelete() const{ return ((d->md & Delete) == Delete) ;}/*! \overload Returns a formatted string composed of the \a prefix (e.g. table or view name), ".", the \a field name, the \a fieldSep and the field value. If the \a prefix is empty then the string will begin with the \a field name. This function is useful for generating SQL statements.*/QString Q3SqlCursor::toString(const QString& prefix, QSqlField* field, const QString& fieldSep) const{ QString f; if (field && driver()) { f = (prefix.length() > 0 ? prefix + QLatin1Char('.') : QString()) + field->name(); f += QLatin1Char(' ') + fieldSep + QLatin1Char(' '); if (field->isNull()) { f += QLatin1String("NULL"); } else { f += driver()->formatValue(field); } } return f;}/*! Returns a formatted string composed of all the fields in \a rec. Each field is composed of the \a prefix (e.g. table or view name), ".", the field name, the \a fieldSep and the field value. If the \a prefix is empty then each field will begin with the field name. The fields are then joined together separated by \a sep. Fields where isGenerated() returns false are not included. This function is useful for generating SQL statements.*/QString Q3SqlCursor::toString(QSqlRecord* rec, const QString& prefix, const QString& fieldSep, const QString& sep) const{ static QString blank(QLatin1Char(' ')); QString filter; bool separator = false; for (int j = 0; j < count(); ++j) { QSqlField f = rec->field(j); if (rec->isGenerated(j)) { if (separator) filter += sep + blank; filter += toString(prefix, &f, fieldSep); filter += blank; separator = true; } } return filter;}/*! \overload Returns a formatted string composed of all the fields in the index \a i. Each field is composed of the \a prefix (e.g. table or view name), ".", the field name, the \a fieldSep and the field value. If the \a prefix is empty then each field will begin with the field name. The field values are taken from \a rec. The fields are then joined together separated by \a sep. Fields where isGenerated() returns false are ignored. This function is useful for generating SQL statements.*/QString Q3SqlCursor::toString(const QSqlIndex& i, QSqlRecord* rec, const QString& prefix, const QString& fieldSep, const QString& sep) const{ QString filter; bool separator = false; for(int j = 0; j < i.count(); ++j){ if (rec->isGenerated(j)) { if(separator) { filter += QLatin1Char(' ') + sep + QLatin1Char(' ') ; } QString fn = i.fieldName(j); QSqlField f = rec->field(fn); filter += toString(prefix, &f, fieldSep); separator = true; } } return filter;}/*! Inserts the current contents of the cursor's edit record buffer into the database, if the cursor allows inserts. Returns the number of rows affected by the insert. For error information, use lastError(). If \a invalidate is true (the default), the cursor will no longer be positioned on a valid record and can no longer be navigated. A new select() call must be made before navigating to a valid record. \code Q3SqlCursor cur("prices"); QSqlRecord *buffer = cur.primeInsert(); buffer->setValue("id", 53981); buffer->setValue("name", "Thingy"); buffer->setValue("price", 105.75); cur.insert(); \endcode In the above example, a cursor is created on the 'prices' table and a pointer to the insert buffer is acquired using primeInsert(). Each field's value is set to the desired value and then insert() is called to insert the data into 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 setMode() lastError()*/int Q3SqlCursor::insert(bool invalidate){ if ((d->md & Insert) != Insert || !driver()) return false; int k = d->editBuffer.count(); if (k == 0) return 0; QString fList; QString vList; bool comma = false; // use a prepared query if the driver supports it if (driver()->hasFeature(QSqlDriver::PreparedQueries)) { 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(','); vList += QLatin1Char(','); } fList += driver()->escapeIdentifier(f.name(), QSqlDriver::FieldName); vList += (oraStyle == true) ? QLatin1String(":f") + QString::number(cnt) : QString(QLatin1Char('?')); cnt++; comma = true; } } if (!comma) { return 0; } QString str; str.append(QLatin1String("insert into ")).append(name()) .append(QLatin1String(" (")).append(fList) .append(QLatin1String(") values (")).append(vList). append(QLatin1Char(')')); return applyPrepared(str, invalidate); } else { for(int j = 0; j < k; ++j) { QSqlField f = d->editBuffer.field(j); if (d->editBuffer.isGenerated(j)) { if (comma) { fList += QLatin1Char(','); vList += QLatin1Char(','); } fList += driver()->escapeIdentifier(f.name(), QSqlDriver::FieldName); vList += driver()->formatValue(&f); comma = true; } } if (!comma) { // no valid fields found return 0; } QString str; str.append(QLatin1String("insert into ")).append(name()).append(QLatin1String(" (")) .append(fList).append(QLatin1String(") values (")).append(vList). append (QLatin1String(")")); return apply(str, invalidate); }}/*! Returns the current internal edit buffer. If \a copy is true (the default is false), the current cursor field values are first copied into the edit buffer. The edit buffer is valid as long as the cursor remains valid. The cursor retains ownership of the returned pointer, so it must not be deleted or modified. \sa primeInsert(), primeUpdate() primeDelete()*/QSqlRecord* Q3SqlCursor::editBuffer(bool copy){ sync(); if (copy) { for(int i = 0; i < d->editBuffer.count(); i++) { if (QSqlRecord::isNull(i)) { d->editBuffer.setNull(i); } else { d->editBuffer.setValue(i, value(i)); } } } return &d->editBuffer;}/*! This function primes the edit buffer's field values for update and returns the edit buffer. The default implementation copies the field values from the current cursor record into the edit buffer (therefore, this function is equivalent to calling editBuffer( true)). The cursor retains ownership of the returned pointer, so it must not be deleted or modified. \sa editBuffer() update()*/QSqlRecord* Q3SqlCursor::primeUpdate(){ // memorize the primary keys as they were before the user changed the values in editBuffer QSqlRecord* buf = editBuffer(true); QSqlIndex idx = primaryIndex(false); if (!idx.isEmpty()) d->editIndex = toString(idx, buf, d->nm, QString(QLatin1Char('=')), QLatin1String("and")); else d->editIndex = qWhereClause(buf, d->nm, QLatin1String("and"), driver()); return buf;}/*! This function primes the edit buffer's field values for delete and returns the edit buffer. The default implementation copies the field values from the current cursor record into the edit buffer (therefore, this function is equivalent to calling editBuffer( true)). The cursor retains ownership of the returned pointer, so it must not be deleted or modified. \sa editBuffer() del()*/QSqlRecord* Q3SqlCursor::primeDelete(){ return editBuffer(true);}/*! This function primes the edit buffer's field values for insert and returns the edit buffer. The default implementation clears all field values in the edit buffer. The cursor retains ownership of the returned pointer, so it must not be deleted or modified. \sa editBuffer() insert()*/QSqlRecord* Q3SqlCursor::primeInsert(){ d->editBuffer.clearValues(); return &d->editBuffer;}/*! Updates the database with the current contents of the edit buffer. Returns the number of records which were updated. For error information, use lastError(). Only records which meet the filter criteria specified by the cursor's primary index are updated. If the cursor does not contain a primary index, no update 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=202"); if (cur.next()) { QSqlRecord *buffer = cur.primeUpdate(); double price = buffer->value("price").toDouble(); double newprice = price * 1.05; buffer->setValue("price", newprice);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -