sqlx_core_oldapi/
column.rs1use 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 fn ordinal(&self) -> usize;
13
14 fn name(&self) -> &str;
19
20 fn type_info(&self) -> &<Self::Database as Database>::TypeInfo;
22}
23
24pub(crate) mod private_column {
26 pub trait Sealed {}
27}
28
29pub trait ColumnIndex<T: ?Sized>: private_column_index::Sealed + Debug {
43 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
92mod 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}