athena_rs 3.3.0

Database gateway API
Documentation
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
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
//! Unified Athena client surface used by downstream crates.
pub mod backend;
pub mod backends;
pub mod builder;
pub mod config;
pub mod error;
pub mod gateway_api;
pub mod query_builder;
pub mod translator;

use crate::drivers::scylla::client::ScyllaConnectionInfo;
use crate::drivers::supabase::client::SupabaseConnectionInfo;
use backend::{
    BackendError, BackendResult, BackendType, DatabaseBackend, HealthStatus, QueryResult,
};
use backends::{
    gateway::GatewayBackend, postgres::PostgresBackend, scylla::ScyllaBackend,
    supabase::SupabaseBackend,
};
use builder::AthenaClientBuilder;
use config::ClientConfig;
pub use gateway_api::{
    Gateway, GatewayDeleteRequest, GatewayDriverRequest, GatewayFetchRequest, GatewayInsertRequest,
    GatewayOperation, GatewayPath, GatewayQueryResult, GatewayRequest, GatewayRequestFactory,
    GatewayRequestPayload, GatewayRoutes, GatewayRpcFilter, GatewayRpcFilterOperator,
    GatewayRpcRequest, GatewaySqlRequest, GatewayUpdateRequest, GatewayUpdateScope,
    build_gateway_endpoint, request,
};
use query_builder::{DeleteBuilder, InsertBuilder, SelectBuilder, UpdateBuilder};
use serde_json::Value;
use translator::{CqlTranslator, PostgrestTranslator, QueryTranslator, SqlTranslator};

pub use backend::{QueryLanguage, TranslatedQuery};
pub use query_builder::Condition;
pub use query_builder::ConditionOperator;
pub use query_builder::OrderDirection;
pub use query_builder::RpcBuilder;

/// Backward-compatible route alias. Prefer `Gateway::FETCH_PATH`.
pub const GATEWAY_FETCH_PATH: &str = Gateway::FETCH_PATH;
/// Backward-compatible route alias. Prefer `Gateway::INSERT_PATH`.
pub const GATEWAY_INSERT_PATH: &str = Gateway::INSERT_PATH;
/// Backward-compatible route alias. Prefer `Gateway::UPDATE_PATH`.
pub const GATEWAY_UPDATE_PATH: &str = Gateway::UPDATE_PATH;
/// Backward-compatible route alias. Prefer `Gateway::DELETE_PATH`.
pub const GATEWAY_DELETE_PATH: &str = Gateway::DELETE_PATH;
/// Backward-compatible route alias. Prefer `Gateway::QUERY_PATH`.
pub const GATEWAY_QUERY_PATH: &str = Gateway::QUERY_PATH;
/// Backward-compatible route alias. Prefer `Gateway::SQL_PATH`.
pub const GATEWAY_SQL_PATH: &str = Gateway::SQL_PATH;
/// Backward-compatible route alias. Prefer `Gateway::RPC_PATH`.
pub const GATEWAY_RPC_PATH: &str = Gateway::RPC_PATH;
/// Backward-compatible route alias. Prefer `Gateway::LEGACY_SQL_PATH`.
pub const LEGACY_SQL_PATH: &str = Gateway::LEGACY_SQL_PATH;

/// ## `AthenaClient`
/// The main client for Athena.
///
/// # Arguments
///
/// * `backend` - The backend.
/// * `config` - The client configuration.
///
/// # Returns
///
/// A `AthenaClient` containing the client.
///
pub struct AthenaClient {
    backend: Box<dyn DatabaseBackend>,
    config: ClientConfig,
}

impl AthenaClient {
    /// ## `builder`
    /// Create a new Athena client builder.
    ///
    /// # Arguments
    ///
    /// # Returns
    ///
    /// A `AthenaClientBuilder` containing the builder.
    ///
    pub fn builder() -> AthenaClientBuilder {
        AthenaClientBuilder::new()
    }

