summaryrefslogtreecommitdiff
path: root/app/backend/rcal/db.py
blob: 7e892a1805dca190add92b590a0a8f4b17076380 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import json
from os import path

import sqlalchemy.engine.url as url
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker


class Session(object):

    engine = None

    def __init__(self):
        config = json.load(
            open(path.join(path.dirname(
                path.realpath(__file__)), '..', 'config', 'db.json')))
        db_str = url.URL(
            drivername=config['type'],
            host=config['host'],
            username=config['user'],
            password=config['pass'],
            database=config['name'],
            query={'charset': config['cset']}
        )
        self.engine = create_engine(db_str, encoding=config['cset'])

    def get_maker(self):
        return sessionmaker(bind=self.engine)

    @staticmethod
    def create():
        session = Session()
        maker = session.get_maker()
        return maker()