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
58#[allow(unused_macros)]
59macro_rules! impl_column_index_for_row {
60    ($R:ident) => {
61        impl crate::column::ColumnIndex<$R> for usize {
62            fn index(&self, row: &$R) -> Result<usize, crate::error::Error> {
63                let len = crate::row::Row::len(row);
64
65                if *self >= len {
66                    return Err(crate::error::Error::ColumnIndexOutOfBounds { len, index: *self });
67                }
68
69                Ok(*self)
70            }
71        }
72    };
73}
74
75#[allow(unused_macros)]
76macro_rules! impl_column_index_for_statement {
77    ($S:ident) => {
78        impl crate::column::ColumnIndex<$S<'_>> for usize {
79            fn index(&self, statement: &$S<'_>) -> Result<usize, crate::error::Error> {
80                let len = crate::statement::Statement::columns(statement).len();
81
82                if *self >= len {
83                    return Err(crate::error::Error::ColumnIndexOutOfBounds { len, index: *self });
84                }
85
86                Ok(*self)
87            }
88        }
89    };
90}
91
92// Prevent users from implementing the `ColumnIndex` trait.
93mod private_column_index {
94    pub trait Sealed {}
95
96    impl Sealed for usize {}
97    impl Sealed for str {}
98    impl<T> Sealed for &'_ T where T: Sealed + ?Sized {}
99}