    /// ## `build`
    /// Build a new Athena client.
    ///
    /// # Arguments
    ///
    /// * `builder` - The Athena client builder.
    ///
    /// # Returns
    ///
    /// A `BackendResult` containing the client.
    ///
    pub async fn build(builder: AthenaClientBuilder) -> BackendResult<Self> {
        let config: ClientConfig = builder.build_config()?;
        Self::from_config(config).await
    }

    /// ## `new`
    /// Create a new Athena client.
    ///
    /// # Arguments
    ///
    /// * `url` - The connection URL.
    /// * `key` - The connection key.
    /// * `client` - The client name.
    /// * `backend` - The backend type.
    ///
    /// # Returns
    ///
    /// A `BackendResult` containing the client.
    ///
    pub async fn new(
        url: impl Into<String>,
        key: impl Into<String>,
        client: impl Into<String>,
    ) -> BackendResult<Self> {
        Self::new_with_backend(url, key, client, BackendType::Native).await
    }

    /// ## `new_with_backend`
    /// Create a new Athena client with a specific backend.
    ///
    /// # Arguments
    ///
    /// * `url` - The connection URL.
    /// * `key` - The connection key.
    /// * `client` - The client name.
    /// * `backend` - The backend type.
    ///
    /// # Returns
    ///
    /// A `BackendResult` containing the client.
    ///
    pub async fn new_with_backend(
        url: impl Into<String>,
        key: impl Into<String>,
        client: impl Into<String>,
        backend: BackendType,
    ) -> BackendResult<Self> {
        let builder: AthenaClientBuilder = Self::builder()
            .backend(backend)
            .url(url)
            .key(key)
            .client(client);
        Self::build(builder).await
    }

    /// ## `new_with_backend_name`
    /// Create a new Athena client with a specific backend name.
    ///
    /// # Arguments
    ///
    /// * `url` - The connection URL.
    /// * `key` - The connection key.
    /// * `client` - The client name.
    /// * `backend_name` - The backend name.
    ///
    /// # Returns
    ///
    /// A `BackendResult` containing the client.
    ///
    pub async fn new_with_backend_name(
        url: impl Into<String>,
        key: impl Into<String>,
        client: impl Into<String>,
        backend_name: &str,
    ) -> BackendResult<Self> {
        let backend: BackendType = parse_backend_name(backend_name);
        Self::new_with_backend(url, key, client, backend).await
    }

    /// ## `new_direct`
    /// Create a new Athena client directly.
    ///
    /// # Arguments
    ///
    /// * `url` - The connection URL.
    /// * `key` - The connection key.
    ///
    /// # Returns
    ///
    /// A `BackendResult` containing the client.
    ///
    pub async fn new_direct(url: impl Into<String>, key: impl Into<String>) -> BackendResult<Self> {
        let builder: AthenaClientBuilder = Self::builder().url(url).key(key);
        Self::build(builder).await
    }

    /// Return fully-qualified gateway endpoints for this client base URL.
    ///
    /// # Arguments
    ///
    /// * `base_url` - The base URL to build the gateway routes for.
    ///
    /// # Returns
    ///
    /// A `GatewayRoutes` containing the gateway routes.
    ///
    pub fn gateway_routes(&self) -> GatewayRoutes {
        GatewayRoutes::for_base_url(&self.config.connection.url)
    }

    /// Build a fully-qualified endpoint for the provided path using this client URL.
    ///
    /// # Arguments
    ///
    /// * `path` - The path to build the endpoint for.
    ///
    /// # Returns
    ///
    /// A `String` containing the fully-qualified endpoint.
    ///
    pub fn gateway_endpoint(&self, path: &str) -> String {
        build_gateway_endpoint(&self.config.connection.url, path)
    }

