QSqlTableModel: expose methods for getting primary values
These methods are very useful in subclasses. Change-Id: Ifdfee9d90cbdad97f349e46b587582f1d9e7cd7a Reviewed-by: Andy Shaw <andy.shaw@digia.com>
This commit is contained in:
parent
4131bfb2c2
commit
8dfe1385b5
@ -524,4 +524,19 @@ QDebug operator<<(QDebug dbg, const QSqlRecord &r)
|
||||
}
|
||||
#endif
|
||||
|
||||
/*!
|
||||
\since 5.1
|
||||
Returns a record containing the fields represented in \a keyFields set to values
|
||||
that match by field name.
|
||||
*/
|
||||
QSqlRecord QSqlRecord::keyValues(const QSqlRecord &keyFields) const
|
||||
{
|
||||
QSqlRecord retValues(keyFields);
|
||||
|
||||
for (int i = retValues.count() - 1; i >= 0; --i)
|
||||
retValues.setValue(i, value(retValues.fieldName(i)));
|
||||
|
||||
return retValues;
|
||||
}
|
||||
|
||||
QT_END_NAMESPACE
|
||||
|
@ -95,6 +95,7 @@ public:
|
||||
void clear();
|
||||
void clearValues();
|
||||
int count() const;
|
||||
QSqlRecord keyValues(const QSqlRecord &keyFields) const;
|
||||
|
||||
private:
|
||||
void detach();
|
||||
|
@ -195,29 +195,6 @@ bool QSqlTableModelPrivate::exec(const QString &stmt, bool prepStatement,
|
||||
return true;
|
||||
}
|
||||
|
||||
QSqlRecord QSqlTableModelPrivate::primaryValues(const QSqlRecord &rec, const QSqlRecord &pIndex)
|
||||
{
|
||||
QSqlRecord pValues(pIndex);
|
||||
|
||||
for (int i = pValues.count() - 1; i >= 0; --i)
|
||||
pValues.setValue(i, rec.value(pValues.fieldName(i)));
|
||||
|
||||
return pValues;
|
||||
}
|
||||
|
||||
QSqlRecord QSqlTableModelPrivate::primaryValues(int row) const
|
||||
{
|
||||
Q_Q(const QSqlTableModel);
|
||||
|
||||
const QSqlRecord &pIndex = primaryIndex.isEmpty() ? rec : primaryIndex;
|
||||
|
||||
ModifiedRow mr = cache.value(row);
|
||||
if (mr.op() != None)
|
||||
return mr.primaryValues(pIndex);
|
||||
else
|
||||
return primaryValues(q->QSqlQueryModel::record(row), pIndex);
|
||||
}
|
||||
|
||||
/*!
|
||||
\class QSqlTableModel
|
||||
\brief The QSqlTableModel class provides an editable data model
|
||||
@ -432,7 +409,7 @@ bool QSqlTableModel::selectRow(int row)
|
||||
const QString table_filter = d->filter;
|
||||
d->filter = d->db.driver()->sqlStatement(QSqlDriver::WhereStatement,
|
||||
d->tableName,
|
||||
d->primaryValues(row),
|
||||
primaryValues(row),
|
||||
false);
|
||||
static const QString wh = Sql::where() + Sql::sp();
|
||||
if (d->filter.startsWith(wh, Qt::CaseInsensitive))
|
||||
@ -652,7 +629,7 @@ bool QSqlTableModel::updateRowInTable(int row, const QSqlRecord &values)
|
||||
QSqlRecord rec(values);
|
||||
emit beforeUpdate(row, rec);
|
||||
|
||||
const QSqlRecord whereValues = d->primaryValues(row);
|
||||
const QSqlRecord whereValues = primaryValues(row);
|
||||
const bool prepStatement = d->db.driver()->hasFeature(QSqlDriver::PreparedQueries);
|
||||
const QString stmt = d->db.driver()->sqlStatement(QSqlDriver::UpdateStatement, d->tableName,
|
||||
rec, prepStatement);
|
||||
@ -718,7 +695,7 @@ bool QSqlTableModel::deleteRowFromTable(int row)
|
||||
Q_D(QSqlTableModel);
|
||||
emit beforeDelete(row);
|
||||
|
||||
const QSqlRecord whereValues = d->primaryValues(row);
|
||||
const QSqlRecord whereValues = primaryValues(row);
|
||||
const bool prepStatement = d->db.driver()->hasFeature(QSqlDriver::PreparedQueries);
|
||||
const QString stmt = d->db.driver()->sqlStatement(QSqlDriver::DeleteStatement,
|
||||
d->tableName,
|
||||
@ -1439,4 +1416,24 @@ bool QSqlTableModel::setRecord(int row, const QSqlRecord &values)
|
||||
return true;
|
||||
}
|
||||
|
||||
/*!
|
||||
\since 5.1
|
||||
Returns a record containing the fields represented in the primary key set to the values
|
||||
at \a row. If no primary key is defined, the returned record will contain all fields.
|
||||
|
||||
\sa primaryKey()
|
||||
*/
|
||||
QSqlRecord QSqlTableModel::primaryValues(int row) const
|
||||
{
|
||||
Q_D(const QSqlTableModel);
|
||||
|
||||
const QSqlRecord &pIndex = d->primaryIndex.isEmpty() ? d->rec : d->primaryIndex;
|
||||
|
||||
QSqlTableModelPrivate::ModifiedRow mr = d->cache.value(row);
|
||||
if (mr.op() != QSqlTableModelPrivate::None)
|
||||
return mr.primaryValues(pIndex);
|
||||
else
|
||||
return QSqlQueryModel::record(row).keyValues(pIndex);
|
||||
}
|
||||
|
||||
QT_END_NAMESPACE
|
||||
|
@ -134,6 +134,7 @@ protected:
|
||||
void setPrimaryKey(const QSqlIndex &key);
|
||||
void setQuery(const QSqlQuery &query);
|
||||
QModelIndex indexInQuery(const QModelIndex &item) const;
|
||||
QSqlRecord primaryValues(int row) const;
|
||||
};
|
||||
|
||||
QT_END_NAMESPACE
|
||||
|
@ -71,8 +71,6 @@ public:
|
||||
busyInsertingRows(false)
|
||||
{}
|
||||
void clear();
|
||||
static QSqlRecord primaryValues(const QSqlRecord &rec, const QSqlRecord &pIndex);
|
||||
QSqlRecord primaryValues(int index) const;
|
||||
virtual void clearCache();
|
||||
QSqlRecord record(const QVector<QVariant> &values) const;
|
||||
|
||||
@ -170,7 +168,7 @@ public:
|
||||
if (m_op == None || m_op == Insert)
|
||||
return QSqlRecord();
|
||||
|
||||
return QSqlTableModelPrivate::primaryValues(m_db_values, pi);
|
||||
return m_db_values.keyValues(pi);
|
||||
}
|
||||
private:
|
||||
inline static void setGenerated(QSqlRecord& r, bool g)
|
||||
|
Loading…
Reference in New Issue
Block a user