Skip to content

Commit

Permalink
grammar: Detect parse errors in primary key and unique constraints
Browse files Browse the repository at this point in the history
When having a table definition like this, detect the parse errors in the
table constraints correctly:

CREATE TABLE test(
	a INTEGER,
	b TEXT,
	PRIMARY KEY(a ASC),		-- ASC here
	UNIQUE(b COLLATE NOCASE)	-- COLLATE here
);

The next step is to actually parse and store this information.
  • Loading branch information
MKleusberg committed Feb 17, 2017
1 parent 9ea807f commit fd1f4f2
Showing 1 changed file with 39 additions and 14 deletions.
53 changes: 39 additions & 14 deletions src/sqlitetypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -673,25 +673,38 @@ TablePtr CreateTableWalker::table()
FieldVector fields;
do
{
QString col = columnname(tc);
antlr::RefAST indexed_column = tc->getFirstChild();

QString col = columnname(indexed_column);
FieldPtr field = tab->field(tab->findField(col));
fields.push_back(field);

tc = tc->getNextSibling();
if(tc != antlr::nullAST
&& (tc->getType() == sqlite3TokenTypes::ASC
|| tc->getType() == sqlite3TokenTypes::DESC))
indexed_column = indexed_column->getNextSibling();
if(indexed_column != antlr::nullAST
&& (indexed_column->getType() == sqlite3TokenTypes::ASC
|| indexed_column->getType() == sqlite3TokenTypes::DESC))
{
// TODO save ASC / DESC information?
tab->setFullyParsed(false);
tc = tc->getNextSibling();
indexed_column = indexed_column->getNextSibling();
}

if(indexed_column != antlr::nullAST && indexed_column->getType() == sqlite3TokenTypes::COLLATE)
{
indexed_column = indexed_column->getNextSibling(); // COLLATE
// TODO save collation name
tab->setFullyParsed(false);
indexed_column = indexed_column->getNextSibling(); // collation name
}

if(tc != antlr::nullAST && tc->getType() == sqlite3TokenTypes::AUTOINCREMENT)
if(indexed_column != antlr::nullAST && indexed_column->getType() == sqlite3TokenTypes::AUTOINCREMENT)
{
field->setAutoIncrement(true);
tc = tc->getNextSibling();
indexed_column = indexed_column->getNextSibling();
}

tc = tc->getNextSibling(); // indexed column

while(tc != antlr::nullAST && tc->getType() == sqlite3TokenTypes::COMMA)
{
tc = tc->getNextSibling(); // skip ident and comma
Expand All @@ -711,20 +724,32 @@ TablePtr CreateTableWalker::table()
FieldVector fields;
do
{
QString col = columnname(tc);
antlr::RefAST indexed_column = tc->getFirstChild();

QString col = columnname(indexed_column);
FieldPtr field = tab->field(tab->findField(col));
fields.push_back(field);

tc = tc->getNextSibling();
if(tc != antlr::nullAST
&& (tc->getType() == sqlite3TokenTypes::ASC
|| tc->getType() == sqlite3TokenTypes::DESC))
indexed_column = indexed_column->getNextSibling();
if(indexed_column != antlr::nullAST
&& (indexed_column->getType() == sqlite3TokenTypes::ASC
|| indexed_column->getType() == sqlite3TokenTypes::DESC))
{
// TODO save ASC / DESC information?
tab->setFullyParsed(false);
tc = tc->getNextSibling();
indexed_column = indexed_column->getNextSibling();
}

if(indexed_column != antlr::nullAST && indexed_column->getType() == sqlite3TokenTypes::COLLATE)
{
indexed_column = indexed_column->getNextSibling(); // COLLATE
// TODO save collation name
tab->setFullyParsed(false);
indexed_column = indexed_column->getNextSibling(); // collation name
}

tc = tc->getNextSibling(); // indexed column

while(tc != antlr::nullAST && tc->getType() == sqlite3TokenTypes::COMMA)
{
tc = tc->getNextSibling(); // skip ident and comma
Expand Down

0 comments on commit fd1f4f2

Please sign in to comment.