Allow Microsoft style [ ] column references to be handled by QgsSqlStatement

This commit is contained in:
Nyall Dawson 2020-08-07 16:32:27 +10:00
parent 8c9e7c1618
commit bb1b0d2362
5 changed files with 40 additions and 0 deletions

View File

@ -91,6 +91,13 @@ otherwise the original string.
%Docstring
Remove double quotes from an identifier.
.. seealso:: :py:func:`quotedIdentifier`
%End
static QString stripMsQuotedIdentifier( QString text );
%Docstring
Remove double quotes from an Microsoft style identifier.
.. seealso:: :py:func:`quotedIdentifier`
%End

View File

@ -106,6 +106,16 @@ QString QgsSQLStatement::stripQuotedIdentifier( QString text )
return text;
}
QString QgsSQLStatement::stripMsQuotedIdentifier( QString text )
{
if ( text.length() >= 2 && text[0] == '[' && text[text.length() - 1] == ']' )
{
// strip square brackets on start,end
text = text.mid( 1, text.length() - 2 );
}
return text;
}
QString QgsSQLStatement::quotedString( QString text )
{
text.replace( '\'', QLatin1String( "''" ) );

View File

@ -107,6 +107,12 @@ class CORE_EXPORT QgsSQLStatement
*/
static QString stripQuotedIdentifier( QString text );
/**
* Remove double quotes from an Microsoft style identifier.
* \see quotedIdentifier()
*/
static QString stripMsQuotedIdentifier( QString text );
/**
* Returns a quoted version of a string (in single quotes)
* \see quotedIdentifier(), quotedIdentifierIfNeeded()

View File

@ -94,6 +94,8 @@ identifier {identifier_first}{identifier_next}*
identifier_str_char "\"\""|[^\"]
identifier_quoted "\""{identifier_str_char}*"\""
ms_identifier_quoted "\["[^.]*"\]"
dig [0-9]
num_int [-]?{dig}+{identifier_first}*
num_float [-]?{dig}*(\.{dig}+([eE][-+]?{dig}+)?|[eE][-+]?{dig}+)
@ -190,6 +192,8 @@ string "'"{str_char}*"'"
{identifier_quoted} { TEXT_FILTER(QgsSQLStatement::stripQuotedIdentifier); return IDENTIFIER; }
{ms_identifier_quoted} { TEXT_FILTER(QgsSQLStatement::stripMsQuotedIdentifier); return IDENTIFIER; }
{white} /* skip blanks and tabs */
. { return Unknown_CHARACTER; }

View File

@ -218,6 +218,19 @@ class TestQgsSQLStatementCustomFunctions(unittest.TestCase):
self.checkFragmentError("FROM b")
self.checkFragmentError("ORDER BY a")
def testMsFragment(self):
# Microsoft style identifiers can have a bunch of weird characters in them!
exp = QgsSQLStatementFragment('[col$_# :]')
self.assertFalse(exp.hasParserError())
self.assertIsInstance(exp.rootNode(), QgsSQLStatement.NodeColumnRef)
self.assertEqual(exp.rootNode().name(), 'col$_# :')
exp = QgsSQLStatementFragment('[table$_# :].[col$_# :]')
self.assertFalse(exp.hasParserError())
self.assertIsInstance(exp.rootNode(), QgsSQLStatement.NodeColumnRef)
self.assertEqual(exp.rootNode().name(), 'col$_# :')
self.assertEqual(exp.rootNode().tableName(), 'table$_# :')
if __name__ == "__main__":
unittest.main()