QGIS has some support for parsing of SQL-like expressions. Only a small subset of SQL syntax is supported. The expressions can be evaluated either as boolean predicates (returning True or False) or as functions (returning a scalar value).
Three basic types are supported:
The following operations are available:
And following predicates are supported:
Compatibility note: mathematical, geometry, conversion functions and power operator ^ are available from QGIS 1.4.
Examples of predicates:
Examples of scalar expressions:
TODO: parsing, error handling
>>> s = QgsSearchString()
>>> s.setString("1 + 1 = 2")
True
>>> s.setString("1 + 1 =")
False
>>> s.parserErrorMsg()
PyQt4.QtCore.QString(u'syntax error, unexpected $end')
TODO: working with the tree, evaluation as a predicate, as a function, error handling
st = ss.tree()
if not st:
raise ValueError, "empty expression was used"
print st.makeSearchString()
res = st.checkAgainst(fields, feature.attributeMap())
res, value = st.getValue(st, fields, feature.attributeMap(), feature.geometry())
print st.errorMsg()