Package pydo :: Module operators
[show private | hide private]
[frames | no frames]

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'}

Classes
AND  
BETWEEN  
BindingConverter A value converter that uses bind variables.
CONSTANT a way to represent a constant or field name in a sql expression
DIV  
EQ  
EXP  
FIELD a way to represent a constant or field name in a sql expression
GT  
GT_EQ  
ILIKE  
IN  
IREGEX  
IS  
ISNULL  
LIKE  
LT  
LT_EQ  
MINUS  
MOD  
MULT  
NE  
NOT  
NOTNULL  
OR  
OVERLAPS  
PLUS  
REGEX  
SET a way of passing a set into PyDO where clauses (for IN), e.g.:
SIMILAR  
SQLOperator A special kind of tuple that knows how to represent itself as a SQL string.
DyadicOperator  
MonadicOperator  
PolyadicOperator  

Function Summary
  _repr_single_and_or(self)
  _repr_single_isnull(self)
  sqlquote(s)

Variable Summary
CONSTANT NULL = NULL

Variable Details

NULL

Type:
CONSTANT
Value:
NULL                                                                   

Generated by Epydoc 2.0 on Wed Mar 8 10:18:58 2006 http://epydoc.sf.net