diesel/sqlite/types/
mod.rs

1mod date_and_time;
2mod numeric;
3
4use super::connection::SqliteValue;
5use super::Sqlite;
6use crate::deserialize::{self, FromSql, Queryable};
7use crate::query_builder::QueryId;
8use crate::serialize::{self, IsNull, Output, ToSql};
9use crate::sql_types;
10use crate::sql_types::SqlType;
11
12/// The returned pointer is *only* valid for the lifetime to the argument of
13/// `from_sql`. This impl is intended for uses where you want to write a new
14/// impl in terms of `String`, but don't want to allocate. We have to return a
15/// raw pointer instead of a reference with a lifetime due to the structure of
16/// `FromSql`
17#[cfg(feature = "sqlite")]
18impl FromSql<sql_types::VarChar, Sqlite> for *const str {
19    fn from_sql(mut value: SqliteValue<'_, '_, '_>) -> deserialize::Result<Self> {
20        let text = value.read_text();
21        Ok(text as *const _)
22    }
23}
24
25#[cfg(feature = "sqlite")]
26impl Queryable<sql_types::VarChar, Sqlite> for *const str {
27    type Row = Self;
28
29    fn build(row: Self::Row) -> deserialize::Result<Self> {
30        Ok(row)
31    }
32}
33
34/// The returned pointer is *only* valid for the lifetime to the argument of
35/// `from_sql`. This impl is intended for uses where you want to write a new
36/// impl in terms of `Vec<u8>`, but don't want to allocate. We have to return a
37/// raw pointer instead of a reference with a lifetime due to the structure of
38/// `FromSql`
39#[cfg(feature = "sqlite")]
40impl FromSql<sql_types::Binary, Sqlite> for *const [u8] {
41    fn from_sql(mut bytes: SqliteValue<'_, '_, '_>) -> deserialize::Result<Self> {
42        let bytes = bytes.read_blob();
43        Ok(bytes as *const _)
44    }
45}
46
47#[cfg(feature = "sqlite")]
48impl Queryable<sql_types::Binary, Sqlite> for *const [u8] {
49    type Row = Self;
50
51    fn build(row: Self::Row) -> deserialize::Result<Self> {
52        Ok(row)
53    }
54}
55
56#[cfg(feature = "sqlite")]
57#[allow(clippy::cast_possible_truncation)] // we want to truncate here
58impl FromSql<sql_types::SmallInt, Sqlite> for i16 {
59    fn from_sql(mut value: SqliteValue<'_, '_, '_>) -> deserialize::Result<Self> {
60        Ok(value.read_integer() as i16)
61    }
62}
63
64#[cfg(feature = "sqlite")]
65impl FromSql<sql_types::Integer, Sqlite> for i32 {
66    fn from_sql(mut value: SqliteValue<'_, '_, '_>) -> deserialize::Result<Self> {
67        Ok(value.read_integer())
68    }
69}
70
71#[cfg(feature = "sqlite")]
72impl FromSql<sql_types::Bool, Sqlite> for bool {
73    fn from_sql(mut value: SqliteValue<'_, '_, '_>) -> deserialize::Result<Self> {
74        Ok(value.read_integer() != 0)
75    }
76}
77
78#[cfg(feature = "sqlite")]
79impl FromSql<sql_types::BigInt, Sqlite> for i64 {
80    fn from_sql(mut value: SqliteValue<'_, '_, '_>) -> deserialize::Result<Self> {
81        Ok(value.read_long())
82    }
83}
84
85#[cfg(feature = "sqlite")]
86#[allow(clippy::cast_possible_truncation)] // we want to truncate here
87impl FromSql<sql_types::Float, Sqlite> for f32 {
88    fn from_sql(mut value: SqliteValue<'_, '_, '_>) -> deserialize::Result<Self> {
89        Ok(value.read_double() as f32)
90    }
91}
92
93#[cfg(feature = "sqlite")]
94impl FromSql<sql_types::Double, Sqlite> for f64 {
95    fn from_sql(mut value: SqliteValue<'_, '_, '_>) -> deserialize::Result<Self> {
96        Ok(value.read_double())
97    }
98}
99
100#[cfg(feature = "sqlite")]
101impl ToSql<sql_types::Bool, Sqlite> for bool {
102    fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Sqlite>) -> serialize::Result {
103        let int_value = if *self { &1 } else { &0 };
104        <i32 as ToSql<sql_types::Integer, Sqlite>>::to_sql(int_value, out)
105    }
106}
107
108#[cfg(feature = "sqlite")]
109impl ToSql<sql_types::Text, Sqlite> for str {
110    fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Sqlite>) -> serialize::Result {
111        out.set_value(self);
112        Ok(IsNull::No)
113    }
114}
115
116#[cfg(feature = "sqlite")]
117impl ToSql<sql_types::Binary, Sqlite> for [u8] {
118    fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Sqlite>) -> serialize::Result {
119        out.set_value(self);
120        Ok(IsNull::No)
121    }
122}
123
124#[cfg(feature = "sqlite")]
125impl ToSql<sql_types::SmallInt, Sqlite> for i16 {
126    fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Sqlite>) -> serialize::Result {
127        out.set_value(*self as i32);
128        Ok(IsNull::No)
129    }
130}
131
132#[cfg(feature = "sqlite")]
133impl ToSql<sql_types::Integer, Sqlite> for i32 {
134    fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Sqlite>) -> serialize::Result {
135        out.set_value(*self);
136        Ok(IsNull::No)
137    }
138}
139
140#[cfg(feature = "sqlite")]
141impl ToSql<sql_types::BigInt, Sqlite> for i64 {
142    fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Sqlite>) -> serialize::Result {
143        out.set_value(*self);
144        Ok(IsNull::No)
145    }
146}
147
148#[cfg(feature = "sqlite")]
149impl ToSql<sql_types::Float, Sqlite> for f32 {
150    fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Sqlite>) -> serialize::Result {
151        out.set_value(*self as f64);
152        Ok(IsNull::No)
153    }
154}
155
156#[cfg(feature = "sqlite")]
157impl ToSql<sql_types::Double, Sqlite> for f64 {
158    fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Sqlite>) -> serialize::Result {
159        out.set_value(*self);
160        Ok(IsNull::No)
161    }
162}
163
164/// The SQLite timestamp with time zone type
165///
166/// ### [`ToSql`] impls
167///
168/// - [`chrono::NaiveDateTime`] with `feature = "chrono"`
169/// - [`chrono::DateTime`] with `feature = "chrono"`
170/// - [`time::PrimitiveDateTime`] with `feature = "time"`
171/// - [`time::OffsetDateTime`] with `feature = "time"`
172///
173/// ### [`FromSql`] impls
174///
175/// - [`chrono::NaiveDateTime`] with `feature = "chrono"`
176/// - [`chrono::DateTime`] with `feature = "chrono"`
177/// - [`time::PrimitiveDateTime`] with `feature = "time"`
178/// - [`time::OffsetDateTime`] with `feature = "time"`
179///
180/// [`ToSql`]: crate::serialize::ToSql
181/// [`FromSql`]: crate::deserialize::FromSql
182#[cfg_attr(
183    feature = "chrono",
184    doc = " [`chrono::NaiveDateTime`]: chrono::naive::NaiveDateTime"
185)]
186#[cfg_attr(
187    not(feature = "chrono"),
188    doc = " [`chrono::NaiveDateTime`]: https://docs.rs/chrono/0.4.19/chrono/naive/struct.NaiveDateTime.html"
189)]
190#[cfg_attr(feature = "chrono", doc = " [`chrono::DateTime`]: chrono::DateTime")]
191#[cfg_attr(
192    not(feature = "chrono"),
193    doc = " [`chrono::DateTime`]: https://docs.rs/chrono/0.4.19/chrono/struct.DateTime.html"
194)]
195#[cfg_attr(
196    feature = "time",
197    doc = " [`time::PrimitiveDateTime`]: time::PrimitiveDateTime"
198)]
199#[cfg_attr(
200    not(feature = "time"),
201    doc = " [`time::PrimitiveDateTime`]: https://docs.rs/time/0.3.9/time/struct.PrimitiveDateTime.html"
202)]
203#[cfg_attr(
204    feature = "time",
205    doc = " [`time::OffsetDateTime`]: time::OffsetDateTime"
206)]
207#[cfg_attr(
208    not(feature = "time"),
209    doc = " [`time::OffsetDateTime`]: https://docs.rs/time/0.3.9/time/struct.OffsetDateTime.html"
210)]
211#[derive(Debug, Clone, Copy, Default, QueryId, SqlType)]
212#[diesel(sqlite_type(name = "Text"))]
213#[cfg(feature = "sqlite")]
214pub struct Timestamptz;