This repository was archived by the owner on Jun 27, 2018. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathdb_operations.py
More file actions
288 lines (235 loc) · 6.73 KB
/
Copy pathdb_operations.py
File metadata and controls
288 lines (235 loc) · 6.73 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
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
284
285
286
287
288
#!/usr/bin/env python3
# for install psycopg2 for Python 3 use sudo apt-get install python3-psycopg2
from classy import *
import psycopg2
import json
database_name = "test_database"
user_name = "test_master"
host_name = "test-database.cvzlexvifitu.eu-central-1.rds.amazonaws.com"
port = "5432"
passwd = "testpassword"
airports = {}
with open("airports.txt") as data_file:
airports = json.loads(data_file.read())
conn = psycopg2.connect(dbname = database_name, user = user_name, host = host_name, port = port, password = passwd)
cur = conn.cursor()
create_tables_command = (
"""
CREATE TABLE Users (
user_id SERIAL PRIMARY KEY,
name VARCHAR (50) NOT NULL,
surname VARCHAR (50) NOT NULL,
age INTEGER NOT NULL,
foto BYTEA,
contact TEXT
)
""",
"""
CREATE TABLE Airport_City (
city_name VARCHAR (100) NOT NULL,
airport_abbr VARCHAR (4) PRIMARY KEY
)
""",
"""
CREATE TABLE Trips (
trip_id SERIAL PRIMARY KEY,
dest VARCHAR (4) NOT NULL,
date DATE NOT NULL,
time TIME NOT NULL,
time_diff BIGINT NOT NULL,
max_cap INTEGER NOT NULL,
CONSTRAINT dest_to_airport_abbr FOREIGN KEY (dest)
REFERENCES Airport_City (airport_abbr) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION
)
""",
"""
CREATE TABLE Flights (
flight_id SERIAL PRIMARY KEY,
from_tmsp BIGINT NOT NULL,
to_tmsp BIGINT NOT NULL,
cost INTEGER NOT NULL,
duration INTEGER NOT NULL,
from_location VARCHAR (4) NOT NULL,
book_url TEXT,
CONSTRAINT from_loc_to_airport_abbr FOREIGN KEY (from_location)
REFERENCES Airport_City (airport_abbr) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION
)
""",
"""
CREATE TABLE User_Trip (
user_id INTEGER NOT NULL,
trip_id INTEGER NOT NULL,
CONSTRAINT users_ids FOREIGN KEY (user_id)
REFERENCES Users (user_id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE CASCADE,
CONSTRAINT trips_ids FOREIGN KEY (trip_id)
REFERENCES Trips (trip_id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE CASCADE
)
"""
)
drop_tables = (
"""
DROP TABLE User_Trip
""",
"""
DROP TABLE Users
""",
"""
DROP TABLE Trips
""",
"""
DROP TABLE Flights
""",
"""
DROP TABLE Airport_City
"""
)
def get_all_trips():
trips = []
cur.execute(""" SELECT * FROM Trips """)
trip_list = cur.fetchall()
for trip in trip_list:
new_trip = Trip(trip[1], trip[2], trip[3], trip[4], trip[5])
new_trip.id = trip[0]
trips.append(trips)
return trips
def create_user(user):
add_user = (
"""
INSERT INTO Users (name, surname, age, foto, contact)
VALUES (%s, %s, %s, %s, %s)
RETURNING user_id
"""
)
user_info = (user.name, user.surname, user.age, user.foto_path, user.kontakt_path)
cur.execute(add_user, user_info)
user_id = cur.fetchone()[0]
conn.commit()
return user_id
def add_user(user, trip):
add_usr = (
"""
INSERT INTO User_Trip (user_id, trip_id)
VALUES (%s, %s)
"""
)
cur.execute(add_usr, (user.id, trip.id))
conn.commit()
def remove_user(user):
rem_user = (
"""
DELETE FROM User_Trip
WHERE user_id = %s
"""
)
cur.execute(rem_user, (user.id,))
conn.commit()
def create_trip(trip):
add_trip = (
"""
INSERT INTO Trips (dest, date, time, time_diff, max_cap)
VALUES (%s, %s, %s, %s, %s)
RETURNING trip_id
"""
)
trip_info = (trip.dest, trip.date, trip.time, trip.time_diff, trip.max_cap)
cur.execute(add_trip, trip_info)
trip_id = cur.fetchone()[0]
conn.commit()
return trip_id
def create_flight(flight):
add_flight = (
"""
INSERT INTO Flights (from_tmsp, to_tmsp, cost, duration)
VALUES (%s, %s, %s, %s)
"""
)
flight_info = (flight.from_tmsp, flight.to_tmsp, flight.cost, flight.duration)
cur.execute(add_flight, flight_info)
conn.commit()
def get_trips(city, date, time = None):
trips = []
try:
airp_list = airports[city]
for airport in airp_list:
if time:
select_trips = """ SELECT * FROM Trips WHERE dest = %s AND date = %s AND time = %s """
trip_info = (airport, date, time)
else:
select_trips = """ SELECT * FROM Trips WHERE dest = %s AND date = %s """
trip_info = (airport, date)
cur.execute(select_trips, trip_info)
trip_list = cur.fetchall()
for trip in trip_list:
new_trip = Trip(trip[1], trip[2], trip[3], trip[4], trip[5])
new_trip.id = trip[0]
trips.append(new_trip)
except (ValueError, FileNotFoundError):
print("Trips are not found!")
return trips
def get_trip_by_id(trip_id):
select = (
"""
SELECT * FROM Trips
WHERE trip_id = %s
"""
)
cur.execute(select, (trip_id,))
trip = cur.fetchone()[0]
conn.commit()
return Trip(trip[0], trip[1], trip[2], trip[3], trip[4], trip[5])
def get_partic(trip):
users = []
select_users = (
"""
SELECT * FROM User_Trip NATURAL JOIN Users
WHERE User_Trip.trip_id = %s
"""
)
cur.execute(select_users, (trip.id,))
user_list = cur.fetchall()
for user in user_list:
new_user = User(user[2], user[3], user[4], user[5], user[6])
new_user.id = user[1]
users.append(new_user)
conn.commit()
return users
def fill_test_data():
create_user(User("Vova"))
create_user(User("Andrii"))
create_user(User("Lecha"))
create_user(User("Andrei"))
create_user(User("Pavel"))
create_user(User("Roma"))
create_user(User("Jakub"))
def create_tables():
try:
print("Connected!")
print("Tables creating...")
for command in create_tables_command:
cur.execute(command)
print("Tables are created!")
print("Fill airport list...")
print("Filled!")
print("Fill data...")
# TEST
fill_test_data()
print("Filled!")
print("Drop tables...")
for command in drop_tables:
cur.execute(command)
print("Dropped!")
print("Close connection...")
cur.close()
conn.commit()
except Exception as e:
print(e)
print("Something wrong!")
finally:
if conn is not None:
conn.close()
if __name__ == "__main__":
create_tables()