mirror of
https://github.com/qgis/QGIS.git
synced 2025-10-16 00:05:45 -04:00
Allow Microsoft style [ ] column references to be handled by QgsSqlStatement
This commit is contained in:
parent
8c9e7c1618
commit
bb1b0d2362
@ -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
|
||||
|
||||
|
@ -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( "''" ) );
|
||||
|
@ -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()
|
||||
|
@ -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; }
|
||||
|
@ -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()
|
||||
|
Loading…
x
Reference in New Issue
Block a user