1use std::io::Write;
2use std::mem;
3use std::os::raw as libc;
4use std::ptr;
5
6use crate::deserialize::{self, FromSql, FromSqlRow};
7use crate::expression::AsExpression;
8use crate::mysql::{Mysql, MysqlValue};
9use crate::serialize::{self, IsNull, Output, ToSql};
10use crate::sql_types::{Date, Datetime, Time, Timestamp};
11
12#[cfg(feature = "chrono")]
13mod chrono;
14#[cfg(feature = "time")]
15mod time;
16
17#[repr(C)]
25#[derive(#[automatically_derived]
impl ::core::fmt::Debug for MysqlTime {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
let names: &'static _ =
&["year", "month", "day", "hour", "minute", "second",
"second_part", "neg", "time_type",
"time_zone_displacement"];
let values: &[&dyn ::core::fmt::Debug] =
&[&self.year, &self.month, &self.day, &self.hour, &self.minute,
&self.second, &self.second_part, &self.neg, &self.time_type,
&&self.time_zone_displacement];
::core::fmt::Formatter::debug_struct_fields_finish(f, "MysqlTime",
names, values)
}
}Debug, #[automatically_derived]
impl ::core::clone::Clone for MysqlTime {
#[inline]
fn clone(&self) -> MysqlTime {
let _: ::core::clone::AssertParamIsClone<libc::c_uint>;
let _: ::core::clone::AssertParamIsClone<libc::c_uint>;
let _: ::core::clone::AssertParamIsClone<libc::c_uint>;
let _: ::core::clone::AssertParamIsClone<libc::c_uint>;
let _: ::core::clone::AssertParamIsClone<libc::c_uint>;
let _: ::core::clone::AssertParamIsClone<libc::c_uint>;
let _: ::core::clone::AssertParamIsClone<libc::c_ulong>;
let _: ::core::clone::AssertParamIsClone<bool>;
let _: ::core::clone::AssertParamIsClone<MysqlTimestampType>;
let _: ::core::clone::AssertParamIsClone<libc::c_int>;
*self
}
}Clone, #[automatically_derived]
impl ::core::marker::Copy for MysqlTime { }Copy, const _: () =
{
use diesel;
impl<'__expr> diesel::expression::AsExpression<Timestamp> for
&'__expr MysqlTime {
type Expression =
diesel::internal::derives::as_expression::Bound<Timestamp,
Self>;
fn as_expression(self)
->
<Self as
diesel::expression::AsExpression<Timestamp>>::Expression {
diesel::internal::derives::as_expression::Bound::new(self)
}
}
#[diagnostic::do_not_recommend]
impl<'__expr>
diesel::expression::AsExpression<diesel::sql_types::Nullable<Timestamp>>
for &'__expr MysqlTime {
type Expression =
diesel::internal::derives::as_expression::Bound<diesel::sql_types::Nullable<Timestamp>,
Self>;
fn as_expression(self)
->
<Self as
diesel::expression::AsExpression<diesel::sql_types::Nullable<Timestamp>>>::Expression {
diesel::internal::derives::as_expression::Bound::new(self)
}
}
#[diagnostic::do_not_recommend]
impl<'__expr, '__expr2> diesel::expression::AsExpression<Timestamp>
for &'__expr2 &'__expr MysqlTime {
type Expression =
diesel::internal::derives::as_expression::Bound<Timestamp,
Self>;
fn as_expression(self)
->
<Self as
diesel::expression::AsExpression<Timestamp>>::Expression {
diesel::internal::derives::as_expression::Bound::new(self)
}
}
#[diagnostic::do_not_recommend]
impl<'__expr, '__expr2>
diesel::expression::AsExpression<diesel::sql_types::Nullable<Timestamp>>
for &'__expr2 &'__expr MysqlTime {
type Expression =
diesel::internal::derives::as_expression::Bound<diesel::sql_types::Nullable<Timestamp>,
Self>;
fn as_expression(self)
->
<Self as
diesel::expression::AsExpression<diesel::sql_types::Nullable<Timestamp>>>::Expression {
diesel::internal::derives::as_expression::Bound::new(self)
}
}
impl<__DB>
diesel::serialize::ToSql<diesel::sql_types::Nullable<Timestamp>,
__DB> for MysqlTime where __DB: diesel::backend::Backend,
Self: diesel::serialize::ToSql<Timestamp, __DB> {
fn to_sql<'__b>(&'__b self,
out: &mut diesel::serialize::Output<'__b, '_, __DB>)
-> diesel::serialize::Result {
diesel::serialize::ToSql::<Timestamp, __DB>::to_sql(self, out)
}
}
impl diesel::expression::AsExpression<Timestamp> for MysqlTime {
type Expression =
diesel::internal::derives::as_expression::Bound<Timestamp,
Self>;
fn as_expression(self)
->
<Self as
diesel::expression::AsExpression<Timestamp>>::Expression {
diesel::internal::derives::as_expression::Bound::new(self)
}
}
impl diesel::expression::AsExpression<diesel::sql_types::Nullable<Timestamp>>
for MysqlTime {
type Expression =
diesel::internal::derives::as_expression::Bound<diesel::sql_types::Nullable<Timestamp>,
Self>;
fn as_expression(self)
->
<Self as
diesel::expression::AsExpression<diesel::sql_types::Nullable<Timestamp>>>::Expression {
diesel::internal::derives::as_expression::Bound::new(self)
}
}
impl<'__expr> diesel::expression::AsExpression<Time> for
&'__expr MysqlTime {
type Expression =
diesel::internal::derives::as_expression::Bound<Time, Self>;
fn as_expression(self)
->
<Self as
diesel::expression::AsExpression<Time>>::Expression {
diesel::internal::derives::as_expression::Bound::new(self)
}
}
#[diagnostic::do_not_recommend]
impl<'__expr>
diesel::expression::AsExpression<diesel::sql_types::Nullable<Time>>
for &'__expr MysqlTime {
type Expression =
diesel::internal::derives::as_expression::Bound<diesel::sql_types::Nullable<Time>,
Self>;
fn as_expression(self)
->
<Self as
diesel::expression::AsExpression<diesel::sql_types::Nullable<Time>>>::Expression {
diesel::internal::derives::as_expression::Bound::new(self)
}
}
#[diagnostic::do_not_recommend]
impl<'__expr, '__expr2> diesel::expression::AsExpression<Time> for
&'__expr2 &'__expr MysqlTime {
type Expression =
diesel::internal::derives::as_expression::Bound<Time, Self>;
fn as_expression(self)
->
<Self as
diesel::expression::AsExpression<Time>>::Expression {
diesel::internal::derives::as_expression::Bound::new(self)
}
}
#[diagnostic::do_not_recommend]
impl<'__expr, '__expr2>
diesel::expression::AsExpression<diesel::sql_types::Nullable<Time>>
for &'__expr2 &'__expr MysqlTime {
type Expression =
diesel::internal::derives::as_expression::Bound<diesel::sql_types::Nullable<Time>,
Self>;
fn as_expression(self)
->
<Self as
diesel::expression::AsExpression<diesel::sql_types::Nullable<Time>>>::Expression {
diesel::internal::derives::as_expression::Bound::new(self)
}
}
impl<__DB>
diesel::serialize::ToSql<diesel::sql_types::Nullable<Time>, __DB>
for MysqlTime where __DB: diesel::backend::Backend,
Self: diesel::serialize::ToSql<Time, __DB> {
fn to_sql<'__b>(&'__b self,
out: &mut diesel::serialize::Output<'__b, '_, __DB>)
-> diesel::serialize::Result {
diesel::serialize::ToSql::<Time, __DB>::to_sql(self, out)
}
}
impl diesel::expression::AsExpression<Time> for MysqlTime {
type Expression =
diesel::internal::derives::as_expression::Bound<Time, Self>;
fn as_expression(self)
->
<Self as
diesel::expression::AsExpression<Time>>::Expression {
diesel::internal::derives::as_expression::Bound::new(self)
}
}
impl diesel::expression::AsExpression<diesel::sql_types::Nullable<Time>>
for MysqlTime {
type Expression =
diesel::internal::derives::as_expression::Bound<diesel::sql_types::Nullable<Time>,
Self>;
fn as_expression(self)
->
<Self as
diesel::expression::AsExpression<diesel::sql_types::Nullable<Time>>>::Expression {
diesel::internal::derives::as_expression::Bound::new(self)
}
}
impl<'__expr> diesel::expression::AsExpression<Date> for
&'__expr MysqlTime {
type Expression =
diesel::internal::derives::as_expression::Bound<Date, Self>;
fn as_expression(self)
->
<Self as
diesel::expression::AsExpression<Date>>::Expression {
diesel::internal::derives::as_expression::Bound::new(self)
}
}
#[diagnostic::do_not_recommend]
impl<'__expr>
diesel::expression::AsExpression<diesel::sql_types::Nullable<Date>>
for &'__expr MysqlTime {
type Expression =
diesel::internal::derives::as_expression::Bound<diesel::sql_types::Nullable<Date>,
Self>;
fn as_expression(self)
->
<Self as
diesel::expression::AsExpression<diesel::sql_types::Nullable<Date>>>::Expression {
diesel::internal::derives::as_expression::Bound::new(self)
}
}
#[diagnostic::do_not_recommend]
impl<'__expr, '__expr2> diesel::expression::AsExpression<Date> for
&'__expr2 &'__expr MysqlTime {
type Expression =
diesel::internal::derives::as_expression::Bound<Date, Self>;
fn as_expression(self)
->
<Self as
diesel::expression::AsExpression<Date>>::Expression {
diesel::internal::derives::as_expression::Bound::new(self)
}
}
#[diagnostic::do_not_recommend]
impl<'__expr, '__expr2>
diesel::expression::AsExpression<diesel::sql_types::Nullable<Date>>
for &'__expr2 &'__expr MysqlTime {
type Expression =
diesel::internal::derives::as_expression::Bound<diesel::sql_types::Nullable<Date>,
Self>;
fn as_expression(self)
->
<Self as
diesel::expression::AsExpression<diesel::sql_types::Nullable<Date>>>::Expression {
diesel::internal::derives::as_expression::Bound::new(self)
}
}
impl<__DB>
diesel::serialize::ToSql<diesel::sql_types::Nullable<Date>, __DB>
for MysqlTime where __DB: diesel::backend::Backend,
Self: diesel::serialize::ToSql<Date, __DB> {
fn to_sql<'__b>(&'__b self,
out: &mut diesel::serialize::Output<'__b, '_, __DB>)
-> diesel::serialize::Result {
diesel::serialize::ToSql::<Date, __DB>::to_sql(self, out)
}
}
impl diesel::expression::AsExpression<Date> for MysqlTime {
type Expression =
diesel::internal::derives::as_expression::Bound<Date, Self>;
fn as_expression(self)
->
<Self as
diesel::expression::AsExpression<Date>>::Expression {
diesel::internal::derives::as_expression::Bound::new(self)
}
}
impl diesel::expression::AsExpression<diesel::sql_types::Nullable<Date>>
for MysqlTime {
type Expression =
diesel::internal::derives::as_expression::Bound<diesel::sql_types::Nullable<Date>,
Self>;
fn as_expression(self)
->
<Self as
diesel::expression::AsExpression<diesel::sql_types::Nullable<Date>>>::Expression {
diesel::internal::derives::as_expression::Bound::new(self)
}
}
impl<'__expr> diesel::expression::AsExpression<Datetime> for
&'__expr MysqlTime {
type Expression =
diesel::internal::derives::as_expression::Bound<Datetime,
Self>;
fn as_expression(self)
->
<Self as
diesel::expression::AsExpression<Datetime>>::Expression {
diesel::internal::derives::as_expression::Bound::new(self)
}
}
#[diagnostic::do_not_recommend]
impl<'__expr>
diesel::expression::AsExpression<diesel::sql_types::Nullable<Datetime>>
for &'__expr MysqlTime {
type Expression =
diesel::internal::derives::as_expression::Bound<diesel::sql_types::Nullable<Datetime>,
Self>;
fn as_expression(self)
->
<Self as
diesel::expression::AsExpression<diesel::sql_types::Nullable<Datetime>>>::Expression {
diesel::internal::derives::as_expression::Bound::new(self)
}
}
#[diagnostic::do_not_recommend]
impl<'__expr, '__expr2> diesel::expression::AsExpression<Datetime> for
&'__expr2 &'__expr MysqlTime {
type Expression =
diesel::internal::derives::as_expression::Bound<Datetime,
Self>;
fn as_expression(self)
->
<Self as
diesel::expression::AsExpression<Datetime>>::Expression {
diesel::internal::derives::as_expression::Bound::new(self)
}
}
#[diagnostic::do_not_recommend]
impl<'__expr, '__expr2>
diesel::expression::AsExpression<diesel::sql_types::Nullable<Datetime>>
for &'__expr2 &'__expr MysqlTime {
type Expression =
diesel::internal::derives::as_expression::Bound<diesel::sql_types::Nullable<Datetime>,
Self>;
fn as_expression(self)
->
<Self as
diesel::expression::AsExpression<diesel::sql_types::Nullable<Datetime>>>::Expression {
diesel::internal::derives::as_expression::Bound::new(self)
}
}
impl<__DB>
diesel::serialize::ToSql<diesel::sql_types::Nullable<Datetime>,
__DB> for MysqlTime where __DB: diesel::backend::Backend,
Self: diesel::serialize::ToSql<Datetime, __DB> {
fn to_sql<'__b>(&'__b self,
out: &mut diesel::serialize::Output<'__b, '_, __DB>)
-> diesel::serialize::Result {
diesel::serialize::ToSql::<Datetime, __DB>::to_sql(self, out)
}
}
impl diesel::expression::AsExpression<Datetime> for MysqlTime {
type Expression =
diesel::internal::derives::as_expression::Bound<Datetime,
Self>;
fn as_expression(self)
->
<Self as
diesel::expression::AsExpression<Datetime>>::Expression {
diesel::internal::derives::as_expression::Bound::new(self)
}
}
impl diesel::expression::AsExpression<diesel::sql_types::Nullable<Datetime>>
for MysqlTime {
type Expression =
diesel::internal::derives::as_expression::Bound<diesel::sql_types::Nullable<Datetime>,
Self>;
fn as_expression(self)
->
<Self as
diesel::expression::AsExpression<diesel::sql_types::Nullable<Datetime>>>::Expression {
diesel::internal::derives::as_expression::Bound::new(self)
}
}
};AsExpression, const _: () =
{
use diesel;
impl<__DB, __ST> diesel::deserialize::Queryable<__ST, __DB> for
MysqlTime where __DB: diesel::backend::Backend,
__ST: diesel::sql_types::SingleValue,
Self: diesel::deserialize::FromSql<__ST, __DB> {
type Row = Self;
fn build(row: Self) -> diesel::deserialize::Result<Self> {
diesel::deserialize::Result::Ok(row)
}
}
};FromSqlRow)]
26#[non_exhaustive]
27#[diesel(sql_type = Timestamp)]
28#[diesel(sql_type = Time)]
29#[diesel(sql_type = Date)]
30#[diesel(sql_type = Datetime)]
31pub struct MysqlTime {
32 pub year: libc::c_uint,
34 pub month: libc::c_uint,
36 pub day: libc::c_uint,
38 pub hour: libc::c_uint,
40 pub minute: libc::c_uint,
42 pub second: libc::c_uint,
44 pub second_part: libc::c_ulong,
46 pub neg: bool,
48 pub time_type: MysqlTimestampType,
50 pub time_zone_displacement: libc::c_int,
52}
53
54impl MysqlTime {
55 #[allow(clippy::too_many_arguments)]
57 pub const fn new(
58 year: libc::c_uint,
59 month: libc::c_uint,
60 day: libc::c_uint,
61 hour: libc::c_uint,
62 minute: libc::c_uint,
63 second: libc::c_uint,
64 second_part: libc::c_ulong,
65 neg: bool,
66 time_type: MysqlTimestampType,
67 time_zone_displacement: libc::c_int,
68 ) -> Self {
69 Self {
70 year,
71 month,
72 day,
73 hour,
74 minute,
75 second,
76 second_part,
77 neg,
78 time_type,
79 time_zone_displacement,
80 }
81 }
82
83 #[doc = " Serialize a given `MysqlTime` instance to a byte buffer"]
#[allow(unsafe_code)]
pub fn serialize(&self) -> [u8; mem::size_of::<MysqlTime>()] {
unsafe fn copy_bytes<T>(out: &mut [u8], field_ptr: &T,
start: *const MysqlTime) where T: Copy {
let field_ptr = ptr::from_ref(field_ptr);
let offset =
unsafe {
(field_ptr as *const u8).offset_from(start as *const u8)
};
let out_ptr = out.as_mut_ptr();
unsafe {
ptr::copy::<u8>(field_ptr as *const u8, out_ptr.offset(offset),
mem::size_of::<T>())
};
}
let mut buffer = [0_u8; mem::size_of::<MysqlTime>()];
let start = ptr::from_ref(self);
let MysqlTime {
year,
month,
day,
hour,
minute,
second,
second_part,
neg,
time_type,
time_zone_displacement } = self;
unsafe {
copy_bytes(&mut buffer, year, start);
copy_bytes(&mut buffer, month, start);
copy_bytes(&mut buffer, day, start);
copy_bytes(&mut buffer, hour, start);
copy_bytes(&mut buffer, minute, start);
copy_bytes(&mut buffer, second, start);
copy_bytes(&mut buffer, second_part, start);
copy_bytes(&mut buffer, neg, start);
copy_bytes(&mut buffer, time_type, start);
copy_bytes(&mut buffer, time_zone_displacement, start);
}
buffer
}#[diesel_derives::__diesel_public_if(
85 feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes"
86 )]
87 #[allow(unsafe_code)] pub(crate) fn serialize(&self) -> [u8; mem::size_of::<MysqlTime>()] {
89 unsafe fn copy_bytes<T>(out: &mut [u8], field_ptr: &T, start: *const MysqlTime)
90 where
91 T: Copy,
92 {
93 let field_ptr = ptr::from_ref(field_ptr);
94 let offset = unsafe {
95 (field_ptr as *const u8).offset_from(start as *const u8)
98 };
99 let out_ptr = out.as_mut_ptr();
100 unsafe {
101 ptr::copy::<u8>(
108 field_ptr as *const u8,
109 out_ptr.offset(offset),
110 mem::size_of::<T>(),
111 )
112 };
113 }
114 let mut buffer = [0_u8; mem::size_of::<MysqlTime>()];
116
117 let start = ptr::from_ref(self);
119
120 let MysqlTime {
122 year,
123 month,
124 day,
125 hour,
126 minute,
127 second,
128 second_part,
129 neg,
130 time_type,
131 time_zone_displacement,
132 } = self;
133 unsafe {
137 copy_bytes(&mut buffer, year, start);
141 copy_bytes(&mut buffer, month, start);
142 copy_bytes(&mut buffer, day, start);
143 copy_bytes(&mut buffer, hour, start);
144 copy_bytes(&mut buffer, minute, start);
145 copy_bytes(&mut buffer, second, start);
146 copy_bytes(&mut buffer, second_part, start);
147 copy_bytes(&mut buffer, neg, start);
148 copy_bytes(&mut buffer, time_type, start);
149 copy_bytes(&mut buffer, time_zone_displacement, start);
150 }
151 buffer
152 }
153}
154
155#[derive(#[automatically_derived]
impl ::core::cmp::PartialEq for MysqlTimestampType {
#[inline]
fn eq(&self, other: &MysqlTimestampType) -> bool { self.0 == other.0 }
}PartialEq, #[automatically_derived]
impl ::core::fmt::Debug for MysqlTimestampType {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"MysqlTimestampType", &&self.0)
}
}Debug, #[automatically_derived]
impl ::core::marker::Copy for MysqlTimestampType { }Copy, #[automatically_derived]
impl ::core::clone::Clone for MysqlTimestampType {
#[inline]
fn clone(&self) -> MysqlTimestampType {
let _: ::core::clone::AssertParamIsClone<libc::c_int>;
*self
}
}Clone, #[automatically_derived]
impl ::core::cmp::Eq for MysqlTimestampType {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {
let _: ::core::cmp::AssertParamIsEq<libc::c_int>;
}
}Eq)]
162#[repr(transparent)]
163pub struct MysqlTimestampType(libc::c_int);
164
165impl MysqlTimestampType {
166 pub const MYSQL_TIMESTAMP_NONE: MysqlTimestampType = MysqlTimestampType(-2);
169 pub const MYSQL_TIMESTAMP_ERROR: MysqlTimestampType = MysqlTimestampType(-1);
172 pub const MYSQL_TIMESTAMP_DATE: MysqlTimestampType = MysqlTimestampType(0);
175 pub const MYSQL_TIMESTAMP_DATETIME: MysqlTimestampType = MysqlTimestampType(1);
178 pub const MYSQL_TIMESTAMP_TIME: MysqlTimestampType = MysqlTimestampType(2);
181 pub const MYSQL_TIMESTAMP_DATETIME_TZ: MysqlTimestampType = MysqlTimestampType(3);
184}
185
186macro_rules! mysql_time_impls {
187 ($ty:ty) => {
188 #[cfg(feature = "mysql_backend")]
189 impl ToSql<$ty, Mysql> for MysqlTime {
190 fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Mysql>) -> serialize::Result {
191 let buffer = self.serialize();
192
193 out.write_all(&buffer)?;
194 Ok(IsNull::No)
195 }
196 }
197
198 #[cfg(feature = "mysql_backend")]
199 impl FromSql<$ty, Mysql> for MysqlTime {
200 fn from_sql(value: MysqlValue<'_>) -> deserialize::Result<Self> {
201 value.time_value()
202 }
203 }
204 };
205}
206
207impl ToSql<Datetime, Mysql> for MysqlTime {
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Mysql>)
-> serialize::Result {
let buffer = self.serialize();
out.write_all(&buffer)?;
Ok(IsNull::No)
}
}
impl FromSql<Datetime, Mysql> for MysqlTime {
fn from_sql(value: MysqlValue<'_>) -> deserialize::Result<Self> {
value.time_value()
}
}mysql_time_impls!(Datetime);
208impl ToSql<Timestamp, Mysql> for MysqlTime {
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Mysql>)
-> serialize::Result {
let buffer = self.serialize();
out.write_all(&buffer)?;
Ok(IsNull::No)
}
}
impl FromSql<Timestamp, Mysql> for MysqlTime {
fn from_sql(value: MysqlValue<'_>) -> deserialize::Result<Self> {
value.time_value()
}
}mysql_time_impls!(Timestamp);
209impl ToSql<Time, Mysql> for MysqlTime {
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Mysql>)
-> serialize::Result {
let buffer = self.serialize();
out.write_all(&buffer)?;
Ok(IsNull::No)
}
}
impl FromSql<Time, Mysql> for MysqlTime {
fn from_sql(value: MysqlValue<'_>) -> deserialize::Result<Self> {
value.time_value()
}
}mysql_time_impls!(Time);
210impl ToSql<Date, Mysql> for MysqlTime {
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Mysql>)
-> serialize::Result {
let buffer = self.serialize();
out.write_all(&buffer)?;
Ok(IsNull::No)
}
}
impl FromSql<Date, Mysql> for MysqlTime {
fn from_sql(value: MysqlValue<'_>) -> deserialize::Result<Self> {
value.time_value()
}
}mysql_time_impls!(Date);