-
Notifications
You must be signed in to change notification settings - Fork 0
/
Database.py
209 lines (171 loc) · 7.74 KB
/
Database.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
205
206
207
208
209
#!/usr/bin/python
__author__ = 'mgeorge@vlacs.org (Mike George)'
from Config import config
import psycopg2
import psycopg2.extras
def connect():
conn = psycopg2.connect(host=config.DATABASE['host'], user=config.DATABASE['user'],
password=config.DATABASE['password'], database=config.DATABASE['database'])
return conn
def enrollment_query_string(limit=None, offset=None, where=None):
query_string = "SELECT * FROM view_vlacs_class_folders"
if where != None:
query_string = "SELECT * FROM view_vlacs_class_folders WHERE %s" % (where)
if limit != None and offset != None:
query_string = "SELECT * FROM view_vlacs_class_folders LIMIT(%s) OFFSET(%s)" % (limit, offset)
elif limit != None:
query_string = "SELECT * FROM view_vlacs_class_folders LIMIT(%s)" % (limit)
elif offset != None:
query_string = "SELECT * FROM view_vlacs_class_folders OFFSET(%s)" % (offset)
return query_string
def compare_query_string():
query_string = "SELECT * FROM vlacs_class_folders_structure"
return query_string
def folder_exists_query_string(class_id):
query_string = "SELECT class_id, folder_id FROM vlacs_class_folders_structure WHERE class_id = %s" % (class_id)
return query_string
def structure_insert_string(folder_name, folder_id, folder_parent=None, class_id=None, student_id=None):
if folder_parent != None and class_id != None and student_id != None:
insert_string = "INSERT INTO vlacs_class_folders_structure (folder_name, folder_id, folder_parent, class_id, student_id) VALUES ('%s', '%s', '%s', '%s', '%s')" % (folder_name, folder_id, folder_parent, class_id, student_id)
elif folder_parent != None and class_id != None:
insert_string = "INSERT INTO vlacs_class_folders_structure (folder_name, folder_id, folder_parent, class_id) VALUES ('%s', '%s', '%s', '%s')" % (folder_name, folder_id, folder_parent, class_id)
elif class_id != None and student_id != None:
insert_string = "INSERT INTO vlacs_class_folders_structure (folder_name, folder_id, class_id, student_id) VALUES ('%s', '%s', '%s', '%s')" % (folder_name, folder_id, class_id, student_id)
elif folder_parent != None and student_id != None:
insert_string = "INSERT INTO vlacs_class_folders_structure (folder_name, folder_id, folder_parent, student_id) VALUES ('%s', '%s', '%s', '%s')" % (folder_name, folder_id, folder_parent, student_id)
elif folder_parent != None:
insert_string = "INSERT INTO vlacs_class_folders_structure (folder_name, folder_id, folder_parent) VALUES ('%s', '%s', '%s')" % (folder_name, folder_id, folder_parent)
elif class_id != None:
insert_string = "INSERT INTO vlacs_class_folders_structure (folder_name, folder_id, class_id) VALUES ('%s', '%s', '%s')" % (folder_name, folder_id, class_id)
elif student_id != None:
insert_string = "INSERT INTO vlacs_class_folders_structure (folder_name, folder_id, student_id) VALUES ('%s', '%s', '%s')" % (folder_name, folder_id, student_id)
else:
insert_string = "INSERT INTO vlacs_class_folders_structure (folder_name, folder_id) VALUES ('%s', '%s')" % (folder_name, folder_id)
return insert_string
def structure_get_folder_id_string(folder_name, class_id, student_id):
query_string = "SELECT folder_id FROM vlacs_class_folders_structure WHERE folder_name = '%s' AND class_id = '%s' AND student_id = '%s'" % (folder_name, class_id, student_id)
return query_string
def set_entry_to_archived(conn, entry):
execute(conn, "UPDATE vlacs_class_folders_structure SET isactive = 0 WHERE class_id = '%s' AND student_id = '%s' AND folder_name = '%s' AND folder_id = '%s' AND folder_parent = '%s'" % (entry['class_id'], entry['student_id'], entry['folder_name'], entry['folder_id'], entry['folder_parent']))
def execute(conn, query):
cursor = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor)
cursor.execute(query)
return cursor
def insert(conn, statement):
cursor = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor)
cursor.execute(statement)
conn.commit()
cursor.close()
def insert_if_not_exists(conn, table, cols):
cursor = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor)
q_string = construct_query_string(table, cols)
q = get(execute(conn, q_string))
if q:
return q
else:
i_string = construct_insert_string(table, cols)
insert(conn, i_string)
return True
def update(conn, table, cols, wheres):
insert(conn, construct_update_string(table, cols, wheres))
def construct_insert_string(table, cols):
i_string = "INSERT INTO %s (" % (table)
count = 1
#Construct query string and insert string
for n, col in cols.iteritems():
if count == 1:
if len(cols) > 1:
i_string += "%s, " % (n)
else:
i_string += "%s)" % (n)
count += 1
elif count == len(cols):
i_string += "%s)" % (n)
count += 1
else:
i_string += "%s, " % (n)
count += 1
count = 1
for n, col in cols.iteritems():
if count == 1:
if col['type'] == 's':
i_string += " VALUES ('%s'" % (col['value'])
else:
i_string += " VALUES (%s" % (col['value'])
if len(cols) > 1:
i_string += ","
else:
i_string += ")"
count += 1
elif count == len(cols):
if col['type'] == 's':
i_string += " '%s')" % (col['value'])
else:
i_string += " %s)" % (col['value'])
count += 1
else:
if col['type'] == 's':
i_string += " '%s'," % (col['value'])
else:
i_string += " %s," % (col['value'])
count += 1
return i_string
def construct_query_string(table, cols):
q_string = "SELECT * FROM %s WHERE " % (table)
count = 1
for n, col in cols.iteritems():
if count == 1:
if col['type'] == 's':
q_string += "%s = '%s'" % (n, col['value'])
else:
q_string += "%s = %s" % (n, col['value'])
count += 1
else:
if col['type'] == 's':
q_string += " AND %s = '%s'" % (n, col['value'])
else:
q_string += " AND %s = %s" % (n, col['value'])
count += 1
return q_string
def construct_update_string(table, cols, wheres):
u_string = "UPDATE %s SET " % (table)
count = 1
for n, col in cols.iteritems():
if count == 1:
if col['type'] == 's':
u_string += "%s = '%s'" % (n, col['value'])
else:
u_string += "%s = %s" % (n, col['value'])
count += 1
else:
if col['type'] == 's':
u_string += " AND %s = '%s'" % (n, col['value'])
else:
u_string += " AND %s = %s" % (n, col['value'])
count += 1
count = 1
for n, where in wheres.iteritems():
if count == 1:
if where['type'] == 's':
u_string += " WHERE %s = '%s'" % (n, where['value'])
else:
u_string += " WHERE %s = %s" % (n, where['value'])
count += 1
else:
if where['type'] == 's':
u_string += " AND %s = '%s'" % (n, where['value'])
else:
u_string += " AND %s = %s" % (n, where['value'])
count += 1
return u_string
def get(cursor):
results = cursor.fetchall()
cursor.close
if len(results) < 1:
return False
elif len(results) > 1:
return results
else:
return getone(results)
def getone(result_list):
return result_list[0]