Use references instead of pointers where it's possible · dwlcj/sqlitebrowser@210916c · GitHub
Skip to content

Commit 210916c

Browse files
committed
Use references instead of pointers where it's possible
In our case DDBrowserDB shares lifetime scope with MainWindow, so there's no need to pass pointers back and forth.
1 parent 5697e47 commit 210916c

13 files changed

Lines changed: 72 additions & 72 deletions

‎src/CreateIndexDialog.cpp‎

Lines changed: 5 additions & 5 deletions

‎src/CreateIndexDialog.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class CreateIndexDialog : public QDialog
1414
Q_OBJECT
1515

1616
public:
17-
explicit CreateIndexDialog(DBBrowserDB* db, QWidget* parent = 0);
17+
explicit CreateIndexDialog(DBBrowserDB& db, QWidget* parent = 0);
1818
~CreateIndexDialog();
1919

2020
private slots:
@@ -23,7 +23,7 @@ private slots:
2323
void checkInput();
2424

2525
private:
26-
DBBrowserDB* pdb;
26+
DBBrowserDB& pdb;
2727
Ui::CreateIndexDialog* ui;
2828
};
2929

‎src/DbStructureModel.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ QMimeData* DbStructureModel::mimeData(const QModelIndexList& indices) const
229229
// If it is a table also add the content
230230
if(data(index.sibling(index.row(), 1), Qt::DisplayRole).toString() == "table")
231231
{
232-
SqliteTableModel tableModel(0, &m_db);
232+
SqliteTableModel tableModel(m_db);
233233
tableModel.setTable(data(index.sibling(index.row(), 0), Qt::DisplayRole).toString());
234234
for(int i=0; i < tableModel.rowCount(); ++i)
235235
{

‎src/EditTableDialog.cpp‎

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#include <QDateTime>
1111
#include <QKeyEvent>
1212

13-
EditTableDialog::EditTableDialog(DBBrowserDB* db, const QString& tableName, bool createTable, QWidget* parent)
13+
EditTableDialog::EditTableDialog(DBBrowserDB& db, const QString& tableName, bool createTable, QWidget* parent)
1414
: QDialog(parent),
1515
ui(new Ui::EditTableDialog),
1616
pdb(db),
@@ -28,7 +28,7 @@ EditTableDialog::EditTableDialog(DBBrowserDB* db, const QString& tableName, bool
2828
if(m_bNewTable == false)
2929
{
3030
// Existing table, so load and set the current layout
31-
QString sTablesql = pdb->getObjectByName(curTable).getsql();
31+
QString sTablesql = pdb.getObjectByName(curTable).getsql();
3232
QPair<sqlb::Table, bool> parse_result = sqlb::Table::parseSQL(sTablesql);
3333
m_table = parse_result.first;
3434
ui->labelEditWarning->setVisible(!parse_result.second);
@@ -39,7 +39,7 @@ EditTableDialog::EditTableDialog(DBBrowserDB* db, const QString& tableName, bool
3939
}
4040

4141
// And create a savepoint
42-
pdb->setSavepoint(m_sRestorePointName);
42+
pdb.setSavepoint(m_sRestorePointName);
4343

4444
// Update UI
4545
ui->editTableName->setText(curTable);
@@ -138,12 +138,12 @@ void EditTableDialog::accept()
138138
if(m_bNewTable)
139139
{
140140
// Creation of new table
141-
if(!pdb->executeSQL(m_table.sql()))
141+
if(!pdb.executeSQL(m_table.sql()))
142142
{
143143
QMessageBox::warning(
144144
this,
145145
QApplication::applicationName(),
146-
tr("Error creating table. Message from database engine:\n%1").arg(pdb->lastErrorMessage));
146+
tr("Error creating table. Message from database engine:\n%1").arg(pdb.lastErrorMessage));
147147
return;
148148
}
149149
} else {
@@ -152,9 +152,9 @@ void EditTableDialog::accept()
152152
// Rename table if necessary
153153
if(ui->editTableName->text() != curTable)
154154
{
155-
if(!pdb->renameTable(curTable, ui->editTableName->text()))
155+
if(!pdb.renameTable(curTable, ui->editTableName->text()))
156156
{
157-
QMessageBox::warning(this, QApplication::applicationName(), pdb->lastErrorMessage);
157+
QMessageBox::warning(this, QApplication::applicationName(), pdb.lastErrorMessage);
158158
return;
159159
}
160160
}
@@ -166,7 +166,7 @@ void EditTableDialog::accept()
166166
void EditTableDialog::reject()
167167
{
168168
// Then rollback to our savepoint
169-
pdb->revertToSavepoint(m_sRestorePointName);
169+
pdb.revertToSavepoint(m_sRestorePointName);
170170

171171
QDialog::reject();
172172
}
@@ -206,7 +206,7 @@ void EditTableDialog::updateTypes()
206206

207207
m_table.fields().at(index)->setType(type);
208208
if(!m_bNewTable)
209-
pdb->renameColumn(curTable, column, m_table.fields().at(index));
209+
pdb.renameColumn(curTable, column, m_table.fields().at(index));
210210
checkInput();
211211
}
212212
}
@@ -239,7 +239,7 @@ void EditTableDialog::itemChanged(QTreeWidgetItem *item, int column)
239239
if(!m_bNewTable)
240240
{
241241
sqlb::FieldVector pk = m_table.primaryKey();
242-
foreach(const DBBrowserObject& fkobj, pdb->objMap.values("table"))
242+
foreach(const DBBrowserObject& fkobj, pdb.objMap.values("table"))
243243
{
244244
QList<sqlb::ConstraintPtr> fks = fkobj.table.constraints(sqlb::FieldVector(), sqlb::Constraint::ForeignKeyConstraintType);
245245
foreach(sqlb::ConstraintPtr fkptr, fks)
@@ -303,9 +303,9 @@ void EditTableDialog::itemChanged(QTreeWidgetItem *item, int column)
303303
// Because our renameColumn() function fails when setting a column to Not Null when it already contains some NULL values
304304
// we need to check for this case and cancel here. Maybe we can think of some way to modify the INSERT INTO ... SELECT statement
305305
// to at least replace all troublesome NULL values by the default value
306-
SqliteTableModel m(this, pdb);
306+
SqliteTableModel m(pdb, this);
307307
m.setQuery(QString("SELECT COUNT(%1) FROM %2 WHERE %3 IS NULL;")
308-
.arg(sqlb::escapeIdentifier(pdb->getObjectByName(curTable).table.rowidColumn()))
308+
.arg(sqlb::escapeIdentifier(pdb.getObjectByName(curTable).table.rowidColumn()))
309309
.arg(sqlb::escapeIdentifier(curTable))
310310
.arg(sqlb::escapeIdentifier(field->name())));
311311
if(m.data(m.index(0, 0)).toInt() > 0)
@@ -330,7 +330,7 @@ void EditTableDialog::itemChanged(QTreeWidgetItem *item, int column)
330330
// First check if the contents of this column are all integers. If not this field cannot be set to AI
331331
if(!m_bNewTable)
332332
{
333-
SqliteTableModel m(this, pdb);
333+
SqliteTableModel m(pdb, this);
334334
m.setQuery(QString("SELECT COUNT(*) FROM %1 WHERE %2 <> CAST(%3 AS INTEGER);")
335335
.arg(sqlb::escapeIdentifier(curTable))
336336
.arg(sqlb::escapeIdentifier(field->name()))
@@ -374,7 +374,7 @@ void EditTableDialog::itemChanged(QTreeWidgetItem *item, int column)
374374
if(!m_bNewTable && item->checkState(column) == Qt::Checked)
375375
{
376376
// Because our renameColumn() function fails when setting a column to unique when it already contains the same values
377-
SqliteTableModel m(this, pdb);
377+
SqliteTableModel m(pdb, this);
378378
m.setQuery(QString("SELECT COUNT(%2) FROM %1;").arg(sqlb::escapeIdentifier(curTable)).arg(sqlb::escapeIdentifier(field->name())));
379379
int rowcount = m.data(m.index(0, 0)).toInt();
380380
m.setQuery(QString("SELECT COUNT(DISTINCT %2) FROM %1;").arg(sqlb::escapeIdentifier(curTable)).arg(sqlb::escapeIdentifier(field->name())));
@@ -441,7 +441,7 @@ void EditTableDialog::itemChanged(QTreeWidgetItem *item, int column)
441441
}
442442

443443
if(callRenameColumn)
444-
pdb->renameColumn(curTable, oldFieldName, field);
444+
pdb.renameColumn(curTable, oldFieldName, field);
445445
}
446446

447447
checkInput();
@@ -498,7 +498,7 @@ void EditTableDialog::addField()
498498

499499
// Actually add the new column to the table if we're editing an existing table
500500
if(!m_bNewTable)
501-
pdb->addColumn(curTable, f);
501+
pdb.addColumn(curTable, f);
502502

503503
checkInput();
504504
}
@@ -526,12 +526,12 @@ void EditTableDialog::removeField()
526526
QString msg = tr("Are you sure you want to delete the field '%1'?\nAll data currently stored in this field will be lost.").arg(ui->treeWidget->currentItem()->text(0));
527527
if(QMessageBox::warning(this, QApplication::applicationName(), msg, QMessageBox::Yes | QMessageBox::No, QMessageBox::No) == QMessageBox::Yes)
528528
{
529-
if(!pdb->renameColumn(curTable, ui->treeWidget->currentItem()->text(0), sqlb::FieldPtr()))
529+
if(!pdb.renameColumn(curTable, ui->treeWidget->currentItem()->text(0), sqlb::FieldPtr()))
530530
{
531-
QMessageBox::warning(0, QApplication::applicationName(), pdb->lastErrorMessage);
531+
QMessageBox::warning(0, QApplication::applicationName(), pdb.lastErrorMessage);
532532
} else {
533533
//relayout
534-
QString sTablesql = pdb->getObjectByName(curTable).getsql();
534+
QString sTablesql = pdb.getObjectByName(curTable).getsql();
535535
m_table = sqlb::Table::parseSQL(sTablesql).first;
536536
populateFields();
537537
}
@@ -604,17 +604,17 @@ void EditTableDialog::moveCurrentField(bool down)
604604
// Editing an old one
605605

606606
// Move the actual column
607-
if(!pdb->renameColumn(
607+
if(!pdb.renameColumn(
608608
curTable,
609609
ui->treeWidget->currentItem()->text(0),
610610
m_table.fields().at(ui->treeWidget->indexOfTopLevelItem(ui->treeWidget->currentItem())),
611611
(down ? 1 : -1)
612612
))
613613
{
614-
QMessageBox::warning(0, QApplication::applicationName(), pdb->lastErrorMessage);
614+
QMessageBox::warning(0, QApplication::applicationName(), pdb.lastErrorMessage);
615615
} else {
616616
// Reload table SQL
617-
QString sTablesql = pdb->getObjectByName(curTable).getsql();
617+
QString sTablesql = pdb.getObjectByName(curTable).getsql();
618618
m_table = sqlb::Table::parseSQL(sTablesql).first;
619619
populateFields();
620620

‎src/EditTableDialog.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class EditTableDialog : public QDialog
1717
Q_OBJECT
1818

1919
public:
20-
explicit EditTableDialog(DBBrowserDB* pdb, const QString& tableName, bool createTable, QWidget* parent = 0);
20+
explicit EditTableDialog(DBBrowserDB& pdb, const QString& tableName, bool createTable, QWidget* parent = 0);
2121
~EditTableDialog();
2222

2323
protected:
@@ -60,7 +60,7 @@ private slots:
6060

6161
private:
6262
Ui::EditTableDialog* ui;
63-
DBBrowserDB* pdb;
63+
DBBrowserDB& pdb;
6464
QString curTable;
6565
sqlb::Table m_table;
6666
QStringList types;

‎src/ExportDataDialog.cpp‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#include <QJsonArray>
1414
#include <QJsonObject>
1515

16-
ExportDataDialog::ExportDataDialog(DBBrowserDB* db, ExportFormats format, QWidget* parent, const QString& query, const QString& selection)
16+
ExportDataDialog::ExportDataDialog(DBBrowserDB& db, ExportFormats format, QWidget* parent, const QString& query, const QString& selection)
1717
: QDialog(parent),
1818
ui(new Ui::ExportDataDialog),
1919
pdb(db),
@@ -40,7 +40,7 @@ ExportDataDialog::ExportDataDialog(DBBrowserDB* db, ExportFormats format, QWidge
4040
if(query.isEmpty())
4141
{
4242
// Get list of tables to export
43-
objectMap objects = pdb->getBrowsableObjects();
43+
objectMap objects = pdb.getBrowsableObjects();
4444
foreach(const DBBrowserObject& obj, objects)
4545
ui->listTables->addItem(new QListWidgetItem(QIcon(QString(":icons/%1").arg(obj.gettype())), obj.getname()));
4646

@@ -101,7 +101,7 @@ bool ExportDataDialog::exportQueryCsv(const QString& sQuery, const QString& sFil
101101
QByteArray utf8Query = sQuery.toUtf8();
102102
sqlite3_stmt *stmt;
103103

104-
int status = sqlite3_prepare_v2(pdb->_db, utf8Query.data(), utf8Query.size(), &stmt, NULL);
104+
int status = sqlite3_prepare_v2(pdb._db, utf8Query.data(), utf8Query.size(), &stmt, NULL);
105105
if(SQLITE_OK == status)
106106
{
107107
if(ui->checkHeader->isChecked())
@@ -177,7 +177,7 @@ bool ExportDataDialog::exportQueryJson(const QString& sQuery, const QString& sFi
177177
{
178178
QByteArray utf8Query = sQuery.toUtf8();
179179
sqlite3_stmt *stmt;
180-
int status = sqlite3_prepare_v2(pdb->_db, utf8Query.data(), utf8Query.size(), &stmt, NULL);
180+
int status = sqlite3_prepare_v2(pdb._db, utf8Query.data(), utf8Query.size(), &stmt, NULL);
181181

182182
QJsonArray json_table;
183183

‎src/ExportDataDialog.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class ExportDataDialog : public QDialog
2020
ExportFormatJson,
2121
};
2222

23-
explicit ExportDataDialog(DBBrowserDB* db, ExportFormats format, QWidget* parent = 0, const QString& query = "", const QString& selection = "");
23+
explicit ExportDataDialog(DBBrowserDB& db, ExportFormats format, QWidget* parent = 0, const QString& query = "", const QString& selection = "");
2424
~ExportDataDialog();
2525

2626
private slots:
@@ -43,7 +43,7 @@ private slots:
4343

4444
private:
4545
Ui::ExportDataDialog* ui;
46-
DBBrowserDB* pdb;
46+
DBBrowserDB& pdb;
4747

4848
ExportFormats m_format;
4949

‎src/MainWindow.cpp‎

Lines changed: 7 additions & 7 deletions

0 commit comments

Comments
 (0)