sqlx_core_oldapi/
column.rs

1use crate::database::Database;
2use crate::error::Error;
3use std::fmt::Debug;
4
5pub trait Column: private_column::Sealed + 'static + Send + Sync + Debug {
6    type Database: Database;
7
8    /// Gets the column ordinal.
9    ///
10    /// This can be used to unambiguously refer to this column within a row in case more than
11    /// one column have the same name
12    fn ordinal(&self) -> usize;
13
14    /// Gets the column name or alias.
15    ///
16    /// The column name is unreliable (and can change between database minor versions) if this
17    /// column is an expression that has not been aliased.
18    fn name(&self) -> &str;
19
20    /// Gets the type information for the column.
21    fn type_info(&self) -> &<Self::Database as Database>::TypeInfo;
22}
23
24// Prevent users from implementing the `Row` trait.
25pub(crate) mod private_column {
26    pub trait Sealed {}
27}
28
29/// A type that can be used to index into a [`Row`] or [`Statement`].
30///
31/// The [`get`] and [`try_get`] methods of [`Row`] accept any type that implements `ColumnIndex`.
32/// This trait is implemented for strings which are used to look up a column by name, and for
33/// `usize` which is used as a positional index into the row.
34///
35/// This trait is sealed and cannot be implemented for types outside of SQLx.
36///
37/// [`Row`]: crate::row::Row
38/// [`Statement`]: crate::statement::Statement
39/// [`get`]: crate::row::Row::get
40/// [`try_get`]: crate::row::Row::try_get
41///
42pub trait ColumnIndex<T: ?Sized>: private_column_index::Sealed + Debug {
43    /// Returns a valid positional index into the row or statement, [`ColumnIndexOutOfBounds`], or,
44    /// [`ColumnNotFound`].
45    ///
46    /// [`ColumnNotFound`]: Error::ColumnNotFound
47    /// [`ColumnIndexOutOfBounds`]: Error::ColumnIndexOutOfBounds
48    fn index(&self, container: &T) -> Result<usize, Error>;
49}
50
51impl<T: ?Sized, I: ColumnIndex<T> + ?Sized> ColumnIndex<T> for &'_ I {
52    #[inline]
53    fn index(&self, row: &T) -> Result<usize, Error> {
54        (**self).index(row)
55    }
56}
57
58macro_rules! impl_column_index_for_row {
59    ($R:ident) => {
60        impl crate::column::ColumnIndex<$R> for usize {
61            fn index(&self, row: &$R) -> Result<usize, crate::error::Error> {
62                let len = crate::row::Row::len(row);
63
64                if *self >= len {
65                    return Err(crate::error::Error::ColumnIndexOutOfBounds { len, index: *self });
66                }
67
68                Ok(*self)
69            }
70        }
71    };
72}
73
74macro_rules! impl_column_index_for_statement {
75    ($S:ident) => {
76        impl crate::column::ColumnIndex<$S<'_>> for usize {
77            fn index(&self, statement: &$S<'_>) -> Result<usize, crate::error::Error> {
78                let len = crate::statement::Statement::columns(statement).len();
79
80                if *self >= len {
81                    return Err(crate::error::Error::ColumnIndexOutOfBounds { len, index: *self });
82                }
83
84                Ok(*self)
85            }
86        }
87    };
88}
89
90// Prevent users from implementing the `ColumnIndex` trait.
91mod private_column_index {
92    pub trait Sealed {}
93
94    impl Sealed for usize {}
95    impl Sealed for str {}
96    impl<T> Sealed for &'_ T where T: Sealed + ?Sized {}
97}