Skip to main content

Nullable

Struct Nullable 

Source
pub struct Nullable<T>
where T: NotNull,
{ /* private fields */ }
Expand description

Type implementation for a nullable column.

The T parameter must be a NotNull type marker. This constraint prevents assymetric types from being defined.

So something like this is supported:

ty::Nullable<ty::Integer>

While something like this is denied at compile time:

// This will not compile because Nullable does not implement `NotNull`.
ty::Nullable<ty::Nullable<ty::Integer>>;

§Examples

use sqll::{Connection, FromColumn, Result, Statement};
use sqll::ty;

#[derive(Debug, PartialEq, Eq)]
struct MyOptional(Option<u32>);

impl FromColumn<'_> for MyOptional {
    type Type = ty::Nullable<ty::Integer>;

    #[inline]
    fn from_column(stmt: &Statement, index: ty::Nullable<ty::Integer>) -> Result<Self> {
        Ok(MyOptional(<_>::from_column(stmt, index)?))
    }
}

let mut c = Connection::open_in_memory()?;

c.execute(r#"
    CREATE TABLE test (value INTEGER);

    INSERT INTO test (value) VALUES (42), (NULL);
"#)?;

let mut stmt = c.prepare("SELECT value FROM test")?;

assert_eq!(stmt.next::<MyOptional>()?, Some(MyOptional(Some(42))));
assert_eq!(stmt.next::<MyOptional>()?, Some(MyOptional(None)));
assert_eq!(stmt.next::<MyOptional>()?, None);

Trait Implementations§

Source§

impl<T> Type for Nullable<T>
where T: NotNull,

Type implementation for a nullable column.

§Examples

use sqll::{Connection, FromColumn, Result, Statement};
use sqll::ty;

#[derive(Debug, PartialEq, Eq)]
struct MyOptional(Option<u32>);

impl FromColumn<'_> for MyOptional {
    type Type = ty::Nullable<ty::Integer>;

    #[inline]
    fn from_column(stmt: &Statement, index: ty::Nullable<ty::Integer>) -> Result<Self> {
        Ok(MyOptional(<_>::from_column(stmt, index)?))
    }
}

let mut c = Connection::open_in_memory()?;

c.execute(r#"
    CREATE TABLE test (value INTEGER);

    INSERT INTO test (value) VALUES (42), (NULL);
"#)?;

let mut stmt = c.prepare("SELECT value FROM test")?;

assert_eq!(stmt.next::<MyOptional>()?, Some(MyOptional(Some(42))));
assert_eq!(stmt.next::<MyOptional>()?, Some(MyOptional(None)));
assert_eq!(stmt.next::<MyOptional>()?, None);
Source§

fn check(stmt: &mut Statement, column: Column) -> Result<Self>

Perform checks and warm up for the given column ensuring that any auto-conversion that needs to occur to load the field is done.

Auto Trait Implementations§

§

impl<T> Freeze for Nullable<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for Nullable<T>
where T: RefUnwindSafe,

§

impl<T> Send for Nullable<T>
where T: Send,

§

impl<T> Sync for Nullable<T>
where T: Sync,

§

impl<T> Unpin for Nullable<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for Nullable<T>
where T: UnsafeUnpin,

§

impl<T> UnwindSafe for Nullable<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.