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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
//! Various helper functionality

use sqlx::{
    database::HasArguments, migrate::Migrator, query::Query, Database, Decode, Encode, Type,
};

use self::private::Sealed;

/// Private module for the [`Sealed`] trait.
mod private {

    /// A trait for “sealing” a trait
    #[allow(unreachable_pub)]
    pub trait Sealed {}

    #[cfg(feature = "postgres")]
    impl Sealed for sqlx::postgres::Postgres {}
    #[cfg(feature = "sqlite")]
    impl Sealed for sqlx::sqlite::Sqlite {}
}

/// Helper trait that marks an SQL-Compatible type
pub trait SqlType<DB: Database>:
    for<'a> Encode<'a, DB> + for<'a> Decode<'a, DB> + Type<DB>
{
}
impl<DB: Database, T> SqlType<DB> for T where
    T: for<'a> Encode<'a, DB> + for<'a> Decode<'a, DB> + Type<DB>
{
}

/// Helper trait for a borrowed SQL-Compatible type
pub trait BorrowedSqlType<'a, DB: Database>:
    Encode<'a, DB> + Decode<'a, DB> + Type<DB> + 'a
{
}
impl<'a, DB: Database, T> BorrowedSqlType<'a, DB> for T where
    T: Encode<'a, DB> + Decode<'a, DB> + Type<DB> + 'a
{
}

/// Supported Database trait
///
/// It contains many methods that try to generate queries for the supported databases.
#[allow(single_use_lifetimes)]
#[doc(hidden)]
pub trait SupportedDatabase: Database + Sealed {
    /// Returns the migrator for the current database type
    fn get_migrator() -> &'static Migrator;