    /// ## `select`
    /// Create a new select builder.
    ///
    /// # Arguments
    ///
    /// * `table` - The table name.
    ///
    /// # Returns
    ///
    /// A `SelectBuilder` containing the builder.
    ///
    pub fn select(&self, table: &str) -> SelectBuilder<'_> {
        SelectBuilder::new(self, table)
    }

    /// Alias for `select` that mirrors gateway `/gateway/fetch` naming.
    pub fn fetch(&self, table: &str) -> SelectBuilder<'_> {
        self.select(table)
    }

    /// ## `insert`
    /// Create a new insert builder.
    ///
    /// # Arguments
    ///
    /// * `table` - The table name.
    ///
    /// # Returns
    ///
    /// A `InsertBuilder` containing the builder.
    ///
    pub fn insert(&self, table: &str) -> InsertBuilder<'_> {
        InsertBuilder::new(self, table)
    }

    /// ## `update`
    /// Create a new update builder.
    ///
    /// # Arguments
    ///
    /// * `table` - The table name.
    /// # Returns
    ///
    /// A `UpdateBuilder` containing the builder.
    ///
    pub fn update(&self, table: &str) -> UpdateBuilder<'_> {
        UpdateBuilder::new(self, table, None)
    }

    /// ## `update_by_id`
    /// Create a new update builder pre-filtered by row id (`id = <row_id>`).
    pub fn update_by_id(&self, table: &str, row_id: impl Into<String>) -> UpdateBuilder<'_> {
        UpdateBuilder::new(self, table, Some(row_id.into()))
    }

    /// ## `delete`
    /// Create a new delete builder.
    ///
    /// # Arguments
    ///
    /// * `table` - The table name.
    ///
    /// # Returns
    ///
    /// A `DeleteBuilder` containing the builder.
    ///
    pub fn delete(&self, table: &str) -> DeleteBuilder<'_> {
        DeleteBuilder::new(self, table, None)
    }

    /// ## `delete_by_id`
    /// Create a new delete builder pre-filtered by row id (`id = <row_id>`).
    pub fn delete_by_id(&self, table: &str, row_id: impl Into<String>) -> DeleteBuilder<'_> {
        DeleteBuilder::new(self, table, Some(row_id.into()))
    }

    /// Create an RPC builder for Postgres function execution.
    pub fn rpc(&self, function: &str, args: Value) -> query_builder::RpcBuilder<'_> {
        query_builder::RpcBuilder::new(self, function, args)
    }

    /// ## `gateway_request`
    /// Execute a typed gateway request.
    ///
    /// # Arguments
    ///
    /// * `request` - The gateway request to execute.
    ///
    /// # Returns
    ///
    /// A `BackendResult` containing the query result.
    ///
    pub async fn gateway_request<R>(&self, request: R) -> BackendResult<GatewayQueryResult>
    where
        R: Into<GatewayRequest>,
    {
        let request: GatewayRequest = request.into();
        match request.into_payload() {
            GatewayRequestPayload::Fetch(request) => {
                let mut builder: SelectBuilder<'_> = self.select(&request.table);

                if !request.columns.is_empty() {
                    builder = builder.columns(request.columns);
                }

                if let Some(raw_select) = request.raw_select {
                    builder = builder.raw_select(raw_select);
                }
                builder = builder.where_conditions(request.conditions);

                for (column, direction) in request.order_by {
                    builder = builder.order_by(&column, direction);
                }
                if let Some(limit) = request.limit {
                    builder = builder.limit(limit);
                }
                if let Some(offset) = request.offset {
                    builder = builder.offset(offset);
                }
                builder.execute().await
            }
            GatewayRequestPayload::Insert(request) => {
                self.insert(&request.table)
                    .payload(request.payload)
                    .execute()
                    .await
            }
            GatewayRequestPayload::Update(request) => {
                let update_scope: GatewayUpdateScope = request.scope;
                let mut builder: UpdateBuilder<'_> = if let Some(row_id) = update_scope.row_id {
                    self.update_by_id(&request.table, row_id)
                } else {
                    self.update(&request.table)
                };
                builder = builder.where_conditions(update_scope.conditions);
                if update_scope.allow_unfiltered {
                    builder = builder.unsafe_unfiltered();
                }
                builder.payload(request.payload).execute().await
            }
            GatewayRequestPayload::Delete(request) => {
                let mut builder: DeleteBuilder<'_> = if let Some(row_id) = request.row_id {
                    self.delete_by_id(&request.table, row_id)
                } else {
                    self.delete(&request.table)
                };
                builder = builder.where_conditions(request.conditions);
                if request.allow_unfiltered {
                    builder = builder.unsafe_unfiltered();
                }
                builder.execute().await
            }
            GatewayRequestPayload::Sql(request) => self.sql(&request.query).await,
            GatewayRequestPayload::Rpc(request) => self.rpc_request(request).await,
        }
    }

    /// Execute a typed fetch/select request using shared condition primitives.
    ///
    /// # Arguments
    ///
    /// * `request` - The gateway request to execute.
    ///
    /// # Returns
    ///
    /// A `BackendResult` containing the query result.
    ///
    pub async fn fetch_request<R>(&self, request: R) -> BackendResult<GatewayQueryResult>
    where
        R: Into<GatewayRequest>,
    {
        self.gateway_request(request).await
    }

    /// Execute a typed insert request.
    ///
    /// # Arguments
    ///
    /// * `request` - The gateway request to execute.
    ///
    /// # Returns
    ///
    /// A `BackendResult` containing the query result.
    ///
    pub async fn insert_request<R>(&self, request: R) -> BackendResult<GatewayQueryResult>
    where
        R: Into<GatewayRequest>,
    {
        self.gateway_request(request).await
    }

    /// Execute a typed update request.
    ///
    /// # Arguments
    ///
    /// * `request` - The gateway request to execute.
    ///
    /// # Returns
    ///
    /// A `BackendResult` containing the query result.
    ///
    pub async fn update_request<R>(&self, request: R) -> BackendResult<GatewayQueryResult>
    where
        R: Into<GatewayRequest>,
    {
        self.gateway_request(request).await
    }

    /// Execute a typed delete request.
    ///
    /// # Arguments
    ///
    /// * `request` - The gateway request to execute.
    ///
    /// # Returns
    ///
    /// A `BackendResult` containing the query result.
    ///
    pub async fn delete_request<R>(&self, request: R) -> BackendResult<GatewayQueryResult>
    where
        R: Into<GatewayRequest>,
    {
        self.gateway_request(request).await
    }

    /// Execute a typed RPC request.
    pub async fn rpc_request(
        &self,
        request: GatewayRpcRequest,
    ) -> BackendResult<GatewayQueryResult> {
        self.execute_rpc_request(request).await
    }

    /// ## `execute_sql`
    /// Execute a SQL query.
    ///
    /// # Arguments
    ///
    /// * `sql` - The SQL query.
    ///
    /// # Returns
    ///
    /// A `BackendResult` containing the query result.
    ///
    pub async fn execute_sql(&self, sql: &str) -> BackendResult<QueryResult> {
        let translated: TranslatedQuery = TranslatedQuery::sql(sql, Vec::new(), None);
        self.backend.execute_query(translated).await
    }

    /// Alias for `execute_sql` matching athena-js style.
    ///
    /// # Arguments
    ///
    /// * `sql` - The SQL query.
    ///
    /// # Returns
    ///
    /// A `BackendResult` containing the query result.
    ///
    pub async fn sql(&self, sql: &str) -> BackendResult<QueryResult> {
        self.execute_sql(sql).await
    }

    /// Execute a typed SQL request.
    ///
    /// # Arguments
    ///
    /// * `request` - The gateway request to execute.
    ///
    /// # Returns
    ///
    /// A `BackendResult` containing the query result.
    ///
    pub async fn sql_request<R>(&self, request: R) -> BackendResult<GatewayQueryResult>
    where
        R: Into<GatewayRequest>,
    {
        self.gateway_request(request).await
    }

    /// ## `execute_cql`
    /// Execute a CQL query.
    ///
    /// # Arguments
    ///
    /// * `cql` - The CQL query.
    ///
    /// # Returns
    ///
    /// A `BackendResult` containing the query result.
    ///
    pub async fn execute_cql(&self, cql: &str) -> BackendResult<QueryResult> {
        let translated: TranslatedQuery = TranslatedQuery::cql(cql, Vec::new(), None);
        self.backend.execute_query(translated).await
    }

    /// ## `health_check`
    /// Check the health of the client.
    ///
    /// # Arguments
    ///
    /// # Returns
    ///
    /// A `BackendResult` containing the health status.
    ///
    pub async fn health_check(&self) -> BackendResult<HealthStatus> {
        self.backend.health_check().await
    }

    /// ## `config`
    /// Get the client configuration.
    ///
    /// # Arguments
    ///
    /// # Returns
    ///
    /// A `ClientConfig` containing the client configuration.
    ///
    pub fn config(&self) -> &ClientConfig {
        &self.config
    }

    /// ## `uses_gateway_crud_routing`
    /// Returns true when the client is configured to route through gateway CRUD APIs.
    ///
    /// # Arguments
    ///
    /// # Returns
    ///
    /// A `bool` indicating if the client is configured to route through gateway CRUD APIs.
    ///
    fn uses_gateway_crud_routing(&self) -> bool {
        self.config.client_name.is_some() && self.backend.supports_sql()
    }

    /// ## `translate_request`
    /// Translate a typed gateway request to the appropriate backend-specific query.
    ///
    /// # Arguments
    ///
    /// * `request` - The gateway request to translate.
    ///
    /// # Returns
    ///
    /// A `BackendResult` containing the translated query.
    /// Translate a typed gateway request to the appropriate backend-specific query.
    ///
    /// # Arguments
    ///
    /// * `request` - The gateway request to translate.
    ///
    /// # Returns
    ///
    /// A `BackendResult` containing the translated query.
    ///
    fn translate_request(&self, request: &GatewayRequest) -> BackendResult<TranslatedQuery> {
        if self.uses_gateway_crud_routing() {
            return PostgrestTranslator.translate_request(request);
        }

        match self.backend.backend_type() {
            BackendType::Supabase | BackendType::Postgrest => {
                PostgrestTranslator.translate_request(request)
            }
            BackendType::Scylla => CqlTranslator.translate_request(request),
            BackendType::PostgreSQL | BackendType::Native | BackendType::Neon => {
                SqlTranslator.translate_request(request)
            }
        }
    }

    /// ## `execute_select`
    /// Execute a select query.
    ///
    /// # Arguments
    ///
    /// * `builder` - The select builder.
    ///
    /// # Returns
    ///
    /// A `BackendResult` containing the query result.
    ///
    pub(crate) async fn execute_select(
        &self,
        builder: SelectBuilder<'_>,
    ) -> BackendResult<QueryResult> {
        let request: GatewayRequest = builder.into_request().into();
        let translated: TranslatedQuery = self.translate_request(&request)?;
        self.backend.execute_query(translated).await
    }

    /// ## `execute_insert`
    /// Execute an insert query.
    ///
    /// # Arguments
    ///
    /// * `builder` - The insert builder.
    ///
    /// # Returns
    ///
    /// A `BackendResult` containing the query result.
    ///
    pub(crate) async fn execute_insert(
        &self,
        builder: InsertBuilder<'_>,
    ) -> BackendResult<QueryResult> {
        let request: GatewayRequest = builder.into_request().into();
        let translated: TranslatedQuery = self.translate_request(&request)?;
        self.backend.execute_query(translated).await
    }

    /// ## `execute_update`
    /// Execute an update query.
    ///
    /// # Arguments
    ///
    /// * `builder` - The update builder.
    ///
    /// # Returns
    ///
    /// A `BackendResult` containing the query result.
    ///
    pub(crate) async fn execute_update(
        &self,
        builder: UpdateBuilder<'_>,
    ) -> BackendResult<QueryResult> {
        let request: GatewayUpdateRequest = builder.into_request();
        if !query_has_scope(request.scope.row_id.as_deref(), &request.scope.conditions)
            && !request.scope.allow_unfiltered
        {
            return Err(BackendError::Generic(
                "Refusing unfiltered update; add `where_*`/`row_id(...)` or call `unsafe_unfiltered()`"
                    .to_string(),
            ));
        }
        let request: GatewayRequest = request.into();
        let translated: TranslatedQuery = self.translate_request(&request)?;
        self.backend.execute_query(translated).await
    }

    /// ## `execute_delete`
    /// Execute a delete query.
    ///
    /// # Arguments
    ///
    /// * `builder` - The delete builder.
    ///
    /// # Returns   
    pub(crate) async fn execute_delete(
        &self,
        builder: DeleteBuilder<'_>,
    ) -> BackendResult<QueryResult> {
        let request: GatewayDeleteRequest = builder.into_request();
        if !query_has_scope(request.row_id.as_deref(), &request.conditions)
            && !request.allow_unfiltered
        {
            return Err(BackendError::Generic(
                "Refusing unfiltered delete; add `where_*`/`row_id(...)` or call `unsafe_unfiltered()`"
                    .to_string(),
            ));
        }
        let request: GatewayRequest = request.into();
        let translated: TranslatedQuery = self.translate_request(&request)?;
        self.backend.execute_query(translated).await
    }

    pub(crate) async fn execute_rpc(
        &self,
        builder: query_builder::RpcBuilder<'_>,
    ) -> BackendResult<QueryResult> {
        self.execute_rpc_request(builder.into_request()).await
    }

    async fn execute_rpc_request(&self, request: GatewayRpcRequest) -> BackendResult<QueryResult> {
        if let Some(gateway_backend) = self.backend.as_any().downcast_ref::<GatewayBackend>() {
            return gateway_backend.execute_rpc_request(&request).await;
        }

        Err(BackendError::Generic(
            "RPC requests are only supported in Athena gateway client mode".to_string(),
        ))
    }

    /// ## `from_config`
    /// Create a new Athena client from a configuration.
    ///
    /// # Arguments
    ///
    /// * `config` - The client configuration.
    ///
    /// # Returns
    ///
    /// A `BackendResult` containing the client.
    ///
    async fn from_config(config: ClientConfig) -> BackendResult<Self> {
        if let Some(client_name) = config.client_name.clone() {
            let key: String = config.connection.key.clone().ok_or_else(|| {
                BackendError::Generic(
                    "Athena key is required when using client-routed gateway mode".to_string(),
                )
            })?;
            let backend: GatewayBackend = GatewayBackend::new(
                config.connection.url.clone(),
                key,
                client_name,
                config.backend_type,
            );
            return Ok(Self {
                backend: Box::new(backend),
                config,
            });
        }

        let backend: Box<dyn DatabaseBackend> = match config.backend_type {
            BackendType::Supabase => {
                let key: String =
                    config.connection.key.clone().ok_or_else(|| {
                        BackendError::Generic("Supabase key is required".to_string())
                    })?;
                let info = SupabaseConnectionInfo::new(config.connection.url.clone(), key);
                Box::new(SupabaseBackend::new(info)?)
            }
            BackendType::Scylla => {
                let info: ScyllaConnectionInfo = ScyllaConnectionInfo {
                    host: config.connection.url.clone(),
                    username: config.connection.database.clone().unwrap_or_default(),
                    password: config.connection.key.clone().unwrap_or_default(),
                };
                Box::new(ScyllaBackend::new(info))
            }
            BackendType::PostgreSQL
            | BackendType::Postgrest
            | BackendType::Native
            | BackendType::Neon => {
                let backend: PostgresBackend =
                    PostgresBackend::from_connection_string(&config.connection.url).await?;
                Box::new(backend)
            }
        };

        Ok(Self { backend, config })
    }
}

