Code refactoring

This changes the class structure in the sqlb namespace as well as the
DBBrowserObject class. The rest of the commit are changes that are
required by the modifications in sqlb and DBBrowserObject.

The idea behind this refactoring is this: we currently have the
DBBrowserObject class which holds some basic information about the
database object (name, type, SQL string, etc.). It also contains a
sqlb::Table and a sqlb::Index object. Those are used if the type of
the object is table or index and they contain a whole lot more
information on the object than the DBBrowserObject class, including the
name, the type, the SQL string, etc.

So we have a duplication here. There are two class structures for
storing the same information. This has historic reasons but other than
that there is no point in keeping it this way. With this commit I start
the work of consolidating the sqlb classes in order to get rid of the
DBBrowserObject class entirely.

This commit only starts this task, it doesn't finish it. This is why it
is a little messy here and there, but then again the old structure was a
little messy, too. We will need at least a very basic trigger and view
parser before finishing this is even possible. When this is done, I hope
the ode will be much easier to read and understand. But even in the
current state there already is some progress: we save a little bit of
memory, don't copy big objects all the time anymore, and replace a lot
of unnecessary string comparisons with integer comparisons.
This commit is contained in:
Martin Kleusberg
2017-01-20 17:31:54 +01:00
parent e3ef13a916
commit e5a79ec0fa
12 changed files with 252 additions and 164 deletions

View File

@@ -33,11 +33,11 @@ EditTableDialog::EditTableDialog(DBBrowserDB& db, const QString& tableName, bool
if(m_bNewTable == false)
{
// Existing table, so load and set the current layout
DBBrowserObject obj = pdb.getObjectByName(curTable);
QString sTablesql = obj.getsql();
QPair<sqlb::Table, bool> parse_result = sqlb::Table::parseSQL(sTablesql);
m_table = parse_result.first;
m_table.setTemporary(obj.isTemporary());
sqlb::TablePtr obj = pdb.getObjectByName(curTable).dynamicCast<sqlb::Table>();
QString sTablesql = obj->originalSql();
QPair<sqlb::ObjectPtr, bool> parse_result = sqlb::Table::parseSQL(sTablesql);
m_table = *(parse_result.first.dynamicCast<sqlb::Table>());
m_table.setTemporary(obj->isTemporary());
ui->labelEditWarning->setVisible(!parse_result.second);
// Set without rowid and temporary checkboxex. No need to trigger any events here as we're only loading a table exactly as it is stored by SQLite, so no need
@@ -278,7 +278,7 @@ void EditTableDialog::itemChanged(QTreeWidgetItem *item, int column)
sqlb::FieldVector pk = m_table.primaryKey();
foreach(const DBBrowserObject& fkobj, pdb.objMap.values("table"))
{
QList<sqlb::ConstraintPtr> fks = fkobj.table.constraints(sqlb::FieldVector(), sqlb::Constraint::ForeignKeyConstraintType);
QList<sqlb::ConstraintPtr> fks = fkobj.object.dynamicCast<sqlb::Table>()->constraints(sqlb::FieldVector(), sqlb::Constraint::ForeignKeyConstraintType);
foreach(sqlb::ConstraintPtr fkptr, fks)
{
QSharedPointer<sqlb::ForeignKeyClause> fk = fkptr.dynamicCast<sqlb::ForeignKeyClause>();
@@ -350,7 +350,7 @@ void EditTableDialog::itemChanged(QTreeWidgetItem *item, int column)
// to at least replace all troublesome NULL values by the default value
SqliteTableModel m(pdb, this);
m.setQuery(QString("SELECT COUNT(%1) FROM %2 WHERE %3 IS NULL;")
.arg(sqlb::escapeIdentifier(pdb.getObjectByName(curTable).table.rowidColumn()))
.arg(sqlb::escapeIdentifier(pdb.getObjectByName(curTable).dynamicCast<sqlb::Table>()->rowidColumn()))
.arg(sqlb::escapeIdentifier(curTable))
.arg(sqlb::escapeIdentifier(field->name())));
if(m.data(m.index(0, 0)).toInt() > 0)
@@ -578,8 +578,7 @@ void EditTableDialog::removeField()
QMessageBox::warning(0, QApplication::applicationName(), pdb.lastError());
} else {
//relayout
QString sTablesql = pdb.getObjectByName(curTable).getsql();
m_table = sqlb::Table::parseSQL(sTablesql).first;
m_table = *(pdb.getObjectByName(curTable).dynamicCast<sqlb::Table>());
populateFields();
}
}
@@ -662,8 +661,7 @@ void EditTableDialog::moveCurrentField(bool down)
QMessageBox::warning(0, QApplication::applicationName(), pdb.lastError());
} else {
// Reload table SQL
QString sTablesql = pdb.getObjectByName(curTable).getsql();
m_table = sqlb::Table::parseSQL(sTablesql).first;
m_table = *(pdb.getObjectByName(curTable).dynamicCast<sqlb::Table>());
populateFields();
// Select old item at new position