Skip to content

Commit

Permalink
grammar: Parse collations in column definitions
Browse files Browse the repository at this point in the history
Parse the COLLATE term in CREATE TABLE statements like these:

CREATE TABLE test(a TEXT COLLATE NOCASE);

See issue sqlitebrowser#411.
  • Loading branch information
MKleusberg committed Feb 17, 2017
1 parent bef6dc0 commit 9ea807f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
12 changes: 11 additions & 1 deletion src/sqlitetypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ QString Field::toString(const QString& indent, const QString& sep) const
str += " PRIMARY KEY AUTOINCREMENT";
if(m_unique)
str += " UNIQUE";
if(!m_collation.isEmpty())
str += " COLLATE " + m_collation;
return str;
}

Expand Down Expand Up @@ -819,6 +821,7 @@ void CreateTableWalker::parsecolumn(Table* table, antlr::RefAST c)
bool unique = false;
QString defaultvalue;
QString check;
QString collation;
sqlb::PrimaryKeyConstraint* primaryKey = 0;
sqlb::ForeignKeyClause* foreignKey = 0;

Expand Down Expand Up @@ -938,6 +941,13 @@ void CreateTableWalker::parsecolumn(Table* table, antlr::RefAST c)
foreignKey->setConstraint(concatTextAST(con, true));
}
break;
case sqlite3TokenTypes::COLLATE:
{
con = con->getNextSibling(); // COLLATE
collation = identifier(con);
con = con->getNextSibling(); // collation name
}
break;
default:
{
table->setFullyParsed(false);
Expand All @@ -947,7 +957,7 @@ void CreateTableWalker::parsecolumn(Table* table, antlr::RefAST c)
c = c->getNextSibling();
}

FieldPtr f = FieldPtr(new Field(colname, type, notnull, defaultvalue, check, unique));
FieldPtr f = FieldPtr(new Field(colname, type, notnull, defaultvalue, check, unique, collation));
f->setAutoIncrement(autoincrement);
table->addField(f);

Expand Down
7 changes: 6 additions & 1 deletion src/sqlitetypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -191,14 +191,16 @@ class Field
bool notnull = false,
const QString& defaultvalue = "",
const QString& check = "",
bool unique = false)
bool unique = false,
const QString& collation = QString())
: m_name(name)
, m_type(type)
, m_notnull(notnull)
, m_check(check)
, m_defaultvalue(defaultvalue)
, m_autoincrement(false)
, m_unique(unique)
, m_collation(collation)
{}

QString toString(const QString& indent = "\t", const QString& sep = "\t") const;
Expand All @@ -210,6 +212,7 @@ class Field
void setDefaultValue(const QString& defaultvalue) { m_defaultvalue = defaultvalue; }
void setAutoIncrement(bool autoinc) { m_autoincrement = autoinc; }
void setUnique(bool u) { m_unique = u; }
void setCollation(const QString& collation) { m_collation = collation; }

bool isText() const;
bool isInteger() const;
Expand All @@ -221,6 +224,7 @@ class Field
const QString& defaultValue() const { return m_defaultvalue; }
bool autoIncrement() const { return m_autoincrement; }
bool unique() const { return m_unique; }
const QString& collation() const { return m_collation; }

static QStringList Datatypes;
private:
Expand All @@ -231,6 +235,7 @@ class Field
QString m_defaultvalue;
bool m_autoincrement; //! this is stored here for simplification
bool m_unique;
QString m_collation;
};

class Table : public Object
Expand Down

0 comments on commit 9ea807f

Please sign in to comment.