Пример #1
0
/*!
    Removes \a count rows starting at \a row. Since this model
    does not support hierarchical structures, \a parent must be
    an invalid model index.

    Emits the beforeDelete() signal before a row is deleted. When
    the edit strategy is OnManualSubmit signal emission is delayed
    until submitAll() is called.

    Returns true if all rows could be removed; otherwise returns
    false. Detailed error information can be retrieved using
    lastError().

    \sa removeColumns(), insertRows()
*/
bool QSqlTableModel::removeRows(int row, int count, const QModelIndex &parent)
{
    Q_D(QSqlTableModel);
    if (parent.isValid() || row < 0 || count <= 0)
        return false;

    int i;
    switch (d->strategy) {
    case OnFieldChange:
    case OnRowChange:
        for (i = 0; i < count; ++i) {
            if (row + i == d->insertIndex)
                d->revertInsertedRow();
            else if (!deleteRowFromTable(row + i))
                return false;
        }
        select();
        break;
    case OnManualSubmit:
        for (i = 0; i < count; ++i) {
            int idx = row + i;
            if (idx >= rowCount())
                return false;
            if (d->cache.value(idx).op == QSqlTableModelPrivate::Insert)
                revertRow(idx);
            else {
                d->cache[idx].op = QSqlTableModelPrivate::Delete;
                emit headerDataChanged(Qt::Vertical, idx, idx);
            }
        }
        break;
    }
    return true;
}
Пример #2
0
bool RunsTableModel::postRow(int row_no, bool throw_exc)
{
	bool is_single_user = sqlConnection().driverName().endsWith(QLatin1String("SQLITE"), Qt::CaseInsensitive);
	if(is_single_user)
		return Super::postRow(row_no, throw_exc);

	int col_stime = columnIndex("startTimeMs");
	QF_ASSERT(col_stime >= 0, "Bad startTimeMs column!", return false);
	if(isDirty(row_no, col_stime)) {
		int id = value(row_no, "runs.id").toInt();
		int orig_msec = origValue(row_no, col_stime).toInt();
		int db_msec = 0;

		qf::core::sql::Transaction transaction(sqlConnection());
		QString qs = "SELECT id, startTimeMs FROM runs WHERE id=" QF_IARG(id) " FOR UPDATE";
		qf::core::sql::Query q(transaction.connection());
		q.exec(qs, qf::core::Exception::Throw);
		if(q.next()) {
			db_msec = q.value("startTimeMs").toInt();
		}
		if(orig_msec == db_msec) {
			bool ret = Super::postRow(row_no, throw_exc);
			transaction.commit();
			return ret;
		}
		else {
			QString err_msg = tr("Mid-air collision setting start time, reload table and try it again.");
			revertRow(row_no);
			if(throw_exc)
				QF_EXCEPTION(err_msg);
			return false;
		}
	}
	return Super::postRow(row_no, throw_exc);
}
Пример #3
0
/*!
    Reverts all pending changes.

    \sa revert(), revertRow(), submitAll()
*/
void QSqlTableModel::revertAll()
{
    Q_D(QSqlTableModel);

    const QList<int> rows(d->cache.keys());
    for (int i = rows.size() - 1; i >= 0; --i)
        revertRow(rows.value(i));
}
Пример #4
0
/*!
    Reverts all pending changes.

    \sa revert(), revertRow(), submitAll()
*/
void QSqlTableModel::revertAll()
{
    Q_D(QSqlTableModel);
    switch (d->strategy) {
    case OnFieldChange:
        break;
    case OnRowChange:
        if (d->editIndex != -1)
            revertRow(d->editIndex);
        else if (d->insertIndex != -1)
            revertRow(d->insertIndex);
        break;
    case OnManualSubmit:
        while (!d->cache.isEmpty())
            revertRow(d->cache.constBegin().key());
        break;
    }
}
Пример #5
0
/*!
    Inserts the \a record at position \a row. If \a row is negative,
    the record will be appended to the end. Calls insertRows() and
    setRecord() internally.

    Returns \c true if the record could be inserted, otherwise false.

    Changes are submitted immediately for OnFieldChange and
    OnRowChange. Failure does not leave a new row in the model.

    \sa insertRows(), removeRows(), setRecord()
*/
bool QSqlTableModel::insertRecord(int row, const QSqlRecord &record)
{
    if (row < 0)
        row = rowCount();
    if (!insertRow(row, QModelIndex()))
        return false;
    if (!setRecord(row, record)) {
        revertRow(row);
        return false;
    }
    return true;
}
Пример #6
0
/*!
    Removes \a count rows starting at \a row. Since this model
    does not support hierarchical structures, \a parent must be
    an invalid model index.

    Emits the beforeDelete() signal before a row is deleted. When
    the edit strategy is OnManualSubmit signal emission is delayed
    until submitAll() is called.

    Returns true if all rows could be removed; otherwise returns
    false. Detailed error information can be retrieved using
    lastError().

    \sa removeColumns(), insertRows()
*/
bool QSqlTableModel::removeRows(int row, int count, const QModelIndex &parent)
{
    Q_D(QSqlTableModel);
    if (parent.isValid() || row < 0 || count <= 0)
        return false;

    int i;
    switch (d->strategy) {
    case OnFieldChange:
    case OnRowChange:
        for (i = 0; i < count; ++i) {
            if (row + i == d->insertIndex)
                d->revertInsertedRow();
            else if (!deleteRowFromTable(row + i))
                return false;
        }
        select();
        break;
    case OnManualSubmit:
        for (i = 0; i < count; ++i) {
            int idx = row + i;
            if (idx >= rowCount())
                return false;
            if (d->cache.value(idx).op == QSqlTableModelPrivate::Insert) {
                revertRow(idx);
                // Reverting a row means all the other cache entries have been adjusted downwards
                // so fake this by adjusting row
                --row;
            } else {
                d->cache[idx].op = QSqlTableModelPrivate::Delete;
                d->cache[idx].primaryValues = d->primaryValues(indexInQuery(createIndex(idx, 0)).row());
                emit headerDataChanged(Qt::Vertical, idx, idx);
            }
        }
        break;
    }
    return true;
}
Пример #7
0
/*!
    Removes \a count rows starting at \a row. Since this model
    does not support hierarchical structures, \a parent must be
    an invalid model index.

    When the edit strategy is OnManualSubmit, deletion of rows from
    the database is delayed until submitAll() is called.

    For OnFieldChange and OnRowChange, only one row may be deleted
    at a time and only if no other row has a cached change. Deletions
    are submitted immediately to the database. The model retains a
    blank row for successfully deleted row until refreshed with select().

    After failed deletion, the operation is not reverted in the model.
    The application may resubmit or revert.

    Inserted but not yet successfully submitted rows in the range to be
    removed are immediately removed from the model.

    Before a row is deleted from the database, the beforeDelete()
    signal is emitted.

    If row < 0 or row + count > rowCount(), no action is taken and
    false is returned. Returns \c true if all rows could be removed;
    otherwise returns \c false. Detailed database error information
    can be retrieved using lastError().

    \sa removeColumns(), insertRows()
*/
bool QSqlTableModel::removeRows(int row, int count, const QModelIndex &parent)
{
    Q_D(QSqlTableModel);
    if (parent.isValid() || row < 0 || count <= 0)
        return false;
    else if (row + count > rowCount())
        return false;
    else if (!count)
        return true;

    if (d->strategy != OnManualSubmit)
        if (count > 1 || (d->cache.value(row).submitted() && isDirty()))
            return false;

    // Iterate backwards so we don't have to worry about removed rows causing
    // higher cache entries to shift downwards.
    for (int idx = row + count - 1; idx >= row; --idx) {
        QSqlTableModelPrivate::ModifiedRow& mrow = d->cache[idx];
        if (mrow.op() == QSqlTableModelPrivate::Insert) {
            revertRow(idx);
        } else {
            if (mrow.op() == QSqlTableModelPrivate::None)
                mrow = QSqlTableModelPrivate::ModifiedRow(QSqlTableModelPrivate::Delete,
                                                          QSqlQueryModel::record(idx));
            else
                mrow.setOp(QSqlTableModelPrivate::Delete);
            if (d->strategy == OnManualSubmit)
                emit headerDataChanged(Qt::Vertical, idx, idx);
        }
    }

    if (d->strategy != OnManualSubmit)
        return submit();

    return true;
}