Gnumed.pycommon.gmPG

Package Gnumed :: Package pycommon :: Module gmPG |
[frames] | no frames]

# Module gmPG

source code

Version: $Revision: 1.90 $

Author: H.Herb <hherb@gnumed.net>, I.Haywood <i.haywood@ugrad.unimelb.edu.au>, K.Hilbert <Karsten.Hilbert@gmx.net>

License: GPL v2 or later (details at http://www.gnu.org)

Classes
| ConnectionPool maintains a static dictionary of available database connections

Functions
fieldNames(cursor) returns the attribute names of the fetched rows in natural sequence as a list | source code
run_query(aCursor=None, verbosity=None, aQuery=None, *args) | source code
run_commit2(link_obj=None, queries=None, end_tx=False, max_tries=1, extra_verbose=False, get_col_idx=False) Convenience function for running a transaction that is supposed to get committed. | source code
run_commit(link_obj=None, queries=None, return_err_msg=None) Convenience function for running a transaction that is supposed to get committed. | source code
run_ro_query(link_obj=None, aQuery=None, get_col_idx=False, *args) Runs a read-only query. | source code
get_col_indices(aCursor=None) | source code
get_pkey_name(aCursor=None, aTable=None) | source code
get_fkey_defs(source, table) Returns a dictionary of referenced foreign keys. | source code
add_housekeeping_todo(reporter='$RCSfile: gmPG.py,v $ $Revision: 1.90 $', receiver='DEFAULT', problem='lazy programmer', solution='lazy programmer', context='lazy programmer', category='lazy programmer') | source code

Variables
| tz = `time.timezone`
| QTablePrimaryKeyIndex = ``...``
| query_pkey_name = ``...``
| query_fkey_names = ``...``
| query_table_col_defs = `"""sel`...``
| query_table_attributes = `"""sel`...``
| query_child_tables = ``...``
| last_ro_cursor_desc = `None`

Imports: sys

Function Details

### run_commit2(link_obj=None, queries=None, end_tx=False, max_tries=1, extra_verbose=False, get_col_idx=False) | source code
    Convenience function for running a transaction
       that is supposed to get committed.

    <link_obj>
            can be either:
            - a cursor
            - a connection
            - a service name

    <queries>
            is a list of (query, [args]) tuples to be
            executed as a single transaction, the last
            query may usefully return rows (such as a
            "select currval('some_sequence')" statement)

    <end_tx>
            - controls whether the transaction is finalized (eg.
              committed/rolled back) or not, this allows the
              call to run_commit2() to be part of a framing
              transaction
            - if <link_obj> is a service name the transaction is
              always finalized regardless of what <end_tx> says
            - if link_obj is a connection then <end_tx> will
              default to False unless it is explicitly set to
              True which is taken to mean "yes, you do have full
              control over the transaction" in which case the
              transaction is properly finalized

    <max_tries>
            - controls the number of times a transaction is retried
              after a concurrency error
            - note that *all* <queries> are rerun if a concurrency
              error occurrs
            - max_tries is honored if and only if link_obj is a service
              name such that we have full control over the transaction

    <get_col_idx>
            - if true, the returned data will include a dictionary
              mapping field names to column positions
            - if false, the returned data returns an empty dict

    method result:
            - returns a tuple (status, data)
            - <status>:
                    * True - if all queries succeeded (also if there were 0 queries)
                    * False - if *any* error occurred
            - <data> if <status> is True:
                    * (None, {}) if last query did not return rows
                    * ("fetchall() result", <index>) if last query returned any rows
                    * for <index> see <get_col_idx>
            - <data> if <status> is False:
                    * a tuple (error, message) where <error> can be:
                    * 1: unspecified error
                    * 2: concurrency error
                    * 3: constraint violation (non-primary key)
                    * 4: access violation

### run_commit(link_obj=None, queries=None, return_err_msg=None) | source code
    Convenience function for running a transaction
         that is supposed to get committed.

      - link_obj can be
        - a cursor: rollback/commit must be done by the caller
        - a connection: rollback/commit is handled
        - a service name: rollback/commit is handled

      - queries is a list of (query, [args]) tuples
        - executed as a single transaction

      - returns:
        - a tuple (<value>, error) if return_err_msg is True
        - a scalar <value> if return_err_msg is False

      - <value> will be
        - None: if any query failed
        - 1: if all queries succeeded (also 0 queries)
    - data: if the last query returned rows

### run_ro_query(link_obj=None, aQuery=None, get_col_idx=False, *args) | source code
    Runs a read-only query.

    - link_obj can be a service name, connection or cursor object

    - return status:
            - return data                   if get_col_idx is False
            - return (data, idx)    if get_col_idx is True

    - if query fails: data is None
    - if query is not a row-returning SQL statement: data is None

    - data is a list of tuples [(w,x,y,z), (a,b,c,d), ...] where each tuple is a table row
    - idx is a map of column name to their position in the row tuples
            e.g. { 'name': 3, 'id':0, 'job_description': 2, 'location':1 }

            usage:  e.g. data[0][idx['name']] would return z from [(w,x,y,z ),(a,b,c,d)]

### get_fkey_defs(source, table) | source code
Returns a dictionary of referenced foreign keys. key = column name of this table value = (referenced table name, referenced column name) tuple

Variables Details

  ### QTablePrimaryKeyIndex Value:
      """
      SELECT
      	indkey
      FROM
      	pg_index
      WHERE
      	indrelid =
      	(SELECT oid FROM pg_class WHERE relname = '%s');
      ...

  ### query_pkey_name Value:
      """
      SELECT
      	pga.attname
      FROM
      	(pg_attribute pga inner join pg_index pgi on (pga.attrelid=pgi.indrel
      id))
      WHERE
      	pga.attnum=pgi.indkey[0]
      ...

  ### query_fkey_names Value:
      """
      select tgargs from pg_trigger where
      	tgname like 'RI%%'
      		and
      	tgrelid = (
      		select oid from pg_class where relname=%s
      	)
      """

  ### query_table_col_defs Value:
      """select
      	cols.column_name,
      	cols.udt_name
      from
      	information_schema.columns cols
      where
      	cols.table_schema = %s
      		and
      ...

  ### query_table_attributes Value:
      """select
      	cols.column_name
      from
      	information_schema.columns cols
      where
      	cols.table_schema = %s
      		and
      	cols.table_name = %s
      ...

  ### query_child_tables Value:
      """
      select
      	pgn.nspname as namespace,
      	pgc.relname as table
      from
      	pg_namespace pgn,
      	pg_class pgc
      where
      ...