-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.py
53 lines (37 loc) · 1.27 KB
/
db.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
46
47
48
49
50
51
52
53
import os
from psycopg2 import pool
conn_pool = pool.SimpleConnectionPool(
1, 40,
database=os.getenv('DB_NAME'),
host=os.getenv('DB_HOST'),
port=os.getenv('DB_POST'),
user=os.getenv('DB_USER'),
password=os.getenv('DB_PASS')
)
class db:
def __init__(self, table):
self.table = table
self.pool = conn_pool
def select(self,columns='*', condition=None, joins=None):
conn = self.pool.getconn()
cursor = conn.cursor()
joins_stat=None
if joins:
for j in joins:
tmp_st = f'FULL OUTER JOIN {j["table"]} ON {j["on_cond"]}'
joins_stat = f'{str(joins_stat or "")} {tmp_st} '
cursor.execute(f'SELECT {columns} FROM {self.table} {str(joins_stat or "")} {str(condition or "")} ORDER BY {self.table}.id ASC')
rows = cursor.fetchall()
cursor.close()
conn.close()
return rows
def insert(self, columns, values):
conn = self.pool.getconn()
cursor = conn.cursor()
sql = f"INSERT INTO {self.table} ({columns}) VALUES ({values}) RETURNING id;"
cursor.execute(sql)
curr_id = cursor.fetchone()[0]
conn.commit()
cursor.close()
conn.close()
return curr_id