ichorORM.connection
index

Copyright (c) 2016-2018 Timothy Savannah
 
Licensed under the terms of the Lesser GNU Lesser General Public License version 2.1
 
  license can be found at https://raw.githubusercontent.com/kata198/ichorORM/master/LICENSE
 
connection - Database access stuff

 
Modules
       
psycopg2
psycopg2.extensions
sys
threading
time
traceback

 
Classes
       
builtins.Exception(builtins.BaseException)
DatabaseConnectionFailure
builtins.object
DatabaseConnection

 
class DatabaseConnection(builtins.object)
    DatabaseConnection - Manages connections to the postgresql database
 
  Methods defined here:
__init__(self, host=UseGlobalSetting, port=UseGlobalSetting, dbname=UseGlobalSetting, user=UseGlobalSetting, password=UseGlobalSetting, isTransactionMode=False)
__init__ - Create a DatabaseConnection object
 
  @param host <str/None/UseGlobalSetting> Default UseGlobalSetting -
                        IP or hostname to postgresql server.
                          If left at default UseGlobalSetting, the global value
                            will be used. Use "None" to not provide this element when connecting,
                            otherwise give a value to use.
 
  @param port <int/None/UseGlobalSetting> Default UseGlobalSetting -
                        Port number to access postgresql on.
                          If left at default UseGlobalSetting, the global value
                            will be used. Use "None" to use the default [5432],
                            otherwise provide an alternate port
 
  @param dbname <str/None/UseGlobalSetting> Default UseGlobalSetting -
                        Name of database to use
                          If left at default UseGlobalSetting, the global value
                            will be used. Use "None" to not provide this element when connecting,
                            otherwise give a value to use.
 
  @param user <str/None/UseGlobalSetting> Default UseGlobalSetting -
                        Username to use
                          If left at default UseGlobalSetting, the global value
                            will be used. Use "None" to not provide this element when connecting,
                            otherwise give a value to use.
 
  @param password <str/None/UseGlobalSetting> Default UseGlobalSetting -
                        Password to use
                          If left at default UseGlobalSetting, the global value
                            will be used. Use "None" to not provide this element when connecting,
                            otherwise give a value to use.
 
  @param isTransactionMode <bool> default False, whether or not to default this connection to using transactions.
    If False, autocommit is enabled.
beginTransactionMode(self)
beginTransactionMode - Set transaction mode.
  This disables autocommit on future connection, and closes current connection.
 
@see #commitTransaction to commit the current transaction
@see #endTransactionMode to unset transaction mode
 
Alias is "startTransactionMode"
closeConnection(self)
closeConnection - Close the database connection
commit(self)
commit - Commit whatever is on the current connection
commitTransaction(self)
commitTransaction - Commit the current in-progress transaction.
 
  Requires transaction mode to be on, @see #beginTransactionMode
doInsert(self, query, valueDicts=None, autoCommit=True, returnPk=True)
doInsert - Perform an INSERT query with a parameterized query
 
@param query - Query string.
 
  For fields, e.x. "name" and "value":
 
    INSERT INTO mytable (name, number) VALUES ( %(name)s , %(value)s )
 
@param valueDicts - A list of dicts, where each dict key = column name and value = column value.
 
    So with above example select, valueDicts might be:
 
    [
        { 'name' : 'Jimbo' , 'value' : 18 },
        { 'name' : 'Timbob', 'value' : 3033 },
    ]
 
    which would cause the insert line to be executed twice,
      once for each row to be inserted (element in the list)
 
 @param autoCommit - If True, will commit transaction after these inserts
 
 @param returnPk <bool> - If True, will return the primary key(s) inserted
 
 @return list<int> - if returnPk is True, otherwise None
doSelect(self, query)
doSelect - Perform a SELECT query and return all the rows.
    Results are ordered by SELECT order
 
@param list<tuple> - List of rows, each tuple of cols
doSelectParams(self, query, params)
endTransactionMode(self)
endTransactionMode - Disable transaction mode.
  This will enable autocommit on future connections, and closes current connection.
 
