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