Parse and save foreign key constraints

When parsing a CREATE TABLE statement read and try to parse any foreign
key constraints and save that information.
This commit is contained in:
Martin Kleusberg
2014-11-06 18:28:28 +01:00
parent fca39e9e1c
commit f2c3c2b1a4
2 changed files with 30 additions and 0 deletions

View File

@@ -190,6 +190,14 @@ QString Table::sql() const
if(pks_found)
sql += pk + ")";
}
// foreign keys
foreach(FieldPtr f, m_fields)
{
if(!f->foreignKey().isEmpty())
sql += QString(",\n\tFOREIGN KEY(`%1`) REFERENCES %2").arg(f->name()).arg(f->foreignKey());
}
sql += "\n)";
// without rowid
@@ -345,6 +353,25 @@ Table CreateTableWalker::table()
}
}
break;
case sqlite3TokenTypes::FOREIGN:
{
tc = tc->getNextSibling(); // FOREIGN
tc = tc->getNextSibling(); // KEY
tc = tc->getNextSibling(); // LPAREN
QString column_name = identifier(tc);
tc = tc->getNextSibling(); // identifier
if(tc->getType() == sqlite3TokenTypes::COMMA)
{
// No support for composite foreign keys
m_bModifySupported = false;
break;
}
tc = tc->getNextSibling(); // RPAREN
tc = tc->getNextSibling(); // REFERENCES
tab.fields().at(tab.findField(column_name))->setForeignKey(concatTextAST(tc, true));
}
break;
default:
{
m_bModifySupported = false;