rbdc_mysql/describe.rs
1use crate::result_set::{MySqlColumn, MySqlTypeInfo};
2use either::Either;
3use std::convert::identity;
4
5/// Provides extended information on a statement.
6///
7/// Returned from [`Executor::describe`].
8///
9/// The query macros (e.g., `query!`, `query_as!`, etc.) use the information here to validate
10/// output and parameter types; and, generate an anonymous record.
11#[derive(Debug)]
12#[doc(hidden)]
13pub struct Describe {
14 pub(crate) columns: Vec<MySqlColumn>,
15 pub(crate) parameters: Option<Either<Vec<MySqlTypeInfo>, usize>>,
16 pub(crate) nullable: Vec<Option<bool>>,
17}
18
19impl Describe {
20 /// Gets all columns in this statement.
21 pub fn columns(&self) -> &[MySqlColumn] {
22 &self.columns
23 }
24
25 /// Gets the column information at `index`.
26 ///
27 /// Panics if `index` is out of bounds.
28 pub fn column(&self, index: usize) -> &MySqlColumn {
29 &self.columns[index]
30 }
31
32 /// Gets the available information for parameters in this statement.
33 ///
34 /// Some drivers may return more or less than others. As an example, **PostgreSQL** will
35 /// return `Some(Either::Left(_))` with a full list of type information for each parameter.
36 /// However, **MSSQL** will return `None` as there is no information available.
37 pub fn parameters(&self) -> Option<Either<&[MySqlTypeInfo], usize>> {
38 self.parameters.as_ref().map(|p| match p {
39 Either::Left(params) => Either::Left(&**params),
40 Either::Right(count) => Either::Right(*count),
41 })
42 }
43
44 /// Gets whether a column may be `NULL`, if this information is available.
45 pub fn nullable(&self, column: usize) -> Option<bool> {
46 self.nullable.get(column).copied().and_then(identity)
47 }
48}