| |
- builtins.Exception(builtins.BaseException)
-
- ichorORM.connection.DatabaseConnectionFailure
- builtins.object
-
- ichorORM.connection.DatabaseConnection
- ichorORM.model.DatabaseModel
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, doCommit=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 doCommit <bool> Default True - If True, will commit transaction after these inserts
(formerly named autoCommit)
@param returnPk <bool> Default True - 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
|
class DatabaseModel(builtins.object) |
|
DatabaseModel - Models should extend this |
|
Methods defined here:
- __getattribute__(self, attrName)
- Return getattr(self, name).
- __init__(self, **kwargs)
- __init__ - Create an object of this type.
Arguments are in form of fieldName=fieldValue
- __repr__(self)
- __repr__ - Get object representation of this instance
@return <str> - A descriptive string of this object
- asDict(self, includePk=True)
- asDict - Return a dict representation of this model
- delete(self, dbConn=None)
- delete - Delete current object.
Should generally NOT be used, instead things should be "archived" to the best of ability.
Will clear the primary key field on this object.
If object is already deleted, this will return None
@param dbConn <None/DatabaseConnection> Default None- A specific DatabaseConnection to use,
if None generate a new connection with global settings
@return - Old ID
- getRelated(self, relationKey)
- getRelated - Returns the objects following the relation assigned to #relationKey
@param relationKey <str/???> - The key used in #getModelRelations to describe this relation
@return - If OneToOneRelation, a single object or None, if ManyToOne or OneToMany a list of objs
- insertObject(self, dbConn=None, doCommit=True)
- insertObject - Inserts current object
@param dbConn <None/DatabaseConnection> Default None- A specific DatabaseConnection to use,
if None generate a new connection with global settings
@param doCommit <bool> default True - If True, will commit upon insert.
If False, you must call dbConn.commitTransaction yourself when ready.
Primary key is set either way.
If doCommit is False, dbConn must be specified (obviously, so you can commit later)
Will raise exception if object is already saved, or a REQUIRED_FIELDS is not present.
- updateObject(self, updateFieldNames, dbConn=None, doCommit=True)
- updateObject - Performs an UPDATE on a given list of field names, based on value held on current object.
@param updateFieldNames < list<str> > - A list of field names to update.
@param dbConn <None/DatabaseConnection> Default None- A specific DatabaseConnection to use,
if None generate a new connection with global settings
@param doCommit <bool> default True - If True, will commit upon insert.
If False, you must call dbConn.commitTransaction yourself when ready.
Primary key is set either way.
If doCommit is False, dbConn must be specified (obviously, so you can commit later)
Will raise exception if current object is not saved.
Class methods defined here:
- all(orderByField=None, orderByDir='', dbConn=None) from builtins.type
- all - Get all objects associated with this model
@param orderByField <None/str> Default None, if provided the list returned
will be ordered by this sql field
@param orderByDir <str> Default empty string, if provided the list will be ordered
in this direction (DESC or ASC)
@param dbConn <None/DatabaseConnection> Default None- A specific DatabaseConnection to use,
if None generate a new connection with global settings
@return list<DatabaseModel> - A list of all objects in the database for this model
- createAndSave(dbConn=None, doCommit=True, **kwargs) from builtins.type
- createAndSave - Creates an object of this type and saves it.
Parameters are the fieldName=fieldValue for this model.
At least all entries in REQUIRED_FIELDS must be present!
@param dbConn <None/DatabaseConnection> Default None- A specific DatabaseConnection to use,
if None generate a new connection with global settings
@param doCommit <bool> default True - If True, will commit upon insert.
If False, you must call dbConn.commitTransaction yourself when ready.
Primary key is set either way.
If doCommit is False, dbConn must be specified (obviously, so you can commit later)
@return - An object of this model's type
- filter(whereType='AND', dbConn=None, **kwargs) from builtins.type
- filter - Filter and return objects of this type
@param whereType <WHERE_AND/WHERE_OR> - Whether filter criteria should be AND or OR'd together
@param dbConn <DatabaseConnection/None> default None- If present and not None,
will use this as the postgres connection.
Otherwise, will start a new connection based on global settings
Optionals:
@param orderByField - If present, results will be ordered using this field
@param orderByDir - If present, ordered results will follow this direction
All other parameters should be in the form "fieldName=value" for equality comparison,
otherwise fieldName should end with __OPERATION, e.x. fieldName__ne=value for not-equals,
fieldName__like="Start%End" for like, etc.
@return list<objs> - List of objects of this model type
- get(_pk, dbConn=None) from builtins.type
- get - Gets a single object of this model type by primary key (id)
@param _pk <str/int> - Value of primary key"
@param dbConn <None/DatabaseConnection> Default None- A specific DatabaseConnection to use,
if None generate a new connection with global settings
@return object of this type with all fields populated
- getModelRelations() from builtins.type
- getModelRelations - This method should return a list of relations.ForeignRelation objects
for any relations on this object.
This is a method instead of a property so that you can import models within this function
without creating circular references.
This method will be called once per class and the value cached.
@return dict < <str> : relations.ForeignRelations> - Foreign relations on this model
If the key is a string, this model will gain an attribute with that name which will
return the related model when accessed.
For example, { 'person' : OneToManyRelation('id_person', Person, 'id') }
If you access myModelObj.person it will return the Person object related by Person.id = myModel.id_person
The key could also be a model class or whatever, and you can get the related object(s)
by calling myModelObj.getRelated(#key)
Data descriptors defined here:
- __dict__
- dictionary for instance variables (if defined)
- __weakref__
- list of weak references to the object (if defined)
Data and other attributes defined here:
- DEFAULT_FIELD_VALUES = {}
- FIELDS = []
- MODEL_RELATIONS = []
- PRIMARY_KEY = 'id'
- REQUIRED_FIELDS = []
- SERIAL_PRIMARY_KEY = True
- TABLE_NAME = None
| |