-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnect.py
204 lines (164 loc) · 5.66 KB
/
connect.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
'''
This script is about the connection between the app and PSQL
'''
import psycopg2
import psycopg2.extras
from psycopg2.extensions import AsIs
# We put database password for tourist and Miner here
# since they are not important and
# should be available to the public
roles_config = {
# role: database password
'tourist': '1234',
'miner': 'miner'
}
class Connect():
'''
Connect to the PostgreSQL database server with different users,
execute commands, and return results
'''
def __init__(self,
ID='tourist',
password=roles_config['tourist'],
transaction=False):
self.host = 'localhost'
self.database = 'saltlibrary'
# Connect the database as ...
# Defaut is as tourist
self.ID = ID
self.password = password
# If trans = True, then the connection won't commit and close automatically
self.trans = transaction
self.state = 0 # 0 for closed, 1 for connected, -1 for error
self.err = None # Exception message
self.conn = None
self.cur = None
try:
# Connect to the PSQL server
self.conn = psycopg2.connect(host=self.host,
database=self.database,
user=self.ID,
password=self.password)
# Create a dictionar-like cursor
self.cur = self.conn.cursor(
cursor_factory=psycopg2.extras.DictCursor)
self.state = 1 # for "connected"
print('Connected!')
except (Exception, psycopg2.DatabaseError) as error:
self.state = -1
print(error)
self.err = error
self.cc()
def cc(self):
'''Commit & Close'''
if self.cur is not None:
self.cur.close()
if self.conn is not None:
# If error occured, then rollback
if self.state == -1:
self.conn.rollback()
else:
self.conn.commit()
self.conn.close()
if self.state == -1:
print('Errors occured!')
self.state = 0
def create(self, command):
'''CREATE commands'''
if self.state != 1:
raise Exception('Not connected!')
try:
self.cur.execute(command)
# Commit & close
if self.trans == False:
self.cc()
print('Table/View created!')
except (Exception, psycopg2.DatabaseError) as error:
self.state = -1
print(error)
self.err = error
self.cc()
def modify(self, command, value_list=None):
'''Modification commands'''
if self.state != 1:
raise Exception('Not connected!')
try:
self.cur.execute(command, value_list)
rows_affected = self.cur.rowcount
# Commit & close
if self.trans == False:
self.cc()
print('%d rows modified!' % rows_affected)
except (Exception, psycopg2.DatabaseError) as error:
self.state = -1
print(error)
self.err = error
self.cc()
def execute_many(self, command, tuple_list):
'''Execute many commands at once'''
if self.state != 1:
raise Exception('Not connected!')
try:
self.cur.executemany(command, tuple_list)
# Commit & close
if self.trans == False:
self.cc()
print('Multiple execution completed!')
except (Exception, psycopg2.DatabaseError) as error:
self.state = -1
print(error)
self.err = error
self.cc()
def query(self, command, values=None, size=-1):
'''Execute a quert'''
if self.state != 1:
raise Exception('Not connected!')
try:
self.cur.execute(command, values)
# Choose the size of the returned result
if size == -1:
result = self.cur.fetchall() # list
elif size == 1:
result = self.cur.fetchone() # dict
else:
result = self.cur.fetchmany(size=size) # list
# Commit & close
if self.trans == False:
self.cc()
print('Data fetched!')
return result
except (Exception, psycopg2.DatabaseError) as error:
self.state = -1
print(error)
self.err = error
self.cc()
def create_user(self, command, values):
'''The first placeholder must be user_name'''
if self.state != 1:
raise Exception('Not connected!')
try:
self.cur.execute(command, [AsIs(values[0])] + list(values[1:]))
# Commit & close
if self.trans == False:
self.cc()
print('User created!')
except (Exception, psycopg2.DatabaseError) as error:
self.state = -1
print(error)
self.err = error
self.cc()
def execute(self, command, value_list=None):
'''Execute a command'''
if self.state != 1:
raise Exception('Not connected!')
try:
self.cur.execute(command, value_list)
# Commit & close
if self.trans == False:
self.cc()
print('Command executed!')
except (Exception, psycopg2.DatabaseError) as error:
self.state = -1
print(error)
self.err = error
self.cc()