sqlx-turso-driver 0.0.1

An asynchronous SQLx driver for embedded Turso databases
use crate::{Turso, TursoArguments, TursoColumn, TursoTypeInfo};
use either::Either;
use sqlx_core::{column::ColumnIndex, error::Error, sql_str::SqlStr, statement::Statement};

/// Owned SQL and engine-provided column metadata, reusable across connections.
/// Execution re-prepares on the target connection; no engine handle escapes.
#[derive(Debug, Clone)]
pub struct TursoStatement {
    pub(crate) sql: SqlStr,
    pub(crate) columns: Vec<TursoColumn>,
}
impl Statement for TursoStatement {
    type Database = Turso;
    fn into_sql(self) -> SqlStr {
        self.sql
    }
    fn sql(&self) -> &SqlStr {
        &self.sql
    }
    fn parameters(&self) -> Option<Either<&[TursoTypeInfo], usize>> {
        // Turso's public Statement exposes neither parameter counts nor
        // types. Do not fabricate them by parsing placeholder text.
        None
    }
    fn columns(&self) -> &[TursoColumn] {
        &self.columns
    }
    sqlx_core::impl_statement_query!(TursoArguments);
}
sqlx_core::impl_column_index_for_statement!(TursoStatement);

impl ColumnIndex<TursoStatement> for &str {
    fn index(&self, statement: &TursoStatement) -> Result<usize, Error> {
        statement
            .columns
            .iter()
            .position(|column| column.name == *self)
            .ok_or_else(|| Error::ColumnNotFound((*self).to_owned()))
    }
}