radloggerpy.database.objects.base module
- class radloggerpy.database.objects.base.DatabaseObject(**kwargs)[source]
Bases:
object
Abstract database object providing abstract CRUD interfaces
When using SQLAlchemy database sessions all interactions with these sessions should be achieved using objects which implement
DatabaseObject
. These objects provide CRUD methods to handle interactions allowing to obfuscate that many of the objects in the database are consistent of multiple models.As an example, to commit an object to the database one would call:
DatabaseObject.add(session, object)
Classes implementing these interfaces should implement at least
add()
,update()
,delete()
andfind()
, however, also implementingfind_all()
andadd_all()
is preferred.All reference objects used as parameter by static methods should be instances of the implementing class itself. Likewise, find and find_all should only return objects which are instances of the class itself.
Below is a demonstration of how interactions should look:
dbo = DatabaseObject(**{field1: value1, field2: value2})
result = DatabaseObject.find(session, dbo)
print(result.field1)
Alternatively the fields can be set after the object is instantiated:
dbo = DatabaseObject()
dbo.field1 = 'hello world'
result = DatabaseObject.find(session, dbo)
print(result.field1)
For models using enums or choicetypes the values should be set as object attributes while the keys should be used for internal models.
- _abc_impl = <_abc._abc_data object>
- static _filter(filter_object, ignore=[])[source]
Filters the object depending on it’s set attributes
Removes certain empty objects such as empty collections but not empty strings or byte arrays.
- abstract static add(session, reference)[source]
Add the reference object to the database
- Parameters:
session – an active
sqlalchemy.orm.session.Session
reference – add database entries based on this object
- abstract static add_all(session, references)[source]
Add all specified objects to the database
- Parameters:
session – an active
sqlalchemy.orm.session.Session
references – add all these objects to the database
- abstract static delete(session, reference, allow_multiple=False)[source]
Remove the object(s) that match the reference
- Parameters:
session – an active
sqlalchemy.orm.session.Session
reference – remove database entries based on this object
allow_multiple – if updating multiple database items is allowed
- Raises:
MultipleResultsFound – if multiple results were found with allow_multiple as False of type
sqlalchemy.exc.MultipleResultsFound
- abstract static find(session, reference, allow_multiple=True)[source]
Return object(s) that match the reference
- Parameters:
session – an active
sqlalchemy.orm.session.Session
reference – find database results based on this object
allow_multiple – if updating multiple database items is allowed
- Raises:
MultipleResultsFound – if multiple results were found with allow_multiple as False of type
sqlalchemy.exc.MultipleResultsFound
- Returns:
A single object, list of objects or none, all objects will be instances of the class.
- abstract static find_all(session, references)[source]
For every specified object find all its matching database objects
- Parameters:
session – an active
sqlalchemy.orm.session.Session
references – find database results based on these objects
- Returns:
list of objects or none, all objects will be instances of the class.
- abstract static update(session, reference, base, allow_multiple=False)[source]
Find the reference(s) in the database and update with own state
- Parameters:
session – an active
sqlalchemy.orm.session.Session
reference – the object with the desired changes
base – current state of the object in the database
allow_multiple – if updating multiple database items is allowed
- Raises:
MultipleResultsFound – if multiple results were found with allow_multiple as False of type
sqlalchemy.exc.MultipleResultsFound