1use strum_macros::EnumString;
2
3use crate::{from_sql_for_enum, get_column, get_derived_vec, get_set, node_type, util::ID};
4
5#[derive(Clone, Copy, PartialEq, Eq, Debug, EnumString)]
6pub enum RailMode {
7 #[strum(serialize = "warp")]
8 Warp,
9 #[strum(serialize = "cart")]
10 Cart,
11 #[strum(serialize = "traincarts")]
12 TrainCarts,
13 #[strum(serialize = "vehicles")]
14 Vehicles,
15}
16from_sql_for_enum!(RailMode);
17
18node_type!(RailCompany);
19impl RailCompany {
20 get_column!("RailCompany", name, String);
21 get_column!("RailCompany", link, Option<String>);
22 get_derived_vec!(lines, RailLine, "SELECT i FROM RailLine WHERE company = ?");
23 get_derived_vec!(
24 stations,
25 RailStation,
26 "SELECT i FROM RailStation WHERE company = ?"
27 );
28 get_derived_vec!(
29 platforms,
30 RailPlatform,
31 concat!(
32 "SELECT DISTINCT RailPlatform.i ",
33 "FROM (SELECT i FROM RailStation WHERE company = ?) A ",
34 "INNER JOIN RailPlatform on A.i = RailPlatform.station"
35 )
36 );
37}
38
39node_type!(RailLine);
40impl RailLine {
41 get_column!("RailLine", code, String);
42 get_column!("RailLine", company, RailCompany);
43 get_column!("RailLine", name, Option<String>);
44 get_column!("RailLine", colour, Option<String>);
45 get_column!("RailLine", mode, Option<RailMode>);
46 get_column!("RailLine", local, Option<bool>);
47
48 get_derived_vec!(
49 platforms,
50 RailPlatform,
51 concat!(
52 "SELECT DISTINCT RailPlatform.i ",
53 "FROM (SELECT \"from\", \"to\" FROM RailConnection WHERE line = ?) A ",
54 "LEFT JOIN RailPlatform ON A.\"from\" = RailPlatform.i OR A.\"to\" = RailPlatform.i"
55 )
56 );
57 get_derived_vec!(
58 stations,
59 RailStation,
60 concat!(
61 "SELECT DISTINCT RailPlatform.station ",
62 "FROM (SELECT \"from\", \"to\" FROM RailConnection WHERE line = ?) A ",
63 "LEFT JOIN RailPlatform ON A.\"from\" = RailPlatform.i OR A.\"to\" = RailPlatform.i"
64 )
65 );
66}
67
68node_type!(located RailStation);
69impl RailStation {
70 get_set!("RailStationCodes", codes, "code", String);
71 get_column!("RailStation", company, RailCompany);
72 get_column!("RailStation", name, Option<String>);
73
74 get_derived_vec!(
75 platforms,
76 RailPlatform,
77 "SELECT i FROM RailPlatform WHERE station = ?"
78 );
79 get_derived_vec!(
80 connections_from_here,
81 RailConnection,
82 concat!(
83 "SELECT DISTINCT RailConnection.i ",
84 "FROM (SELECT i FROM RailPlatform WHERE station = ?) A ",
85 "INNER JOIN RailConnection ON A.i = RailConnection.\"from\""
86 )
87 );
88 get_derived_vec!(
89 connections_to_here,
90 RailConnection,
91 concat!(
92 "SELECT DISTINCT RailConnection.i ",
93 "FROM (SELECT i FROM RailPlatform WHERE station = ?) A ",
94 "INNER JOIN RailConnection ON A.i = RailConnection.\"to\""
95 )
96 );
97 get_derived_vec!(
98 lines,
99 RailLine,
100 concat!(
101 "SELECT DISTINCT RailConnection.line ",
102 "FROM (SELECT i FROM RailPlatform WHERE station = ?) A ",
103 "LEFT JOIN RailConnection ON A.i = RailConnection.\"from\" OR A.i = RailConnection.\"to\""
104 )
105 );
106}
107
108node_type!(RailPlatform);
109impl RailPlatform {
110 get_column!("RailPlatform", code, Option<String>);
111 get_column!("RailPlatform", station, RailStation);
112
113 get_derived_vec!(
114 connections_from_here,
115 RailConnection,
116 "SELECT RailConnection.i FROM RailConnection WHERE RailConnection.\"from\" = ?"
117 );
118 get_derived_vec!(
119 connections_to_here,
120 RailConnection,
121 "SELECT RailConnection.i FROM RailConnection WHERE RailConnection.\"to\" = ?"
122 );
123 get_derived_vec!(
124 lines,
125 RailLine,
126 concat!(
127 "SELECT DISTINCT RailConnection.line FROM RailConnection ",
128 "WHERE RailConnection.\"from\" = ? OR RailConnection.\"to\" = ?"
129 )
130 );
131}
132
133node_type!(RailConnection);
134impl RailConnection {
135 get_column!("RailConnection", line, RailLine);
136 get_column!("RailConnection", from, RailPlatform);
137 get_column!("RailConnection", to, RailPlatform);
138 get_column!("RailConnection", direction, Option<String>);
139 get_column!("RailConnection", duration, Option<u32>);
140}