Skip to main content

diesel/pg/types/
primitives.rs

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