SQL/models: use qsizetype/range-base for loops where possible

Replace some 'native' for loops with range-base for loops or use
qsiztype instead int, mark some helper functions as const.

Change-Id: Ib3f368d6c07bc170ab4ed7cbf28181a226dbeac5
Reviewed-by: Axel Spoerl <axel.spoerl@qt.io>
This commit is contained in:
Christian Ehrlicher 2024-04-17 20:55:17 +02:00
parent 434494dfb7
commit e337f14707
4 changed files with 22 additions and 42 deletions

View File

@ -618,7 +618,7 @@ bool QSqlQueryModel::insertColumns(int column, int count, const QModelIndex &par
d->colOffsets.append(nVal); d->colOffsets.append(nVal);
Q_ASSERT(d->colOffsets.size() >= d->rec.count()); Q_ASSERT(d->colOffsets.size() >= d->rec.count());
} }
for (int i = column + 1; i < d->colOffsets.size(); ++i) for (qsizetype i = column + 1; i < d->colOffsets.size(); ++i)
++d->colOffsets[i]; ++d->colOffsets[i];
} }
endInsertColumns(); endInsertColumns();
@ -644,10 +644,9 @@ bool QSqlQueryModel::removeColumns(int column, int count, const QModelIndex &par
beginRemoveColumns(parent, column, column + count - 1); beginRemoveColumns(parent, column, column + count - 1);
int i; for (int i = 0; i < count; ++i)
for (i = 0; i < count; ++i)
d->rec.remove(column); d->rec.remove(column);
for (i = column; i < d->colOffsets.size(); ++i) for (qsizetype i = column; i < d->colOffsets.size(); ++i)
d->colOffsets[i] -= count; d->colOffsets[i] -= count;
endRemoveColumns(); endRemoveColumns();

View File

@ -21,7 +21,7 @@ QT_BEGIN_NAMESPACE
using namespace Qt::StringLiterals; using namespace Qt::StringLiterals;
class QSqlRelationalTableModelSql: public QSqlTableModelSql class QSqlRelationalTableModelSql: public QSqlQueryModelSql
{ {
public: public:
inline const static QString relTablePrefix(int i) { return QString::number(i).prepend("relTblAl_"_L1); } inline const static QString relTablePrefix(int i) { return QString::number(i).prepend("relTblAl_"_L1); }
@ -108,12 +108,12 @@ struct QRelation
void populateModel(); void populateModel();
bool isDictionaryInitialized(); bool isDictionaryInitialized() const;
void populateDictionary(); void populateDictionary();
void clearDictionary(); void clearDictionary();
void clear(); void clear();
bool isValid(); bool isValid() const;
QSqlRelation rel; QSqlRelation rel;
QRelatedTableModel *model; QRelatedTableModel *model;
@ -158,7 +158,7 @@ void QRelation::populateModel()
} }
} }
bool QRelation::isDictionaryInitialized() bool QRelation::isDictionaryInitialized() const
{ {
return m_dictInitialized; return m_dictInitialized;
} }
@ -204,7 +204,7 @@ void QRelation::clear()
clearDictionary(); clearDictionary();
} }
bool QRelation::isValid() bool QRelation::isValid() const
{ {
return (rel.isValid() && m_parent != nullptr); return (rel.isValid() && m_parent != nullptr);
} }
@ -253,11 +253,9 @@ public:
void QSqlRelationalTableModelPrivate::clearChanges() void QSqlRelationalTableModelPrivate::clearChanges()
{ {
for (int i = 0; i < relations.size(); ++i) { for (auto &rel : relations)
QRelation &rel = relations[i];
rel.clear(); rel.clear();
} }
}
void QSqlRelationalTableModelPrivate::revertCachedRow(int row) void QSqlRelationalTableModelPrivate::revertCachedRow(int row)
{ {
@ -277,8 +275,8 @@ int QSqlRelationalTableModelPrivate::nameToIndex(const QString &name) const
void QSqlRelationalTableModelPrivate::clearCache() void QSqlRelationalTableModelPrivate::clearCache()
{ {
for (int i = 0; i < relations.size(); ++i) for (auto &rel : relations)
relations[i].clearDictionary(); rel.clearDictionary();
QSqlTableModelPrivate::clearCache(); QSqlTableModelPrivate::clearCache();
} }

View File

@ -19,12 +19,7 @@ QT_BEGIN_NAMESPACE
using namespace Qt::StringLiterals; using namespace Qt::StringLiterals;
using SqlTm = QSqlTableModelSql; using SqlTm = QSqlQueryModelSql;
QSqlTableModelPrivate::~QSqlTableModelPrivate()
{
}
/*! \internal /*! \internal
Populates our record with values. Populates our record with values.
@ -143,11 +138,10 @@ bool QSqlTableModelPrivate::exec(const QString &stmt, bool prepStatement,
return false; return false;
} }
} }
int i; for (int i = 0; i < rec.count(); ++i)
for (i = 0; i < rec.count(); ++i)
if (rec.isGenerated(i)) if (rec.isGenerated(i))
editQuery.addBindValue(rec.value(i)); editQuery.addBindValue(rec.value(i));
for (i = 0; i < whereValues.count(); ++i) for (int i = 0; i < whereValues.count(); ++i)
if (whereValues.isGenerated(i) && !whereValues.isNull(i)) if (whereValues.isGenerated(i) && !whereValues.isNull(i))
editQuery.addBindValue(whereValues.value(i)); editQuery.addBindValue(whereValues.value(i));
@ -475,10 +469,8 @@ QVariant QSqlTableModel::headerData(int section, Qt::Orientation orientation, in
bool QSqlTableModel::isDirty() const bool QSqlTableModel::isDirty() const
{ {
Q_D(const QSqlTableModel); Q_D(const QSqlTableModel);
QSqlTableModelPrivate::CacheMap::ConstIterator i = d->cache.constBegin(); for (const auto &val : std::as_const(d->cache)) {
const QSqlTableModelPrivate::CacheMap::ConstIterator e = d->cache.constEnd(); if (!val.submitted())
for (; i != e; ++i) {
if (!i.value().submitted())
return true; return true;
} }
return false; return false;
@ -1360,8 +1352,7 @@ bool QSqlTableModel::setRecord(int row, const QSqlRecord &values)
return false; return false;
// Check field names and remember mapping // Check field names and remember mapping
typedef QMap<int, int> Map; QMap<int, int> map;
Map map;
for (int i = 0; i < values.count(); ++i) { for (int i = 0; i < values.count(); ++i) {
int idx = d->nameToIndex(values.fieldName(i)); int idx = d->nameToIndex(values.fieldName(i));
if (idx == -1) if (idx == -1)
@ -1374,18 +1365,16 @@ bool QSqlTableModel::setRecord(int row, const QSqlRecord &values)
mrow = QSqlTableModelPrivate::ModifiedRow(QSqlTableModelPrivate::Update, mrow = QSqlTableModelPrivate::ModifiedRow(QSqlTableModelPrivate::Update,
QSqlQueryModel::record(row)); QSqlQueryModel::record(row));
Map::const_iterator i = map.constBegin(); for (const auto i : map.asKeyValueRange()) {
const Map::const_iterator e = map.constEnd();
for ( ; i != e; ++i) {
// have to use virtual setData() here rather than mrow.setValue() // have to use virtual setData() here rather than mrow.setValue()
EditStrategy strategy = d->strategy; EditStrategy strategy = d->strategy;
d->strategy = OnManualSubmit; d->strategy = OnManualSubmit;
QModelIndex cIndex = createIndex(row, i.value()); QModelIndex cIndex = createIndex(row, i.second);
setData(cIndex, values.value(i.key())); setData(cIndex, values.value(i.first));
d->strategy = strategy; d->strategy = strategy;
// setData() sets generated to TRUE, but source record should prevail. // setData() sets generated to TRUE, but source record should prevail.
if (!values.isGenerated(i.key())) if (!values.isGenerated(i.first))
mrow.recRef().setGenerated(i.value(), false); mrow.recRef().setGenerated(i.second, false);
} }
if (d->strategy != OnManualSubmit) if (d->strategy != OnManualSubmit)

View File

@ -35,7 +35,6 @@ public:
strategy(QSqlTableModel::OnRowChange), strategy(QSqlTableModel::OnRowChange),
busyInsertingRows(false) busyInsertingRows(false)
{} {}
~QSqlTableModelPrivate();
void clear(); void clear();
virtual void clearCache(); virtual void clearCache();
@ -154,11 +153,6 @@ public:
CacheMap cache; CacheMap cache;
}; };
class QSqlTableModelSql: public QSqlQueryModelSql
{
public:
};
QT_END_NAMESPACE QT_END_NAMESPACE
#endif // QSQLTABLEMODEL_P_H #endif // QSQLTABLEMODEL_P_H