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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
|
use std::mem;
use std::time::Duration;
use crate::database::error::DatabaseError;
use chrono::{DateTime, NaiveDate, TimeZone, Utc};
use proto::touchpad::common::v1::{Event, EventTime, EventTimeResult, Gender, individual_event, IndividualEvent, relay_event, RelayEvent, Stroke, SwimMeet, Swimmer, Team};
use sqlx::postgres::{PgPool, PgPoolOptions, PgQueryResult};
use proto::ProtoTimestamp;
use proto::touchpad::common::v1;
pub struct PostgresClient {
pool: PgPool,
}
impl PostgresClient {
pub async fn new<S: AsRef<str>>(url: S) -> Result<PostgresClient, sqlx::Error> {
Ok(PostgresClient {
pool: PgPoolOptions::new().connect(url.as_ref()).await?,
})
}
}
#[async_trait::async_trait]
impl super::DatabaseClient for PostgresClient {
async fn get_swimmer(&self, id: u32) -> Result<Swimmer, DatabaseError> {
let swimmer: SwimmersTableSchema = sqlx::query_as!(
SwimmersTableSchema,
"SELECT * FROM swimmers WHERE id = $1;",
id as i32
)
.fetch_one(&self.pool)
.await?;
Ok(Swimmer {
id,
name: swimmer.name,
gender: Gender::from_iso_5218(swimmer.gender as u8) as i32,
})
}
async fn add_swimmer(&self, swimmer: &Swimmer) -> Result<(), DatabaseError> {
let gender = Gender::from_i32(swimmer.gender)
.ok_or(DatabaseError::BadInput("Invalid gender".into()))?
.to_iso_5218();
sqlx::query!(
"INSERT INTO swimmers VALUES ($1, $2, $3);",
swimmer.id as i32,
swimmer.name,
gender as i16
)
.execute(&self.pool)
.await?;
Ok(())
}
async fn get_team(&self, id: u32) -> crate::database::Result<Team> {
let team: TeamsTableSchema = sqlx::query_as!(
TeamsTableSchema,
"SELECT * FROM teams WHERE id = $1;",
id as i32
)
.fetch_one(&self.pool)
.await?;
Ok(Team {
id,
name: team.name,
})
}
async fn add_team(&self, team: &Team) -> crate::database::Result<()> {
sqlx::query!(
"INSERT INTO teams VALUES ($1, $2);",
team.id as i32,
team.name
)
.execute(&self.pool)
.await?;
Ok(())
}
async fn get_meet(&self, id: u32) -> crate::database::Result<SwimMeet> {
let mut meet: Vec<GetMeetSchema> = sqlx::query_as!(
GetMeetSchema,
"
SELECT meets.name as meet_name, start_date, end_date, team_id, t.name as team_name
FROM meets
INNER JOIN meets_teams mt on meets.id = mt.meet_id
INNER JOIN teams t on mt.team_id = t.id
WHERE meets.id = $1;
",
id as i32
)
.fetch_all(&self.pool)
.await?;
let meet_name = mem::take(&mut meet.get_mut(0).ok_or(DatabaseError::NotFound)?.meet_name);
let start = meet.get(0).ok_or(DatabaseError::NotFound)?.start_date.and_hms(0, 0, 0);
let end = meet.get(0).ok_or(DatabaseError::NotFound)?.end_date.and_hms(0, 0, 0);
let teams = meet.into_iter()
.map(|m| Team {
id: m.team_id as u32,
name: m.team_name,
})
.collect();
Ok(SwimMeet {
id,
meet_name,
start: Some(Utc.from_utc_datetime(&start).into()),
end: Some(Utc.from_utc_datetime(&end).into()),
teams,
points: Default::default(),
})
}
async fn add_meet(&self, meet: &SwimMeet) -> crate::database::Result<()> {
let mut tx = self.pool.begin().await?;
// Convert protobuf timestamp into chrono Datetime
let start_date: DateTime<Utc> = {
let proto_timestamp: ProtoTimestamp = meet.start.clone()
.ok_or(DatabaseError::BadInput("empty start date".into()))?
.into();
proto_timestamp.into()
};
let end_date: DateTime<Utc> = {
let proto_timestamp: ProtoTimestamp = meet.end.clone()
.ok_or(DatabaseError::BadInput("empty end date".into()))?
.into();
proto_timestamp.into()
};
// Insert into meets table
sqlx::query!(
"INSERT INTO meets VALUES ($1, $2, $3, $4)",
meet.id as i32,
meet.meet_name,
start_date.date_naive(),
end_date.date_naive(),
)
.execute(&mut tx)
.await?;
for team in &meet.teams {
sqlx::query!(
"INSERT INTO teams VALUES ($1, $2) ON CONFLICT DO NOTHING",
team.id as i32,
team.name,
)
.execute(&mut tx)
.await?;
sqlx::query!(
"INSERT INTO meets_teams VALUES ($1, $2)",
meet.id as i32,
team.id as i32,
)
.execute(&mut tx)
.await?;
}
tx.commit().await?;
Ok(())
}
async fn get_event(&self, id: u32) -> crate::database::Result<Event> {
let event: Vec<GetEventSchema> = sqlx::query_as!(
GetEventSchema,
"
SELECT *
FROM events e
LEFT JOIN timings t on e.id = t.event_id
WHERE e.id = $1;
",
id as i32,
)
.fetch_all(&self.pool)
.await?;
db_event_to_proto(event)
}
async fn get_event_by_number(&self, meet_id: u32, number: u32) -> crate::database::Result<Event> {
let event: Vec<GetEventSchema> = sqlx::query_as!(
GetEventSchema,
"
SELECT *
FROM events e
LEFT JOIN timings t on e.id = t.event_id
WHERE e.meet_id = $1 AND e.event_num = $2;
",
meet_id as i32,
number as i32,
)
.fetch_all(&self.pool)
.await?;
db_event_to_proto(event)
}
async fn add_event(&self, event: &Event) -> crate::database::Result<()> {
let mut tx = self.pool.begin().await?;
let stroke = Stroke::from_i32(event.stroke)
.ok_or(DatabaseError::BadInput("invalid stroke".into()))?;
let gender = Gender::from_i32(event.gender)
.ok_or(DatabaseError::BadInput("invalid gender".into()))?
.to_iso_5218();
sqlx::query!(
"INSERT INTO events VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9);",
event.id as i32,
event.meet_id as i32,
event.event_num as i16,
event.age_lo as i16,
event.age_hi as i16,
event.distance as i32,
stroke as i16,
gender as i16,
event.is_relay
)
.execute(&mut tx)
.await?;
match &event.event {
Some(v1::event::Event::Individual(ev)) => {
for t in &ev.times {
if let Some(time) = &t.time {
insert_event_timing(
&mut tx,
time,
None,
Some(t.swimmer_id),
).await?;
}
}
}
Some(v1::event::Event::Relay(ev)) => {
for t in &ev.times {
if let Some(time) = &t.time {
insert_event_timing(
&mut tx,
time,
Some(&t.name),
None,
).await?;
}
}
}
None => {}
}
tx.commit().await?;
Ok(())
}
}
struct SwimmersTableSchema {
id: i32,
name: String,
gender: i16,
}
struct TeamsTableSchema {
id: i32,
name: String,
}
struct GetMeetSchema {
meet_name: String,
start_date: NaiveDate,
end_date: NaiveDate,
team_id: i32,
team_name: String,
}
struct GetEventSchema {
id: i32,
meet_id: i32,
event_num: i16,
age_lo: i16,
age_hi: i16,
distance: i32,
stroke: i16,
gender: i16,
is_relay: bool,
name: Option<String>,
swimmer_id: Option<i32>,
event_id: i32,
team_id: i32,
heat: i16,
lane: i16,
result: i16,
final_time: Option<i64>,
seed_time: Option<i64>,
rank: i16,
points: f32,
}
fn db_event_to_proto(event: Vec<GetEventSchema>) -> crate::database::Result<Event> {
let first = event.first().ok_or(DatabaseError::NotFound)?;
let mut proto_event = Event {
id: first.event_id as u32,
meet_id: first.meet_id as u32,
event_num: first.event_num as u32,
age_lo: first.age_lo as u32,
age_hi: first.age_hi as u32,
distance: first.distance as u32,
stroke: Stroke::from_i32(first.stroke as i32).unwrap_or_default() as i32,
gender: Gender::from_iso_5218(first.stroke as u8) as i32,
is_relay: first.is_relay,
event: None,
};
if proto_event.is_relay {
proto_event.event = Some(v1::event::Event::Relay(RelayEvent {
times: event.into_iter()
.map(|mut e| relay_event::RelayEventTime {
name: e.name.take().unwrap_or_default(),
time: Some(create_proto_ev_time(&e)),
})
.collect()
}));
} else {
proto_event.event = Some(v1::event::Event::Individual(IndividualEvent {
times: event.into_iter()
.map(|e| individual_event::IndividualEventTime {
swimmer_id: e.swimmer_id.unwrap_or_default() as u32,
time: Some(create_proto_ev_time(&e)),
})
.collect()
}))
}
Ok(proto_event)
}
fn create_proto_ev_time(e: &GetEventSchema) -> EventTime {
EventTime {
event_id: e.event_id as u32,
team_id: e.team_id as u32,
heat: e.heat as u32,
lane: e.lane as u32,
result: EventTimeResult::from_i32(e.result as i32).unwrap_or_default() as i32,
time: e.final_time.map(|t| Duration::from_millis(t as u64).into()),
seed: e.seed_time.map(|t| Duration::from_millis(t as u64).into()),
rank: e.rank as i32,
points: e.points,
}
}
async fn insert_event_timing(
ex: impl sqlx::PgExecutor<'_>,
time: &EventTime,
name: Option<&str>,
swimmer_id: Option<u32>,
) -> Result<PgQueryResult, sqlx::Error> {
let result = EventTimeResult::from_i32(time.result)
.unwrap_or_default();
let final_time: Option<i64> = time.time.clone()
.and_then(|t| t.try_into().ok())
.and_then(|t: Duration| t.as_millis().try_into().ok());
let seed_time: Option<i64> = time.seed.clone()
.and_then(|t| t.try_into().ok())
.and_then(|t: Duration| t.as_millis().try_into().ok());
sqlx::query!(
"INSERT INTO timings VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11);",
name,
swimmer_id.map(|id| id as i32),
time.event_id as i32,
time.team_id as i32,
time.heat as i16,
time.lane as i16,
result as i16,
time.rank as i32,
time.points,
final_time,
seed_time,
)
.execute(ex)
.await
}
|