Skip to content
Permalink
Browse files

QgsSQLStatement::quotedIdentifierIfNeeded(): make it quote SQL reserv…

…ed keywords
  • Loading branch information
rouault committed Apr 22, 2016
1 parent 84a797e commit 8259b3fcc998708b110d2f4342ddb72bc8377787
Showing with 18 additions and 2 deletions.
  1. +16 −0 src/core/qgssqlstatement.cpp
  2. +2 −2 tests/src/python/test_qgssqlstatement.py
@@ -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 );
}

@@ -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):

0 comments on commit 8259b3f

Please sign in to comment.
You can’t perform that action at this time.