sqlx_core_oldapi/
describe.rs

1use crate::database::Database;
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#[cfg_attr(feature = "offline", derive(serde::Serialize, serde::Deserialize))]
13#[cfg_attr(
14    feature = "offline",
15    serde(bound(
16        serialize = "DB::TypeInfo: serde::Serialize, DB::Column: serde::Serialize",
17        deserialize = "DB::TypeInfo: serde::de::DeserializeOwned, DB::Column: serde::de::DeserializeOwned",
18    ))
19)]
20#[doc(hidden)]
21pub struct Describe<DB: Database> {
22    pub(crate) columns: Vec<DB::Column>,
23    pub(crate) parameters: Option<Either<Vec<DB::TypeInfo>, usize>>,
24    pub(crate) nullable: Vec<Option<bool>>,
25}
26
27impl<DB: Database> Describe<DB> {
28    /// Gets all columns in this statement.
29    pub fn columns(&self) -> &[DB::Column] {
30        &self.columns
31    }
32
33    /// Gets the column information at `index`.
34    ///
35    /// Panics if `index` is out of bounds.
36    pub fn column(&self, index: usize) -> &DB::Column {
37        &self.columns[index]
38    }
39
40    /// Gets the available information for parameters in this statement.
41    ///
42    /// Some drivers may return more or less than others. As an example, **PostgreSQL** will
43    /// return `Some(Either::Left(_))` with a full list of type information for each parameter.
44    /// However, **MSSQL** will return `None` as there is no information available.
45    pub fn parameters(&self) -> Option<Either<&[DB::TypeInfo], usize>> {
46        self.parameters.as_ref().map(|p| match p {
47            Either::Left(params) => Either::Left(&**params),
48            Either::Right(count) => Either::Right(*count),
49        })
50    }
51
52    /// Gets whether a column may be `NULL`, if this information is available.
53    pub fn nullable(&self, column: usize) -> Option<bool> {
54        self.nullable.get(column).copied().and_then(identity)
55    }
56}