Permalink
Browse files

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 677d36074afe7add7b54984a93fba97b8349f061
Showing with 5 additions and 4 deletions.
  1. +5 −4 src/sqlitetypes.cpp
@@ -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?
@@ -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
@@ -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:
@@ -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;

0 comments on commit 677d360

Please sign in to comment.