Package gavo :: Package rsc :: Module dbtable :: Class DBTable
[frames] | no frames]

Class DBTable

source code

                        object --+            
                                 |            
base.sqlsupport.PostgresQueryMixin --+        
                                     |        
                        object --+   |        
                                 |   |        
base.sqlsupport.StandardQueryMixin --+        
                                     |        
          base.sqlsupport.QuerierMixin --+    
                                         |    
                            DBMethodsMixin --+
                                             |
                            object --+       |
                                     |       |
                   base.meta.MetaMixin --+   |
                                         |   |
                            object --+   |   |
                                     |   |   |
                     common.ParamMixin --+   |
                                         |   |
                           table.BaseTable --+
                                             |
                                object --+   |
                                         |   |
                            MetaTableMixin --+
                                             |
                                            DBTable
Known Subclasses:

An interface to a table in the database.

These are usually created using ``api.TableForDef(tableDef)`` with a
table definition obtained, e.g., from an RD, saying ``onDisk=True``.

When constructing a DBTable, it will be created if necessary (unless
``create=False`` is passed), but indices or primary keys keys will only be
created on a call to ``importFinished``.

The constructor does not check if the schema of the table on disk matches 
the tableDef.  If the two diverge, all kinds of failures are conceivable;
use ``dachs val -c``  to make sure on-disk structure match the RDs.

You can pass a ``nometa`` boolean kw argument to suppress entering the table
into the ``dc_tables`` table.

You can pass an exclusive boolean kw argument; if you do, the
``iterQuery`` (and possibly similar methods in the future) method
will block concurrent writes to the selected rows ("FOR UPDATE")
as long as the transaction is active.

The main attributes (with API guarantees) include:

* tableDef -- the defining tableDef
* getFeeder() -- returns a function you can call with rowdicts to 
  insert them into the table.
* importFinished() -- must be called after you've fed all rows when
  importing data.
* drop() -- drops the table in the database
* recreate() -- drops the table and generates a new empty one.
* getTableForQuery(...) -- returns a Table instance built from a query
  over this table (you probably to use ``conn.query*`` and 
  ``td.getSimpleQuery`` instead).

Instance Methods
 
__init__(self, tableDef, **kwargs)
is a constructor for standalone use.
source code
 
__iter__(self) source code
 
__len__(self) source code
 
exists(self) source code
 
getFeeder(self, **kwargs) source code
 
importFinished(self) source code
 
importFailed(self, *excInfo) source code
 
feedRows(self, rows)
Feeds a sequence of rows to the table.
source code
 
addRow(self, row)
adds a row to the table.
source code
 
getRow(self, *key)
returns the row with the primary key key from the table.
source code
 
createUniquenessRules(self) source code
 
setStatisticsTargets(self) source code
 
configureTable(self) source code
 
create(self) source code
 
updateMeta(self) source code
 
createIfNecessary(self) source code
 
drop(self)
drops the table.
source code
 
recreate(self) source code
 
query(self, query, data={})
runs query within this table's connection.
source code
 
getSelectClause(self, resultTableDef)
returns the select clause to come up with resultTableDef.
source code
 
getQuery(self, resultTableDef, fragment, pars=None, distinct=False, limits=None, groupBy=None)
returns a result table definition, query string and a parameters dictionary for a query against this table.
source code
 
iterQuery(self, resultTableDef=None, fragment='', pars=None, distinct=False, limits=None, groupBy=None)
like getTableForQuery, except that an iterator over the result rows is returned.
source code
 
getTableForQuery(self, resultTableDef=None, fragment='', pars=None, distinct=False, limits=None, groupBy=None)
returns a Table instance for a query on this table.
source code

Inherited from DBMethodsMixin: copyIn, copyOut, deleteMatching, dropIndices, ensureOnDiskMatches, ensureSchema, getDeleteQuery, makeIndices

