summaryrefslogtreecommitdiffstats
path: root/src/sql/models
diff options
context:
space:
mode:
authorFrederik Gladhorn <frederik.gladhorn@digia.com>2013-02-13 11:58:07 +0100
committerFrederik Gladhorn <frederik.gladhorn@digia.com>2013-02-14 14:24:57 +0100
commite65cd6f3794e12e6bc5c2ee985eae8e70ff5f333 (patch)
tree8965835c375422d63b2ccfa927b31a56e64bda1d /src/sql/models
parentd1ee7189553e13337b198fe4ba66d79fb7a7f41d (diff)
parente95a758236cf2c68e33da4ddb62bff4fe8d9dd8b (diff)
Merge remote-tracking branch 'origin/stable' into dev
Conflicts: src/concurrent/doc/qtconcurrent.qdocconf src/corelib/doc/qtcore.qdocconf src/corelib/global/qglobal.h src/dbus/doc/qtdbus.qdocconf src/dbus/qdbusmessage.h src/gui/doc/qtgui.qdocconf src/gui/image/qimagereader.cpp src/network/doc/qtnetwork.qdocconf src/opengl/doc/qtopengl.qdocconf src/opengl/qgl.h src/plugins/platforms/windows/qwindowswindow.cpp src/printsupport/doc/qtprintsupport.qdocconf src/sql/doc/qtsql.qdocconf src/testlib/doc/qttestlib.qdocconf src/tools/qdoc/doc/config/qt-cpp-ignore.qdocconf src/widgets/doc/qtwidgets.qdocconf src/xml/doc/qtxml.qdocconf Change-Id: Ie9a1fa2cc44bec22a0b942e817a1095ca3414629
Diffstat (limited to 'src/sql/models')
-rw-r--r--src/sql/models/qsqlquerymodel.cpp21
-rw-r--r--src/sql/models/qsqlquerymodel_p.h1
-rw-r--r--src/sql/models/qsqltablemodel.cpp25
-rw-r--r--src/sql/models/qsqltablemodel_p.h1
4 files changed, 35 insertions, 13 deletions
diff --git a/src/sql/models/qsqlquerymodel.cpp b/src/sql/models/qsqlquerymodel.cpp
index 59103b72d3..00fc410ca5 100644
--- a/src/sql/models/qsqlquerymodel.cpp
+++ b/src/sql/models/qsqlquerymodel.cpp
@@ -95,6 +95,13 @@ void QSqlQueryModelPrivate::initColOffsets(int size)
memset(colOffsets.data(), 0, colOffsets.size() * sizeof(int));
}
+int QSqlQueryModelPrivate::columnInQuery(int modelColumn) const
+{
+ if (modelColumn < 0 || modelColumn >= rec.count() || !rec.isGenerated(modelColumn) || modelColumn >= colOffsets.size())
+ return -1;
+ return modelColumn - colOffsets[modelColumn];
+}
+
/*!
\class QSqlQueryModel
\brief The QSqlQueryModel class provides a read-only data model for SQL
@@ -370,11 +377,7 @@ QVariant QSqlQueryModel::headerData(int section, Qt::Orientation orientation, in
val = d->headers.value(section).value(Qt::EditRole);
if (val.isValid())
return val;
-
- // See if it's an inserted column (iiq.column() != -1)
- QModelIndex dItem = indexInQuery(createIndex(0, section));
-
- if (role == Qt::DisplayRole && d->rec.count() > section && dItem.column() != -1)
+ if (role == Qt::DisplayRole && d->rec.count() > section && d->columnInQuery(section) != -1)
return d->rec.fieldName(section);
}
return QAbstractItemModel::headerData(section, orientation, role);
@@ -668,12 +671,10 @@ bool QSqlQueryModel::removeColumns(int column, int count, const QModelIndex &par
QModelIndex QSqlQueryModel::indexInQuery(const QModelIndex &item) const
{
Q_D(const QSqlQueryModel);
- if (item.column() < 0 || item.column() >= d->rec.count()
- || !d->rec.isGenerated(item.column())
- || item.column() >= d->colOffsets.size())
+ int modelColumn = d->columnInQuery(item.column());
+ if (modelColumn < 0)
return QModelIndex();
- return createIndex(item.row(), item.column() - d->colOffsets[item.column()],
- item.internalPointer());
+ return createIndex(item.row(), modelColumn, item.internalPointer());
}
QT_END_NAMESPACE
diff --git a/src/sql/models/qsqlquerymodel_p.h b/src/sql/models/qsqlquerymodel_p.h
index ecf69003f4..a79b62cda1 100644
--- a/src/sql/models/qsqlquerymodel_p.h
+++ b/src/sql/models/qsqlquerymodel_p.h
@@ -72,6 +72,7 @@ public:
void prefetch(int);
void initColOffsets(int size);
+ int columnInQuery(int modelColumn) const;
mutable QSqlQuery query;
mutable QSqlError error;
diff --git a/src/sql/models/qsqltablemodel.cpp b/src/sql/models/qsqltablemodel.cpp
index 32dd517a7a..2e395b0a59 100644
--- a/src/sql/models/qsqltablemodel.cpp
+++ b/src/sql/models/qsqltablemodel.cpp
@@ -355,6 +355,16 @@ void QSqlTableModel::setTable(const QString &tableName)
if (d->rec.count() == 0)
d->error = QSqlError(QLatin1String("Unable to find table ") + d->tableName, QString(),
QSqlError::StatementError);
+
+ // Remember the auto index column if there is one now.
+ // The record that will be obtained from the query after select lacks this feature.
+ d->autoColumn.clear();
+ for (int c = 0; c < d->rec.count(); ++c) {
+ if (d->rec.field(c).isAutoValue()) {
+ d->autoColumn = d->rec.fieldName(c);
+ break;
+ }
+ }
}
/*!
@@ -587,7 +597,10 @@ bool QSqlTableModel::setData(const QModelIndex &index, const QVariant &value, in
if (!(flags(index) & Qt::ItemIsEditable))
return false;
- if (QSqlTableModel::data(index, role) == value)
+ const QVariant oldValue = QSqlTableModel::data(index, role);
+ if (value == oldValue
+ && value.isNull() == oldValue.isNull()
+ && d->cache.value(index.row()).op() != QSqlTableModelPrivate::Insert)
return true;
QSqlTableModelPrivate::ModifiedRow &row = d->cache[index.row()];
@@ -772,6 +785,11 @@ bool QSqlTableModel::submitAll()
}
if (success) {
+ if (d->strategy != OnManualSubmit && mrow.op() == QSqlTableModelPrivate::Insert) {
+ int c = mrow.rec().indexOf(d->autoColumn);
+ if (c != -1 && !mrow.rec().isGenerated(c))
+ mrow.setValue(c, d->editQuery.lastInsertId());
+ }
mrow.setSubmitted();
if (d->strategy != OnManualSubmit)
success = selectRow(row);
@@ -821,7 +839,8 @@ bool QSqlTableModel::submit()
user canceled editing the current row.
Reverts the changes if the model's strategy is set to
- OnRowChange. Does nothing for the other edit strategies.
+ OnRowChange or OnFieldChange. Does nothing for the OnManualSubmit
+ strategy.
Use revertAll() to revert all pending changes for the
OnManualSubmit strategy or revertRow() to revert a specific row.
@@ -831,7 +850,7 @@ bool QSqlTableModel::submit()
void QSqlTableModel::revert()
{
Q_D(QSqlTableModel);
- if (d->strategy == OnRowChange)
+ if (d->strategy == OnRowChange || d->strategy == OnFieldChange)
revertAll();
}
diff --git a/src/sql/models/qsqltablemodel_p.h b/src/sql/models/qsqltablemodel_p.h
index 56db09b7e0..825490ea39 100644
--- a/src/sql/models/qsqltablemodel_p.h
+++ b/src/sql/models/qsqltablemodel_p.h
@@ -96,6 +96,7 @@ public:
QSqlIndex primaryIndex;
QString tableName;
QString filter;
+ QString autoColumn;
enum Op { None, Insert, Update, Delete };