1 # frozen_string_literal: true
14 CREATE TABLE calendar (
15 service_id VARCHAR(26),
27 calendar_dates_sql = <<~EOS
28 CREATE TABLE calendar_dates (
29 service_id VARCHAR(26),
30 calendar_date CHAR(8),
31 exception_type CHAR(1)
37 route_short_name VARCHAR(16),
39 route_color VARCHAR(6),
40 route_text_color VARCHAR(6)
43 stop_times_sql = <<~EOS
44 CREATE TABLE stop_times (
46 arrival_time VARCHAR(8),
47 departure_time VARCHAR(8),
49 stop_sequence VARCHAR(2),
58 stop_name VARCHAR(48),
66 service_id VARCHAR(26),
68 trip_headsign VARCHAR(48),
88 calendar: %w(service_id monday tuesday wednesday thursday friday saturday sunday start_date end_date),
89 calendar_dates: %w(service_id date exception_type),
90 routes: %w(route_id route_short_name route_type route_color route_text_color),
91 stop_times: %w(trip_id arrival_time departure_time stop_id stop_sequence pickup_type drop_off_type),
92 stops: %w(stop_id stop_code stop_name stop_lat stop_lon),
93 trips: %w(route_id service_id trip_id trip_headsign direction_id block_id shape_id),
96 def load_table(base_path, table_name)
97 print "Loading #{table_name}..."
99 db.execute("BEGIN TRANSACTION;")
101 csv_cols = TABLE_COLUMNS[table_name.to_sym]
102 sql_cols = csv_cols.map{|name| "date" == name ? "calendar_date" : name}
105 CSV.foreach("#{base_path}/#{table_name}.txt", headers: true, col_sep: ",") do |row|
107 values = csv_cols.map{|col| row[col.to_s]}
108 placeholders = (["?"] * values.count).join(",")
109 sql = "INSERT INTO #{table_name} (#{sql_cols.join(",")}) VALUES (#{placeholders});"
111 db.execute(sql, values)
113 if (0 == count % 10_000)
114 db.execute("COMMIT;")
115 db.execute("BEGIN TRANSACTION;")
121 db.execute("COMMIT;")
126 %w(calendar_dates calendar routes stops stop_times trips).each do |table|
127 load_table("gtfs/2022-12-14/", table)
134 @db ||= SQLite3::Database.new("gtfs.db")