-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.py
283 lines (220 loc) · 7.36 KB
/
db.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
from business import Passenger, Plane, Flight
import sqlite3
from contextlib import closing
conn = None
def connect():
global conn
if not conn:
DB_FILE = "Final.sqlite"
conn = sqlite3.connect(DB_FILE)
conn.row_factory = sqlite3.Row
#
# These functions query the DB to get planes
#
def get_plane(plane_id=0):
try:
query = """
SELECT PlaneID, Make, Model, Capacity, Age
FROM planes
WHERE PlaneID = ?
"""
with closing(conn.cursor()) as c:
c.execute(query, (plane_id,))
row = c.fetchone()
if row:
plane = Plane()
plane.id = row["PlaneID"]
plane.make = row["Make"]
plane.model = row["Model"]
plane.capacity = row["Capacity"]
plane.age = row["Age"]
return plane
return False
except sqlite3.OperationalError:
return False
def get_planes():
try:
query = """
SELECT PlaneID, Make, Model, Capacity, Age
FROM planes
"""
with closing(conn.cursor()) as c:
c.execute(query)
rows = c.fetchall()
planes = []
for row in rows:
plane = Plane()
plane.id = row["PlaneID"]
plane.make = row["Make"]
plane.model = row["Model"]
plane.capacity = row["Capacity"]
plane.age = row["Age"]
planes.append(plane)
return planes
except sqlite3.OperationalError:
return False
#
# These functions query the DB to get flights
#
def get_flights():
query = """
SELECT FlightID, PlaneID, STD, STA, Origin, Destination, MilesTravelled
FROM flights
"""
with closing(conn.cursor()) as c:
c.execute(query)
rows = c.fetchall()
flights = []
for row in rows:
flight = Flight()
flight.id = row["FlightID"]
flight.plane = get_plane(row["PlaneID"])
flight.start_time = row["STD"]
flight.arrival_time = row["STA"]
flight.origin = row["Origin"]
flight.destination = row["Destination"]
flight.miles_travelled = row["MilesTravelled"]
ids = get_passenger_ids(flight.id)
for id in ids:
flight.passengers.append(get_passenger_by_id(id))
flights.append(flight)
return flights
def get_flight(id):
query = """
SELECT FlightID, PlaneID, STD, STA, Origin, Destination, MilesTravelled
FROM flights
WHERE FlightID = ?
"""
with closing(conn.cursor()) as c:
c.execute(query, (id,))
rows = c.fetchall()
flight = Flight()
for row in rows:
flight.id = row["FlightID"]
flight.plane = get_plane(row["PlaneID"])
flight.start_time = row["STD"]
flight.arrival_time = row["STA"]
flight.origin = row["Origin"]
flight.destination = row["Destination"]
flight.miles_travelled = row["MilesTravelled"]
return flight
#
# These functions query the DB to get the passengers
#
def get_passengers(flight_id):
query = """
SELECT PassengerID, FirstName, LastName, PhoneNumber, Email, Address
FROM passengers
WHERE PassengerID IN (
SELECT PassengerID
FROM passengerToFlights
WHERE FlightID = ?
);
"""
with closing(conn.cursor()) as c:
c.execute(query, (flight_id,))
rows = c.fetchall()
passengers = []
for row in rows:
passenger = Passenger()
passenger.id = row["PassengerID"]
passenger.first_name = row["FirstName"]
passenger.last_name = row["LastName"]
passenger.phone = row["PhoneNumber"]
passenger.email = row["Email"]
passenger.address = row["Address"]
passengers.append(passenger)
return passengers
def get_passengers_by_name(last_name):
query = """
SELECT PassengerID, FirstName, LastName, PhoneNumber, Email, Address
FROM passengers
WHERE LastName = ?
"""
with closing(conn.cursor()) as c:
c.execute(query, (last_name,))
rows = c.fetchall()
passengers = []
for row in rows:
passenger = Passenger()
passenger.id = row["PassengerID"]
passenger.first_name = row["FirstName"]
passenger.last_name = row["LastName"]
passenger.phone = row["PhoneNumber"]
passenger.email = row["Email"]
passenger.address = row["Address"]
passengers.append(passenger)
return passengers
def get_passenger_by_id(id):
query = """
SELECT PassengerID, FirstName, LastName, PhoneNumber, Email, Address
FROM passengers
WHERE PassengerID = ?
"""
with closing(conn.cursor()) as c:
c.execute(query, (id,))
row = c.fetchone()
passenger = Passenger()
passenger.id = row["PassengerID"]
passenger.first_name = row["FirstName"]
passenger.last_name = row["LastName"]
passenger.phone = row["PhoneNumber"]
passenger.email = row["Email"]
passenger.address = row["Address"]
return passenger
def get_passenger_ids(flight_id):
query = """
SELECT PassengerID
FROM passengerToFlights
WHERE FlightID = ?
"""
with closing(conn.cursor()) as c:
c.execute(query, (flight_id,))
rows = c.fetchall()
ids = []
for row in rows:
ids.append(row["PassengerID"])
return ids
#
# These functions insert values the user entered into the DB
#
def save_plane(plane:Plane):
try:
query = """
INSERT INTO planes (Make, Model, Capacity, Age) VALUES (?, ?, ?, ?)
"""
with closing(conn.cursor()) as c:
c.execute(query, (plane.make, plane.model, plane.capacity, plane.age))
conn.commit()
except sqlite3.OperationalError:
print("Failed to save plane")
def save_passenger(passenger:Passenger):
try:
query = """
INSERT INTO passengers (FirstName, LastName, PhoneNumber, Email, Address) VALUES (?, ?, ?, ?, ?)
"""
with closing(conn.cursor()) as c:
c.execute(query, (passenger.first_name, passenger.last_name, passenger.phone, passenger.email, passenger.address))
conn.commit()
except sqlite3.OperationalError:
print("Falied to save passenger")
def save_flight(flight:Flight):
try:
query = """
INSERT INTO flights (PlaneID, STD, STA, Origin, Destination, MilesTravelled) VALUES (?, ?, ?, ?, ?, ?)
"""
with closing(conn.cursor()) as c:
c.execute(query, (flight.plane.id, flight.start_time, flight.arrival_time, flight.origin, flight.destination, flight.miles_travelled))
conn.commit()
except sqlite3.OperationalError:
print("Failed to save flight")
def assign_passanger(passenger_id, flight_id):
try:
query = """
INSERT INTO passengerToFlights (PassengerID, FlightID) VALUES (?, ?)
"""
with closing(conn.cursor()) as c:
c.execute(query, (passenger_id, flight_id))
conn.commit()
except sqlite3.OperationalError:
print("Failed to assign passenger to flight")