web.web.db.DB

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

# Class DB

source code

Known Subclasses:
  - FirebirdDB
  - , MSSQLDB
  - , MySQLDB
  - , OracleDB
  - , PostgresDB
  - , SqliteDB

Database

Instance Methods | [hide private]
__init__(self, db_module, keywords) Creates a database. | source code
_getctx(self) | source code
_load_context(self, ctx) | source code
_unload_context(self, ctx) | source code
_connect(self, keywords) | source code
_connect_with_pooling(self, keywords) | source code
_db_cursor(self) | source code
_param_marker(self) Returns parameter marker based on paramstyle attribute if this database. | source code
_db_execute(self, cur, sql_query) executes an sql query | source code
_where(self, where, vars) | source code
query(self, sql_query, vars=None, processed=False, _test=False) Execute SQL query `sql_query` using dictionary `vars` to interpolate it. | source code
select(self, tables, vars=None, what=`'``*``'`, where=None, order=None, group=None, limit=None, offset=None, _test=False) Selects `what` from `tables` with clauses `where`, `order`, `group`, `limit`, and `offset`. | source code
where(self, table, what=`'``*``'`, order=None, group=None, limit=None, offset=None, _test=False, **kwargs) Selects from `table` where keys are equal to values in `kwargs`. | source code
sql_clauses(self, what, tables, where, group, order, limit, offset) | source code
gen_clause(self, sql, val, vars) | source code
insert(self, tablename, seqname=None, _test=False, **values) Inserts `values` into `tablename`. | source code
multiple_insert(self, tablename, values, seqname=None, _test=False) Inserts multiple rows into `tablename`. | source code
update(self, tables, where, vars=None, _test=False, **values) Update `tables` with clause `where` (interpolated using `vars`) and setting `values`. | source code
delete(self, table, where, using=None, vars=None, _test=False) Deletes from `table` with clauses `where` and `using`. | source code
_process_insert_query(self, query, tablename, seqname) | source code
transaction(self) Start a transaction. | source code

Properties | [hide private]
| ctx

Method Details | [hide private]

### query(self, sql_query, vars=None, processed=False, _test=False) | source code
Execute SQL query `sql_query` using dictionary `vars` to interpolate it. If `processed=True`, `vars` is a `reparam`-style list to use instead of interpolating.
    >>> db = DB(None, {})
    >>> db.query("SELECT * FROM foo", _test=True)
    <sql: 'SELECT * FROM foo'>
    >>> db.query("SELECT * FROM foo WHERE x = $x", vars=dict(x='f'), _test=True)
    <sql: "SELECT * FROM foo WHERE x = 'f'">
    >>> db.query("SELECT * FROM foo WHERE x = " + sqlquote('f'), _test=True)
    <sql: "SELECT * FROM foo WHERE x = 'f'">

### select(self, tables, vars=None, what=`'``*``'`, where=None, order=None, group=None, limit=None, offset=None, _test=False) | source code
Selects `what` from `tables` with clauses `where`, `order`, `group`, `limit`, and `offset`. Uses vars to interpolate. Otherwise, each clause can be a SQLQuery.
    >>> db = DB(None, {})
    >>> db.select('foo', _test=True)
    <sql: 'SELECT * FROM foo'>
    >>> db.select(['foo', 'bar'], where="foo.bar_id = bar.id", limit=5, _test=True)
    <sql: 'SELECT * FROM foo, bar WHERE foo.bar_id = bar.id LIMIT 5'>

### where(self, table, what=`'``*``'`, order=None, group=None, limit=None, offset=None, _test=False, **kwargs) | source code
Selects from `table` where keys are equal to values in `kwargs`.
    >>> db = DB(None, {})
    >>> db.where('foo', bar_id=3, _test=True)
    <sql: 'SELECT * FROM foo WHERE bar_id = 3'>
    >>> db.where('foo', source=2, crust='dewey', _test=True)
    <sql: "SELECT * FROM foo WHERE source = 2 AND crust = 'dewey'">

### insert(self, tablename, seqname=None, _test=False, **values) | source code
Inserts `values` into `tablename`. Returns current sequence ID. Set `seqname` to the ID if it's not the default, or to `False` if there isn't one.
    >>> db = DB(None, {})
    >>> q = db.insert('foo', name='bob', age=2, created=SQLLiteral('NOW()'), _test=True)
    >>> q
    <sql: "INSERT INTO foo (age, name, created) VALUES (2, 'bob', NOW())">
    >>> q.query()
    'INSERT INTO foo (age, name, created) VALUES (%s, %s, NOW())'
    >>> q.values()
    [2, 'bob']

### multiple_insert(self, tablename, values, seqname=None, _test=False) | source code
Inserts multiple rows into `tablename`. The `values` must be a list of dictioanries, one for each row to be inserted, each with the same set of keys. Returns the list of ids of the inserted rows. Set `seqname` to the ID if it's not the default, or to `False` if there isn't one.
    >>> db = DB(None, {})
    >>> db.supports_multiple_insert = True
    >>> values = [{"name": "foo", "email": "foo@example.com"}, {"name": "bar", "email": "bar@example.com"}]
    >>> db.multiple_insert('person', values=values, _test=True)
    <sql: "INSERT INTO person (name, email) VALUES ('foo', 'foo@example.com'), ('bar', 'bar@example.com')">

### update(self, tables, where, vars=None, _test=False, **values) | source code
Update `tables` with clause `where` (interpolated using `vars`) and setting `values`.
    >>> db = DB(None, {})
    >>> name = 'Joseph'
    >>> q = db.update('foo', where='name = $name', name='bob', age=2,
    ...     created=SQLLiteral('NOW()'), vars=locals(), _test=True)
    >>> q
    <sql: "UPDATE foo SET age = 2, name = 'bob', created = NOW() WHERE name = 'Joseph'">
    >>> q.query()
    'UPDATE foo SET age = %s, name = %s, created = NOW() WHERE name = %s'
    >>> q.values()
    [2, 'bob', 'Joseph']

### delete(self, table, where, using=None, vars=None, _test=False) | source code
Deletes from `table` with clauses `where` and `using`.
    >>> db = DB(None, {})
    >>> name = 'Joe'
    >>> db.delete('foo', where='name = $name', vars=locals(), _test=True)
    <sql: "DELETE FROM foo WHERE name = 'Joe'">

Property Details | [hide private]

### ctx Get Method: _getctx(self)