rust-query 0.9.0

A query builder using rust concepts.
Documentation
use std::{fmt::Debug, marker::PhantomData};

use crate::Table;

/// Row reference that can be used in any query in the same transaction.
///
/// [TableRow] is restricted to a single thread to prevent it from being used in a different transaction.
pub struct TableRow<T: Table> {
    pub(crate) _local: PhantomData<*const ()>,
    pub(crate) inner: TableRowInner<T>,
}

impl<T: Table> TableRow<T> {
    pub(crate) fn migrate_row(prev: TableRow<T::MigrateFrom>) -> Self {
        Self {
            _local: PhantomData,
            inner: TableRowInner {
                _p: PhantomData,
                idx: prev.inner.idx,
            },
        }
    }
}

impl<T: Table> Eq for TableRow<T> {}

impl<T: Table> PartialOrd for TableRow<T> {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        Some(self.cmp(other))
    }
}

impl<T: Table> Ord for TableRow<T> {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        self.inner.idx.cmp(&other.inner.idx)
    }
}

pub(crate) struct TableRowInner<T> {
    pub(crate) _p: PhantomData<T>,
    pub(crate) idx: i64,
}

impl<T: Table> PartialEq for TableRow<T> {
    fn eq(&self, other: &Self) -> bool {
        self.inner.idx == other.inner.idx
    }
}

impl<T: Table> Debug for TableRow<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "db_{}", self.inner.idx)
    }
}

impl<T: Table> Clone for TableRow<T> {
    fn clone(&self) -> Self {
        *self
    }
}

impl<T: Table> Copy for TableRow<T> {}

impl<T> Clone for TableRowInner<T> {
    fn clone(&self) -> Self {
        *self
    }
}
impl<T> Copy for TableRowInner<T> {}

/// This makes it possible to use TableRow as a parameter in
/// rusqlite queries and statements.
impl<T: Table> rusqlite::ToSql for TableRow<T> {
    fn to_sql(&self) -> rusqlite::Result<rusqlite::types::ToSqlOutput<'_>> {
        self.inner.idx.to_sql()
    }
}