Skip to content

Commit 64a596a

Browse files
committed
Use even less Qt containers
1 parent 37aaa4c commit 64a596a

13 files changed

Lines changed: 131 additions & 97 deletions

src/DbStructureModel.cpp

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <QMessageBox>
99
#include <QApplication>
1010
#include <unordered_map>
11+
#include <map>
1112

1213
DbStructureModel::DbStructureModel(DBBrowserDB& db, QObject* parent)
1314
: QAbstractItemModel(parent),
@@ -170,7 +171,7 @@ void DbStructureModel::reloadData()
170171
buildTree(itemAll, "main");
171172

172173
// Add the temporary database as a node if it isn't empty. Make sure it's always second if it exists.
173-
if(!m_db.schemata["temp"].isEmpty())
174+
if(!m_db.schemata["temp"].empty())
174175
{
175176
QTreeWidgetItem* itemTemp = new QTreeWidgetItem(itemAll);
176177
itemTemp->setIcon(ColumnName, QIcon(QString(":/icons/database")));
@@ -180,16 +181,16 @@ void DbStructureModel::reloadData()
180181
}
181182

182183
// Now load all the other schemata last
183-
for(auto it=m_db.schemata.constBegin();it!=m_db.schemata.constEnd();++it)
184+
for(const auto& it : m_db.schemata)
184185
{
185186
// Don't load the main and temp schema again
186-
if(it.key() != "main" && it.key() != "temp")
187+
if(it.first != "main" && it.first != "temp")
187188
{
188189
QTreeWidgetItem* itemSchema = new QTreeWidgetItem(itemAll);
189190
itemSchema->setIcon(ColumnName, QIcon(QString(":/icons/database")));
190-
itemSchema->setText(ColumnName, QString::fromStdString(it.key()));
191+
itemSchema->setText(ColumnName, QString::fromStdString(it.first));
191192
itemSchema->setText(ColumnObjectType, "database");
192-
buildTree(itemSchema, it.key());
193+
buildTree(itemSchema, it.first);
193194
}
194195
}
195196

@@ -301,6 +302,15 @@ bool DbStructureModel::dropMimeData(const QMimeData* data, Qt::DropAction action
301302
}
302303
}
303304

