QSqlTM: add reimplemented record() method
authorMark Brand <mabrand@mabrand.nl>
Wed, 11 Jul 2012 14:29:53 +0000 (16:29 +0200)
committerQt by Nokia <qt-info@nokia.com>
Tue, 17 Jul 2012 06:13:42 +0000 (08:13 +0200)
QSqlQueryModel::record(row) populates the record it returns with values
from virtual data(), so the values themselves can be supplied by
QSqlTableModel. However, it is also desirable to be able to interrogate
QSqlTableModel for the actual current record in its cache, including
properties such as the generated flag.

Change-Id: I733901913b7d237d5762448e953a99b5bd83fc7f
Reviewed-by: Michael Goddard <michael.goddard@nokia.com>
src/sql/models/qsqltablemodel.cpp
src/sql/models/qsqltablemodel.h

index 0416656..4f3db2b 100644 (file)
@@ -538,7 +538,7 @@ bool QSqlTableModel::setData(const QModelIndex &index, const QVariant &value, in
 
     if (row.op() == QSqlTableModelPrivate::None)
         row = QSqlTableModelPrivate::ModifiedRow(QSqlTableModelPrivate::Update,
-                                                 record(index.row()));
+                                                 QSqlQueryModel::record(index.row()));
 
     row.setValue(index.column(), value);
     emit dataChanged(index, index);
@@ -1044,7 +1044,8 @@ bool QSqlTableModel::removeRows(int row, int count, const QModelIndex &parent)
             revertRow(idx);
         } else {
             if (mrow.op() == QSqlTableModelPrivate::None)
-                mrow = QSqlTableModelPrivate::ModifiedRow(QSqlTableModelPrivate::Delete, record(idx));
+                mrow = QSqlTableModelPrivate::ModifiedRow(QSqlTableModelPrivate::Delete,
+                                                          QSqlQueryModel::record(idx));
             else
                 mrow.setOp(QSqlTableModelPrivate::Delete);
             if (d->strategy == OnManualSubmit)
@@ -1250,6 +1251,34 @@ Qt::ItemFlags QSqlTableModel::flags(const QModelIndex &index) const
         return Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable;
 }
 
+QSqlRecord QSqlTableModel::record() const
+{
+    return QSqlQueryModel::record();
+}
+
+/*!
+\reimp
+\since 5.0
+    Returns the record at \a row in the model.
+
+    If \a row is the index of a valid row, the record
+    will be populated with values from that row.
+
+    If the model is not initialized, an empty record will be
+    returned.
+
+    \sa QSqlRecord::isEmpty()
+*/
+QSqlRecord QSqlTableModel::record(int row) const
+{
+    Q_D(const QSqlTableModel);
+
+    if (d->cache.contains(row))
+        return d->cache.value(row).rec();
+
+    return QSqlQueryModel::record(row);
+}
+
 /*!
     Applies \a values to the \a row in the model. The source and
     target fields are mapped by field name, not by position in
@@ -1301,7 +1330,7 @@ bool QSqlTableModel::setRecord(int row, const QSqlRecord &values)
     QSqlTableModelPrivate::ModifiedRow &mrow = d->cache[row];
     if (mrow.op() == QSqlTableModelPrivate::None)
         mrow = QSqlTableModelPrivate::ModifiedRow(QSqlTableModelPrivate::Update,
-                                                  record(row));
+                                                  QSqlQueryModel::record(row));
 
     Map::const_iterator i = map.constBegin();
     const Map::const_iterator e = map.constEnd();
index 62e0986..0cdab4b 100644 (file)
@@ -74,6 +74,8 @@ public:
 
     Qt::ItemFlags flags(const QModelIndex &index) const;
 
+    QSqlRecord record() const;
+    QSqlRecord record(int row) const;
     QVariant data(const QModelIndex &idx, int role = Qt::DisplayRole) const;
     bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole);