-
Notifications
You must be signed in to change notification settings - Fork 1
/
mysql_ddl.py
253 lines (220 loc) · 7.34 KB
/
mysql_ddl.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
import MySQLdb
from mysql_cred import mysql_user, mysql_passowrd, mysql_database
import sys
def drop_bart(cursor):
#drop bart tables
cursor.execute("""drop table if exists adv_bsa;""")
cursor.execute("""drop table if exists adv_count;""")
cursor.execute("""drop table if exists adv_elev;""")
cursor.execute("""drop table if exists rout_routes;""")
cursor.execute("""drop table if exists rout_routeinfo;""")
cursor.execute("""drop table if exists rt_etd;""")
cursor.execute("""drop table if exists sched_special;""")
def create_bart(cursor):
#create all bart tables
cursor.execute("""CREATE TABLE if not exists adv_bsa (
uid bigint NOT NULL AUTO_INCREMENT,
fetchtime varchar(26),
date varchar(10),
time varchar(15),
id varchar(7),
station varchar(4),
type varchar(9),
description text,
posted varchar(28),
expires varchar(28),
PRIMARY KEY (uid))""")
result = cursor.fetchall()
#print result
cursor.execute("""CREATE TABLE if not exists adv_count (
uid bigint NOT NULL AUTO_INCREMENT,
fetchtime varchar(26),
date varchar(10),
time varchar(15),
traincount smallint,
PRIMARY KEY (uid))""")
result = cursor.fetchall()
#print result
cursor.execute("""CREATE TABLE if not exists adv_elev (
uid bigint NOT NULL AUTO_INCREMENT,
fetchtime varchar(26),
date varchar(10),
time varchar(15),
id varchar(7),
station varchar(4),
type varchar(9),
description text,
posted varchar(28),
expires varchar(28),
PRIMARY KEY (uid))""")
result = cursor.fetchall()
#print result
cursor.execute("""CREATE TABLE if not exists sched_special (
uid bigint NOT NULL AUTO_INCREMENT,
fetchtime varchar(26),
start_date varchar(10),
end_date varchar(10),
start_time varchar(5),
end_time varchar(5),
text_ text,
link text,
orig varchar(4),
dest varchar(4),
day_of_week varchar(20),
routes_affected text,
PRIMARY KEY (uid))""")
result = cursor.fetchall()
#print result
cursor.execute("""CREATE TABLE if not exists rout_routes (
uid bigint NOT NULL AUTO_INCREMENT,
fetchtime varchar(26),
sched_num smallint,
name text,
abbr varchar(9),
routeid varchar(8),
number tinyint,
color varchar(7),
PRIMARY KEY (uid))""")
result = cursor.fetchall()
#print result
cursor.execute("""CREATE TABLE if not exists rout_routeinfo (
uid bigint NOT NULL AUTO_INCREMENT,
fetchtime varchar(26),
sched_num smallint,
name text,
abbr varchar(9),
routeid varchar(8),
number tinyint,
origin varchar(4),
destination varchar(4),
direction varchar(5),
color varchar(7),
holidays tinyint,
num_stns tinyint,
stations text,
PRIMARY KEY (uid))""")
result = cursor.fetchall()
#print result
cursor.execute("""CREATE TABLE if not exists rt_etd (
uid bigint NOT NULL AUTO_INCREMENT,
fetchtime varchar(26),
date varchar(10),
time varchar(15),
name varchar(40),
abbr varchar(4),
destination varchar(40),
abbreviation varchar(4),
minutes varchar(10),
platform tinyint,
direction varchar(5),
length tinyint,
color varchar(10),
hexcolor varchar(7),
bikeflag tinyint,
PRIMARY KEY (uid))""")
result = cursor.fetchall()
#print result
cursor.execute("""show tables;""")
result = cursor.fetchall()
print result
def drop_weather(cursor):
#drop weather tables
cursor.execute("""drop table if exists weather_current;""")
cursor.execute("""drop table if exists weather_forecast;""")
def create_weather(cursor):
#create all bart tables
cursor.execute("""CREATE TABLE if not exists weather_current (
uid bigint NOT NULL AUTO_INCREMENT,
reception_time varchar(19),
location varchar(21),
clouds smallint,
detailed_status varchar(30),
dewpoint varchar(5),
heat_index varchar(4),
humidex varchar(4),
humidity smallint,
pressure_pres float,
pressure_sea_level float,
rain float,
reference_time varchar(19),
snow float,
status varchar(30),
sunrise_time varchar(19),
sunset_time varchar(19),
temperature_temp float,
temperature_temp_kf float,
temperature_temp_max float,
temperature_temp_min float,
visibility_dist mediumint,
weather_code smallint,
wind_deg float,
wind_speed float,
PRIMARY KEY (uid))""")
result = cursor.fetchall()
#print result
forecast_cols = ["clouds", "detailed_status", "dewpoint", "heat_index", "humidex", "humidity", "pressure_pres", "pressure_sea_level", "rain", "reference_time", "snow", "status", "sunrise_time", "sunset_time", "temperature_temp", "temperature_temp_kf", "temperature_temp_max", "temperature_temp_min", "visibility_dist", "weather_code", "wind_deg", "wind_speed"]
c_types = ["smallint," ,"varchar(30)," ,"varchar(5)," ,"varchar(4)," ,"varchar(4)," ,"smallint," ,"float," ,"float," ,"float," ,"varchar(19)," ,"float," ,"varchar(30)," ,"varchar(19)," ,"varchar(19)," ,"float," ,"float," ,"float," ,"float," ,"mediumint," ,"smallint," ,"float," ,"float,"]
col_names = ['reception_time', 'location']
for i in range(40):
col_names += [c+"_"+str((i+1)*3) for c in forecast_cols]
col_types = ["varchar(19),", "varchar(21),"]
for i in range(40):
col_types += c_types
weather_forecast_query = ""
for a, b in zip(col_names, col_types):
weather_forecast_query += a + " " + b + " \n"
weather_forecast_query = "CREATE TABLE if not exists weather_forecast (uid bigint NOT NULL AUTO_INCREMENT, " + weather_forecast_query + "PRIMARY KEY (uid))"
cursor.execute(weather_forecast_query)
result = cursor.fetchall()
#print result
cursor.execute("""show tables;""")
result = cursor.fetchall()
print result
def drop_onetime(cursor):
#drop weather tables
cursor.execute("""drop table if exists sched_routesched;""")
cursor.execute("""drop table if exists sched_load;""")
def create_onetime(cursor):
cursor.execute("""CREATE TABLE if not exists sched_routesched (
uid bigint NOT NULL AUTO_INCREMENT,
sched_num tinyint,
day varchar(2),
route tinyint,
train tinyint,
station varchar(4),
origtime varchar(8),
bikeflag tinyint,
PRIMARY KEY (uid))""")
result = cursor.fetchall()
#print result
cursor.execute("""CREATE TABLE if not exists sched_load (
uid bigint NOT NULL AUTO_INCREMENT,
sched_num tinyint,
station varchar(4),
route tinyint,
train tinyint,
passload tinyint,
PRIMARY KEY (uid))""")
result = cursor.fetchall()
#print result
cursor.execute("""show tables;""")
result = cursor.fetchall()
print result
if __name__ == "__main__":
con = MySQLdb.connect("localhost", mysql_user, mysql_passowrd, mysql_database)
cursor = con.cursor()
if sys.argv[1] == "drop_bart":
drop_bart(cursor)
elif sys.argv[1] == "create_bart":
create_bart(cursor)
elif sys.argv[1] == "drop_weather":
drop_weather(cursor)
elif sys.argv[1] == "create_weather":
create_weather(cursor)
elif sys.argv[1] == "drop_onetime":
drop_onetime(cursor)
elif sys.argv[1] == "create_onetime":
create_onetime(cursor)
else:
print "You need to specify an option: drop_bart, create_bart, drop_weather, create_weather"
con.close()