305+
static long calc_number_of_objects_by_type(const objectMap& objmap, const std::string& type)
306+
{
307+
auto objects = objmap.equal_range(type);
308+
if(objects.first == objmap.end())
309+
return 0;
310+
else
311+
return std::distance(objects.first, objects.second) + 1;
312+
}
313+
304314
void DbStructureModel::buildTree(QTreeWidgetItem* parent, const std::string& schema)
305315
{
306316
// Build a map from object type to tree node to simplify finding the correct tree node later
@@ -312,32 +322,34 @@ void DbStructureModel::buildTree(QTreeWidgetItem* parent, const std::string& sch
312322
// Prepare tree
313323
QTreeWidgetItem* itemTables = new QTreeWidgetItem(parent);
314324
itemTables->setIcon(ColumnName, QIcon(QString(":/icons/table")));
315-
itemTables->setText(ColumnName, tr("Tables (%1)").arg(objmap.values("table").count()));
325+
itemTables->setText(ColumnName, tr("Tables (%1)").arg(calc_number_of_objects_by_type(objmap, "table")));
316326
typeToParentItem.insert({"table", itemTables});
317327

318328
QTreeWidgetItem* itemIndices = new QTreeWidgetItem(parent);
319329
itemIndices->setIcon(ColumnName, QIcon(QString(":/icons/index")));
320-
itemIndices->setText(ColumnName, tr("Indices (%1)").arg(objmap.values("index").count()));
330+
itemIndices->setText(ColumnName, tr("Indices (%1)").arg(calc_number_of_objects_by_type(objmap, "index")));
321331
typeToParentItem.insert({"index", itemIndices});
322332

323333
QTreeWidgetItem* itemViews = new QTreeWidgetItem(parent);
324334
itemViews->setIcon(ColumnName, QIcon(QString(":/icons/view")));
325-
itemViews->setText(ColumnName, tr("Views (%1)").arg(objmap.values("view").count()));
335+
itemViews->setText(ColumnName, tr("Views (%1)").arg(calc_number_of_objects_by_type(objmap, "view")));
326336
typeToParentItem.insert({"view", itemViews});
327337

328338
QTreeWidgetItem* itemTriggers = new QTreeWidgetItem(parent);
329339
itemTriggers->setIcon(ColumnName, QIcon(QString(":/icons/trigger")));
330-
itemTriggers->setText(ColumnName, tr("Triggers (%1)").arg(objmap.values("trigger").count()));
340+
itemTriggers->setText(ColumnName, tr("Triggers (%1)").arg(calc_number_of_objects_by_type(objmap, "trigger")));
331341
typeToParentItem.insert({"trigger", itemTriggers});
332342

333343
// Get all database objects and sort them by their name
334-
QMultiMap<std::string, sqlb::ObjectPtr> dbobjs;
335-
for(auto it : objmap)
336-
dbobjs.insert(it->name(), it);
344+
std::map<std::string, sqlb::ObjectPtr> dbobjs;
345+
for(const auto& it : objmap)
346+
dbobjs.insert({it.second->name(), it.second});
337347

338348
// Add the database objects to the tree nodes
339-
for(auto it : dbobjs)
349+
for(const auto& obj : dbobjs)
340350
{
351+
sqlb::ObjectPtr it = obj.second;
352+
341353
// Object node
342354
QTreeWidgetItem* item = addNode(typeToParentItem.at(sqlb::Object::typeToString(it->type())), it, schema);
343355

src/EditIndexDialog.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,22 @@ EditIndexDialog::EditIndexDialog(DBBrowserDB& db, const sqlb::ObjectIdentifier&
2222
std::map<std::string, sqlb::ObjectIdentifier> dbobjs; // Map from display name to full object identifier
2323
if(newIndex) // If this is a new index, offer all tables of all database schemata
2424
{
25-
for(auto it=pdb.schemata.constBegin();it!=pdb.schemata.constEnd();++it)
25+
for(const auto& it : pdb.schemata)
2626
{
27-
QList<sqlb::ObjectPtr> tables = it->values("table");
28-
for(auto jt=tables.constBegin();jt!=tables.constEnd();++jt)
27+
auto tables = it.second.equal_range("table");
28+
for(auto jt=tables.first;jt!=tables.second;++jt)
2929
{
3030
// Only show the schema name for non-main schemata
31-
sqlb::ObjectIdentifier obj(it.key(), (*jt)->name());
31+
sqlb::ObjectIdentifier obj(it.first, jt->second->name());
3232
dbobjs.insert({obj.toDisplayString(), obj});
3333
}
3434
}
3535
} else { // If this is an existing index, only offer tables of the current database schema
36-
QList<sqlb::ObjectPtr> tables = pdb.schemata[curIndex.schema()].values("table");
37-
for(auto it : tables)
36+
auto tables = pdb.schemata[curIndex.schema()].equal_range("table");
37+
for(auto it=tables.first;it!=tables.second;++it)
3838
{
3939
// Only show the schema name for non-main schemata
40-
sqlb::ObjectIdentifier obj(curIndex.schema(), it->name());
40+
sqlb::ObjectIdentifier obj(curIndex.schema(), it->second->name());
4141
dbobjs.insert({obj.toDisplayString(), obj});
4242
}
4343
}

src/EditTableDialog.cpp

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,16 @@ EditTableDialog::EditTableDialog(DBBrowserDB& db, const sqlb::ObjectIdentifier&
5151
ui->checkWithoutRowid->setChecked(m_table.withoutRowidTable());
5252
ui->checkWithoutRowid->blockSignals(false);
5353
ui->comboSchema->blockSignals(true);
54-
for(const auto& n : pdb.schemata.keys()) // Load list of database schemata
55-
ui->comboSchema->addItem(QString::fromStdString(n));
54+
for(const auto& n : pdb.schemata) // Load list of database schemata
55+
ui->comboSchema->addItem(QString::fromStdString(n.first));
5656
ui->comboSchema->setCurrentText(QString::fromStdString(curTable.schema()));
5757
ui->comboSchema->blockSignals(false);
5858

5959
populateFields();
6060
populateConstraints();
6161
} else {
62-
for(const auto& n : pdb.schemata.keys()) // Load list of database schemata
63-
ui->comboSchema->addItem(QString::fromStdString(n));
62+
for(const auto& n : pdb.schemata) // Load list of database schemata
63+
ui->comboSchema->addItem(QString::fromStdString(n.first));
6464
ui->comboSchema->setCurrentText("main"); // Always create tables in the main schema by default
6565
ui->labelEditWarning->setVisible(false);
6666
}
@@ -363,8 +363,12 @@ void EditTableDialog::itemChanged(QTreeWidgetItem *item, int column)
363363
if(!m_bNewTable)
364364
{
365365
sqlb::StringVector pk = m_table.primaryKey();
366-
for(const sqlb::ObjectPtr& fkobj : pdb.schemata[curTable.schema()].values("table"))
366+
const auto tables = pdb.schemata[curTable.schema()].equal_range("table");
367+
for(auto it=tables.first;it!=tables.second;++it)
367368
{
369+
const sqlb::ObjectPtr& fkobj = it->second;
370+
371+
368372
auto fks = std::dynamic_pointer_cast<sqlb::Table>(fkobj)->constraints(sqlb::StringVector(), sqlb::Constraint::ForeignKeyConstraintType);
369373
for(const sqlb::ConstraintPtr& fkptr : fks)
370374
{
@@ -394,10 +398,10 @@ void EditTableDialog::itemChanged(QTreeWidgetItem *item, int column)
394398
// Update the field name in the map of old column names to new column names
395399
if(!m_bNewTable)
396400
{
397-
for(const auto& key : trackColumns.keys())
401+
for(const auto& it : trackColumns)
398402
{
399-
if(trackColumns[key] == oldFieldName)
400-
trackColumns[key] = QString::fromStdString(field.name());
403+
if(trackColumns[it.first] == oldFieldName)
404+
trackColumns[it.first] = QString::fromStdString(field.name());
401405
}
402406
}
403407

@@ -651,7 +655,7 @@ void EditTableDialog::addField()
651655

652656
// Add the new column to the list of tracked columns to indicate it has been added
653657
if(!m_bNewTable)
654-
trackColumns.insert(QString(), tbitem->text(kName));
658+
trackColumns.insert({QString(), tbitem->text(kName)});
655659

656660
checkInput();
657661
}
@@ -671,10 +675,10 @@ void EditTableDialog::removeField()
671675

672676
// Update the map of tracked columns to indicate the column is deleted
673677
QString name = ui->treeWidget->currentItem()->text(0);
674-
for(const auto& key : trackColumns.keys())
678+
for(const auto& it : trackColumns)
675679
{
676-
if(trackColumns[key] == name)
677-
trackColumns[key] = QString();
680+
if(trackColumns[it.first] == name)
681+
trackColumns[it.first] = QString();
678682
}
679683
}
680684

src/EditTableDialog.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
#include "sql/ObjectIdentifier.h"
55
#include "sql/sqlitetypes.h"
66

7+
#include <map>
8+
79
#include <QDialog>
8-
#include <QMap>
910

1011
class DBBrowserDB;
1112
class QTreeWidgetItem;
@@ -74,7 +75,7 @@ private slots:
7475
DBBrowserDB& pdb;
7576
ForeignKeyEditorDelegate* m_fkEditorDelegate;
7677
sqlb::ObjectIdentifier curTable;
77-
QMap<QString, QString> trackColumns;
78+
std::map<QString, QString> trackColumns;
7879
sqlb::Table m_table;
7980
bool m_bNewTable;
8081
QString m_sRestorePointName;

src/ExportDataDialog.cpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,20 @@ ExportDataDialog::ExportDataDialog(DBBrowserDB& db, ExportFormats format, QWidge
4343
if(query.isEmpty())
4444
{
4545
// Get list of tables to export
46-
for(auto it=pdb.schemata.constBegin();it!=pdb.schemata.constEnd();++it)
46+
for(const auto& it : pdb.schemata)
4747
{
48-
QList<sqlb::ObjectPtr> tables = it->values("table") + it->values("view");
49-
for(auto jt=tables.constBegin();jt!=tables.constEnd();++jt)
48+
const auto tables = it.second.equal_range("table");
49+
const auto views = it.second.equal_range("view");
50+
std::map<std::string, sqlb::ObjectPtr> objects;
51+
for(auto jt=tables.first;jt!=tables.second;++jt)
52+
objects.insert({jt->second->name(), jt->second});
53+
for(auto jt=views.first;jt!=views.second;++jt)
54+
objects.insert({jt->second->name(), jt->second});
55+
56+
for(const auto& jt : objects)
5057
{
51-
sqlb::ObjectIdentifier obj(it.key(), (*jt)->name());
52-
QListWidgetItem* item = new QListWidgetItem(QIcon(QString(":icons/%1").arg(QString::fromStdString(sqlb::Object::typeToString((*jt)->type())))), QString::fromStdString(obj.toDisplayString()));
58+
sqlb::ObjectIdentifier obj(it.first, jt.second->name());
59+
QListWidgetItem* item = new QListWidgetItem(QIcon(QString(":icons/%1").arg(QString::fromStdString(sqlb::Object::typeToString(jt.second->type())))), QString::fromStdString(obj.toDisplayString()));
5360
item->setData(Qt::UserRole, QString::fromStdString(obj.toSerialised()));
5461
ui->listTables->addItem(item);
5562
}

src/ExportSqlDialog.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ ExportSqlDialog::ExportSqlDialog(DBBrowserDB* db, QWidget* parent, const QString
2828
ui->comboOldSchema->setCurrentIndex(Settings::getValue("exportsql", "oldschema").toInt());
2929

3030
// Get list of tables to export
31-
QList<sqlb::ObjectPtr> objects = pdb->schemata["main"].values("table");
32-
for(const sqlb::ObjectPtr& it : objects)
33-
ui->listTables->addItem(new QListWidgetItem(QIcon(QString(":icons/%1").arg(QString::fromStdString(sqlb::Object::typeToString(it->type())))), QString::fromStdString(it->name())));
31+
const auto objects = pdb->schemata["main"].equal_range("table");
32+
for(auto it=objects.first;it!=objects.second;++it)
33+
ui->listTables->addItem(new QListWidgetItem(QIcon(QString(":icons/%1").arg(QString::fromStdString(sqlb::Object::typeToString(it->second->type())))), QString::fromStdString(it->second->name())));
3434

3535
// Sort list of tables and select the table specified in the
3636
// selection parameter or all tables if table not specified

src/ForeignKeyEditorDelegate.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,12 @@ ForeignKeyEditorDelegate::ForeignKeyEditorDelegate(const DBBrowserDB& db, sqlb::
8181
, m_db(db)
8282
, m_table(table)
8383
{
84-
for(auto it=m_db.schemata.constBegin();it!=m_db.schemata.constEnd();++it)
84+
for(const auto& it : m_db.schemata)
8585
{
86-
for(auto jt=it->constBegin();jt!=it->constEnd();++jt)
86+
for(const auto& jt : it.second)
8787
{
88-
if((*jt)->type() == sqlb::Object::Types::Table)
89-
m_tablesIds.insert({(*jt)->name(), std::dynamic_pointer_cast<sqlb::Table>(*jt)->fieldNames()});
88+
if(jt.second->type() == sqlb::Object::Types::Table)
89+
m_tablesIds.insert({jt.second->name(), std::dynamic_pointer_cast<sqlb::Table>(jt.second)->fieldNames()});
9090
}
9191
}
9292
}

src/MainWindow.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -661,19 +661,19 @@ void MainWindow::populateStructure(const QString& old_table)
661661

662662
// Update table and column names for syntax highlighting
663663
SqlUiLexer::QualifiedTablesMap qualifiedTablesMap;
664-
for(auto it=db.schemata.constBegin();it!=db.schemata.constEnd();++it)
664+
for(const auto& it : db.schemata)
665665
{
666666
SqlUiLexer::TablesAndColumnsMap tablesToColumnsMap;
667-
objectMap tab = db.getBrowsableObjects(it.key());
668-
for(auto jt : tab)
667+
objectMap tab = db.getBrowsableObjects(it.first);
668+
for(const auto& jt : tab)
669669
{
670-
QString objectname = QString::fromStdString(jt->name());
670+
QString objectname = QString::fromStdString(jt.second->name());
671671

672-
sqlb::FieldInfoList fi = jt->fieldInformation();
672+
sqlb::FieldInfoList fi = jt.second->fieldInformation();
673673
for(const sqlb::FieldInfo& f : fi)
674674
tablesToColumnsMap[objectname].push_back(QString::fromStdString(f.name));
675675
}
676-
qualifiedTablesMap[QString::fromStdString(it.key())] = tablesToColumnsMap;
676+
qualifiedTablesMap[QString::fromStdString(it.first)] = tablesToColumnsMap;
677677
}
678678
SqlTextEdit::sqlLexer->setTableNames(qualifiedTablesMap);
679679
ui->editLogApplication->reloadKeywords();

src/VacuumDialog.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ VacuumDialog::VacuumDialog(DBBrowserDB* _db, QWidget* parent) :
1515
// Show warning if DB is dirty
1616
ui->labelSavepointWarning->setVisible(db->getDirty());
1717

18-
// Populate list of objects to compact. We just support vacuuming the main schema here.
19-
for(auto it=db->schemata.constBegin();it!=db->schemata.constEnd();++it)
18+
// Populate list of objects to compact. We just support vacuuming the different schemas here.
19+
for(const auto& it : db->schemata)
2020
{
2121
QTreeWidgetItem* item = new QTreeWidgetItem(ui->treeDatabases);
22-
item->setText(0, QString::fromStdString(it.key()));
22+
item->setText(0, QString::fromStdString(it.first));
2323
item->setIcon(0, QIcon(QString(":icons/database")));
2424
ui->treeDatabases->addTopLevelItem(item);
2525
}

0 commit comments

Comments
 (0)