diff --git a/apps/desktop/src/components/grid/TemporalCellEditor.vue b/apps/desktop/src/components/grid/TemporalCellEditor.vue index cd037d384a..111b0ead02 100644 --- a/apps/desktop/src/components/grid/TemporalCellEditor.vue +++ b/apps/desktop/src/components/grid/TemporalCellEditor.vue @@ -214,10 +214,10 @@ function twoDigit(value: string | number): string {
- -
@@ -226,10 +226,10 @@ function twoDigit(value: string | number): string {
- -
@@ -238,10 +238,10 @@ function twoDigit(value: string | number): string {
- -
@@ -252,10 +252,10 @@ function twoDigit(value: string | number): string {
- -
@@ -264,10 +264,10 @@ function twoDigit(value: string | number): string {
- -
@@ -276,10 +276,10 @@ function twoDigit(value: string | number): string {
- -
@@ -287,11 +287,11 @@ function twoDigit(value: string | number): string {
- - +
diff --git a/crates/dbx-core/src/data_grid_sql.rs b/crates/dbx-core/src/data_grid_sql.rs index 85c49f0c5f..645a7bb2dd 100644 --- a/crates/dbx-core/src/data_grid_sql.rs +++ b/crates/dbx-core/src/data_grid_sql.rs @@ -1104,7 +1104,7 @@ fn is_oracle_temporal_literal_database(database_type: Option) -> b fn format_oracle_temporal_literal(text: &str, data_type: Option<&str>) -> Option { let kind = oracle_temporal_column_kind(data_type?)?; - let parts = regex_like_rfc3339(text)?; + let parts = regex_like_oracle_temporal(text)?; let fraction = parts.fraction.unwrap_or_default(); let datetime = format!("{} {}{}", parts.date, parts.time, fraction); match kind { @@ -1114,6 +1114,10 @@ fn format_oracle_temporal_literal(text: &str, data_type: Option<&str>) -> Option Some(format!("TO_TIMESTAMP('{datetime}', '{mask}')")) } OracleTemporalKind::TimestampWithTimeZone => { + if parts.zone.is_empty() { + let mask = oracle_timestamp_format_mask(datetime.contains('.')); + return Some(format!("TO_TIMESTAMP('{datetime}', '{mask}')")); + } let zone = oracle_timezone_suffix(&parts.zone); let mask = oracle_timestamp_format_mask(datetime.contains('.')); Some(format!("TO_TIMESTAMP_TZ('{datetime} {zone}', '{mask} TZH:TZM')")) @@ -1150,6 +1154,50 @@ fn oracle_timezone_suffix(zone: &str) -> String { } } +fn regex_like_oracle_temporal(text: &str) -> Option { + if let Some(parts) = regex_like_rfc3339(text) { + return Some(parts); + } + regex_like_local_datetime(text) +} + +fn regex_like_local_datetime(text: &str) -> Option { + let bytes = text.as_bytes(); + if bytes.len() < 10 || bytes.get(4) != Some(&b'-') || bytes.get(7) != Some(&b'-') { + return None; + } + let date = &text[0..10]; + if bytes.len() == 10 { + return Some(Rfc3339Parts { + date: date.to_string(), + time: "00:00:00".to_string(), + fraction: None, + zone: String::new(), + }); + } + let separator = *bytes.get(10)?; + if separator != b'T' && separator != b' ' { + return None; + } + if bytes.len() < 19 || bytes.get(13) != Some(&b':') || bytes.get(16) != Some(&b':') { + return None; + } + let time = &text[11..19]; + let rest = &text[19..]; + let fraction = if let Some(rest) = rest.strip_prefix('.') { + let digit_count = rest.chars().take_while(|ch| ch.is_ascii_digit()).count(); + if digit_count == 0 || digit_count > 9 || digit_count != rest.len() { + return None; + } + Some(format!(".{}", &rest[..digit_count])) + } else if rest.is_empty() { + None + } else { + return None; + }; + Some(Rfc3339Parts { date: date.to_string(), time: time.to_string(), fraction, zone: String::new() }) +} + fn is_mysql_bit_literal_column(database_type: Option, column_info: Option<&DataGridColumnInfo>) -> bool { is_mysql_datetime_literal_database(database_type) && column_info.map(|column| is_bit_column_type(&column.data_type)).unwrap_or(false) @@ -2245,6 +2293,25 @@ mod tests { ); } + #[test] + fn formats_oracle_temporal_literals_from_editor_values_without_nls_parsing() { + let timestamp = column("created_at", "TIMESTAMP(6)", true, None); + let date = column("event_day", "DATE", true, None); + + assert_eq!( + format_grid_sql_literal(&json!("2022-08-25 09:58:43"), Some(DatabaseType::Oracle), Some(×tamp)), + "TO_TIMESTAMP('2022-08-25 09:58:43', 'YYYY-MM-DD HH24:MI:SS')" + ); + assert_eq!( + format_grid_sql_literal(&json!("2022-08-25 09:58:43.123456"), Some(DatabaseType::Oracle), Some(×tamp)), + "TO_TIMESTAMP('2022-08-25 09:58:43.123456', 'YYYY-MM-DD HH24:MI:SS.FF')" + ); + assert_eq!( + format_grid_sql_literal(&json!("2022-08-25 09:58:43"), Some(DatabaseType::Oracle), Some(&date)), + "TO_DATE('2022-08-25 09:58:43', 'YYYY-MM-DD HH24:MI:SS')" + ); + } + #[test] fn prepares_oracle_timestamp_insert_from_iso_grid_value() { let result = prepare_data_grid_save(DataGridSaveStatementOptions { diff --git a/crates/dbx-core/src/table_structure_sql/column_alter.rs b/crates/dbx-core/src/table_structure_sql/column_alter.rs index 37e988fb93..4070e3464f 100644 --- a/crates/dbx-core/src/table_structure_sql/column_alter.rs +++ b/crates/dbx-core/src/table_structure_sql/column_alter.rs @@ -360,12 +360,6 @@ pub(super) fn build_oracle_like_existing_column_sql( if type_changed || nullable_changed || default_changed { let data_type = column_data_type(dialect, column); let mut parts = vec![quote_ident(dialect, ¤t_name), data_type]; - // Always include nullability so the statement is self-contained (required by Dameng). - if !column.is_nullable { - parts.push("NOT NULL".to_string()); - } else { - parts.push("NULL".to_string()); - } let default_value = normalize_default(Some(&column.default_value)); if !default_value.is_empty() { parts.push(format!("DEFAULT {}", format_default_for_sql(dialect, &column.data_type, &default_value))); @@ -373,6 +367,13 @@ pub(super) fn build_oracle_like_existing_column_sql( // User cleared the default — explicitly drop it. parts.push("DEFAULT NULL".to_string()); } + if nullable_changed { + if column.is_nullable { + parts.push("NULL".to_string()); + } else { + parts.push("NOT NULL".to_string()); + } + } statements.push(format!("ALTER TABLE {table} MODIFY ({});", parts.join(" "))); } if clean(&column.comment) != original_comment(column) { diff --git a/crates/dbx-core/src/table_structure_sql/tests.rs b/crates/dbx-core/src/table_structure_sql/tests.rs index 01b931390b..3fd2fcafaa 100644 --- a/crates/dbx-core/src/table_structure_sql/tests.rs +++ b/crates/dbx-core/src/table_structure_sql/tests.rs @@ -292,6 +292,60 @@ fn oracle_does_not_generate_drop_sql_for_all_columns() { ); } +#[test] +fn oracle_timestamp_default_precedes_nullability_in_modify_sql() { + let mut col = column("time"); + col.data_type = "TIMESTAMP(6)".to_string(); + col.default_value = "CURRENT_TIMESTAMP".to_string(); + col.original = Some(ColumnInfo { + name: "time".to_string(), + data_type: "TIMESTAMP(6)".to_string(), + is_nullable: true, + column_default: None, + is_primary_key: false, + extra: None, + comment: Some(String::new()), + }); + + let result = build_single_column_alter_sql(SingleColumnAlterSqlOptions { + database_type: Some(DatabaseType::Oracle), + schema: Some("DBX_TEST".to_string()), + table_name: "test".to_string(), + column: col, + }); + + assert_eq!(result.warnings, Vec::::new()); + assert_eq!( + result.statements, + vec!["ALTER TABLE \"DBX_TEST\".\"test\" MODIFY (\"time\" TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP);"] + ); +} + +#[test] +fn oracle_timestamp_precision_change_does_not_repeat_unchanged_nullability() { + let mut col = column("time"); + col.data_type = "TIMESTAMP(9)".to_string(); + col.original = Some(ColumnInfo { + name: "time".to_string(), + data_type: "TIMESTAMP(6)".to_string(), + is_nullable: true, + column_default: None, + is_primary_key: false, + extra: None, + comment: Some(String::new()), + }); + + let result = build_single_column_alter_sql(SingleColumnAlterSqlOptions { + database_type: Some(DatabaseType::Oracle), + schema: Some("DBX_TEST".to_string()), + table_name: "test".to_string(), + column: col, + }); + + assert_eq!(result.warnings, Vec::::new()); + assert_eq!(result.statements, vec!["ALTER TABLE \"DBX_TEST\".\"test\" MODIFY (\"time\" TIMESTAMP(9));"]); +} + #[test] fn iris_drop_index_includes_table_name() { let mut old_index = index("index_id", &["ID"]);