Skip to main content

camel_component_sql/
endpoint.rs

1use std::sync::Arc;
2use tokio::sync::OnceCell;
3
4use camel_api::{BoxProcessor, CamelError};
5use camel_component::{Consumer, Endpoint, ProducerContext};
6use sqlx::AnyPool;
7
8use crate::config::SqlEndpointConfig;
9use crate::consumer::SqlConsumer;
10use crate::producer::SqlProducer;
11
12pub(crate) struct SqlEndpoint {
13    uri: String,
14    pub(crate) config: SqlEndpointConfig,
15    pub(crate) pool: Arc<OnceCell<AnyPool>>,
16}
17
18impl SqlEndpoint {
19    pub fn new(uri: String, config: SqlEndpointConfig) -> Self {
20        Self {
21            uri,
22            config,
23            pool: Arc::new(OnceCell::new()),
24        }
25    }
26}
27
28impl Endpoint for SqlEndpoint {
29    fn uri(&self) -> &str {
30        &self.uri
31    }
32
33    fn create_producer(&self, _ctx: &ProducerContext) -> Result<BoxProcessor, CamelError> {
34        Ok(BoxProcessor::new(SqlProducer::new(
35            self.config.clone(),
36            Arc::clone(&self.pool),
37        )))
38    }
39
40    fn create_consumer(&self) -> Result<Box<dyn Consumer>, CamelError> {
41        Ok(Box::new(SqlConsumer::new(
42            self.config.clone(),
43            Arc::clone(&self.pool),
44        )))
45    }
46}