QSqlTableModel: long live selectRow()!
Change-Id: If26dbcc8a1e8ef1376ef7a688c946ce5270e5706 Reviewed-by: Yunqiao Yin <charles.yin@nokia.com>
This commit is contained in:
parent
b979956ec4
commit
291e2c7d54
@ -391,6 +391,49 @@ bool QSqlTableModel::select()
|
||||
return true;
|
||||
}
|
||||
|
||||
/*!
|
||||
Refreshes \a row in the model with values from the database table row matching
|
||||
on primary key values. Without a primary key, all column values must match. If
|
||||
no matching row is found, the model will show an empty row.
|
||||
|
||||
Returns true if successful; otherwise returns false.
|
||||
|
||||
\sa select()
|
||||
*/
|
||||
bool QSqlTableModel::selectRow(int row)
|
||||
{
|
||||
Q_D(QSqlTableModel);
|
||||
|
||||
if (row < 0 || row >= rowCount())
|
||||
return false;
|
||||
|
||||
const int table_sort_col = d->sortColumn;
|
||||
d->sortColumn = -1;
|
||||
const QString table_filter = d->filter;
|
||||
d->filter = d->db.driver()->sqlStatement(QSqlDriver::WhereStatement,
|
||||
d->tableName,
|
||||
d->primaryValues(row),
|
||||
false);
|
||||
if (d->filter.startsWith(QLatin1String("WHERE "), Qt::CaseInsensitive))
|
||||
d->filter.remove(0, 6);
|
||||
const QString stmt = selectStatement();
|
||||
d->sortColumn = table_sort_col;
|
||||
d->filter = table_filter;
|
||||
|
||||
QSqlQuery q(d->db);
|
||||
q.setForwardOnly(true);
|
||||
if (!q.exec(stmt))
|
||||
return false;
|
||||
|
||||
bool exists = q.next();
|
||||
d->cache[row].refresh(exists, q.record());
|
||||
|
||||
emit headerDataChanged(Qt::Vertical, row, row);
|
||||
emit dataChanged(createIndex(row, 0), createIndex(row, columnCount() - 1));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/*!
|
||||
\reimp
|
||||
*/
|
||||
|
@ -67,6 +67,7 @@ public:
|
||||
virtual ~QSqlTableModel();
|
||||
|
||||
virtual bool select();
|
||||
virtual bool selectRow(int row);
|
||||
|
||||
virtual void setTable(const QString &tableName);
|
||||
QString tableName() const;
|
||||
|
@ -135,6 +135,20 @@ public:
|
||||
setGenerated(m_db_values, true);
|
||||
}
|
||||
}
|
||||
inline void refresh(bool exists, const QSqlRecord& newvals)
|
||||
{
|
||||
m_submitted = true;
|
||||
if (exists) {
|
||||
m_op = Update;
|
||||
m_db_values = newvals;
|
||||
m_rec = newvals;
|
||||
setGenerated(m_rec, false);
|
||||
} else {
|
||||
m_op = Delete;
|
||||
m_rec.clear();
|
||||
m_db_values.clear();
|
||||
}
|
||||
}
|
||||
inline bool insert() const { return m_insert; }
|
||||
inline void revert()
|
||||
{
|
||||
|
Loading…
x
Reference in New Issue
Block a user