Skip to content

Commit

Permalink
Overload the assignment operators for the Table and Index classes
Browse files Browse the repository at this point in the history
This fixes issues that assigning the main representation of the database
in DBBrowserDB::objectMap is modified when playing around with the
database in the Edit Table and Edit Index dialogs.

These bugs were caused by my refactoring. Let's hope there's not much
more fallout from this refactoring...
  • Loading branch information
MKleusberg committed Jan 23, 2017
1 parent 38144bb commit b6b94a8
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
48 changes: 48 additions & 0 deletions src/sqlitetypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,37 @@ Table::~Table()
clear();
}

Table& Table::operator=(const Table& rhs)
{
// Base class
Object::operator=(rhs);

// Just assign the strings
m_rowidColumn = rhs.m_rowidColumn;
m_virtual = rhs.m_virtual;

// Make copies of the fields and the constraints. This is necessary in order to avoid any unwanted changes to the application's main database
// schema representation just modifying a reference to the fields or constraints and thinking it operates on a copy.
foreach(FieldPtr f, rhs.m_fields)
addField(FieldPtr(new Field(*f)));
for(auto it=rhs.m_constraints.constBegin();it!=rhs.m_constraints.constEnd();++it) // TODO This is so ugly, it should be replaced really by anything else
{
FieldVector key;
ConstraintPtr constraint;
foreach(FieldPtr f, it.key())
key.push_back(m_fields.at(findField(f->name())));
if(it.value()->type() == Constraint::ConstraintTypes::PrimaryKeyConstraintType)
constraint = ConstraintPtr(new PrimaryKeyConstraint(*(it.value().dynamicCast<PrimaryKeyConstraint>())));
else if(it.value()->type() == Constraint::ConstraintTypes::UniqueConstraintType)
constraint = ConstraintPtr(new UniqueConstraint(*(it.value().dynamicCast<UniqueConstraint>())));
else if(it.value()->type() == Constraint::ConstraintTypes::ForeignKeyConstraintType)
constraint = ConstraintPtr(new ForeignKeyClause(*(it.value().dynamicCast<ForeignKeyClause>())));
addConstraint(key, constraint);
}

return *this;
}

void Table::addField(const FieldPtr& f)
{
m_fields.append(FieldPtr(f));
Expand Down Expand Up @@ -942,6 +973,23 @@ Index::~Index()
clear();
}

Index& Index::operator=(const Index& rhs)
{
// Base class
Object::operator=(rhs);

// Just assign the easy stuff
m_unique = rhs.m_unique;
m_table = rhs.m_table;
m_whereExpr = rhs.m_whereExpr;

// Make copies of the column
foreach(IndexedColumnPtr c, rhs.m_columns)
addColumn(IndexedColumnPtr(new IndexedColumn(*c)));

return *this;
}

void Index::clear()
{
m_name.clear();
Expand Down
2 changes: 2 additions & 0 deletions src/sqlitetypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ class Table : public Object
public:
explicit Table(const QString& name): Object(name), m_rowidColumn("_rowid_") {}
virtual ~Table();
Table& operator=(const Table& rhs);

virtual Types type() const { return Object::Table; }

Expand Down Expand Up @@ -337,6 +338,7 @@ class Index : public Object
public:
explicit Index(const QString& name): Object(name), m_unique(false) {}
virtual ~Index();
Index& operator=(const Index& rhs);

virtual Types type() const { return Object::Index; }

Expand Down

0 comments on commit b6b94a8

Please sign in to comment.