-
Notifications
You must be signed in to change notification settings - Fork 0
/
DBUtils.py
126 lines (109 loc) · 3.18 KB
/
DBUtils.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
import psycopg2
# This class will handle all the database functions. You don't need to change this class.
class DBUtils:
# Open a database connection.
#
# @param user
# @param pass
# @param dbSID
# @param host
# @return connection
@staticmethod
def openDBConnection(dbUser,dbPass,dbSID,dbHost,dbPort):
conn = psycopg2.connect("dbname={} user={} password={} host={} port={}".format(dbSID,dbUser,dbPass,dbHost,dbPort))
return conn;
# Test a database connection.
#
# @param conn
# @return current date and time if the connection is open. Otherwise an exception will be thrown.
@staticmethod
def testConnection(conn):
cur = conn.cursor();
cur.execute("select now() as res")
res = ""
row = cur.fetchone()
while row is not None:
res = "Servus: " + str(row[0])
row= cur.fetchone()
cur.close()
return res
# Close the database connection.
#
# @param conn
@staticmethod
def closeConnection(conn):
conn.close()
# Execute an update or a delete query.
# @param conn
# @param query
@staticmethod
def executeUpdate(conn,query,parameters=None):
cur = conn.cursor()
cur.execute(query,parameters)
conn.commit()
cur.close()
# Get a variable that is returned as a result of a query.
# @param conn
# @param query
# @return result
@staticmethod
def getVar(conn,query,parameters=None):
cur = conn.cursor()
cur.execute(query,parameters)
result = cur.fetchone()[0]
cur.close()
return result
# Get a row that is returned as a result of a query.
# @param conn
# @param query
# @return result
@staticmethod
def getRow(conn,query,parameters=None):
cur = conn.cursor()
cur.execute(query,parameters)
result = cur.fetchone()
cur.close()
return result
# Get all rows that is returned as a result of a query.
# @param conn
# @param query
# @return result
@staticmethod
def getAllRows(conn,query, parameters=None):
cur = conn.cursor()
cur.execute(query, parameters)
result = cur.fetchall()
cur.close()
return result
@staticmethod
def getAsListFullnames(conn,query, parameters=None):
myList = []
cur = conn.cursor()
cur.execute(query, parameters)
result = cur.fetchall()
for i in result:
name = str(i[0]) + ' ' + str(i[1])
myList.append(name)
cur.close()
return myList
@staticmethod
def getAsList2(conn,query, parameters=None):
myList = []
cur = conn.cursor()
cur.execute(query, parameters)
result = cur.fetchall()
for i in result:
myList.append(i[0])
cur.close()
return myList
@staticmethod
def getAsList(conn,query, parameters=None):
myList = []
cur = conn.cursor()
cur.execute(query, parameters)
result = cur.fetchall()
myList.append("All")
for i in result:
myList.append(i[0])
cur.close()
return myList