/// ## `parse_backend_name`
/// Parse a backend name to a backend type.
///
/// # Arguments
///
/// * `backend_name` - The backend name.
///
/// # Returns
///
/// A `BackendType` containing the backend type.
///
fn parse_backend_name(backend_name: &str) -> BackendType {
    match backend_name.to_ascii_lowercase().as_str() {
        "supabase" => BackendType::Supabase,
        "postgrest" => BackendType::Postgrest,
        "scylla" => BackendType::Scylla,
        "neon" => BackendType::Neon,
        "postgresql" | "postgres" => BackendType::PostgreSQL,
        _ => BackendType::Native,
    }
}

/// ## `query_has_scope`
/// Check if a query has a scope.
///
/// # Arguments
///
/// * `row_id` - The row ID.
/// * `conditions` - The conditions.
///
/// # Returns
///
/// A `bool` indicating if the query has a scope.
///
fn query_has_scope(row_id: Option<&str>, conditions: &[Condition]) -> bool {
    row_id.is_some() || !conditions.is_empty()
}

#[cfg(test)]
mod tests {
    use super::*;

    /// ## `parse_backend_name_maps_supported_aliases`
    /// Test that the parse_backend_name function maps supported aliases to the correct backend type.
    ///
    /// # Arguments
    ///
    /// # Returns
    ///
    /// A `bool` indicating if the test passed.
    ///
    /// * `backend_name` - The backend name to parse.
    ///
    /// # Returns
    ///
    /// A `BackendType` containing the backend type.
    ///
    #[test]
    fn parse_backend_name_maps_supported_aliases() {
        assert_eq!(parse_backend_name("supabase"), BackendType::Supabase);
        assert_eq!(parse_backend_name("postgrest"), BackendType::Postgrest);
        assert_eq!(parse_backend_name("scylla"), BackendType::Scylla);
        assert_eq!(parse_backend_name("neon"), BackendType::Neon);
        assert_eq!(parse_backend_name("postgresql"), BackendType::PostgreSQL);
        assert_eq!(parse_backend_name("postgres"), BackendType::PostgreSQL);
        assert_eq!(parse_backend_name("unknown"), BackendType::Native);
    }

