Package gavo :: Package base :: Module sqlsupport
[frames] | no frames]

Module sqlsupport

source code

Basic support for communicating with the database server.

This is currently very postgres specific. If we really wanted to support some other database, this would need massive refactoring.

Classes
  Error
  SqlSetAdapter
is an adapter that formats python sequences as SQL sets.
  SqlArrayAdapter
An adapter that formats python lists as SQL arrays
  FloatableAdapter
An adapter for things that do "float", in particular numpy.float*
  IntableAdapter
An adapter for things that do "int", in particular numpy.int*
  NULLAdapter
An adapter for things that should end up as NULL in the DB.
  DebugCursor
  GAVOConnection
A psycopg2 connection with some additional methods.
  DebugConnection
  NullConnection
A standin to pass whereever a function wants a connection but doesn't actually need one in a particular situation.
  PostgresQueryMixin
is a mixin containing various useful queries that are postgres specific.
  StandardQueryMixin
is a mixin containing various useful queries that should work agains all SQL systems.
  QuerierMixin
is a mixin for "queriers", i.e., objects that maintain a db connection.
  UnmanagedQuerier
A simple interface to querying the database through a connection managed by someone else.
  AdhocQuerier
A simple interface to querying the database through pooled connections.
  CustomConnectionPool
A threaded connection pool that returns connections made via profileName.
  adapter
An adapter for things that do "int", in particular numpy.int*
Functions
 
registerAdapter(type, adapter) source code
 
registerType(oid, name, castFunc) source code
 
savepointOn(conn) source code
 
getDBConnection(profile, debug=False, autocommitted=False)
returns an enhanced database connection through profile.
source code
 
parseBannerString(bannerString, digits=2)
returns digits from a postgres server banner.
source code
 
dictifyRowset(descr, rows)
turns a standard, tuple-based rowset into a list of dictionaries, the keys of which are taken from descr (a cursor.description).
source code
 
setDBMeta(conn, key, value)
adds/overwrites (key, value) in the dc.metastore table within conn.
source code
 
getDBMeta(key)
returns the value for key from within dc.metastore.
source code
 
connectionConfiguration(*args, **kwds)
A context manager setting and resetting runtimeVals in conn.
source code
 
getUntrustedConn(*args, **kwds) source code
 
getTableConn(*args, **kwds) source code
 
getAdminConn(*args, **kwds) source code
 
getWritableUntrustedConn(*args, **kwds) source code
 
getWritableTableConn(*args, **kwds) source code
 
getWritableAdminConn(*args, **kwds) source code
Variables
  debug = False
  NUMERIC_TYPES = frozenset(['bigint', 'double precision', 'inte...
  ORDERED_TYPES = frozenset(['bigint', 'double precision', 'inte...
  JOIN_FUNCTION_BODY = '\nSELECT (\n ( \n ((q3c_ang2i...
  __package__ = 'gavo.base'
  numpyType = 'int64'
Function Details

getDBConnection(profile, debug=False, autocommitted=False)

source code 

returns an enhanced database connection through profile.

You will typically rather use the context managers for the standard profiles (``getTableConnection`` and friends). Use this function if you want to keep your connection out of connection pools or if you want to use non-standard profiles.

profile will usually be a string naming a profile defined in ``GAVO_ROOT/etc``.

parseBannerString(bannerString, digits=2)

source code 

returns digits from a postgres server banner.

This hardcodes the response given by postgres 8 and raises a ValueError if the expected format is not found.

setDBMeta(conn, key, value)

source code 

adds/overwrites (key, value) in the dc.metastore table within conn.

conn must be an admin connection; this does not commit.

key must be a string, value something unicodeable.

getDBMeta(key)

source code 

returns the value for key from within dc.metastore.

This always returns a unicode string. Type conversions are the client's business.

If no value exists, this raises a KeyError.

connectionConfiguration(*args, **kwds)

source code 

A context manager setting and resetting runtimeVals in conn.

You pass just pass keyword arguments corresponding to postgres runtime configuration items (as in SET and SHOW). The manager obtains their previous values and restores them before exiting.

When the controlled body is terminated by a DBError, the settings are not reset.

If you set isLocal=False, this works for autocommitted connections, too (and in that case the reset of the run-time parameters will be attempted even when DBErrors occurred.

Since it's so frequent, you can pass timeout to give a statement_timeout in seconds.

Decorators:
  • @contextlib.contextmanager

Variables Details

NUMERIC_TYPES

Value:
frozenset(['bigint',
           'double precision',
           'integer',
           'real',
           'smallint'])

ORDERED_TYPES

Value:
frozenset(['bigint',
           'double precision',
           'integer',
           'real',
           'smallint',
           'text',
           'timestamp',
           'unicode'])

JOIN_FUNCTION_BODY

Value:
'''
SELECT (
    (   
       ((q3c_ang2ipix($3,$4)>=(q3c_nearby_it($1,$2,$5,0))) AND (q3c_an\
g2ipix($3,$4)<=(q3c_nearby_it($1,$2,$5,1))))
    OR ((q3c_ang2ipix($3,$4)>=(q3c_nearby_it($1,$2,$5,2))) AND (q3c_an\
g2ipix($3,$4)<=(q3c_nearby_it($1,$2,$5,3))))
    OR ((q3c_ang2ipix($3,$4)>=(q3c_nearby_it($1,$2,$5,4))) AND (q3c_an\
...