73 lines
2.9 KiB
Python
73 lines
2.9 KiB
Python
|
|
||
|
import sqlalchemy
|
||
|
from sqlalchemy.ext.declarative import declarative_base
|
||
|
from src.ORM_modules.Event import Event
|
||
|
from src.ORM_modules.Organisation import Organisation
|
||
|
from src.ORM_modules.Relationships import Organisation_Events
|
||
|
from src.ORM_modules.base import Base
|
||
|
from sqlalchemy import and_
|
||
|
|
||
|
# Define the MySQL engine using MySQL Connector/Python
|
||
|
engine = sqlalchemy.create_engine('mysql+mysqlconnector://admin:admin@localhost/wordpress', echo=True)
|
||
|
|
||
|
|
||
|
Base.metadata.create_all(engine)
|
||
|
|
||
|
# Create a session
|
||
|
Session = sqlalchemy.orm.sessionmaker()
|
||
|
Session.configure(bind=engine)
|
||
|
session = Session()
|
||
|
|
||
|
eventName = 'event2';
|
||
|
organisationName = 'Supger GMBH2';
|
||
|
|
||
|
# create these objects somwehre, check if their primary keys (names) exists
|
||
|
__event = Event(ETitle=eventName, Edate='20.07.2019', EUrl='www.hallo.de')
|
||
|
__organisation = Organisation(OName=organisationName, ODescription='Super Orga')
|
||
|
|
||
|
rcolumnAttribute = Event.ETitle
|
||
|
lcolumnAttribute = Organisation.OName
|
||
|
|
||
|
assocType = Organisation_Events
|
||
|
|
||
|
def commit_ltarget_rtarget(__linstance, __rinstance, rcolumnAttribute, lcolumnAttribute, assocType):
|
||
|
|
||
|
attrKey = rcolumnAttribute.__dict__['key']
|
||
|
rcolumnAttributeValue = getattr(__rinstance,attrKey)
|
||
|
rcolumnType = type(__rinstance)
|
||
|
_rinstance = session.query(rcolumnType).filter(rcolumnAttribute == rcolumnAttributeValue).first()
|
||
|
|
||
|
attrKey = lcolumnAttribute.__dict__['key']
|
||
|
lcolumnAttributeValue = getattr(__linstance,attrKey)
|
||
|
lcolumnType = type(__linstance)
|
||
|
|
||
|
#check if already in DB
|
||
|
_linstance = session.query(lcolumnType).filter(lcolumnAttribute == lcolumnAttributeValue).first()
|
||
|
_assoc = None
|
||
|
|
||
|
# many to many:
|
||
|
# if both exist dont create either, but check association. => it's possible that an event is there + organisations is there but the event is created by another organisation
|
||
|
if ((_rinstance != None) and (_linstance != None)):
|
||
|
lFK = assocType.getLeftFK()
|
||
|
rFK = assocType.getRightFK()
|
||
|
_assoc = session.query(assocType).filter(and_(rFK == _rinstance.getIndex(), lFK == _linstance.getIndex())).first()
|
||
|
if (_assoc == None):
|
||
|
# create only the new association
|
||
|
_assoc = assocType(rrel = _rinstance, lrel = _linstance)
|
||
|
session.add(_assoc)
|
||
|
session.commit()
|
||
|
|
||
|
else:
|
||
|
if(_rinstance==None):
|
||
|
#_event = Event(ETitle=eventName, Edate='20.07.2019', EUrl='www.hallo.de')
|
||
|
_rinstance = __rinstance
|
||
|
if(_linstance==None):
|
||
|
#_organisation = Organisation(OName=organisationName, ODescription='Super Orga')
|
||
|
_linstance = __organisation
|
||
|
# create only the new association: missing events and/or organisations will be commited automatically
|
||
|
_assoc = assocType(rrel = _rinstance, lrel = _linstance)
|
||
|
session.add(_assoc)
|
||
|
session.commit()
|
||
|
|
||
|
commit_ltarget_rtarget(__organisation, __event, rcolumnAttribute, lcolumnAttribute, assocType)
|