    /// ## `new_defaults_to_native_and_sets_client_routing`
    /// Test that the new function defaults to native and sets client routing.
    ///
    /// # Arguments
    ///
    /// # Returns
    ///
    /// A `bool` indicating if the test passed.
    ///
    /// * `url` - The URL to build the client for.
    /// * `key` - The key to build the client for.
    /// * `client` - The client name to build the client for.
    ///
    /// # Returns
    ///
    /// A `AthenaClient` containing the client.
    ///
    /// * `client` - The client to test.
    ///
    /// # Returns
    ///
    /// A `bool` indicating if the test passed.
    ///
    /// * `client` - The client to test.
    ///
    /// # Returns
    ///
    /// A `bool` indicating if the test passed.
    ///
    #[tokio::test]
    async fn new_defaults_to_native_and_sets_client_routing() {
        let client: AthenaClient =
            AthenaClient::new("http://localhost:4052", "secret", "reporting")
                .await
                .expect("client should build without network");

        assert_eq!(client.config().backend_type, BackendType::Native);
        assert_eq!(client.config().client_name.as_deref(), Some("reporting"));
        assert_eq!(client.config().connection.url, "http://localhost:4052");
    }

    /// ## `new_with_backend_name_resolves_backend_case_insensitive`
    /// Test that the new_with_backend_name function resolves the backend type case-insensitively.
    ///
    /// # Arguments
    ///
    /// # Returns
    ///
    /// A `bool` indicating if the test passed.
    ///
    /// * `url` - The URL to build the client for.
    /// * `key` - The key to build the client for.
    /// * `client` - The client name to build the client for.
    ///
    /// # Returns
    ///
    /// A `AthenaClient` containing the client.
    ///
    /// * `client` - The client to test.
    ///
    /// # Returns
    ///
    /// A `bool` indicating if the test passed.
    ///
    /// * `client` - The client to test.
    ///
    /// A `bool` indicating if the test passed.
    ///
    /// * `client` - The client to test.
    ///
    /// # Returns
    ///
    /// A `bool` indicating if the test passed.
    ///
    #[tokio::test]
    async fn new_with_backend_name_resolves_backend_case_insensitive() {
        let client: AthenaClient = AthenaClient::new_with_backend_name(
            "http://localhost:4052",
            "secret",
            "reporting",
            "NeOn",
        )
        .await
        .expect("client should build without network");

        assert_eq!(client.config().backend_type, BackendType::Neon);
    }

