sqlx-firebirdsql 0.1.0

Firebird SQL driver for SQLx
use std::collections::HashMap;
use std::sync::Arc;

use sqlx_core::column::ColumnIndex;
use sqlx_core::error::Error;
use sqlx_core::row::Row;
use crate::{Firebird, FirebirdColumn, FirebirdValueFormat, FirebirdValueRef};
use bytes::Bytes;

/// Implementation of [`Row`] for Firebird.
pub struct FirebirdRow {
    pub(crate) values: Vec<Option<Bytes>>,
    pub(crate) columns: Arc<Vec<FirebirdColumn>>,
    pub(crate) column_names: Arc<HashMap<String, usize>>,
}

impl Row for FirebirdRow {
    type Database = Firebird;

    fn columns(&self) -> &[FirebirdColumn] {
        &self.columns
    }

    fn try_get_raw<I>(&self, index: I) -> Result<FirebirdValueRef<'_>, Error>
    where
        I: ColumnIndex<Self>,
    {
        let index = index.index(self)?;
        let column = &self.columns[index];
        let value = self.values[index].as_deref();

        Ok(FirebirdValueRef {
            format: FirebirdValueFormat::Binary,
            row: self.values[index].as_ref(),
            type_info: column.type_info.clone(),
            value,
        })
    }
}

impl ColumnIndex<FirebirdRow> for &'_ str {
    fn index(&self, row: &FirebirdRow) -> Result<usize, Error> {
        row.column_names
            .get(*self)
            .ok_or_else(|| Error::ColumnNotFound((*self).into()))
            .copied()
    }
}

impl std::fmt::Debug for FirebirdRow {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        sqlx_core::row::debug_row(self, f)
    }
}