Skip to main content

camel_component_sql/
lib.rs

1pub mod config;
2pub mod consumer;
3pub mod endpoint;
4pub mod headers;
5pub mod producer;
6pub mod query;
7pub(crate) mod utils;
8
9use camel_api::CamelError;
10use camel_component::{Component, Endpoint};
11use camel_endpoint::UriConfig;
12
13pub use config::{SqlEndpointConfig, SqlGlobalConfig, SqlOutputType};
14
15pub struct SqlComponent {
16    config: Option<SqlGlobalConfig>,
17}
18
19impl SqlComponent {
20    pub fn new() -> Self {
21        Self { config: None }
22    }
23
24    pub fn with_config(config: SqlGlobalConfig) -> Self {
25        Self {
26            config: Some(config),
27        }
28    }
29
30    pub fn with_optional_config(config: Option<SqlGlobalConfig>) -> Self {
31        Self { config }
32    }
33}
34
35impl Default for SqlComponent {
36    fn default() -> Self {
37        Self::new()
38    }
39}
40
41impl Component for SqlComponent {
42    fn scheme(&self) -> &str {
43        "sql"
44    }
45
46    fn create_endpoint(&self, uri: &str) -> Result<Box<dyn Endpoint>, CamelError> {
47        let mut config = SqlEndpointConfig::from_uri(uri)?;
48        if let Some(ref global_config) = self.config {
49            config.apply_defaults(global_config);
50        }
51        config.resolve_defaults();
52        Ok(Box::new(endpoint::SqlEndpoint::new(
53            uri.to_string(),
54            config,
55        )))
56    }
57}
58
59#[cfg(test)]
60mod tests {
61    use super::*;
62    use camel_component::Component;
63
64    #[test]
65    fn test_component_scheme() {
66        let c = SqlComponent::new();
67        assert_eq!(c.scheme(), "sql");
68    }
69
70    #[test]
71    fn test_component_creates_endpoint() {
72        let c = SqlComponent::new();
73        let ep = c.create_endpoint("sql:select 1?db_url=postgres://localhost/test");
74        assert!(ep.is_ok());
75    }
76
77    #[test]
78    fn test_component_rejects_wrong_scheme() {
79        let c = SqlComponent::new();
80        let ep = c.create_endpoint("redis://localhost");
81        assert!(ep.is_err());
82    }
83
84    #[test]
85    fn test_endpoint_uri() {
86        let c = SqlComponent::new();
87        let ep = c
88            .create_endpoint("sql:select 1?db_url=postgres://localhost/test")
89            .unwrap();
90        assert_eq!(ep.uri(), "sql:select 1?db_url=postgres://localhost/test");
91    }
92
93    #[test]
94    fn test_component_with_global_config() {
95        let global = SqlGlobalConfig::default().with_max_connections(20);
96        let c = SqlComponent::with_config(global);
97        // Verify the component can create endpoints with global config applied
98        assert_eq!(c.scheme(), "sql");
99        let ep = c.create_endpoint("sql:select 1?db_url=postgres://localhost/test");
100        assert!(ep.is_ok());
101    }
102
103    #[test]
104    fn test_global_config_applied_to_endpoint() {
105        // Verify that when URI does NOT set pool params, global config fills them in.
106        // Tests the same logic as create_endpoint: from_uri + apply_defaults + resolve_defaults.
107        let global = SqlGlobalConfig::default()
108            .with_max_connections(20)
109            .with_min_connections(3)
110            .with_idle_timeout_secs(600)
111            .with_max_lifetime_secs(3600);
112        let mut cfg =
113            config::SqlEndpointConfig::from_uri("sql:select 1?db_url=postgres://localhost/test")
114                .unwrap();
115        cfg.apply_defaults(&global);
116        cfg.resolve_defaults();
117        assert_eq!(cfg.max_connections, Some(20));
118        assert_eq!(cfg.min_connections, Some(3));
119        assert_eq!(cfg.idle_timeout_secs, Some(600));
120        assert_eq!(cfg.max_lifetime_secs, Some(3600));
121    }
122
123    #[test]
124    fn test_uri_param_wins_over_global_config() {
125        // Verify that URI-set pool params are NOT overridden by global config.
126        let global = SqlGlobalConfig::default()
127            .with_max_connections(20)
128            .with_min_connections(3);
129        let mut cfg = config::SqlEndpointConfig::from_uri(
130            "sql:select 1?db_url=postgres://localhost/test&maxConnections=99&minConnections=7",
131        )
132        .unwrap();
133        cfg.apply_defaults(&global);
134        cfg.resolve_defaults();
135        assert_eq!(cfg.max_connections, Some(99)); // URI wins
136        assert_eq!(cfg.min_connections, Some(7)); // URI wins
137    }
138}