-
Notifications
You must be signed in to change notification settings - Fork 2
/
connection.py
45 lines (34 loc) · 1.32 KB
/
connection.py
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
35
36
37
38
39
40
41
42
43
44
45
from time import time
import psycopg2
from logger import logger
class Connection:
"Postgresql connection layer"
def __init__(self, environ):
self._connection_string = environ.get('CONN_STRING', '')
self._conn = None
self._connect()
def _connect(self):
try:
self._conn = psycopg2.connect(self._connection_string)
self._conn.set_session(autocommit=True)
except psycopg2.Error as exc:
logger.error('Connection._connect() error: %s', exc)
self._conn = None
def execute(self, query, params):
"Execute query(SQL) with params(tuple)"
output = False
if not self._conn:
self._connect()
if self._conn and query and params:
try:
init_time, executed_query = time(), ''
with self._conn.cursor() as cur:
cur.execute(query, params)
executed_query = cur.query
output = True
logger.info('Connection.execute(%s) %d ms', executed_query, (time() - init_time) * 1000)
except psycopg2.Error as exc:
logger.error('Connection.execute(query: %s, params: %s) error: %s', query, params, exc)
self._conn = None
return output
__all__ = ['Connection']