web.web.db

Package web :: Package web :: Module db |
[hide private]
[frames] | no frames]

# Module db

source code

Database API (part of web.py)

Classes | [hide private]
| UnknownDB raised for unsupported dbms
| _ItplError
| TransactionError
| UnknownParamstyle raised for unsupported db paramstyles
| SQLParam Parameter in SQLQuery.
| sqlparam Parameter in SQLQuery.
| SQLQuery You can pass this sort of thing as a clause in any db function.
| SQLLiteral Protects a string from `sqlquote`.
| sqlliteral Protects a string from `sqlquote`.
| Transaction Database transaction.
| DB Database
| PostgresDB Postgres driver.
| MySQLDB
| SqliteDB
| FirebirdDB Firebird Database.
| MSSQLDB
| OracleDB

Functions | [hide private]
_sqllist(values) | source code
reparam(string_, dictionary) Takes a string and a dictionary and interpolates the string using values from the dictionary. | source code
sqlify(obj) converts `obj` to its proper SQL version | source code
sqllist(lst) Converts the arguments for use in something like a WHERE clause. | source code
sqlors(left, lst) `left is a SQL clause like `tablename.arg = ` and `lst` is a list of values. | source code
sqlwhere(dictionary, grouping=`'``AND` `'`) Converts a `dictionary` to an SQL WHERE clause `SQLQuery`. | source code
sqlquote(a) Ensures `a` is quoted properly for use in a SQL query. | source code
import_driver(drivers, preferred=None) Import the first available driver or preferred driver. | source code
database(dburl=None, **params) Creates appropriate database using params. | source code
register_database(name, clazz) Register a database. | source code
_interpolate(format) Takes a format string and returns a list of 2-tuples of the form (boolean, string) where boolean says whether string should be evaled or not. | source code

Variables | [hide private]
| config = `<Storage {'debug': True, '__doc__': '\nA configuratio`...``
| _databases = `{}`
| __package__ = ``'``web.web``'``

Function Details | [hide private]

### reparam(string_, dictionary) | source code
Takes a string and a dictionary and interpolates the string using values from the dictionary. Returns an `SQLQuery` for the result.
    >>> reparam("s = $s", dict(s=True))
    <sql: "s = 't'">
    >>> reparam("s IN $s", dict(s=[1, 2]))
    <sql: 's IN (1, 2)'>

### sqlify(obj) | source code
converts `obj` to its proper SQL version
    >>> sqlify(None)
    'NULL'
    >>> sqlify(True)
    "'t'"
    >>> sqlify(3)
    '3'

### sqllist(lst) | source code
Converts the arguments for use in something like a WHERE clause.
    >>> sqllist(['a', 'b'])
    'a, b'
    >>> sqllist('a')
    'a'
    >>> sqllist(u'abc')
    u'abc'

### sqlors(left, lst) | source code
`left is a SQL clause like `tablename.arg = ` and `lst` is a list of values. Returns a reparam-style pair featuring the SQL that ORs together the clause for each item in the lst.
    >>> sqlors('foo = ', [])
    <sql: '1=2'>
    >>> sqlors('foo = ', [1])
    <sql: 'foo = 1'>
    >>> sqlors('foo = ', 1)
    <sql: 'foo = 1'>
    >>> sqlors('foo = ', [1,2,3])
    <sql: '(foo = 1 OR foo = 2 OR foo = 3 OR 1=2)'>

### sqlwhere(dictionary, grouping=`'``AND` `'`) | source code
Converts a `dictionary` to an SQL WHERE clause `SQLQuery`.
    >>> sqlwhere({'cust_id': 2, 'order_id':3})
    <sql: 'order_id = 3 AND cust_id = 2'>
    >>> sqlwhere({'cust_id': 2, 'order_id':3}, grouping=', ')
    <sql: 'order_id = 3, cust_id = 2'>
    >>> sqlwhere({'a': 'a', 'b': 'b'}).query()
    'a = %s AND b = %s'

### sqlquote(a) | source code
Ensures `a` is quoted properly for use in a SQL query.
    >>> 'WHERE x = ' + sqlquote(True) + ' AND y = ' + sqlquote(3)
    <sql: "WHERE x = 't' AND y = 3">
    >>> 'WHERE x = ' + sqlquote(True) + ' AND y IN ' + sqlquote([2, 3])
    <sql: "WHERE x = 't' AND y IN (2, 3)">

### database(dburl=None, **params) | source code
Creates appropriate database using params. Pooling will be enabled if DBUtils module is available. Pooling can be disabled by passing pooling=False in params.

### register_database(name, clazz) | source code
Register a database.
    >>> class LegacyDB(DB):
    ...     def __init__(self, **params):
    ...        pass
    ...
    >>> register_database('legacy', LegacyDB)
    >>> db = database(dbn='legacy', db='test', user='joe', passwd='secret')

### _interpolate(format) | source code
Takes a format string and returns a list of 2-tuples of the form (boolean, string) where boolean says whether string should be evaled or not. from <http://lfw.org/python/Itpl.py> (public domain, Ka-Ping Yee)

Variables Details | [hide private]

  ### config Value:
      <Storage {'debug': True, '__doc__': '\nA configuration object for vari
      ous aspects of web.py.\n\n`debug`\n   : when True, enables reloading,
      disabled template caching and sets internalerror to debugerror.\n', 's
      ession_parameters': <Storage {'ignore_expiry': True, 'expired_message'
      : 'Session expired', 'timeout': 86400, 'ignore_change_ip': True, 'cook
      ie_domain': None, 'secret_key': 'fLjUfxqXtfNoIldA0A0J', 'cookie_name':
       'webpy_session_id'}>}>