    /// ## `gateway_routed_clients_use_postgrest_translation_for_crud`
    /// Test that gateway-routed clients use PostgREST translation for CRUD operations.
    ///
    /// # Arguments
    ///
    /// # Returns
    ///
    /// A `bool` indicating if the test passed.
    ///
    /// * `url` - The URL to build the client for.
    /// * `key` - The key to build the client for.
    /// * `client` - The client name to build the client for.
    ///
    /// # Returns
    ///
    /// A `TranslatedQuery` containing the translated query.
    ///
    /// * `client` - The client to test.
    ///
    /// # Returns
    ///
    /// A `bool` indicating if the test passed.
    ///
    /// * `client` - The client to test.
    ///
    /// A `bool` indicating if the test passed.
    ///
    /// * `client` - The client to test.
    ///
    /// # Returns
    ///
    /// A `bool` indicating if the test passed.
    ///
    #[tokio::test]
    async fn gateway_routed_clients_use_postgrest_translation_for_crud() {
        let client: AthenaClient =
            AthenaClient::new("http://localhost:4052", "secret", "reporting")
                .await
                .expect("client should build without network");

        let translated: TranslatedQuery = client
            .translate_request(&GatewayRequest::fetch("users"))
            .expect("translation should succeed");

        assert_eq!(translated.language, QueryLanguage::Postgrest);
    }

