⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 q3sqlcursor.cpp

📁 qt-x11-opensource-src-4.1.4.tar.gz源码
💻 CPP
📖 第 1 页 / 共 4 页
字号:
{    return d->ftr;}/*!    Sets the name of the cursor to \a name. If \a autopopulate is true    (the default), the \a name must correspond to a valid table or    view name in the database. Also, note that all references to the    cursor edit buffer become invalidated when fields are    auto-populated. See the Q3SqlCursor constructor documentation for    more information.*/void Q3SqlCursor::setName(const QString& name, bool autopopulate){    d->nm = name;    if (autopopulate) {        if (driver()) {            d->infoBuffer = driver()->record(name);            *this = d->infoBuffer.toRecord();            d->editBuffer = *this;            d->priIndx = driver()->primaryIndex(name);        }        if (isEmpty())            qWarning("Q3SqlCursor::setName: unable to build record, does '%s' exist?", name.latin1());    }}/*!    Returns the name of the cursor.*/QString Q3SqlCursor::name() const{    return d->nm;}/*! \internal*/QString Q3SqlCursor::toString(const QString& prefix, const QString& sep) const{    QString pflist;    QString pfix =  prefix.isEmpty() ? prefix : prefix + ".";    bool comma = false;    for (int i = 0; i < count(); ++i) {        const QString fname = fieldName(i);        if (isGenerated(i)) {            if(comma)                pflist += sep + " ";            pflist += pfix + fname;            comma = true;        }    }    return pflist;}/*!  \internal  Assigns the record \a list.*/QSqlRecord & Q3SqlCursor::operator=(const QSqlRecord & list){    return QSqlRecord::operator=(list);}/*!    Append a copy of field \a fieldInfo to the end of the cursor. Note    that all references to the cursor edit buffer become invalidated.*/void Q3SqlCursor::append(const Q3SqlFieldInfo& fieldInfo){    d->editBuffer.append(fieldInfo.toField());    d->infoBuffer.append(fieldInfo);    QSqlRecord::append(fieldInfo.toField());}/*!    Removes all fields from the cursor. Note that all references to    the cursor edit buffer become invalidated.*/void Q3SqlCursor::clear(){    d->editBuffer.clear();    d->infoBuffer.clear();    QSqlRecord::clear();}/*!    Insert a copy of \a fieldInfo at position \a pos. If a field    already exists at \a pos, it is removed. Note that all references    to the cursor edit buffer become invalidated.*/void  Q3SqlCursor::insert(int pos, const Q3SqlFieldInfo& fieldInfo){    d->editBuffer.replace(pos, fieldInfo.toField());    d->infoBuffer[pos] = fieldInfo;    QSqlRecord::replace(pos, fieldInfo.toField());}/*!    Removes the field at \a pos. If \a pos does not exist, nothing    happens. Note that all references to the cursor edit buffer become    invalidated.*/void Q3SqlCursor::remove(int pos){    d->editBuffer.remove(pos);    d->infoBuffer[pos] = Q3SqlFieldInfo();    QSqlRecord::remove(pos);}/*!    Sets the generated flag for the field \a name to \a generated. If    the field does not exist, nothing happens. Only fields that have    \a generated set to true are included in the SQL that is    generated by insert(), update() or del().*/void Q3SqlCursor::setGenerated(const QString& name, bool generated){    int pos = indexOf(name);    if (pos == -1)        return;    QSqlRecord::setGenerated(name, generated);    d->editBuffer.setGenerated(name, generated);    d->infoBuffer[pos].setGenerated(generated);}/*!    \overload    Sets the generated flag for the field \a i to \a generated.*/void Q3SqlCursor::setGenerated(int i, bool generated){    if (i < 0 || i >= (int)d->infoBuffer.count())        return;    QSqlRecord::setGenerated(i, generated);    d->editBuffer.setGenerated(i, generated);    d->infoBuffer[i].setGenerated(generated);}/*!    Returns the primary index associated with the cursor as defined in    the database, or an empty index if there is no primary index. If    \a setFromCursor is true (the default), the index fields are    populated with the corresponding values in the cursor's current    record.*/QSqlIndex Q3SqlCursor::primaryIndex(bool setFromCursor) const{    if (setFromCursor) {        for (int i = 0; i < d->priIndx.count(); ++i) {            const QString fn = d->priIndx.fieldName(i);            if (contains(fn))                d->priIndx.setValue(i, QSqlRecord::value(fn));        }    }    return d->priIndx;}/*!    Sets the primary index associated with the cursor to the index \a    idx. Note that this index must contain a field or set of fields    which identify a unique record within the underlying database    table or view so that update() and del() will execute as expected.    \sa update() del()*/void Q3SqlCursor::setPrimaryIndex(const QSqlIndex& idx){    d->priIndx = idx;}/*!    Returns an index composed of \a fieldNames, all in ASCending    order. Note that all field names must exist in the cursor,    otherwise an empty index is returned.    \sa QSqlIndex*/QSqlIndex Q3SqlCursor::index(const QStringList& fieldNames) const{    QSqlIndex idx;    for (QStringList::ConstIterator it = fieldNames.begin(); it != fieldNames.end(); ++it) {        QSqlField f = field((*it));        if (!f.isValid()) { /* all fields must exist */            idx.clear();            break;        }        idx.append(f);    }    return idx;}/*!    \overload    Returns an index based on \a fieldName.*/QSqlIndex Q3SqlCursor::index(const QString& fieldName) const{    QStringList fl(fieldName);    return index(fl);}/*!    Selects all fields in the cursor from the database matching the    filter criteria \a filter. The data is returned in the order    specified by the index \a sort. Returns true if the data was    successfully selected; otherwise returns false.    The \a filter is a string containing a SQL \c WHERE clause but    without the 'WHERE' keyword. The cursor is initially positioned at    an invalid row after this function is called. To move to a valid    row, use seek(), first(), last(), previous() or next().    Example:    \code    Q3SqlCursor cur("Employee"); // Use the Employee table or view    cur.select("deptno=10"); // select all records in department 10    while(cur.next()) {        ... // process data    }    ...    // select records in other departments, ordered by department number    cur.select("deptno>10", cur.index("deptno"));    ...    \endcode    The filter will apply to any subsequent select() calls that do not    explicitly specify another filter. Similarly the sort will apply    to any subsequent select() calls that do not explicitly specify    another sort.    \code    Q3SqlCursor cur("Employee");    cur.select("deptno=10"); // select all records in department 10    while(cur.next()) {        ... // process data    }    ...    cur.select(); // re-selects all records in department 10    ...    \endcode*/bool Q3SqlCursor::select(const QString & filter, const QSqlIndex & sort){    QString fieldList = toString(d->nm);    if (fieldList.isEmpty())        return false;    QString str= "select " + fieldList;    str += " from " + d->nm;    if (!filter.isEmpty()) {        d->ftr = filter;        str += " where " + filter;    } else        d->ftr = QString();    if (sort.count() > 0)        str += " order by " + sort.toString(d->nm);    d->srt = sort;    return exec(str);}/*!    \overload    Selects all fields in the cursor from the database. The rows are    returned in the order specified by the last call to setSort() or    the last call to select() that specified a sort, whichever is the    most recent. If there is no current sort, the order in which the    rows are returned is undefined. The records are filtered according    to the filter specified by the last call to setFilter() or the    last call to select() that specified a filter, whichever is the    most recent. If there is no current filter, all records are    returned. The cursor is initially positioned at an invalid row. To    move to a valid row, use seek(), first(), last(), previous() or    next().    \sa setSort() setFilter()*/bool Q3SqlCursor::select(){    return select(filter(), sort());}/*!    \overload    Selects all fields in the cursor from the database. The data is    returned in the order specified by the index \a sort. The records    are filtered according to the filter specified by the last call to    setFilter() or the last call to select() that specified a filter,    whichever is the most recent. The cursor is initially positioned    at an invalid row. To move to a valid row, use seek(), first(),    last(), previous() or next().*/bool Q3SqlCursor::select(const QSqlIndex& sort){    return select(filter(), sort);}/*!    \overload    Selects all fields in the cursor matching the filter index \a    filter. The data is returned in the order specified by the index    \a sort. The \a filter index works by constructing a WHERE clause    using the names of the fields from the \a filter and their values    from the current cursor record. The cursor is initially positioned    at an invalid row. To move to a valid row, use seek(), first(),    last(), previous() or next(). This function is useful, for example,    for retrieving data based upon a table's primary index:    \code    Q3SqlCursor cur("Employee");    QSqlIndex pk = cur.primaryIndex();    cur.setValue("id", 10);    cur.select(pk, pk); // generates "SELECT ... FROM Employee WHERE id=10 ORDER BY id"    ...    \endcode    In this example the QSqlIndex, pk, is used for two different    purposes. When used as the filter (first) argument, the field    names it contains are used to construct the WHERE clause, each set    to the current cursor value, \c{WHERE id=10}, in this case. When    used as the sort (second) argument the field names it contains are    used for the ORDER BY clause, \c{ORDER BY id} in this example.*/bool Q3SqlCursor::select(const QSqlIndex & filter, const QSqlIndex & sort){    return select(toString(filter, this, d->nm, "=", "and"), sort);}/*!    Sets the cursor mode to \a mode. This value can be an OR'ed    combination of \l Q3SqlCursor::Mode values. The default mode for a    cursor is Q3SqlCursor::Writable.    \code    Q3SqlCursor cur("Employee");    cur.setMode(Q3SqlCursor::Writable); // allow insert/update/delete    ...    cur.setMode(Q3SqlCursor::Insert | Q3SqlCursor::Update); // allow inserts and updates only    ...    cur.setMode(Q3SqlCursor::ReadOnly); // no inserts/updates/deletes allowed    \endcode*/void Q3SqlCursor::setMode(int mode){    d->md = mode;}/*!    Returns the current cursor mode.    \sa setMode()*/int Q3SqlCursor::mode() const{    return d->md;}/*!    Sets field \a name to \a calculated. If the field \a name does not    exist, nothing happens. The value of a calculated field is set by    the calculateField() virtual function which you must reimplement    (or the field value will be an invalid QVariant). Calculated    fields do not appear in generated SQL statements sent to the    database.    \sa calculateField()*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -