Module pydo.operators
This module permits a useful subset of SQL where clauses to be defined
with a Lispo-Pythonic syntax:
>>> g=SQLOperator(('AND', ('LIKE', FIELD('username'), 'bilbo_bag%'),
... ('>', FIELD('x'), 40)))
>>> print g
((username LIKE 'bilbo_bag%') AND (x > 40))
SQLOperator is a tuple subclass that represents itself as a SQL
string. The first element of the tuple is the SQL operator, and the
remaining elements are arguments, which may be atoms or nested tuples,
which are recursively converted to SQLOperator tuples.
FIELD is a synonym for CONSTANT, a helper class which distinguishes
field names (and names of constants, like NULL) from plain strings, which
will appear in the SQL output as string literals. NULL in particular is
available as a constant, due to the understandable popularity of nullity;
it is equal to CONSTANT('NULL'). Another helper class, SET, is available
to help use the IN operator:
>>> print SQLOperator(('IN', FIELD('x'), SET(1, 2, 3, 4)))
(x IN (1, 2, 3, 4))
For convenience, most SQL operators are additionally wrapped in
operator-specific SQLOperator subclasses, which are exactly equivalent to
the explicit tuple notation.
>>> print IN(FIELD('x'), SET(1, 2, 3, 4))
(x IN (1, 2, 3, 4))
>>> print AND(OR(EQ(FIELD('x'),
... FIELD('y')),
... LT_EQ(FIELD('a'),
... MULT(FIELD('b'),
... EXP(FIELD('c'), 2)))),
... IN(FIELD('e'), SET('Porthos', 'Athos', 'Aramis')))
(((x = y) OR (a <= (b * (c ^ 2)))) AND (e IN ('Porthos', 'Athos', 'Aramis')))
SQLOperators can also take a conversion function that format data
values: SQLOperator(('EQ', FIELD('datecolumn'), myDate),
converter=myFunc). Nested operators will inherit the conversion function
from the enclosing operator class if they don't define one
themselves.
The BindingConverter manages bind variables by inserting the
appropriate formats and accumulating the values inside the converter.
>>> c=BindingConverter('format')
>>> op=AND(OR(EQ(FIELD('x'),
... FIELD('y')),
... LT_EQ(FIELD('a'),
... MULT(FIELD('b'),
... EXP(FIELD('c'), 2)))),
... IN(FIELD('e'), SET('Porthos', 'Athos', 'Aramis')), converter=c)
>>> print op
(((x = y) OR (a <= (b * (c ^ %s)))) AND (e IN (%s, %s, %s)))
>>> c.values
[2, 'Porthos', 'Athos', 'Aramis']
>>> c.paramstyle='pyformat'
>>> c.reset()
>>> print op
(((x = y) OR (a <= (b * (c ^ %(n1)s)))) AND (e IN (%(n2)s, %(n3)s, %(n4)s)))
>>> c.values
{'n1': 2, 'n2': 'Porthos', 'n3': 'Athos', 'n4': 'Aramis'}
>>> c.paramstyle='numeric'
>>> c.reset()
>>> print op
(((x = y) OR (a <= (b * (c ^ :1)))) AND (e IN (:2, :3, :4)))
>>> c.values
[2, 'Porthos', 'Athos', 'Aramis']
>>> c.paramstyle='qmark'
>>> c.reset()
>>> print op
(((x = y) OR (a <= (b * (c ^ ?)))) AND (e IN (?, ?, ?)))
>>> c.values
[2, 'Porthos', 'Athos', 'Aramis']
>>> c.paramstyle='named'
>>> c.reset()
>>> print op
(((x = y) OR (a <= (b * (c ^ :n1)))) AND (e IN (:n2, :n3, :n4)))
>>> c.values
{'n1': 2, 'n2': 'Porthos', 'n3': 'Athos', 'n4': 'Aramis'}
Variable Summary |
CONSTANT |
NULL = NULL
|
NULL
-
- Type:
-
CONSTANT
- Value:
|