Skip to content

Commit

Permalink
QgsSQLStatement::quotedIdentifierIfNeeded(): make it quote SQL reserv…
Browse files Browse the repository at this point in the history
…ed keywords
  • Loading branch information
rouault committed May 12, 2016
1 parent 84a797e commit 8259b3f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
16 changes: 16 additions & 0 deletions src/core/qgssqlstatement.cpp
Expand Up @@ -73,6 +73,22 @@ QString QgsSQLStatement::quotedIdentifier( QString name )

QString QgsSQLStatement::quotedIdentifierIfNeeded( QString name )
{
// This might not be complete, but it must be at least what we recognize
static const char* const reservedKeyWords[] =
{
"AND", "OR", "NOT", "LIKE", "IN", "IS", "BETWEEN", "NULL", "SELECT", "ALL", "DISTINCT", "CAST", "AS",
"FROM", "JOIN", "ON", "USING", "WHERE", "ORDER", "BY", "ASC", "DESC",
"LEFT", "RIGHT", "INNER", "OUTER", "CROSS", "FULL", "NATURAL", "UNION",
"OFFSET", "LIMIT", "GROUP", "HAVING"
};

for ( size_t i = 0; i < sizeof( reservedKeyWords ) / sizeof( reservedKeyWords[0] ); ++i )
{
if ( name.compare( QString( reservedKeyWords[i] ), Qt::CaseInsensitive ) == 0 )
{
return quotedIdentifier( name );
}
}
return identifierRE.exactMatch( name ) ? name : quotedIdentifier( name );
}

Expand Down
4 changes: 2 additions & 2 deletions tests/src/python/test_qgssqlstatement.py
Expand Up @@ -56,10 +56,10 @@ def testNominalSelectDistinct(self):
def testNominalColumns(self):
statement = "SELECT null, 1234567890123456789, a, b b_alias, 'literal', CAST(1 AS varchar), "
statement += "\"1c\", *, \"*\", a.*, foo(), bar(baz, baw), t.c AS \"1quoted\", "
statement += "COUNT(*), COUNT(*) a, COUNT(DISTINCT x), COUNT(DISTINCT x) AS a FROM t"
statement += "COUNT(*), COUNT(*) a, COUNT(DISTINCT x), COUNT(DISTINCT x) AS a, \"select\" FROM t"
expected_dump = "SELECT NULL, 1234567890123456789, a, b AS b_alias, 'literal', CAST(1 AS varchar), "
expected_dump += "\"1c\", *, \"*\", a.*, foo(), bar(baz, baw), t.c AS \"1quoted\", "
expected_dump += "COUNT(*), COUNT(*) AS a, COUNT(DISTINCT x), COUNT(DISTINCT x) AS a FROM t"
expected_dump += "COUNT(*), COUNT(*) AS a, COUNT(DISTINCT x), COUNT(DISTINCT x) AS a, \"select\" FROM t"
self.checkNominal(statement, expected_dump)

def testNominalFrom(self):
Expand Down

0 comments on commit 8259b3f

Please sign in to comment.