oemof / oemof-db

Open Energy Modelling Framework - An extension to use the oemof related postgis database (requires database access on oemof-server)
MIT License
5 stars 5 forks source link

Added sqlalchemy model to store and restore oemof input- and result-data in DB #33

Closed henhuy closed 5 years ago

henhuy commented 6 years ago

Module to easily store and restore input- and result-data from oemof into database.

Notes

API The following code will setup your sqlalchemy session and create all needed tables in database:

from sqlalchemy import orm
import sqlahelper
from oemof.db import results

# Example to setup sqlalchemy db engine :
db_url = '{engine}://{username}:{password}@{host}:{port}/{db_name}'.format(
    engine='postgresql',
    username='postgres',
    password='postgres',
    host='localhost',
    port='5432',
    db_name='oemof_db'
)
engine = sqlalchemy.create_engine(db_url)
sqlahelper.add_engine(engine)
SqlAlchemySession = orm.sessionmaker(bind=engine)
results.Base.metadata.bind = engine
results.Base.metadata.create_all()

The following code stores your data into DB:

sa_session = SqlAlchemySession()
results.store_results(sa_session, input_dict, result_dict)
sa_session.close()

The following code restores your data from DB:

sa_session = SqlAlchemySession()
input_dict, result_dict = results.restore_results(sa_session, result_id)
sa_session.close()
uvchik commented 6 years ago

I guess it should look like this, right?

from oemof.db import results

sa_session = SqlAlchemySession()
results.store_results(sa_session, input_dict, result_dict)
sa_session.close()
henhuy commented 6 years ago

Yes - you are right! I will add that, thx. Did you test functionality?

uvchik commented 6 years ago

Did you test functionality?

No, I first concentrated on the API, but the API looks very good (and clear) to me now.

I will try the code within the next week. I will not have time today. Is that okay or is it urgent for you?

henhuy commented 6 years ago

No - it's definitely not urgent, as I just wanted to share the code. I already use it in my app...