diesel/sqlite/types/
mod.rs

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