    /// Returns a query for upserting into the `statestore_kv` table
    ///
    /// # Arguments
    /// * `$1` - The key to insert
    /// * `$2` - The value to insert
    fn kv_upsert_query<'q>() -> Query<'q, Self, <Self as HasArguments<'q>>::Arguments> {
        sqlx::query(
            r#"
                INSERT INTO statestore_kv (kv_key, kv_value)
                VALUES ($1, $2)
                ON CONFLICT (kv_key) DO UPDATE SET kv_value = $2
            "#,
        )
    }

    /// Returns a query for loading from the `statestore_kv` table
    ///
    /// # Arguments
    /// * `$1` - The key to load
    fn kv_load_query<'q>() -> Query<'q, Self, <Self as HasArguments<'q>>::Arguments> {
        sqlx::query(
            r#"
                SELECT kv_value FROM statestore_kv WHERE kv_key = $1
            "#,
        )
    }

    /// Returns a query for loading from the `statestore_media` table
    ///
    /// # Arguments
    /// * `$1` - The key to load
    fn media_load_query<'q>() -> Query<'q, Self, <Self as HasArguments<'q>>::Arguments> {
        sqlx::query(
            r#"
                UPDATE statestore_media
                SET last_access = NOW()
                WHERE media_url = $1
                RETURNING media_data
            "#,
        )
    }

    /// Returns the first query for storing into the `statestore_media` table
    ///
    /// # Arguments
    /// * `$1` - The key to insert
    /// * `$2` - The value to insert
    fn media_insert_query_1<'q>() -> Query<'q, Self, <Self as HasArguments<'q>>::Arguments> {
        sqlx::query(
            r#"
                INSERT INTO statestore_media (media_url, media_data, last_access)
                VALUES ($1, $2, NOW())
                ON CONFLICT (media_url) DO NOTHING
            "#,
        )
    }

    /// Returns the second query for storing into the `statestore_media` table
    fn media_insert_query_2<'q>() -> Query<'q, Self, <Self as HasArguments<'q>>::Arguments> {
        sqlx::query(
            r#"
                DELETE FROM statestore_media
                WHERE media_url NOT IN
                    (SELECT media_url FROM statestore_media
                     ORDER BY last_access DESC
                     LIMIT 100)
            "#,
        )
    }

    /// Deletes the media with the mxc URL
    ///
    /// # Arguments
    /// * `$1` - The mxc URL
    fn media_delete_query<'q>() -> Query<'q, Self, <Self as HasArguments<'q>>::Arguments> {
        sqlx::query(
            r#"
                DELETE FROM statestore_media
                WHERE media_url = $1
            "#,
        )
    }

    /// Deletes a room given its ID
    ///
    /// # Arguments
    /// * `$1` - The room ID
    #[must_use]
    fn room_remove_queries<'q>() -> Vec<Query<'q, Self, <Self as HasArguments<'q>>::Arguments>> {
        vec![
            sqlx::query("DELETE FROM statestore_rooms WHERE room_id = $1"),
            sqlx::query("DELETE FROM statestore_accountdata WHERE room_id = $1"),
            sqlx::query("DELETE FROM statestore_members WHERE room_id = $1"),
            sqlx::query("DELETE FROM statestore_state WHERE room_id = $1"),
            sqlx::query("DELETE FROM statestore_receipts WHERE room_id = $1"),
        ]
    }

    /// Upserts account data
    ///
    /// # Arguments
    /// * `$1` - The room ID for the account data
    /// * `$2` - The account data event type
    /// * `$3` - The account data event content
    fn account_data_upsert_query<'q>() -> Query<'q, Self, <Self as HasArguments<'q>>::Arguments> {
        sqlx::query(
            r#"
                INSERT INTO statestore_accountdata
                    (room_id, event_type, account_data)
                VALUES ($1, $2, $3)
                ON CONFLICT(room_id, event_type) DO UPDATE SET account_data = $3
            "#,
        )
    }

    /// Retrieves account data
    ///
    /// # Arguments
    /// * `$1` - The room ID for the account data
    /// * `$2` - The account data event type
    fn account_data_load_query<'q>() -> Query<'q, Self, <Self as HasArguments<'q>>::Arguments> {
        sqlx::query(
            r#"
                SELECT account_data FROM statestore_accountdata
                WHERE room_id = $1 AND event_type = $2
            "#,
        )
    }

    /// Upserts user presence data
    ///
    /// # Arguments
    /// * `$1` - The user ID
    /// * `$2` - The presence data
    fn presence_upsert_query<'q>() -> Query<'q, Self, <Self as HasArguments<'q>>::Arguments> {
        sqlx::query(
            r#"
                INSERT INTO statestore_presence
                    (user_id, presence)
                VALUES ($1, $2)
                ON CONFLICT(user_id) DO UPDATE SET presence = $2
            "#,
        )
    }

    /// Retrieves user presence data
    ///
    /// # Arguments
    /// * `$1` - The user ID
    fn presence_load_query<'q>() -> Query<'q, Self, <Self as HasArguments<'q>>::Arguments> {
        sqlx::query(
            r#"
                SELECT presence FROM statestore_presence
                WHERE user_id = $1
            "#,
        )
    }

    /// Upserts room membership information
    ///
    /// # Arguments
    /// * `$1` - The room ID
    /// * `$2` - The user ID
    /// * `$3` - Whether or not the membership event is stripped
    /// * `$4` - The membership event content
    /// * `$5` - The display name of the user
    /// * `$6` - Whether or not the user has joined
    fn member_upsert_query<'q>() -> Query<'q, Self, <Self as HasArguments<'q>>::Arguments> {
        sqlx::query(
            r#"
                INSERT INTO statestore_members
                    (room_id, user_id, is_partial, member_event, displayname, joined)
                VALUES ($1, $2, $3, $4, $5, $6)
                ON CONFLICT(room_id, user_id) DO UPDATE SET is_partial = $3, member_event = $4, displayname = $5, joined = $6
            "#,
        )
    }

    /// Upserts user profile information
    ///
    /// # Arguments
    /// * `$1` - The room ID
    /// * `$2` - The user ID
    /// * `$3` - Whether or not the information is partial
    /// * `$4` - The profile event content
    fn member_profile_upsert_query<'q>() -> Query<'q, Self, <Self as HasArguments<'q>>::Arguments> {
        sqlx::query(
            r#"
                INSERT INTO statestore_members
                    (room_id, user_id, is_partial, user_profile)
                VALUES ($1, $2, $3, $4)
                ON CONFLICT(room_id, user_id) DO UPDATE SET user_profile = $4
            "#,
        )
    }

    /// Upserts a state event
    ///
    /// # Arguments
    /// * `$1` - The room ID
    /// * `$2` - The event type
    /// * `$3` - The state key
    /// * `$4` - Whether or not the state is partial
    /// * `$5` - The event content
    fn state_upsert_query<'q>() -> Query<'q, Self, <Self as HasArguments<'q>>::Arguments> {
        sqlx::query(
            r#"
                INSERT INTO statestore_state
                    (room_id, event_type, state_key, is_partial, state_event)
                VALUES ($1, $2, $3, $4, $5)
                ON CONFLICT(room_id, event_type, state_key) DO UPDATE SET is_partial = $4, state_event = $5
            "#,
        )
    }

    /// Upserts room information
    ///
    /// # Arguments
    /// * `$1` - The room ID
    /// * `$2` - Whether or not the state is partial
    /// * `$3` - The room info
    fn room_upsert_query<'q>() -> Query<'q, Self, <Self as HasArguments<'q>>::Arguments> {
        sqlx::query(
            r#"
                INSERT INTO statestore_rooms
                    (room_id, is_partial, room_info)
                VALUES ($1, $2, $3)
                ON CONFLICT(room_id) DO UPDATE SET is_partial = $2, room_info = $3
            "#,
        )
    }

    /// Upserts an event receipt
    ///
    /// # Arguments
    /// * `$1` - The room ID
    /// * `$2` - The event ID
    /// * `$3` - The receipt type
    /// * `$4` - The user id
    /// * `$5` - The receipt content
    fn receipt_upsert_query<'q>() -> Query<'q, Self, <Self as HasArguments<'q>>::Arguments> {
        sqlx::query(
            r#"
                INSERT INTO statestore_receipts
                    (room_id, event_id, receipt_type, user_id, receipt)
                VALUES ($1, $2, $3, $4, $5)
                ON CONFLICT(room_id, receipt_type, user_id) DO UPDATE SET event_id = $2, receipt_type = $3, receipt = $5
            "#,
        )
    }

    /// Retrieves a state event
    ///
    /// # Arguments
    /// * `$1` - The room ID
    /// * `$2` - The event type
    /// * `$3` - The state key
    fn state_load_query<'q>() -> Query<'q, Self, <Self as HasArguments<'q>>::Arguments> {
        sqlx::query(
            r#"
                SELECT state_event FROM statestore_state
                WHERE room_id = $1 AND event_type = $2 AND state_key = $3 AND is_partial = '0'
            "#,
        )
    }

    /// Retrieves all state events by type in room
    ///
    /// # Arguments
    /// * `$1` - The room ID
    /// * `$2` - The event type
    /// * `$3` - Whether the state is partial
    fn states_load_query<'q>() -> Query<'q, Self, <Self as HasArguments<'q>>::Arguments> {
        sqlx::query(
            r#"
                SELECT state_event FROM statestore_state
                WHERE room_id = $1 AND event_type = $2 AND is_partial = $3
            "#,
        )
    }

    /// Retrieves the user profile event for a user in a room
    ///
    /// # Arguments
    /// * `$1` - The room ID
    /// * `$2` - The user ID
    fn profile_load_query<'q>() -> Query<'q, Self, <Self as HasArguments<'q>>::Arguments> {
        sqlx::query(
            r#"
                SELECT user_profile FROM statestore_members
                WHERE room_id = $1 AND user_id = $2 AND user_profile IS NOT NULL
            "#,
        )
    }

    /// Removes a member from a room
    ///
    /// # Arguments
    /// * `$1` - The room ID
    /// * `$2` - The user ID
    fn member_remove_query<'q>() -> Query<'q, Self, <Self as HasArguments<'q>>::Arguments> {
        sqlx::query(
            r#"
                DELETE FROM statestore_members
                WHERE room_id = $1 AND user_id = $2
            "#,
        )
    }

    /// List all users in a room
    ///
    /// # Arguments
    /// * `$1` - The room ID
    fn members_load_query<'q>() -> Query<'q, Self, <Self as HasArguments<'q>>::Arguments> {
        sqlx::query(
            r#"
                SELECT user_id FROM statestore_members
                WHERE room_id = $1
            "#,
        )
    }

    /// List all users in a room
    ///
    /// # Arguments
    /// * `$1` - The room ID
    /// * `$2` - Whether or not the user has joined
    fn members_load_query_with_join_status<'q>(
    ) -> Query<'q, Self, <Self as HasArguments<'q>>::Arguments> {
        sqlx::query(
            r#"
                SELECT user_id FROM statestore_members
                WHERE room_id = $1 AND joined = $2
            "#,
        )
    }

    /// Get specific member event
    ///
    /// # Arguments
    /// * `$1` - The room ID
    /// * `$2` - The user ID
    fn member_load_query<'q>() -> Query<'q, Self, <Self as HasArguments<'q>>::Arguments> {
        sqlx::query(
            r#"
                SELECT is_partial, member_event FROM statestore_members
                WHERE room_id = $1 AND user_id = $2 AND member_event IS NOT NULL
            "#,
        )
    }

    /// Get room infos
    ///
    /// # Arguments
    /// * `$1` - Whether or not the info is partial
    fn room_info_load_query<'q>() -> Query<'q, Self, <Self as HasArguments<'q>>::Arguments> {
        sqlx::query(
            r#"
                SELECT room_info FROM statestore_rooms
                WHERE is_partial = $1
            "#,
        )
    }

    /// Get users with display name in room
    ///
    /// # Arguments
    /// * `$1` - The room ID
    /// * `$2` - The display name
    fn users_with_display_name_load_query<'q>(
    ) -> Query<'q, Self, <Self as HasArguments<'q>>::Arguments> {
        sqlx::query(
            r#"
                SELECT user_id FROM statestore_members
                WHERE room_id = $1 AND displayname = $2
            "#,
        )
    }

    /// Get latest receipt for user in room
    ///
    /// # Arguments
    /// * `$1` - The room ID
    /// * `$2` - The receipt type
    /// * `$3` - The user ID
    fn receipt_load_query<'q>() -> Query<'q, Self, <Self as HasArguments<'q>>::Arguments> {
        sqlx::query(
            r#"
                SELECT event_id, receipt FROM statestore_receipts
                WHERE room_id = $1 AND receipt_type = $2 AND user_id = $3
            "#,
        )
    }

    /// Get all receipts for event in room
    ///
    /// # Arguments
    /// * `$1` - The room ID
    /// * `$2` - The receipt type
    /// * `$3` - The event ID
    fn event_receipt_load_query<'q>() -> Query<'q, Self, <Self as HasArguments<'q>>::Arguments> {
        sqlx::query(
            r#"
                SELECT user_id, receipt FROM statestore_receipts
                WHERE room_id = $1 AND receipt_type = $2 AND event_id = $3
            "#,
        )
    }

    /// Stores a cryptostore session
    ///
    /// # Arguments
    /// * `$1` - The hashed sender key
    /// * `$2` - The encrypted session data
    #[cfg(feature = "e2e-encryption")]
    fn session_store_query<'q>() -> Query<'q, Self, <Self as HasArguments<'q>>::Arguments> {
        sqlx::query(
            r#"
                INSERT INTO cryptostore_session (sender_key, session_data)
                VALUES ($1, $2)
            "#,
        )
    }

    /// Stores an Olm message hash
    ///
    /// # Arguments
    /// * `$1` - The sender key
    /// * `$2` - The message hash
    #[cfg(feature = "e2e-encryption")]
    fn olm_message_hash_store_query<'q>() -> Query<'q, Self, <Self as HasArguments<'q>>::Arguments>
    {
        sqlx::query(
            r#"
                INSERT INTO cryptostore_message_hash (sender_key, message_hash)
                VALUES ($1, $2)
            "#,
        )
    }

    /// Upserts an inbound group session
    ///
    /// # Arguments
    /// * `$1` - The hashed room ID
    /// * `$2` - The hashed sender key
    /// * `$3` - The hashed session id
    /// * `$4` - The encrypted session data
    #[cfg(feature = "e2e-encryption")]
    fn inbound_group_session_upsert_query<'q>(
    ) -> Query<'q, Self, <Self as HasArguments<'q>>::Arguments> {
        sqlx::query(
            r#"
                INSERT INTO cryptostore_inbound_group_session
                    (room_id, sender_key, session_id, session_data)
                VALUES ($1, $2, $3, $4)
                ON CONFLICT (room_id, sender_key, session_id)
                DO UPDATE SET session_data = $4
            "#,
        )
    }

    /// Upserts an outbound group session
    ///
    /// # Arguments
    /// * `$1` - The hashed room id
    /// * `$2` - The encrypted session data
    #[cfg(feature = "e2e-encryption")]
    fn outbound_group_session_store_query<'q>(
    ) -> Query<'q, Self, <Self as HasArguments<'q>>::Arguments> {
        sqlx::query(
            r#"
                INSERT INTO cryptostore_outbound_group_session (room_id, session_data)
                VALUES ($1, $2)
                ON CONFLICT (room_id)
                DO UPDATE SET session_data = $2
            "#,
        )
    }

    /// Stores a gossip request
    ///
    /// # Arguments
    /// * `$1` - The hashed recipient ID
    /// * `$2` - The hashed request ID
    /// * `$3` - The hashed secret request info
    /// * `$4` - Whether or not the request has been sent
    /// * `$5` - The encrypted request data
    #[cfg(feature = "e2e-encryption")]
    fn gossip_request_store_query<'q>() -> Query<'q, Self, <Self as HasArguments<'q>>::Arguments> {
        sqlx::query(
            r#"
                INSERT INTO cryptostore_gossip_request (recipient_id, request_id, info_key, sent_out, gossip_data)
                VALUES ($1, $2, $3, $4, $5)
                ON CONFLICT (request_id)
                DO UPDATE SET recipient_id = $1, info_key = $3, sent_out = $4, gossip_data = $5
            "#,
        )
    }

    /// Upserts a cryptographic identity
    ///
    /// # Arguments
    /// * `$1` - The hashed user ID
    /// * `$2` - The encrypted identity data
    #[cfg(feature = "e2e-encryption")]
    fn identity_upsert_query<'q>() -> Query<'q, Self, <Self as HasArguments<'q>>::Arguments> {
        sqlx::query(
            r#"
                INSERT INTO cryptostore_identity (user_id, identity_data)
                VALUES ($1, $2)
                ON CONFLICT (user_id) DO UPDATE SET identity_data = $2
            "#,
        )
    }

    /// Upserts a device
    ///
    /// # Arguments
    /// * `$1` - The hashed user ID
    /// * `$2` - The hashed device ID
    /// * `$3` - The encrypted device data
    #[cfg(feature = "e2e-encryption")]
    fn device_upsert_query<'q>() -> Query<'q, Self, <Self as HasArguments<'q>>::Arguments> {
        sqlx::query(
            r#"
                INSERT INTO cryptostore_device (user_id, device_id, device_info)
                VALUES ($1, $2, $3)
                ON CONFLICT (user_id, device_id) DO UPDATE SET device_info = $3
            "#,
        )
    }

    /// Deletes a device
    ///
    /// # Arguments
    /// * `$1` - The hashed user ID
    /// * `$2` - The hashed device ID
    #[cfg(feature = "e2e-encryption")]
    fn device_delete_query<'q>() -> Query<'q, Self, <Self as HasArguments<'q>>::Arguments> {
        sqlx::query(
            r#"
                DELETE FROM cryptostore_device
                WHERE user_id = $1 AND device_id = $2
            "#,
        )
    }

    /// Query to get all sessions for a sender key
    ///
    /// # Arguments
    /// * `$1` - The hashed sender key
    #[cfg(feature = "e2e-encryption")]
    fn sessions_for_user_query<'q>() -> Query<'q, Self, <Self as HasArguments<'q>>::Arguments> {
        sqlx::query(
            r#"
                SELECT session_data FROM cryptostore_session
                WHERE sender_key = $1
            "#,
        )
    }

    /// Fetch an inbound group session
    ///
    /// # Arguments
    /// * `$1` - The hashed room ID
    /// * `$2` - The hashed sender key
    /// * `$3` - The hashed session id
    #[cfg(feature = "e2e-encryption")]
    fn inbound_group_session_fetch_query<'q>(
    ) -> Query<'q, Self, <Self as HasArguments<'q>>::Arguments> {
        sqlx::query(
            r#"
                SELECT session_data FROM cryptostore_inbound_group_session
                WHERE room_id = $1 AND sender_key = $2 AND session_id = $3
            "#,
        )
    }

    /// Fetch all inbound group sessions
    #[cfg(feature = "e2e-encryption")]
    fn inbound_group_sessions_fetch_query<'q>(
    ) -> Query<'q, Self, <Self as HasArguments<'q>>::Arguments> {
        sqlx::query(
            r#"
                SELECT session_data FROM cryptostore_inbound_group_session
            "#,
        )
    }

    /// Load the outbound group session for a room
    ///
    /// # Arguments
    /// * `$1` - The hashed room ID
    #[cfg(feature = "e2e-encryption")]
    fn outbound_group_session_load_query<'q>(
    ) -> Query<'q, Self, <Self as HasArguments<'q>>::Arguments> {
        sqlx::query(
            r#"
                SELECT session_data FROM cryptostore_outbound_group_session
                WHERE room_id = $1
            "#,
        )
    }

    /// Upserts a tracked user
    ///
    /// # Arguments
    /// * `$1` - The hashed user ID
    /// * `$2` - The encrypted tracked user data
    #[cfg(feature = "e2e-encryption")]
    fn tracked_user_upsert_query<'q>() -> Query<'q, Self, <Self as HasArguments<'q>>::Arguments> {
        sqlx::query(
            r#"
                INSERT INTO cryptostore_tracked_user (user_id, tracked_user_data)
                VALUES ($1, $2)
                ON CONFLICT (user_id) DO UPDATE SET tracked_user_data = $2
            "#,
        )
    }

    /// Fetch a device
    ///
    /// # Arguments
    /// * `$1` - The hashed user ID
    /// * `$2` - The hashed device ID
    #[cfg(feature = "e2e-encryption")]
    fn device_fetch_query<'q>() -> Query<'q, Self, <Self as HasArguments<'q>>::Arguments> {
        sqlx::query(
            r#"
                SELECT device_info FROM cryptostore_device
                WHERE user_id = $1 AND device_id = $2
            "#,
        )
    }

    /// Fetch all devices of a user
    ///
    /// # Arguments
    /// * `$1` - The hashed user ID
    #[cfg(feature = "e2e-encryption")]
    fn devices_for_user_query<'q>() -> Query<'q, Self, <Self as HasArguments<'q>>::Arguments> {
        sqlx::query(
            r#"
                SELECT device_info FROM cryptostore_device
                WHERE user_id = $1
            "#,
        )
    }

    /// Retrieves all tracked users
    #[cfg(feature = "e2e-encryption")]
    fn tracked_users_fetch_query<'q>() -> Query<'q, Self, <Self as HasArguments<'q>>::Arguments> {
        sqlx::query(
            r#"
                SELECT tracked_user_data FROM cryptostore_tracked_user
            "#,
        )
    }

    /// Retrieves the cryptographic identity of a user
    ///
    /// # Arguments
    /// * `$1` - The hashed user ID
    #[cfg(feature = "e2e-encryption")]
    fn identity_fetch_query<'q>() -> Query<'q, Self, <Self as HasArguments<'q>>::Arguments> {
        sqlx::query(
            r#"
                SELECT identity_data FROM cryptostore_identity
                WHERE user_id = $1
            "#,
        )
    }

    /// Checks whether a message is known
    ///
    /// # Arguments
    /// * `$1` - The sender key
    /// * `$2` - The message hash
    #[cfg(feature = "e2e-encryption")]
    fn message_known_query<'q>() -> Query<'q, Self, <Self as HasArguments<'q>>::Arguments> {
        sqlx::query(
            r#"
                SELECT 1 FROM cryptostore_message_hash
                WHERE sender_key = $1 AND message_hash = $2
            "#,
        )
    }

    /// Retrieves a gossip equest by ID
    ///
    /// # Arguments
    /// * `$1` - The hashed request ID
    #[cfg(feature = "e2e-encryption")]
    fn gossip_request_fetch_query<'q>() -> Query<'q, Self, <Self as HasArguments<'q>>::Arguments> {
        sqlx::query(
            r#"
                SELECT gossip_data FROM cryptostore_gossip_request
                WHERE request_id = $1
            "#,
        )
    }

    /// Retrieves a gossip equest by info
    ///
    /// # Arguments
    /// * `$1` - The hashed request info
    #[cfg(feature = "e2e-encryption")]
    fn gossip_request_info_fetch_query<'q>(
    ) -> Query<'q, Self, <Self as HasArguments<'q>>::Arguments> {
        sqlx::query(
            r#"
                SELECT gossip_data FROM cryptostore_gossip_request
                WHERE info_key = $1
            "#,
        )
    }

    /// Retrieves gossip requests by sent state
    ///
    /// # Arguments
    /// * `$1` - The sent state
    #[cfg(feature = "e2e-encryption")]
    fn gossip_requests_sent_state_fetch_query<'q>(
    ) -> Query<'q, Self, <Self as HasArguments<'q>>::Arguments> {
        sqlx::query(
            r#"
                SELECT gossip_data FROM cryptostore_gossip_request
                WHERE sent_out = $1
            "#,
        )
    }

    /// Deletes gossip request by transaction ID
    ///
    /// # Arguments
    /// * `$1` - The hashed transaction ID
    #[cfg(feature = "e2e-encryption")]
    fn gossip_request_delete_query<'q>() -> Query<'q, Self, <Self as HasArguments<'q>>::Arguments> {
        sqlx::query(
            r#"
                DELETE FROM cryptostore_gossip_request
                WHERE request_id = $1
            "#,
        )
    }
}

