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};
11
12pub use config::{SqlConfig, SqlOutputType};
13
14pub struct SqlComponent;
15
16impl SqlComponent {
17    pub fn new() -> Self {
18        Self
19    }
20}
21
22impl Default for SqlComponent {
23    fn default() -> Self {
24        Self::new()
25    }
26}
27
28impl Component for SqlComponent {
29    fn scheme(&self) -> &str {
30        "sql"
31    }
32
33    fn create_endpoint(&self, uri: &str) -> Result<Box<dyn Endpoint>, CamelError> {
34        let config = SqlConfig::from_uri(uri)?;
35        Ok(Box::new(endpoint::SqlEndpoint::new(
36            uri.to_string(),
37            config,
38        )))
39    }
40}
41
42#[cfg(test)]
43mod tests {
44    use super::*;
45    use camel_component::Component;
46
47    #[test]
48    fn test_component_scheme() {
49        let c = SqlComponent::new();
50        assert_eq!(c.scheme(), "sql");
51    }
52
53    #[test]
54    fn test_component_creates_endpoint() {
55        let c = SqlComponent::new();
56        let ep = c.create_endpoint("sql:select 1?db_url=postgres://localhost/test");
57        assert!(ep.is_ok());
58    }
59
60    #[test]
61    fn test_component_rejects_wrong_scheme() {
62        let c = SqlComponent::new();
63        let ep = c.create_endpoint("redis://localhost");
64        assert!(ep.is_err());
65    }
66
67    #[test]
68    fn test_endpoint_uri() {
69        let c = SqlComponent::new();
70        let ep = c
71            .create_endpoint("sql:select 1?db_url=postgres://localhost/test")
72            .unwrap();
73        assert_eq!(ep.uri(), "sql:select 1?db_url=postgres://localhost/test");
74    }
75}