Skip to content

Commit 13b15dd

Browse files
committed
parser: Refactor parsing and handling of constraints
This commit refactors the handling of constraints and continues the work started in 43107ea. Instead of heavily relying on runtime polymorphism with these changes we move towards more compile-time polymorphism. Noticeable the constraint data types do not have a type function any longer; instead they are differentiated by their actual data type. This has several benefits: - More likely to catch errors at compile-time and therefore avoid them at runtime. - The program runs faster. This should be most noticeable for databases with a very large amount of tables or other database objects. - The code should be easier to read and maintain. Additionally this should also prepare for even more refactoring in the future.
1 parent dd62577 commit 13b15dd

File tree

7 files changed

+1689
-1834
lines changed

7 files changed

+1689
-1834
lines changed

src/EditTableDialog.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ void EditTableDialog::populateIndexConstraints()
286286
QComboBox* type = new QComboBox(this);
287287
type->addItem(tr("Primary Key"), "pk");
288288
type->addItem(tr("Unique"), "u");
289-
type->setCurrentIndex(it.second->type());
289+
type->setCurrentIndex(it.second->isPrimaryKey() ? 0 : 1);
290290
connect(type, static_cast<void(QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this, [this, type, it](int index) {
291291
// Handle change of constraint type. Effectively this means removing the old constraint and replacing it by an entirely new one.
292292

@@ -298,7 +298,7 @@ void EditTableDialog::populateIndexConstraints()
298298

299299
// Set combo box back to original constraint type
300300
type->blockSignals(true);
301-
type->setCurrentIndex(it.second->type());
301+
type->setCurrentIndex(it.second->isPrimaryKey() ? 0 : 1);
302302
type->blockSignals(false);
303303
return;
304304
}
@@ -307,7 +307,6 @@ void EditTableDialog::populateIndexConstraints()
307307
m_table.removeConstraint(it.second);
308308

309309
// Add new constraint depending on selected type
310-
sqlb::ConstraintPtr new_constraint;
311310
if(type->itemData(index).toString() == "pk")
312311
{
313312
auto pk = std::make_shared<sqlb::PrimaryKeyConstraint>();
@@ -902,9 +901,9 @@ void EditTableDialog::indexConstraintItemDoubleClicked(QTableWidgetItem* item)
902901
{
903902
// Remove the constraint with the old columns and add a new one with the new columns
904903
m_table.removeConstraint(constraint);
905-
if(constraint->type() == sqlb::Constraint::PrimaryKeyConstraintType)
904+
if(constraint->isPrimaryKey())
906905
m_table.addConstraint(new_columns, std::dynamic_pointer_cast<sqlb::PrimaryKeyConstraint>(constraint));
907-
else if(constraint->type() == sqlb::Constraint::UniqueConstraintType)
906+
else
908907
m_table.addConstraint(new_columns, std::dynamic_pointer_cast<sqlb::UniqueConstraint>(constraint));
909908

910909
// Update the UI

src/sql/parser/ParserDriver.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ namespace parser
1818

1919
class ParserDriver
2020
{
21+
friend parser;
22+
2123
public:
2224
ParserDriver();
2325

@@ -51,6 +53,10 @@ class ParserDriver
5153
// Scanner buffer
5254
typedef struct yy_buffer_state* YY_BUFFER_STATE;
5355
YY_BUFFER_STATE buffer;
56+
57+
// This is a purely internal variable used for parsing CREATE TABLE statements. It stores a pointer to the table column which
58+
// is currently being parsed. This allows us to modify the column and add additional constraints when we see them.
59+
std::shared_ptr<sqlb::Field> last_table_column;
5460
};
5561

5662
}

0 commit comments

Comments
 (0)