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

Module gmPG2

source code

GNUmed PostgreSQL connection handling.

TODO: iterator/generator batch fetching:
        - http://groups-beta.google.com/group/comp.lang.python/msg/7ff516d7d9387dad
        - search Google for "Geneator/Iterator Nesting Problem - Any Ideas? 2.4"

winner:
def resultset_functional_batchgenerator(cursor, size=100):
        for results in iter(lambda: cursor.fetchmany(size), []):
                for rec in results:
                        yield rec


Author: K.Hilbert <Karsten.Hilbert@gmx.net>

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

Classes
  cConnectionPool
GNUmed database connection pool.
  cAuthenticationError
  cEncodingError
  cAdapterPyDateTime
Functions
 
set_default_client_encoding(encoding=None) source code
 
set_default_client_timezone(timezone=None) source code
 
request_login_params()
Request login parameters for database connection.
source code
 
make_psycopg2_dsn(database=None, host=None, port=5432, user=None, password=None) source code
 
get_default_login() source code
 
get_default_dsn() source code
 
set_default_login(login=None) source code
 
database_schema_compatible(link_obj=None, version=None, verbose=True) source code
 
get_schema_version(link_obj=None) source code
 
get_schema_structure(link_obj=None) source code
 
get_schema_hash(link_obj=None) source code
 
get_schema_revision_history(link_obj=None) source code
 
get_current_user() source code
 
get_foreign_keys2column(schema='public', table=None, column=None, link_obj=None)
Get the foreign keys pointing to schema.table.column.
source code
 
get_foreign_key_names(src_schema=None, src_table=None, src_column=None, target_schema=None, target_table=None, target_column=None, link_obj=None) source code
 
get_child_tables(schema='public', table=None, link_obj=None)
Return child tables of <table>.
source code
 
schema_exists(link_obj=None, schema=u'gm') source code
 
table_exists(link_obj=None, schema=None, table=None)
Returns false, true.
source code
 
get_col_indices(cursor=None) source code
 
get_col_defs(link_obj=None, schema='public', table=None) source code
 
get_col_names(link_obj=None, schema='public', table=None)
Return column attributes of table
source code
 
export_translations_from_database(filename=None) source code
 
delete_translation_from_database(link_obj=None, language=None, original=None) source code
 
update_translation_in_database(language=None, original=None, translation=None) source code
 
get_translation_languages() source code
 
get_database_translations(language=None, order_by=None) source code
 
get_current_user_language() source code
 
set_user_language(user=None, language=None)
Set the user language in the database.
source code
 
force_user_language(language=None)
Set the user language in the database.
source code
 
send_maintenance_notification() source code
 
send_maintenance_shutdown() source code
 
is_pg_interval(candidate=None) source code
 
lock_row(link_obj=None, table=None, pk=None, exclusive=False)
Uses pg_advisory(_shared).
source code
 
unlock_row(link_obj=None, table=None, pk=None, exclusive=False)
Uses pg_advisory_unlock(_shared).
source code
 
row_is_locked(table=None, pk=None)
Looks at pk_locks
source code
 
bytea2file(data_query=None, filename=None, chunk_size=0, data_size=None, data_size_query=None) source code
 
bytea2file_object(data_query=None, file_obj=None, chunk_size=0, data_size=None, data_size_query=None)
Store data from a bytea field into a file.
source code
 
file2bytea(query=None, filename=None, args=None, conn=None)
Store data from a file into a bytea field.
source code
 
sanitize_pg_regex(expression=None, escape_all=False)
Escape input for use in a PostgreSQL regular expression.
source code
 
run_ro_queries(link_obj=None, queries=None, verbose=False, return_data=True, get_col_idx=False)
Run read-only queries.
source code
 
run_rw_queries(link_obj=None, queries=None, end_tx=False, return_data=None, get_col_idx=False, verbose=False)
Convenience function for running a transaction that is supposed to get committed.
source code
 
run_insert(link_obj=None, schema=None, table=None, values=None, returning=None, end_tx=False, get_col_idx=False, verbose=False)
Generates SQL for an INSERT query.
source code
 
