/*!
    \overload
    \since 5.0

    Returns \c true if the model contains modified values that have not been
    committed to the datase, otherwise false.
*/
bool QSqlTableModel::isDirty() const
{
    Q_D(const QSqlTableModel);
    QSqlTableModelPrivate::CacheMap::ConstIterator i = d->cache.constBegin();
    const QSqlTableModelPrivate::CacheMap::ConstIterator e = d->cache.constEnd();
    for (; i != e; i++)
        if (!i.value().submitted())
            return true;
    return false;
}
Exemple #2
0
/*!
    Returns the index of the value in the database result set for the
    given \a item in the model.

    The return value is identical to \a item if no columns or rows
    have been inserted, removed, or moved around.

    Returns an invalid model index if \a item is out of bounds or if
    \a item does not point to a value in the result set.

    \sa QSqlQueryModel::indexInQuery()
*/
QModelIndex QSqlTableModel::indexInQuery(const QModelIndex &item) const
{
    Q_D(const QSqlTableModel);
    const QModelIndex it = QSqlQueryModel::indexInQuery(item);
    if (d->strategy == OnManualSubmit) {
        int rowOffset = 0;
        QSqlTableModelPrivate::CacheMap::ConstIterator i = d->cache.constBegin();
        while (i != d->cache.constEnd() && i.key() <= it.row()) {
            if (i.value().op == QSqlTableModelPrivate::Insert)
                ++rowOffset;
            ++i;
        }
        return createIndex(it.row() - rowOffset, it.column(), it.internalPointer());
    } else {
        if (d->insertIndex >= 0 && it.row() >= d->insertIndex)
            return createIndex(it.row() - 1, it.column(), it.internalPointer());
    }
    return it;
}
Exemple #3
0
/*! \reimp
*/
int QSqlTableModel::rowCount(const QModelIndex &parent) const
{
    Q_D(const QSqlTableModel);

    if (parent.isValid())
        return 0;

    int rc = QSqlQueryModel::rowCount();
    if (d->strategy == OnManualSubmit) {
        for (QSqlTableModelPrivate::CacheMap::ConstIterator it = d->cache.constBegin();
             it != d->cache.constEnd(); ++it) {
             if (it.value().op == QSqlTableModelPrivate::Insert)
                 ++rc;
        }
    } else if (d->insertIndex >= 0) {
        ++rc;
    }
    return rc;
}
Exemple #4
0
/*!
    Submits all pending changes and returns true on success.
    Returns false on error, detailed error information can be
    obtained with lastError().

    On success the model will be repopulated. Any views 
    presenting it will lose their selections.

    Note: In OnManualSubmit mode, already submitted changes won't
    be cleared from the cache when submitAll() fails. This allows
    transactions to be rolled back and resubmitted again without
    losing data.

    \sa revertAll(), lastError()
*/
bool QSqlTableModel::submitAll()
{
    Q_D(QSqlTableModel);

    switch (d->strategy) {
    case OnFieldChange:
        if (d->insertIndex == -1)
            return true;
        // else fall through
    case OnRowChange:
        if (d->editBuffer.isEmpty())
            return true;
        if (d->insertIndex != -1) {
            if (!insertRowIntoTable(d->editBuffer))
                return false;
            d->bottom = d->bottom.sibling(d->bottom.row() + 1, d->bottom.column());
        } else {
            if (!updateRowInTable(d->editIndex, d->editBuffer))
                return false;
        }
        d->clearEditBuffer();
        d->editIndex = -1;
        d->insertIndex = -1;
        return select();
    case OnManualSubmit:
        for (QSqlTableModelPrivate::CacheMap::ConstIterator it = d->cache.constBegin();
             it != d->cache.constEnd(); ++it) {
            switch (it.value().op) {
            case QSqlTableModelPrivate::Insert:
                if (!insertRowIntoTable(it.value().rec))
                    return false;
                d->bottom = d->bottom.sibling(d->bottom.row() + 1, d->bottom.column());
                break;
            case QSqlTableModelPrivate::Update:
                if (!updateRowInTable(it.key(), it.value().rec))
                    return false;
                break;
            case QSqlTableModelPrivate::Delete:
                if (!deleteRowFromTable(it.key()))
                    return false;
                break;
            case QSqlTableModelPrivate::None:
                Q_ASSERT_X(false, "QSqlTableModel::submitAll()", "Invalid cache operation");
                break;
            }
        }
        d->clearCache();
        return select();
    }
    return false;
}