Package orm2 :: Module sql
[hide private]
[frames] | no frames]

Module sql

source code

This module provides a simple wrapper for SQL within Python. The idea is to obscure the SQL code that is being generated as little as possible, but to hide all the gorry details, especially of quoting and escaping things, away from the programmer. Also this code is supposed to be backend independent. Also this module is independent of the rest of orm2.

The way it works is best described by example:
 >>> ds = datasource(...some params...)
 >>> s = select( ( quotes('first name'), 'lastname', 'age',
                  expression('age + 10'),
                  as(quotes('negative age'), 'age - 10')),
                'person',
                where('age > 10'), order_by('age'))
 >>> print sql(ds)(s)
 SELECT "first name", lastname, age, age + 10,
    age - 10 AS "negative age" FROM person WHERE age > 10  ORDER BY "age"
 >>> u = update( 'person', where('id = 22'),
                firstname = string_literal('Diedrich'),
                lastname=string_literal('Vorberg'))
 >>> print sql(ds)(u)
 UPDATE person SET lastname = 'Vorberg', firstname = 'Diedrich' WHERE id = 22
 >>> d = delete ('person', where('id = 22'))
 >>> print sql(ds)(d)
 DELETE FROM person WHERE id = 22


Classes [hide private]
  ClauseAlreadyExists
  IllegalOrderDirection
  SQLSyntaxError
  UnicodeNotAllowedInSQL
  _part
The _part class is the base class for all SQL statement classes.
  as
Encapsulates an expression that goes into an AS statement in a SELECT's column list.
  bool_literal
  clause
Base class for clauses.
  column
A column name.
  datasource
A mix-in class that is inherited by datasouce.datasource_base.
  delete
Encapsulate a DELETE statement.
  direct_literal
This returns a %s as SQL code and the content you pass to the constructor to be quoted by the cursor's implementation rathern than by orm2.sql.
  expression
Encapsolate an SQL expression as for example a arithmetic or a function call.
  float_literal
  identifyer
Base class that encapsulates all sql identifyers.
  insert
Encapsulate an INSERT statement.
  integer_literal
  limit
Encapsulate a SELECT statement's limit clause.
  long_literal
Python 2.3 still makes a difference between long and int, so I need to classes here.
  offset
Encapsulate a SELECT statement's offset clause.
  order_by
Encapsulate the ORDER BY clause of a SELECT statement.
  orderby
Encapsulate the ORDER BY clause of a SELECT statement.
  quotes
Shorthand for an identifyer that you'd like to be surrounded in quotes within the sql code.
  relation
  select
Encapsulate a SELECT statement.
  sql
This class is used to do something that in Haskell is called Cyrrying.
  statement
Base class for all statements (select, update, delete, etc)
  string_literal
  update
Encapsulate a UPDATE statement.
  where
Encapsulates the WHERE clause of a SELECT, UPDATE and DELETE statement.

Functions [hide private]
  flatten_identifyer_list(runner, arg)
A helper function that takes a list of strings, column and relaton classes and converts if it to sensible sql.

Variables [hide private]
  NULL = 'NULL'
  __author__ = 'Diedrich Vorberg <diedrich@tux4web.de>'
  __version__ = '1.22'

Function Details [hide private]

flatten_identifyer_list(runner, arg)

source code 
A helper function that takes a list of strings, column and relaton classes and converts if it to sensible sql.

Variables Details [hide private]

NULL

None
Value:
'NULL'                                                                 
      

__author__

None
Value:
'Diedrich Vorberg <diedrich@tux4web.de>'                               
      

__version__

None
Value:
'1.22'