-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_conn.py
157 lines (136 loc) · 4.9 KB
/
db_conn.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import psycopg2
import logging
class Postgresconn:
database: str
user:str
password: str
host:str
port:int
conn:any
def __init__(self,database, user, password, host, port):
self.database = database
self.user = user
self.password = password
self.host = host
self.port = port
self.conn = None
def connect(self):
try:
self.conn = psycopg2.connect(
host= self.host,
port= self.port,
database= self.database,
user=self.user,
password=self.password
)
self.conn
print("Postgres database connection successful!")
except Exception as e:
print(f"Error while connecting to the database: {e}")
def disconnect(self):
if self.conn is not None:
self.conn.close()
print("Postgres database connection closed successfully!")
def update(self, query):
try:
cursor = self.conn.cursor()
cursor.execute(query)
self.conn.commit()
cursor.close()
print("Update query executed successfully!")
except Exception as e:
self.conn.rollback()
print(f"Error while executing the update query: {e}")
def insert(self, query:str):
try:
cursor = self.conn.cursor()
cursor.execute(query)
self.conn.commit()
if cursor.description is not None:
last_insert_id = cursor.fetchone()[0]
print("Insert query executed successfully!")
return last_insert_id
else:
print("Insert query executed successfully but no result returned")
return None
except Exception as e:
self.conn.rollback()
print(f"Error while executing the insert query: {e}" )
return None
def create(self, query:str):
try:
cursor = self.conn.cursor()
cursor.execute(query)
self.conn.commit()
cursor.close()
print("Table created successfully!")
except Exception as e:
self.conn.rollback()
print(f"Error while creating the table {e}" )
# QUERY FUNCTIONS WITHOUT EXCEPTION HANDLING, TO HANDLE ROLLBACK AND COMMIT EXTERNALLY
#
def query_norb(self, query:str):
cursor = self.conn.cursor()
cursor.execute(query)
if cursor.description is not None:
last_insert_id = cursor.fetchone()[0]
print("Insert query executed successfully!")
return last_insert_id
else:
print("Insert query executed successfully but no result returned")
return None
def update_norb(self, query):
cursor = self.conn.cursor()
cursor.execute(query)
cursor.close()
print("Update query executed successfully!")
def insert_norb(self, query:str):
try:
cursor = self.conn.cursor()
cursor.execute(query)
self.conn.commit()
if cursor.description is not None:
*_, last_record = cursor
last_insert_id = last_record[0]
return last_insert_id
cursor.close()
except Exception as e:
logging.error("An exception occurred while trying to insert the new data!")
logging.error(e.__str__())
return False
def create_norb(self, query:str):
try:
cursor = self.conn.cursor()
cursor.execute(query)
self.conn.commit()
cursor.close()
logging.warning("Table created successfully!")
except Exception as e:
logging.error("An exception occurred while trying to create the new table!")
logging.error(e.__str__())
def query_norb(self, query:str):
cursor = self.conn.cursor()
cursor.execute(query)
if cursor.description is not None:
last_insert_id = cursor.fetchone()[0]
print("Insert query executed successfully!")
return last_insert_id
else:
print("Insert query executed successfully but no result returned")
return None
def get_data_as_dict_norb(self,query):
try:
cursor = self.conn.cursor()
cursor.execute(query)
columns = [desc[0] for desc in cursor.description]
results = cursor.fetchall()
data = [dict(zip(columns, row)) for row in results]
return data
except Exception as e:
logging.error("An exception occurred in db_conn!")
logging.error(e.__str__())
logging.error(str(dir(e)))
def commit(self):
self.conn.commit()
def rollback(self):
self.conn.rollback()