feat(settings): add global date and time formats
This commit is contained in:
31 files changed
+1109
-88
No files matched your search
@@ -1073,8 +1073,8 @@ pub fn escape_value_typed(val: &serde_json::Value, db_type: &DatabaseType, colum
|
||||
if let Some(numeric_literal) = format_mysql_numeric_string_literal(s, db_type, column_type) {
|
||||
return numeric_literal;
|
||||
}
|
||||
if let Some(date_literal) = format_oracle_date_sql_literal(s, db_type, column_type) {
|
||||
return date_literal;
|
||||
if let Some(temporal_literal) = format_oracle_temporal_sql_literal(s, db_type, column_type) {
|
||||
return temporal_literal;
|
||||
}
|
||||
|
||||
let literal = format_literal_string(s, db_type, column_type);
|
||||
@@ -1178,21 +1178,46 @@ fn format_mysql_binary_sql_literal(value: &str, db_type: &DatabaseType, column_t
|
||||
}
|
||||
}
|
||||
|
||||
fn format_oracle_date_sql_literal(value: &str, db_type: &DatabaseType, column_type: Option<&str>) -> Option<String> {
|
||||
fn format_oracle_temporal_sql_literal(
|
||||
value: &str,
|
||||
db_type: &DatabaseType,
|
||||
column_type: Option<&str>,
|
||||
) -> Option<String> {
|
||||
if !matches!(db_type, DatabaseType::Oracle | DatabaseType::OceanbaseOracle) {
|
||||
return None;
|
||||
}
|
||||
if temporal_column_kind(column_type) != Some("date") {
|
||||
return None;
|
||||
}
|
||||
let kind = temporal_column_kind(column_type)?;
|
||||
let normalized_column_type = column_type?.trim().to_ascii_lowercase();
|
||||
let parts = oracle_export_date_parts(value)?;
|
||||
Some(format_oracle_date_sql_literal_parts(&parts))
|
||||
match kind {
|
||||
"date" => Some(format_oracle_date_sql_literal_parts(&parts)),
|
||||
"datetime"
|
||||
if (normalized_column_type.contains("with time zone")
|
||||
|| normalized_column_type.contains("with local time zone"))
|
||||
&& parts.zone.is_some() =>
|
||||
{
|
||||
let fraction = parts.fraction.unwrap_or_default();
|
||||
let mask = if fraction.is_empty() { "YYYY-MM-DD HH24:MI:SS" } else { "YYYY-MM-DD HH24:MI:SS.FF" };
|
||||
let zone = match parts.zone.unwrap_or_default() {
|
||||
"Z" | "z" => "+00:00",
|
||||
zone => zone,
|
||||
};
|
||||
Some(format!("TO_TIMESTAMP_TZ('{} {}{fraction} {zone}', '{mask} TZH:TZM')", parts.date, parts.time))
|
||||
}
|
||||
"datetime" => {
|
||||
let fraction = parts.fraction.unwrap_or_default();
|
||||
let mask = if fraction.is_empty() { "YYYY-MM-DD HH24:MI:SS" } else { "YYYY-MM-DD HH24:MI:SS.FF" };
|
||||
Some(format!("TO_TIMESTAMP('{} {}{fraction}', '{mask}')", parts.date, parts.time))
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
struct OracleExportDateParts<'a> {
|
||||
date: &'a str,
|
||||
time: &'a str,
|
||||
fraction: Option<&'a str>,
|
||||
zone: Option<&'a str>,
|
||||
}
|
||||
|
||||
fn format_oracle_date_sql_literal_parts(parts: &OracleExportDateParts<'_>) -> String {
|
||||
@@ -1218,7 +1243,7 @@ fn oracle_export_date_parts(value: &str) -> Option<OracleExportDateParts<'_>> {
|
||||
return None;
|
||||
}
|
||||
if bytes.len() == 10 {
|
||||
return Some(OracleExportDateParts { date, time: "00:00:00", fraction: None });
|
||||
return Some(OracleExportDateParts { date, time: "00:00:00", fraction: None, zone: None });
|
||||
}
|
||||
let separator = *bytes.get(10)?;
|
||||
if separator != b'T' && separator != b' ' {
|
||||
@@ -1233,7 +1258,7 @@ fn oracle_export_date_parts(value: &str) -> Option<OracleExportDateParts<'_>> {
|
||||
}
|
||||
let rest = &value[19..];
|
||||
if rest.is_empty() || is_timezone_suffix(rest) {
|
||||
return Some(OracleExportDateParts { date, time, fraction: None });
|
||||
return Some(OracleExportDateParts { date, time, fraction: None, zone: (!rest.is_empty()).then_some(rest) });
|
||||
}
|
||||
if let Some(after_dot) = rest.strip_prefix('.') {
|
||||
let digit_count = after_dot.bytes().take_while(|byte| byte.is_ascii_digit()).count();
|
||||
@@ -1242,7 +1267,12 @@ fn oracle_export_date_parts(value: &str) -> Option<OracleExportDateParts<'_>> {
|
||||
}
|
||||
let zone = &after_dot[digit_count..];
|
||||
if zone.is_empty() || is_timezone_suffix(zone) {
|
||||
return Some(OracleExportDateParts { date, time, fraction: Some(&value[19..19 + 1 + digit_count]) });
|
||||
return Some(OracleExportDateParts {
|
||||
date,
|
||||
time,
|
||||
fraction: Some(&value[19..19 + 1 + digit_count]),
|
||||
zone: (!zone.is_empty()).then_some(zone),
|
||||
});
|
||||
}
|
||||
}
|
||||
None
|
||||
@@ -5987,12 +6017,20 @@ mod tests {
|
||||
|
||||
assert_eq!(
|
||||
sql,
|
||||
"INSERT INTO \"APP\".\"events\" (\"id\", \"created_on\", \"created_at\", \"raw_text\") VALUES\n(1, TO_DATE('2022-08-25 09:58:43', 'YYYY-MM-DD HH24:MI:SS'), '2022-08-25T09:58:43Z', '2022-08-25T09:58:43Z')"
|
||||
"INSERT INTO \"APP\".\"events\" (\"id\", \"created_on\", \"created_at\", \"raw_text\") VALUES\n(1, TO_DATE('2022-08-25 09:58:43', 'YYYY-MM-DD HH24:MI:SS'), TO_TIMESTAMP('2022-08-25 09:58:43', 'YYYY-MM-DD HH24:MI:SS'), '2022-08-25T09:58:43Z')"
|
||||
);
|
||||
assert_eq!(
|
||||
escape_value_typed(&json!("2022-08-25T00:00:00Z"), &DatabaseType::Oracle, Some("DATE")),
|
||||
"DATE '2022-08-25'"
|
||||
);
|
||||
assert_eq!(
|
||||
escape_value_typed(
|
||||
&json!("2022-08-25T09:58:43.123456+08:00"),
|
||||
&DatabaseType::Oracle,
|
||||
Some("TIMESTAMP(6) WITH TIME ZONE")
|
||||
),
|
||||
"TO_TIMESTAMP_TZ('2022-08-25 09:58:43.123456 +08:00', 'YYYY-MM-DD HH24:MI:SS.FF TZH:TZM')"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in new issue
Block a user