diff --git a/lib/src/sql/datetime.rs b/lib/src/sql/datetime.rs index d2eca9bb..69ba6c8d 100644 --- a/lib/src/sql/datetime.rs +++ b/lib/src/sql/datetime.rs @@ -1,16 +1,18 @@ use crate::sql::common::{take_digits, take_digits_range, take_u32_len}; use crate::sql::duration::Duration; use crate::sql::error::IResult; +use crate::sql::escape::escape_str; use crate::sql::serde::is_internal_serialization; -use chrono::{DateTime, FixedOffset, TimeZone, Utc}; +use chrono::{DateTime, FixedOffset, SecondsFormat, TimeZone, Utc}; use nom::branch::alt; use nom::character::complete::char; use nom::combinator::map; use nom::sequence::delimited; use serde::{Deserialize, Serialize}; +use std::fmt::{self, Display, Formatter}; +use std::ops; use std::ops::Deref; use std::str; -use std::{fmt, ops}; const SINGLE: char = '\''; const DOUBLE: char = '"'; @@ -52,9 +54,15 @@ impl Deref for Datetime { } } -impl fmt::Display for Datetime { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "\"{:?}\"", self.0) +impl Datetime { + pub fn to_raw(&self) -> String { + self.0.to_string() + } +} + +impl Display for Datetime { + fn fmt(&self, f: &mut Formatter) -> fmt::Result { + Display::fmt(&escape_str(&self.0.to_rfc3339_opts(SecondsFormat::AutoSi, true)), f) } } diff --git a/lib/src/sql/escape.rs b/lib/src/sql/escape.rs index fae4f1fb..5b276a2d 100644 --- a/lib/src/sql/escape.rs +++ b/lib/src/sql/escape.rs @@ -11,7 +11,7 @@ const BACKTICK: char = '`'; const BACKTICK_ESC: &str = r#"\`"#; #[inline] -pub fn escape_strand(s: &str) -> String { +pub fn escape_str(s: &str) -> String { format!("{}{}{}", DOUBLE, s, DOUBLE) } diff --git a/lib/src/sql/statements/define.rs b/lib/src/sql/statements/define.rs index 28e8c4ef..59695696 100644 --- a/lib/src/sql/statements/define.rs +++ b/lib/src/sql/statements/define.rs @@ -8,7 +8,7 @@ use crate::sql::base::{base, base_or_scope, Base}; use crate::sql::comment::shouldbespace; use crate::sql::duration::{duration, Duration}; use crate::sql::error::IResult; -use crate::sql::escape::escape_strand; +use crate::sql::escape::escape_str; use crate::sql::ident::{ident, Ident}; use crate::sql::idiom; use crate::sql::idiom::{Idiom, Idioms}; @@ -262,13 +262,7 @@ impl DefineLoginStatement { impl fmt::Display for DefineLoginStatement { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!( - f, - "DEFINE LOGIN {} ON {} PASSHASH {}", - self.name, - self.base, - escape_strand(&self.hash) - ) + write!(f, "DEFINE LOGIN {} ON {} PASSHASH {}", self.name, self.base, escape_str(&self.hash)) } } @@ -415,7 +409,7 @@ impl fmt::Display for DefineTokenStatement { self.name, self.base, self.kind, - escape_strand(&self.code) + escape_str(&self.code) ) } } diff --git a/lib/src/sql/strand.rs b/lib/src/sql/strand.rs index 09e59acc..dbd246b8 100644 --- a/lib/src/sql/strand.rs +++ b/lib/src/sql/strand.rs @@ -1,5 +1,5 @@ use crate::sql::error::IResult; -use crate::sql::escape::escape_strand; +use crate::sql::escape::escape_str; use crate::sql::serde::is_internal_serialization; use nom::branch::alt; use nom::bytes::complete::escaped_transform; @@ -55,7 +55,7 @@ impl Strand { impl Display for Strand { fn fmt(&self, f: &mut Formatter) -> fmt::Result { - Display::fmt(&escape_strand(&self.0), f) + Display::fmt(&escape_str(&self.0), f) } } diff --git a/lib/src/sql/value/value.rs b/lib/src/sql/value/value.rs index 7044bac1..09937488 100644 --- a/lib/src/sql/value/value.rs +++ b/lib/src/sql/value/value.rs @@ -656,7 +656,9 @@ impl Value { pub fn as_strand(self) -> Strand { match self { Value::Strand(v) => v, - _ => Strand::from(self.to_string()), + Value::Uuid(v) => v.to_raw().into(), + Value::Datetime(v) => v.to_raw().into(), + _ => self.to_string().into(), } } @@ -701,7 +703,9 @@ impl Value { pub fn to_strand(&self) -> Strand { match self { Value::Strand(v) => v.clone(), - _ => Strand::from(self.to_string()), + Value::Uuid(v) => v.to_raw().into(), + Value::Datetime(v) => v.to_raw().into(), + _ => self.to_string().into(), } }