Ensure UUIDs and Datetimes can be converted to strings correctly

Closes #1338
This commit is contained in:
Tobie Morgan Hitchcock 2022-10-16 17:19:36 +01:00
parent 16638561a2
commit 214971cb86
5 changed files with 25 additions and 19 deletions

View file

@ -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)
}
}

View file

@ -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)
}

View file

@ -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)
)
}
}

View file

@ -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)
}
}

View file

@ -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(),
}
}