get_raw_connection(dsn=None, verbose=False, readonly=True)
Get a raw, unadorned connection.
source code
 
get_connection(dsn=None, readonly=True, encoding=None, verbose=False, pooled=True)
Get a new connection.
source code
 
shutdown() source code
 
log_database_access(action=None) source code
 
sanity_check_time_skew(tolerance=60)
Check server time and local time to be within the given tolerance of each other.
source code
 
sanity_check_database_settings()
Checks database settings.
source code
 
extract_msg_from_pg_exception(exc=None) source code
Variables
  postgresql_version_string = None
  postgresql_version = None
  auto_request_login_params = True
  known_schema_hashes = {0: 'not released, testing only', 2: 'b0...
  map_schema_hash2version = {'03042ae24f3f92877d986fb0a6184d76':...
  map_client_branch2required_db_version = {u'0.3': 9, u'0.4': 10...
  query_table_col_defs = u'sel...
  query_table_attributes = u'sel...
  SQL_foreign_key_name = u'SELECT tc.constraint_schema, tc.const...
  __package__ = 'Gnumed.pycommon'

Imports: time, sys, os, codecs, types, logging, pydt, regex, gmLoginInfo, gmExceptions, gmDateTime, gmBorg, gmI18N, gmLog2, prompted_input, u_replacement_character, dbapi, psycopg2, sql_error_codes, FixedOffsetTimezone


Function Details

get_foreign_keys2column(schema='public', table=None, column=None, link_obj=None)

source code 

Get the foreign keys pointing to schema.table.column.

Does not properly work with multi-column FKs. GNUmed doesn't use any, however.

set_user_language(user=None, language=None)

source code 

Set the user language in the database.

user = None: current db user language = None: unset

force_user_language(language=None)

source code 
Set the user language in the database.

- regardless of whether there is any translation available.
- only for the current user

lock_row(link_obj=None, table=None, pk=None, exclusive=False)

source code 
Uses pg_advisory(_shared).

- locks stack and need one unlock per lock
- same connection:
        - all locks succeed
- different connections:
        - shared + shared succeed
        - shared + exclusive fail

unlock_row(link_obj=None, table=None, pk=None, exclusive=False)

source code 
Uses pg_advisory_unlock(_shared).

- each lock needs one unlock

row_is_locked(table=None, pk=None)

source code 
Looks at pk_locks

- does not take into account locks other than 'advisory', however

bytea2file_object(data_query=None, file_obj=None, chunk_size=0, data_size=None, data_size_query=None)

source code 
Store data from a bytea field into a file.

<data_query>
- dict {'cmd': ..., 'args': ...}
- 'cmd' must be unicode containing "... substring(data from %(start)s for %(size)s) ..."
- 'args' must be a dict
- must return one row with one field of type bytea
<file>
- must be a file like Python object
<data_size>
- integer of the total size of the expected data or None
<data_size_query>
- dict {'cmd': ..., 'args': ...}
- cmd must be unicode
- must return one row with one field with the octet_length() of the data field
- used only when <data_size> is None

file2bytea(query=None, filename=None, args=None, conn=None)

source code 
Store data from a file into a bytea field.

The query must:
- be in unicode
- contain a format spec identifying the row (eg a primary key)
  matching <args> if it is an UPDATE
- contain a format spec %(data)s::bytea

sanitize_pg_regex(expression=None, escape_all=False)

source code 
Escape input for use in a PostgreSQL regular expression.

If a fragment comes from user input and is to be used
as a regular expression we need to make sure it doesn't
contain invalid regex patterns such as unbalanced ('s.

<escape_all>
        True: try to escape *all* metacharacters
        False: only escape those which render the regex invalid

run_ro_queries(link_obj=None, queries=None, verbose=False, return_data=True, get_col_idx=False)

source code 
Run read-only queries.

<queries> must be a list of dicts:
        [
                {'cmd': <string>, 'args': <dict> or <tuple>},
                {...},
                ...
        ]

run_rw_queries(link_obj=None, queries=None, end_tx=False, return_data=None, get_col_idx=False, verbose=False)

source code 
Convenience function for running a transaction
   that is supposed to get committed.

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

<queries>
        is a list of dicts [{'cmd': <string>, 'args': <dict> or <tuple>)
        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_rw_queries() to be part of a framing
          transaction
        - 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
        - if link_obj is a cursor we CANNOT finalize the
          transaction because we would need the connection for that
        - if link_obj is None <end_tx> will, of course, always be True

<return_data>
        - if true, the returned data will include the rows
          the last query selected
        - if false, it returns None instead

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

method result:
        - returns a tuple (data, idx)
        - <data>:
                * (None, None) if last query did not return rows
                * ("fetchall() result", <index>) if last query returned any rows
                * for <index> see <get_col_idx>

run_insert(link_obj=None, schema=None, table=None, values=None, returning=None, end_tx=False, get_col_idx=False, verbose=False)

source code 

Generates SQL for an INSERT query.

values: dict of values keyed by field to insert them into

get_raw_connection(dsn=None, verbose=False, readonly=True)

source code 
Get a raw, unadorned connection.

- this will not set any parameters such as encoding, timezone, datestyle
- the only requirement is a valid DSN
- hence it can be used for "service" connections
  for verifying encodings etc

get_connection(dsn=None, readonly=True, encoding=None, verbose=False, pooled=True)

source code 

Get a new connection.

This assumes the locale system has been initialized unless an encoding is specified.

sanity_check_time_skew(tolerance=60)

source code 

Check server time and local time to be within the given tolerance of each other.

tolerance: seconds

sanity_check_database_settings()

source code 
Checks database settings.

returns (status, message)
status:
        0: no problem
        1: non-fatal problem
        2: fatal problem


Variables Details

known_schema_hashes

Value:
{0: 'not released, testing only',
 2: 'b09d50d7ed3f91ddf4c4ddb8ea507720',
 3: 'e73718eaf230d8f1d2d01afa8462e176',
 4: '4428ccf2e54c289136819e701bb095ea',
 5: '7e7b093af57aea48c288e76632a382e5',
 6: '90e2026ac2efd236da9c8608b8685b2d',
 7: '6c9f6d3981483f8e9433df99d1947b27',
 8: '89b13a7af83337c3aad153b717e52360',
...

map_schema_hash2version

Value:
{'03042ae24f3f92877d986fb0a6184d76': 11,
 '06183a6616db62257e22814007a8ed07': 12,
 '0bcf44ca22c479b52976e5eda1de8161': 16,
 '161428ee97a00e3bf56168c3a15b7b50': 17,
 '4428ccf2e54c289136819e701bb095ea': 4,
 '641a9b2be3c378ffc2bb2f0b1c9f051d': 9,
 '6c9f6d3981483f8e9433df99d1947b27': 7,
 '70012ff960b77ecdff4981c94b5b55b6': 15,
...

map_client_branch2required_db_version

Value:
{u'0.3': 9,
 u'0.4': 10,
 u'0.5': 11,
 u'0.6': 12,
 u'0.7': 13,
 u'0.8': 14,
 u'0.9': 15,
 u'1.0': 16,
...

query_table_col_defs

Value:
u'''select
	cols.column_name,
	cols.udt_name
from
	information_schema.columns cols
where
	cols.table_schema = %s
		and
...

query_table_attributes

Value:
u'''select
	cols.column_name
from
	information_schema.columns cols
where
	cols.table_schema = %s
		and
	cols.table_name = %s
...

SQL_foreign_key_name

Value:
u'''SELECT tc.constraint_schema, tc.constraint_name
FROM
	information_schema.table_constraints tc
		INNER JOIN information_schema.constraint_column_usage ccu USING (con\
straint_catalog, constraint_schema, constraint_name)
			INNER JOIN information_schema.key_column_usage kcu USING (constrain\
t_catalog, constraint_schema, constraint_name)
WHERE
...