f064b302be5baf4455b6043b1c4a6568031a4391
[dcaegen2/platform.git] / oti / event-handler / otihandler / dbclient / apis / db_access.py
1 # ================================================================================
2 # Copyright (c) 2019-2020 AT&T Intellectual Property. All rights reserved.
3 # ================================================================================
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 #      http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 # ============LICENSE_END=========================================================
16
17 """
18 Base class for APIs to interact with application database using sqlAlchemy ORM lib and postgresSql driver
19 """
20
21 from sqlalchemy.orm import sessionmaker
22 from ..db_dao import DaoBase
23 import psycopg2
24 from psycopg2.extras import execute_values
25 import os
26 import logging
27
28
29 class DbAccess(object):
30     logger = logging.getLogger("dti_handler.DbAccess")
31     engine = None
32     session = None
33
34     def __init__(self):
35         self.engine = DaoBase.getDbEngine()
36         # create a configured "Session" class
37         Session = sessionmaker(bind=self.engine)
38
39         # create a Session
40         self.session = Session()
41
42     def saveDomainObject(self, obj):
43         self.session.add(obj)
44         self.session.commit()
45         self.session.close()
46
47     def deleteDomainObject(self,obj):
48         self.session.delete(obj)
49         self.session.commit()
50         self.session.close()