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
// This file is part of https://github.com/SpringQL/SpringQL which is licensed under MIT OR Apache-2.0. See file LICENSE-MIT or LICENSE-APACHE for full license details.
pub mod error;
mod spring_config;
pub use crate::{
api::{
error::{Result, SpringError},
spring_config::*,
SpringConfig,
},
stream_engine::{
time::{SpringEventDuration, SpringTimestamp},
SpringValue,
},
};
use crate::{
connection::Connection,
stream_engine::{Row, SqlValue},
};
/// Pipeline.
#[derive(Debug)]
pub struct SpringPipeline(Connection);
impl SpringPipeline {
/// Creates and open an in-process stream pipeline.
pub fn new(config: &SpringConfig) -> Result<Self> {
let conn = Connection::new(config);
Ok(Self(conn))
}
/// Execute commands (DDL).
///
/// # Failure
///
/// - [SpringError::Sql](crate::api::error::SpringError::Sql) when:
/// - Invalid SQL syntax.
/// - Refers to undefined objects (streams, pumps, etc)
/// - Other semantic errors.
/// - [SpringError::InvalidOption](crate::api::error::SpringError::Sql) when:
/// - `OPTIONS` in `CREATE` statement includes invalid key or value.
pub fn command<S: AsRef<str>>(&self, sql: S) -> Result<()> {
self.0.command(sql.as_ref())
}
/// Pop a row from an in memory queue. This is a blocking function.
///
/// **Do not call this function from threads.**
/// If you need to pop from multiple in-memory queues using threads, use `pop_non_blocking()`.
/// See: <https://github.com/SpringQL/SpringQL/issues/125>
///
/// # Failure
///
/// - [SpringError::Unavailable](crate::api::error::SpringError::Unavailable) when:
/// - queue named `queue` does not exist.
pub fn pop(&self, queue: &str) -> Result<SpringRow> {
self.0.pop(queue).map(SpringRow)
}
/// Pop a row from an in memory queue. This is a non-blocking function.
///
/// # Returns
///
/// - `Ok(Some)` when at least a row is in the queue.
/// - `None` when no row is in the queue.
///
/// # Failure
///
/// - [SpringError::Unavailable](crate::api::error::SpringError::Unavailable) when:
/// - queue named `queue` does not exist.
pub fn pop_non_blocking(&self, queue: &str) -> Result<Option<SpringRow>> {
self.0
.pop_non_blocking(queue)
.map(|opt_row| opt_row.map(SpringRow))
}
}
/// Row object from an in memory queue.
#[derive(Debug)]
pub struct SpringRow(Row);
impl SpringRow {
/// Get a i-th column value from the row.
///
/// # Failure
///
/// - [SpringError::Sql](crate::api::error::SpringError::Sql) when:
/// - Column index out of range
/// - [SpringError::Null](crate::api::error::SpringError::Null) when:
/// - Column value is NULL
pub fn get_not_null_by_index<T>(&self, i_col: usize) -> Result<T>
where
T: SpringValue,
{
let sql_value = self.0.get_by_index(i_col)?;
match sql_value {
SqlValue::Null => Err(SpringError::Null {
stream_name: self.0.stream_model().name().clone(),
i_col,
}),
SqlValue::NotNull(nn_sql_value) => nn_sql_value.unpack(),
}
}
}
impl SpringConfig {
/// Configuration by TOML format string.
///
/// # Parameters
///
/// - `overwrite_config_toml`: TOML format configuration to overwrite default. See `SPRING_CONFIG_DEFAULT` in [spring_config.rs](https://github.com/SpringQL/SpringQL/tree/main/springql-core/src/api/spring_config.rs) for full-set default configuration.
///
/// # Failures
///
/// - [SpringError::InvalidConfig](crate::api::error::SpringError::InvalidConfig) when:
/// - `overwrite_config_toml` includes invalid key and/or value.
/// - [SpringError::InvalidFormat](crate::api::error::SpringError::InvalidFormat) when:
/// - `overwrite_config_toml` is not valid as TOML.
pub fn from_toml(overwrite_config_toml: &str) -> Result<SpringConfig> {
SpringConfig::new(overwrite_config_toml)
}
}