This repository was archived by the owner on Aug 21, 2019. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathsql_controller.py
More file actions
217 lines (180 loc) · 9.25 KB
/
Copy pathsql_controller.py
File metadata and controls
217 lines (180 loc) · 9.25 KB
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
210
211
212
213
214
215
216
217
# Controller to handle UI talking with the SQLite database
import sqlite3
from capra_data_types import Picture, Hike
from sql_statements import SQLStatements
class SQLController:
def __init__(self, database: str):
# TODO - be aware that this could potentially be dangerous for thread safety
self.connection = sqlite3.connect(database, check_same_thread=False)
self.statements = SQLStatements()
# Helper methods
def _build_picture_from_row(self, row: list) -> Picture:
picture = Picture(picture_id=row[0], time=row[1], altitude=row[2],
color=row[3], hike_id=row[4], index_in_hike=row[5],
camera1=row[6], camera2=row[7], camera3=row[8])
return picture
def _build_hike_from_row(self, row: list) -> Hike:
hike = Hike(hike_id=row[0], average_altitude=row[1], average_color=row[2], start_time=row[3],
end_time=row[4], pictures_num=row[5])
return hike
def _get_picture_from_sql_statement(self, statement: str) -> Picture:
cursor = self.connection.cursor()
cursor.execute(statement)
all_rows = cursor.fetchall()
picture = self._build_picture_from_row(all_rows[0])
return picture
# Time - across hikes
def get_first_time_picture(self) -> Picture:
sql = self.statements.select_by_time_first_picture()
return self._get_picture_from_sql_statement(sql)
def get_last_time_picture(self) -> Picture:
sql = self.statements.select_by_time_last_picture()
return self._get_picture_from_sql_statement(sql)
def next_time_picture_across_hikes(self, current_picture: Picture) -> Picture:
cursor = self.connection.cursor()
cursor.execute(self.statements.select_by_time_next_picture(current_picture.time))
all_rows = cursor.fetchall()
if not all_rows:
return self.get_first_time_picture()
else: # there is a next time picture
return self._build_picture_from_row(all_rows[0])
def previous_time_picture_across_hikes(self, current_picture: Picture) -> Picture:
cursor = self.connection.cursor()
cursor.execute(self.statements.select_by_time_previous_picture(current_picture.time))
all_rows = cursor.fetchall()
if not all_rows:
return self.get_last_time_picture()
else: # there is a previous time picture
return self._build_picture_from_row(all_rows[0])
# Time - in a hike
def get_first_time_picture_in_hike(self, hike_id: float) -> Picture:
sql = self.statements.select_by_time_first_picture_in_hike(hike_id)
return self._get_picture_from_sql_statement(sql)
def get_last_time_picture_in_hike(self, hike_id: float) -> Picture:
sql = self.statements.select_by_time_last_picture_in_hike(hike_id)
return self._get_picture_from_sql_statement(sql)
def next_time_picture_in_hike(self, current_picture: Picture) -> Picture:
cursor = self.connection.cursor()
h = current_picture.hike_id
t = current_picture.time
cursor.execute(self.statements.select_by_time_next_picture_in_hike(hike_id=h, time=t))
all_rows = cursor.fetchall()
if not all_rows:
return self.get_first_time_picture_in_hike(hike_id=h)
else: # there is a next time picture
return self._build_picture_from_row(all_rows[0])
def previous_time_picture_in_hike(self, current_picture: Picture) -> Picture:
cursor = self.connection.cursor()
h = current_picture.hike_id
t = current_picture.time
cursor.execute(self.statements.select_by_time_previous_picture_in_hike(hike_id=h, time=t))
all_rows = cursor.fetchall()
if not all_rows:
return self.get_last_time_picture_in_hike(hike_id=h)
else: # there is a next time picture
return self._build_picture_from_row(all_rows[0])
# Altitude - get starting picture
def get_greatest_altitude_picture(self) -> Picture:
sql = self.statements.select_by_altitude_greatest_picture()
return self._get_picture_from_sql_statement(sql)
def get_least_altitude_picture(self) -> Picture:
sql = self.statements.select_by_altitude_least_picture()
return self._get_picture_from_sql_statement(sql)
# Altitude - next & previous across hikes
def next_altitude_picture_across_hikes(self, current_picture: Picture) -> Picture:
cursor = self.connection.cursor()
t = current_picture.time
alt = current_picture.altitude
cursor.execute(self.statements.find_size_by_altitude_greater_time(altitude=alt, time=t))
all_rows = cursor.fetchall()
count = all_rows[0][0]
print(count)
if count == 0:
cursor.execute(self.statements.select_by_greater_altitude_next_picture(altitude=alt))
elif count > 0:
cursor.execute(
self.statements.select_by_equal_altitude_next_picture(altitude=alt, time=t))
all_rows = cursor.fetchall()
# end of the list of altitudes, loop back around to least altitude
if not all_rows:
return self.get_least_altitude_picture()
else: # there is a next picture
return self._build_picture_from_row(all_rows[0])
def previous_altitude_picture_across_hikes(self, current_picture: Picture) -> Picture:
cursor = self.connection.cursor()
t = current_picture.time
alt = current_picture.altitude
cursor.execute(self.statements.find_size_by_altitude_less_time(altitude=alt, time=t))
all_rows = cursor.fetchall()
count = all_rows[0][0]
if count == 0:
cursor.execute(self.statements.select_by_less_altitude_previous_picture(altitude=alt))
elif count > 0:
cursor.execute(
self.statements.select_by_equal_altitude_previous_picture(altitude=alt, time=t))
all_rows = cursor.fetchall()
# end of the list of altitudes, loop back around to greatest altitude
if not all_rows:
return self.get_greatest_altitude_picture()
else: # there is a previous picture
return self._build_picture_from_row(all_rows[0])
# Altitude - greatest & least in a hike
def get_greatest_altitude_picture_in_hike(self, hike_id: float) -> Picture:
sql = self.statements.select_by_altitude_greatest_picture_in_hike(hike_id)
return self._get_picture_from_sql_statement(sql)
def get_least_altitude_picture_in_hike(self, hike_id: float) -> Picture:
sql = self.statements.select_by_altitude_least_picture_in_hike(hike_id)
return self._get_picture_from_sql_statement(sql)
# Altitude - next & previous in a hike
def next_altitude_picture_in_hike(self, current_picture: Picture) -> Picture:
cursor = self.connection.cursor()
h = current_picture.hike_id
t = current_picture.time
alt = current_picture.altitude
cursor.execute(self.statements.find_size_by_altitude_greater_time_in_hike(hike_id=h, altitude=alt, time=t))
all_rows = cursor.fetchall()
count = all_rows[0][0]
# print(count)
if count == 0:
cursor.execute(self.statements.select_by_greater_altitude_next_picture_in_hike(hike_id=h, altitude=alt))
elif count > 0:
cursor.execute(
self.statements.select_by_equal_altitude_next_picture_in_hike(hike_id=h, altitude=alt, time=t))
all_rows = cursor.fetchall()
if not all_rows:
return self.get_least_altitude_picture_in_hike(hike_id=h)
else:
return self._build_picture_from_row(all_rows[0])
def previous_altitude_picture_in_hike(self, current_picture: Picture) -> Picture:
cursor = self.connection.cursor()
h = current_picture.hike_id
t = current_picture.time
alt = current_picture.altitude
cursor.execute(self.statements.find_size_by_altitude_less_time_in_hike(hike_id=h, altitude=alt, time=t))
all_rows = cursor.fetchall()
count = all_rows[0][0]
if count == 0:
cursor.execute(self.statements.select_by_less_altitude_previous_picture_in_hike(hike_id=h, altitude=alt))
elif count > 0:
cursor.execute(
self.statements.select_by_equal_altitude_previous_picture_in_hike(hike_id=h, altitude=alt, time=t))
all_rows = cursor.fetchall()
# end of the list of previous altitudes, loop back around to greatest altitude
if not all_rows:
return self.get_greatest_altitude_picture_in_hike(hike_id=h)
else: # there is a previous picture in hike
return self._build_picture_from_row(all_rows[0])
# Hikes
def get_size_of_hike(self, current_picture: Picture) -> int:
cursor = self.connection.cursor()
h = current_picture.hike_id
cursor.execute(self.statements.select_size_of_hike(hike_id=h))
row = cursor.fetchone()
size = int(row[0])
return size
def get_current_hike(self, current_picture: Picture) -> Hike:
cursor = self.connection.cursor()
h = current_picture.hike_id
cursor.execute(self.statements.select_hike_by_id(hike_id=h))
all_rows = cursor.fetchall()
return self._build_hike_from_row(all_rows[0])