pub mod statement;
pub use statement::Statement;
pub mod proto;
pub use proto::{BatchResult, Col, Value};
#[cfg(feature = "mapping_names_to_values_in_rows")]
pub mod de;
#[cfg(feature = "workers_backend")]
pub use worker;
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
pub struct Row {
pub values: Vec<Value>,
#[cfg(feature = "mapping_names_to_values_in_rows")]
pub value_map: std::collections::HashMap<String, Value>,
}
impl<'a> Row {
pub fn try_get<V: TryFrom<&'a Value, Error = String>>(
&'a self,
index: usize,
) -> anyhow::Result<V> {
let val = self
.values
.get(index)
.ok_or(anyhow::anyhow!("out of bound index {}", index))?;
val.try_into().map_err(|x: String| anyhow::anyhow!(x))
}
#[cfg(feature = "mapping_names_to_values_in_rows")]
pub fn try_column<V: TryFrom<&'a Value, Error = String>>(
&'a self,
col: &str,
) -> anyhow::Result<V> {
let val = self
.value_map
.get(col)
.ok_or(anyhow::anyhow!("column `{}` not present", col))?;
val.try_into().map_err(|x: String| anyhow::anyhow!(x))
}
}
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
pub struct ResultSet {
pub columns: Vec<String>,
pub rows: Vec<Row>,
pub rows_affected: u64,
pub last_insert_rowid: Option<i64>,
}
impl std::convert::From<proto::StmtResult> for ResultSet {
fn from(value: proto::StmtResult) -> Self {
let columns: Vec<String> = value
.cols
.into_iter()
.map(|c| c.name.unwrap_or_default())
.collect();
let rows = value
.rows
.into_iter()
.map(|values| {
#[cfg(feature = "mapping_names_to_values_in_rows")]
let value_map = columns
.iter()
.enumerate()
.map(|(i, c)| (c.to_string(), values[i].clone()))
.collect();
Row {
values,
#[cfg(feature = "mapping_names_to_values_in_rows")]
value_map,
}
})
.collect();
ResultSet {
columns,
rows,
rows_affected: value.affected_row_count,
last_insert_rowid: value.last_insert_rowid,
}
}
}
pub mod client;
pub use client::{Client, Config, SyncClient};
#[cfg(any(
feature = "reqwest_backend",
feature = "workers_backend",
feature = "spin_backend",
))]
pub mod http;
pub mod transaction;
pub use transaction::{SyncTransaction, Transaction};
#[cfg(feature = "workers_backend")]
pub mod workers;
#[cfg(feature = "reqwest_backend")]
pub mod reqwest;
#[cfg(feature = "local_backend")]
pub mod local;
#[cfg(feature = "spin_backend")]
pub mod spin;
#[cfg(feature = "hrana_backend")]
pub mod hrana;
mod utils;
#[macro_export]
macro_rules! args {
() => { &[] };
($($param:expr),+ $(,)?) => {
&[$($param.into()),+] as &[libsql_client::Value]
};
}