Inherited from base.sqlsupport.QuerierMixin: abort, configureConnection, enableAutocommit, finish, queryDicts, queryToDicts

Inherited from base.sqlsupport.PostgresQueryMixin: dropTable, foreignKeyExists, getACLFromRes, getColumnsFromDB, getForeignKeyName, getOIDForTable, getPrimaryIndexName, getRowEstimate, getSchemaPrivileges, getServerVersion, getTablePrivileges, getTableType, hasIndex, parsePGACL, roleExists, schemaExists

Inherited from base.sqlsupport.StandardQueryMixin: getTimeout, setSchemaPrivileges, setTablePrivileges, setTimeout

Inherited from table.BaseTable: addTuple, close, removeRow, runScripts, validateParams

Inherited from base.meta.MetaMixin: addMeta, buildRepr, copyMetaFrom, delMeta, getAllMetaPairs, getMeta, getMetaKeys, getMetaParent, isEmpty, iterMeta, keys, makeOriginal, setMeta, setMetaParent, traverse

Inherited from common.ParamMixin: getParam, getParamByName, getParamByUtype, getParamDict, iterParams, setParam, setParams

Inherited from MetaTableMixin: addToMeta, cleanFromMeta

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __str__, __subclasshook__

Class Variables

Inherited from DBMethodsMixin: scripts

Inherited from base.sqlsupport.QuerierMixin: defaultProfile

Properties

Inherited from object: __class__

Method Details

__init__(self, tableDef, **kwargs)
(Constructor)

source code 

is a constructor for standalone use. You do *not* want to call this when mixing into a Structure.

Overrides: object.__init__
(inherited documentation)

__iter__(self)

source code 
Overrides: table.BaseTable.__iter__

__len__(self)
(Length operator)

source code 
Overrides: table.BaseTable.__len__

getFeeder(self, **kwargs)

source code 
Overrides: table.BaseTable.getFeeder

importFinished(self)

source code 
Overrides: table.BaseTable.importFinished

importFailed(self, *excInfo)

source code 
Overrides: table.BaseTable.importFailed

feedRows(self, rows)

source code 

Feeds a sequence of rows to the table.

The method returns the number of rows affected. Exceptions are handed through upstream, but the connection is rolled back.

addRow(self, row)

source code 

adds a row to the table.

Use this only to add one or two rows, otherwise go for getFeeder.

Overrides: table.BaseTable.addRow

getRow(self, *key)

source code 

returns the row with the primary key key from the table.

This will raise a DataError on tables without primaries.

Overrides: table.BaseTable.getRow

query(self, query, data={})

source code 

runs query within this table's connection.

query is macro-expanded within the table definition (i.e., you can, e.g., write \qName to obtain the table's qualified name).

Overrides: base.sqlsupport.QuerierMixin.query

getQuery(self, resultTableDef, fragment, pars=None, distinct=False, limits=None, groupBy=None)

source code 

returns a result table definition, query string and a parameters dictionary for a query against this table.

See getTableForQuery for the meaning of the arguments.

iterQuery(self, resultTableDef=None, fragment='', pars=None, distinct=False, limits=None, groupBy=None)

source code 

like getTableForQuery, except that an iterator over the result rows is returned.

(there is no advantage in using this as we will pull the entire thing in memory anyway; use qtables if you need streaming).

getTableForQuery(self, resultTableDef=None, fragment='', pars=None, distinct=False, limits=None, groupBy=None)

source code 

returns a Table instance for a query on this table.

resultTableDef is a TableDef with svc.OutputField columns (rscdef.Column instances will do), or possibly just a list of Columns. Fragment is empty or an SQL where-clause with dictionary placeholders, pars is the dictionary filling fragment, distinct, if True, adds a distinct clause, and limits, if given, is a pair of an SQL string to be appended to the SELECT clause and parameters filling it. queryMeta.asSQL returns what you need here.

pars may be mutated in the process.