    /// ## `gateway_routes_expose_expected_endpoints`
    /// Test that the gateway_routes function exposes the expected endpoints.
    ///
    /// # Arguments
    ///
    /// # Returns
    ///
    /// A `bool` indicating if the test passed.
    ///
    /// * `url` - The URL to build the client for.
    /// * `key` - The key to build the client for.
    /// * `client` - The client name to build the client for.
    ///
    /// # Returns
    ///
    /// A `GatewayRoutes` containing the gateway routes.
    ///
    /// * `client` - The client to test.
    ///
    /// # Returns
    ///
    /// A `bool` indicating if the test passed.
    ///
    /// * `client` - The client to test.
    ///
    /// A `bool` indicating if the test passed.
    ///
    /// * `client` - The client to test.
    ///
    /// # Returns
    ///
    /// A `bool` indicating if the test passed.
    ///
    #[tokio::test]
    async fn gateway_routes_expose_expected_endpoints() {
        let client: AthenaClient =
            AthenaClient::new("http://localhost:4052", "secret", "reporting")
                .await
                .expect("client should build without network");

        let routes: GatewayRoutes = client.gateway_routes();
        assert_eq!(routes.fetch, "http://localhost:4052/gateway/fetch");
        assert_eq!(routes.insert, "http://localhost:4052/gateway/insert");
        assert_eq!(routes.update, "http://localhost:4052/gateway/update");
        assert_eq!(routes.delete, "http://localhost:4052/gateway/delete");
        assert_eq!(routes.query, "http://localhost:4052/gateway/query");
        assert_eq!(routes.sql, "http://localhost:4052/gateway/sql");
    }

    /// ## `query_has_scope_requires_row_id_or_conditions`
    /// Test that the query_has_scope function requires a row ID or conditions.
    ///
    /// # Arguments
    ///
    /// # Returns
    ///
    /// A `bool` indicating if the test passed.
    ///
    /// * `row_id` - The row ID.
    /// * `conditions` - The conditions.
    ///
    /// # Returns
    ///
    /// A `bool` indicating if the query has a scope.
    ///
    #[test]
    fn query_has_scope_requires_row_id_or_conditions() {
        assert!(!query_has_scope(None, &[]));
        assert!(query_has_scope(Some("row_1"), &[]));
        assert!(query_has_scope(
            None,
            &[Condition::new(
                "status",
                ConditionOperator::Eq,
                vec![serde_json::json!("active")]
            )]
        ));
    }
}