#[cfg(feature = "postgres")]
impl SupportedDatabase for sqlx::postgres::Postgres {
    fn get_migrator() -> &'static Migrator {
        /// The migrator for postgres
        static MIGRATOR: Migrator = Migrator {
            migrations: sqlx::migrate!("./migrations/postgres").migrations,
            ignore_missing: true,
        };
        &MIGRATOR
    }
}

#[cfg(feature = "sqlite")]
impl SupportedDatabase for sqlx::sqlite::Sqlite {
    fn get_migrator() -> &'static Migrator {
        /// The migrator for sqlite
        static MIGRATOR: Migrator = Migrator {
            migrations: sqlx::migrate!("./migrations/sqlite").migrations,
            ignore_missing: true,
        };
        &MIGRATOR
    }

    fn media_load_query<'q>() -> Query<'q, Self, <Self as HasArguments<'q>>::Arguments> {
        sqlx::query(
            r#"
                UPDATE statestore_media
                SET last_access = datetime(CURRENT_TIMESTAMP, 'localtime')
                WHERE media_url = $1
                RETURNING media_data
            "#,
        )
    }

    fn media_insert_query_1<'q>() -> Query<'q, Self, <Self as HasArguments<'q>>::Arguments> {
        sqlx::query(
            r#"
                INSERT INTO statestore_media (media_url, media_data, last_access)
                VALUES ($1, $2, datetime(CURRENT_TIMESTAMP, 'localtime'))
                ON CONFLICT (media_url) DO NOTHING
            "#,
        )
    }
}