diesel/pg/types/
primitives.rs

1use std::io::prelude::*;
2
3use crate::deserialize::{self, FromSql, Queryable};
4use crate::pg::{Pg, PgValue};
5use crate::serialize::{self, IsNull, Output, ToSql};
6use crate::sql_types;
7
8#[cfg(feature = "postgres_backend")]
9impl FromSql<sql_types::Bool, Pg> for bool {
10    fn from_sql(bytes: PgValue<'_>) -> deserialize::Result<Self> {
11        Ok(bytes.as_bytes()[0] != 0)
12    }
13}
14
15#[cfg(feature = "postgres_backend")]
16impl ToSql<sql_types::Bool, Pg> for bool {
17    fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Pg>) -> serialize::Result {
18        out.write_all(&[*self as u8])
19            .map(|_| IsNull::No)
20            .map_err(Into::into)
21    }
22}
23
24#[cfg(feature = "postgres_backend")]
25impl FromSql<sql_types::CChar, Pg> for u8 {
26    fn from_sql(bytes: PgValue<'_>) -> deserialize::Result<Self> {
27        Ok(bytes.as_bytes()[0])
28    }
29}
30
31#[cfg(feature = "postgres_backend")]
32impl ToSql<sql_types::CChar, Pg> for u8 {
33    fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Pg>) -> serialize::Result {
34        out.write_all(&[*self])
35            .map(|_| IsNull::No)
36            .map_err(Into::into)
37    }
38}
39
40#[test]
41fn cchar_to_sql() {
42    use crate::query_builder::bind_collector::ByteWrapper;
43
44    let mut buffer = Vec::new();
45    let mut bytes = Output::test(ByteWrapper(&mut buffer));
46    ToSql::<sql_types::CChar, Pg>::to_sql(&b'A', &mut bytes).unwrap();
47    ToSql::<sql_types::CChar, Pg>::to_sql(&b'\xc4', &mut bytes).unwrap();
48    assert_eq!(buffer, vec![65u8, 196u8]);
49}
50
51#[test]
52fn cchar_from_sql() {
53    let result = <u8 as FromSql<sql_types::CChar, Pg>>::from_nullable_sql(None);
54    assert_eq!(
55        result.unwrap_err().to_string(),
56        "Unexpected null for non-null column"
57    );
58}
59
60/// The returned pointer is *only* valid for the lifetime to the argument of
61/// `from_sql`. This impl is intended for uses where you want to write a new
62/// impl in terms of `String`, but don't want to allocate. We have to return a
63/// raw pointer instead of a reference with a lifetime due to the structure of
64/// `FromSql`
65#[cfg(feature = "postgres_backend")]
66impl FromSql<sql_types::Text, Pg> for *const str {
67    fn from_sql(value: PgValue<'_>) -> deserialize::Result<Self> {
68        use std::str;
69        let string = str::from_utf8(value.as_bytes())?;
70        Ok(string as *const _)
71    }
72}
73
74#[cfg(feature = "postgres_backend")]
75impl Queryable<sql_types::VarChar, Pg> for *const str {
76    type Row = Self;
77
78    fn build(row: Self::Row) -> deserialize::Result<Self> {
79        Ok(row)
80    }
81}
82
83#[cfg(feature = "postgres_backend")]
84impl ToSql<sql_types::Citext, Pg> for String {
85    fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Pg>) -> serialize::Result {
86        out.write_all(self.as_bytes())?;
87        Ok(IsNull::No)
88    }
89}
90
91#[cfg(feature = "postgres_backend")]
92impl ToSql<sql_types::Citext, Pg> for str {
93    fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Pg>) -> serialize::Result {
94        out.write_all(self.as_bytes())?;
95        Ok(IsNull::No)
96    }
97}
98
99#[cfg(feature = "postgres_backend")]
100impl FromSql<sql_types::Citext, Pg> for String {
101    fn from_sql(value: PgValue<'_>) -> deserialize::Result<Self> {
102        let string = String::from_utf8(value.as_bytes().to_vec())?;
103        Ok(string)
104    }
105}
106
107/// The returned pointer is *only* valid for the lifetime to the argument of
108/// `from_sql`. This impl is intended for uses where you want to write a new
109/// impl in terms of `Vec<u8>`, but don't want to allocate. We have to return a
110/// raw pointer instead of a reference with a lifetime due to the structure of
111/// `FromSql`
112#[cfg(feature = "postgres_backend")]
113impl FromSql<sql_types::Binary, Pg> for *const [u8] {
114    fn from_sql(value: PgValue<'_>) -> deserialize::Result<Self> {
115        Ok(value.as_bytes() as *const _)
116    }
117}
118
119#[cfg(feature = "postgres_backend")]
120impl Queryable<sql_types::Binary, Pg> for *const [u8] {
121    type Row = Self;
122
123    fn build(row: Self::Row) -> deserialize::Result<Self> {
124        Ok(row)
125    }
126}
127
128#[test]
129fn bool_to_sql() {
130    use crate::query_builder::bind_collector::ByteWrapper;
131
132    let mut buffer = Vec::new();
133    let mut bytes = Output::test(ByteWrapper(&mut buffer));
134    ToSql::<sql_types::Bool, Pg>::to_sql(&true, &mut bytes).unwrap();
135    ToSql::<sql_types::Bool, Pg>::to_sql(&false, &mut bytes).unwrap();
136    assert_eq!(buffer, vec![1u8, 0u8]);
137}
138
139#[test]
140fn no_bool_from_sql() {
141    let result = <bool as FromSql<sql_types::Bool, Pg>>::from_nullable_sql(None);
142    assert_eq!(
143        result.unwrap_err().to_string(),
144        "Unexpected null for non-null column"
145    );
146}