Skip to main content

supabase_client_query/
backend.rs

1use std::sync::Arc;
2
3#[cfg(feature = "direct-sql")]
4use sqlx::PgPool;
5
6/// Backend for query execution.
7///
8/// By default, queries are executed via the PostgREST REST API.
9/// With the `direct-sql` feature, queries can be executed directly via sqlx.
10#[derive(Clone)]
11pub enum QueryBackend {
12    /// PostgREST REST API backend (default).
13    Rest {
14        http: reqwest::Client,
15        base_url: Arc<str>,
16        api_key: Arc<str>,
17        schema: String,
18    },
19    /// Direct SQL via sqlx (opt-in with `direct-sql` feature).
20    #[cfg(feature = "direct-sql")]
21    DirectSql {
22        pool: Arc<PgPool>,
23    },
24}
25
26impl std::fmt::Debug for QueryBackend {
27    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
28        match self {
29            Self::Rest { base_url, schema, .. } => {
30                f.debug_struct("Rest")
31                    .field("base_url", base_url)
32                    .field("schema", schema)
33                    .finish()
34            }
35            #[cfg(feature = "direct-sql")]
36            Self::DirectSql { .. } => f.debug_struct("DirectSql").finish(),
37        }
38    }
39}
40
41#[cfg(test)]
42mod tests {
43    use super::*;
44
45    #[test]
46    fn test_query_backend_rest_construction() {
47        let backend = QueryBackend::Rest {
48            http: reqwest::Client::new(),
49            base_url: Arc::from("http://localhost:54321"),
50            api_key: Arc::from("test-api-key"),
51            schema: "public".to_string(),
52        };
53        match &backend {
54            QueryBackend::Rest { base_url, api_key, schema, .. } => {
55                assert_eq!(base_url.as_ref(), "http://localhost:54321");
56                assert_eq!(api_key.as_ref(), "test-api-key");
57                assert_eq!(schema, "public");
58            }
59            #[cfg(feature = "direct-sql")]
60            _ => panic!("expected Rest variant"),
61        }
62    }
63
64    #[test]
65    fn test_query_backend_debug_rest() {
66        let backend = QueryBackend::Rest {
67            http: reqwest::Client::new(),
68            base_url: Arc::from("http://localhost:54321"),
69            api_key: Arc::from("secret-key"),
70            schema: "public".to_string(),
71        };
72        let debug_str = format!("{:?}", backend);
73        // Debug should include base_url and schema but not expose api_key
74        assert!(debug_str.contains("Rest"));
75        assert!(debug_str.contains("http://localhost:54321"));
76        assert!(debug_str.contains("public"));
77        // api_key is not included in the debug output (only base_url and schema are)
78        assert!(!debug_str.contains("secret-key"));
79    }
80
81    #[test]
82    fn test_query_backend_clone() {
83        let backend = QueryBackend::Rest {
84            http: reqwest::Client::new(),
85            base_url: Arc::from("http://localhost"),
86            api_key: Arc::from("key"),
87            schema: "public".to_string(),
88        };
89        let cloned = backend.clone();
90        match (&backend, &cloned) {
91            (
92                QueryBackend::Rest { base_url: a, schema: sa, .. },
93                QueryBackend::Rest { base_url: b, schema: sb, .. },
94            ) => {
95                assert_eq!(a, b);
96                assert_eq!(sa, sb);
97            }
98            #[cfg(feature = "direct-sql")]
99            _ => panic!("expected Rest variants"),
100        }
101    }
102}