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);
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];
}
endInsertColumns();
@ -644,10 +644,9 @@ bool QSqlQueryModel::removeColumns(int column, int count, const QModelIndex &par
beginRemoveColumns(parent, column, column + count - 1);
int i;
for (i = 0; i < count; ++i)
for (int i = 0; i < count; ++i)
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;
endRemoveColumns();

View File

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

View File

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

View File

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