Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

3.4 KB
Raw Blame
mod error;
mod insert;
mod migrate;
mod read;

pub use error::Error;
pub use insert::{insert_compressed_rows, insert_encoded_rows};
pub use migrate::{ClickHouseMigrate, execute_statement, storage_error};
pub use read::{Parameter, Query, READ_LIMITS, ReadLimits, execute_read, fetch, fetch_json};
use url::Url;

#[derive(Clone)]
pub struct Connection {
    url: Url,
}

impl Connection {
    pub fn parse(value: &str) -> Result<Self, Error> {
        let url = Url::parse(value).map_err(|_| Error::InvalidUrl)?;
        if !matches!(url.scheme(), "http" | "https") || url.host().is_none() {
            return Err(Error::InvalidUrl);
        }
        Ok(Self { url })
    }

    pub fn configured(
        url: &str,
        database: &str,
        user: &str,
        password: &str,
    ) -> Result<Self, Error> {
        let mut connection = Self::parse(url)?;
        connection
            .url
            .set_username(user)
            .map_err(|_| Error::InvalidUrl)?;
        connection
            .url
            .set_password(Some(password))
            .map_err(|_| Error::InvalidUrl)?;
        let pairs: Vec<_> = connection
            .url
            .query_pairs()
            .filter(|(key, _)| !matches!(key.as_ref(), "database" | "user" | "password"))
            .map(|(key, value)| (key.into_owned(), value.into_owned()))
            .collect();
        connection
            .url
            .query_pairs_mut()
            .clear()
            .extend_pairs(pairs)
            .append_pair("database", database);
        Ok(connection)
    }

    pub fn writer(url: &str) -> Result<Self, Error> {
        let mut connection = Self::parse(url)?;
        let pairs: Vec<_> = connection
            .url
            .query_pairs()
            .filter(|(key, _)| !matches!(key.as_ref(), "database" | "readonly" | "query"))
            .map(|(key, value)| (key.into_owned(), value.into_owned()))
            .collect();
        connection.url.query_pairs_mut().clear().extend_pairs(pairs);
        Ok(connection)
    }

    pub fn reader(url: &str, database: &str) -> Result<Self, Error> {
        let mut connection = Self::parse(url)?;
        let pairs: Vec<_> = connection
            .url
            .query_pairs()
            .filter(|(key, _)| key != "database")
            .map(|(key, value)| (key.into_owned(), value.into_owned()))
            .collect();
        connection
            .url
            .query_pairs_mut()
            .clear()
            .extend_pairs(pairs)
            .append_pair("database", database);
        Ok(connection)
    }

    pub fn url(&self) -> &Url {
        &self.url
    }
}

#[derive(Clone)]
pub struct Storage {
    database: String,
    writer: Connection,
    reader: Connection,
}

impl Storage {
    pub fn new(database: String, url: &str) -> Result<Self, Error> {
        if !valid_identifier(&database) {
            return Err(Error::InvalidSchema);
        }
        Ok(Self {
            writer: Connection::writer(url)?,
            reader: Connection::reader(url, &database)?,
            database,
        })
    }

    pub fn database(&self) -> &str {
        &self.database
    }

    pub fn writer(&self) -> &Connection {
        &self.writer
    }

    pub fn reader(&self) -> &Connection {
        &self.reader
    }
}

pub(crate) fn valid_identifier(value: &str) -> bool {
    !value.is_empty()
        && value
            .bytes()
            .all(|c| c.is_ascii_alphanumeric() || c == b'_')
}