Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
grammar: Add support for parsing multiple FKs in column constraints
This adds support for multiple foreign keys in a column constraint.
Example:

CREATE TABLE t1(a int, b int);
CREATE TABLE t2(a int, b int);
CREATE TABLE test(
    x int REFERENCES t1(a) REFERENCES t2(a),	-- This is now supported
    y int
);

Multiple foreign keys if they are a table constraint were already
supported before.
  • Loading branch information
MKleusberg committed Sep 15, 2017
1 parent d73fbfc commit 677d360
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/sqlitetypes.cpp
Expand Up @@ -929,7 +929,7 @@ void CreateTableWalker::parsecolumn(Table* table, antlr::RefAST c)
QString check;
QString collation;
sqlb::PrimaryKeyConstraint* primaryKey = nullptr;
sqlb::ForeignKeyClause* foreignKey = nullptr;
QVector<sqlb::ForeignKeyClause*> foreignKeys;

colname = columnname(c);
c = c->getNextSibling(); //type?
Expand Down Expand Up @@ -1028,7 +1028,7 @@ void CreateTableWalker::parsecolumn(Table* table, antlr::RefAST c)
{
con = con->getNextSibling(); // REFERENCES

foreignKey = new ForeignKeyClause;
sqlb::ForeignKeyClause* foreignKey = new ForeignKeyClause;
foreignKey->setTable(identifier(con));
foreignKey->setName(constraint_name);
con = con->getNextSibling(); // identifier
Expand All @@ -1050,6 +1050,7 @@ void CreateTableWalker::parsecolumn(Table* table, antlr::RefAST c)
}

foreignKey->setConstraint(concatTextAST(con, true));
foreignKeys.push_back(foreignKey);
}
break;
case sqlite3TokenTypes::COLLATE:
Expand All @@ -1073,8 +1074,8 @@ void CreateTableWalker::parsecolumn(Table* table, antlr::RefAST c)
f->setAutoIncrement(autoincrement);
table->addField(f);

if(foreignKey)
table->addConstraint({f}, ConstraintPtr(foreignKey));
foreach(sqlb::ForeignKeyClause* fk, foreignKeys)
table->addConstraint({f}, ConstraintPtr(fk));
if(primaryKey)
{
FieldVector v;
Expand Down

0 comments on commit 677d360

Please sign in to comment.