@see #beginTransactionMode to re-enable transaction mode
executeSql(self, query)
executeSql - Execute arbitrary SQL.
 
@param query <str> - SQL query to execute
 
No return. Maybe can guage number of rows returned etc
executeSqlParams(self, query, params)
executeSqlParams - Execute arbitary SQL with parameterized values
 
@param query <str> - SQL Query
 
@param params <dict> - Params to pass,  %(name)s  should have an entry "name"
getConnection(self, forceReconnect=False)
getConnection - Return a psycopg2 connection based on
    connection info on this object.
 
    Will reuse existing connection, if present.
 
  @param forceReconnect <bool> default False - If True,
    will force the connection to be re-established
 
  @return < None/ psycopg2.connection object> -
    Connection, if successful, otherwise None
getCursor(self, forceReconnect=False)
getCursor - Gets a psycopg cursor to the database
 
@param forceReconnect <bool> - Default False, if True
    will force the connection to be re-established
 
@return psycopg2.cursor object
rollback(self)
rollback - rollback whatever transaction is on the current connection
startTransactionMode = beginTransactionMode(self)

Data descriptors defined here:
__dict__
dictionary for instance variables (if defined)
__weakref__
list of weak references to the object (if defined)

 
class DatabaseConnectionFailure(builtins.Exception)
    Common base class for all non-exit exceptions.
 
 
Method resolution order:
DatabaseConnectionFailure
builtins.Exception
builtins.BaseException
builtins.object

Data descriptors defined here:
__weakref__
list of weak references to the object (if defined)

Methods inherited from builtins.Exception:
__init__(self, /, *args, **kwargs)
Initialize self.  See help(type(self)) for accurate signature.
__new__(*args, **kwargs) from builtins.type
Create and return a new object.  See help(type) for accurate signature.

Methods inherited from builtins.BaseException:
__delattr__(self, name, /)
Implement delattr(self, name).
__getattribute__(self, name, /)
Return getattr(self, name).
__reduce__(...)
helper for pickle
__repr__(self, /)
Return repr(self).
__setattr__(self, name, value, /)
Implement setattr(self, name, value).
__setstate__(...)
__str__(self, /)
Return str(self).
with_traceback(...)
Exception.with_traceback(tb) --
set self.__traceback__ to tb and return self.

Data descriptors inherited from builtins.BaseException:
__cause__
exception cause
__context__
exception context
__dict__
__suppress_context__
__traceback__
args

 
Functions
       
getDatabaseConnection(host=UseGlobalSetting, port=UseGlobalSetting, dbname=UseGlobalSetting, user=UseGlobalSetting, password=UseGlobalSetting, isTransactionMode=False)
getDatabaseConnection - Gets a database connection.
 
    Should use this instead of creating a DatabaseConnection manually, to ensure ease of refactoring
      (like if we introduce connection pooling, global connections, etc)
 
@see DatabaseConnection.__init__ for arguments
 
@return DatabaseConnection object
setGlobalConnectionParams(host=IgnoreParameter, port=IgnoreParameter, dbname=IgnoreParameter, user=IgnoreParameter, password=IgnoreParameter)
setGlobalConnectionParams - Sets the global connection parameters which will be used by default
                              for connections to postgresql.
 
                            Every parameter defaults to "IgnoreParameter" and will thus not be set unless
                              specified to be something different.
 
                            i.e. to change the database with which to connect, you can call just:
 
                              setGlobalConnectionParams( dbname="blah" )
 
                            without upsetting the existing host, username, etc.
 
                    @param host <str> default IgnoreParameter - The hostname/ip with which to connect
 
                    @param port <int> default IgnoreParameter - If alternate port than 5432, specify here.
 
                    @param dbname <str> default IgnoreParameter - The database name with which to USE
 
                    @param user <str/None> default IgnoreParameter - The username with which to use.
                                            Use None (the default state) to not provide a username.
 
                    @param password <str> default IgnoreParameter - The password with which to use
                                            Use None (the default state) to not provide a password.

 
Data
        __all__ = ('setGlobalConnectionParams', 'getDatabaseConnection', 'DatabaseConnection', 'DatabaseConnectionFailure')