feat(settings): add global date and time formats

This commit is contained in:
zipg authored and GitHub committed 2026-07-16 14:49:28 +08:00
1 parent df0415333a
commit 0a1efdcb85
31 files changed
+1109 -88

No files matched your search

+70 -16
View File
@@ -56,6 +56,8 @@ pub struct TableExportRequest {
pub batch_size: Option<usize>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub row_limit: Option<usize>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub date_time_format: Option<String>,
}
#[derive(Debug, Clone, Serialize)]
@@ -516,7 +518,12 @@ async fn try_export_native_table_stream(
&cancelled,
cancel_token.clone(),
|row| {
let row_csv = format_csv_rows(&[row.to_vec()]);
let formatted = crate::temporal_format::format_temporal_export_row(
row,
column_types,
request.date_time_format.as_deref(),
);
let row_csv = format_csv_rows(&[formatted]);
write!(file, "\n{row_csv}").map_err(|e| format!("Failed to write CSV rows: {e}"))?;
rows_exported += 1;
if rows_exported % progress_interval == 0 {
@@ -555,7 +562,12 @@ async fn try_export_native_table_stream(
&cancelled,
cancel_token.clone(),
|row| {
let row_tsv = format_tsv_rows(&[row.to_vec()]);
let formatted = crate::temporal_format::format_temporal_export_row(
row,
column_types,
request.date_time_format.as_deref(),
);
let row_tsv = format_tsv_rows(&[formatted]);
write!(file, "\n{row_tsv}").map_err(|e| format!("Failed to write TXT rows: {e}"))?;
rows_exported += 1;
if rows_exported % progress_interval == 0 {
@@ -578,14 +590,14 @@ async fn try_export_native_table_stream(
result
}
"xlsx" => {
let column_types = export_column_types(request);
let xlsx_column_types = export_column_types(request);
let xlsx_file =
std::fs::File::create(&request.file_path).map_err(|e| format!("Failed to create XLSX file: {e}"))?;
let mut writer = start_streaming_xlsx_workbook(
BufWriter::new(xlsx_file),
Some(&request.table_name),
col_names,
&column_types,
&xlsx_column_types,
)?;
let result = stream_native_table_rows(
state,
@@ -596,7 +608,12 @@ async fn try_export_native_table_stream(
&cancelled,
cancel_token.clone(),
|row| {
writer.write_row(row).map_err(|e| format!("Failed to write XLSX row: {e}"))?;
let formatted = crate::temporal_format::format_temporal_export_row(
row,
column_types,
request.date_time_format.as_deref(),
);
writer.write_row(&formatted).map_err(|e| format!("Failed to write XLSX row: {e}"))?;
rows_exported += 1;
if rows_exported % progress_interval == 0 {
on_progress(TableExportProgress {
@@ -645,7 +662,12 @@ async fn try_export_native_table_stream(
if !is_first_row {
file.write_all(b",\n").map_err(|e| format!("Failed to write JSON: {e}"))?;
}
write_json_row_object(&mut file, col_names, row)?;
let formatted = crate::temporal_format::format_temporal_export_row(
row,
column_types,
request.date_time_format.as_deref(),
);
write_json_row_object(&mut file, col_names, &formatted)?;
is_first_row = false;
rows_exported += 1;
if rows_exported % progress_interval == 0 {
@@ -684,7 +706,12 @@ async fn try_export_native_table_stream(
&cancelled,
cancel_token.clone(),
|row| {
let rows_markdown = format_markdown_rows(&[row.to_vec()]);
let formatted = crate::temporal_format::format_temporal_export_row(
row,
column_types,
request.date_time_format.as_deref(),
);
let rows_markdown = format_markdown_rows(&[formatted]);
if !rows_markdown.is_empty() {
if wrote_rows {
file.write_all(b"\n").map_err(|e| format!("Failed to write Markdown: {e}"))?;
@@ -1016,15 +1043,20 @@ pub async fn export_table_data_core(
if row_count == 0 {
break;
}
let formatted_rows = crate::temporal_format::format_temporal_export_rows(
&result.rows,
&column_types,
request.date_time_format.as_deref(),
);
if is_first_batch {
// First batch: write header + rows via format_csv
let csv_content = format_csv(&col_names, &result.rows);
let csv_content = format_csv(&col_names, &formatted_rows);
file.write_all(csv_content.as_bytes()).map_err(|e| format!("Failed to write CSV: {e}"))?;
is_first_batch = false;
} else {
// Subsequent batches: write rows only (prepend newline for separation)
let rows_csv = format_csv_rows(&result.rows);
let rows_csv = format_csv_rows(&formatted_rows);
if !rows_csv.is_empty() {
write!(file, "\n{rows_csv}").map_err(|e| format!("Failed to write CSV rows: {e}"))?;
}
@@ -1098,13 +1130,18 @@ pub async fn export_table_data_core(
if row_count == 0 {
break;
}
let formatted_rows = crate::temporal_format::format_temporal_export_rows(
&result.rows,
&column_types,
request.date_time_format.as_deref(),
);
if is_first_batch {
let rows_tsv = format_tsv_rows(&result.rows);
let rows_tsv = format_tsv_rows(&formatted_rows);
write!(file, "\n{rows_tsv}").map_err(|e| format!("Failed to write TXT rows: {e}"))?;
is_first_batch = false;
} else {
let rows_tsv = format_tsv_rows(&result.rows);
let rows_tsv = format_tsv_rows(&formatted_rows);
if !rows_tsv.is_empty() {
write!(file, "\n{rows_tsv}").map_err(|e| format!("Failed to write TXT rows: {e}"))?;
}
@@ -1135,7 +1172,7 @@ pub async fn export_table_data_core(
}
}
"xlsx" => {
let column_types = export_column_types(request);
let xlsx_column_types = export_column_types(request);
// Create a dedicated file handle for the streaming XLSX writer
// instead of cloning the outer BufWriter's handle. This avoids
// sharing a file descriptor between two independent buffers.
@@ -1145,7 +1182,7 @@ pub async fn export_table_data_core(
BufWriter::new(xlsx_file),
Some(&request.table_name),
&col_names,
&column_types,
&xlsx_column_types,
)?;
loop {
@@ -1188,7 +1225,12 @@ pub async fn export_table_data_core(
}
for row in &result.rows {
writer.write_row(row).map_err(|e| format!("Failed to write XLSX row: {e}"))?;
let formatted = crate::temporal_format::format_temporal_export_row(
row,
&column_types,
request.date_time_format.as_deref(),
);
writer.write_row(&formatted).map_err(|e| format!("Failed to write XLSX row: {e}"))?;
}
rows_exported += row_count as u64;
@@ -1278,7 +1320,12 @@ pub async fn export_table_data_core(
if !is_first_row {
file.write_all(b",\n").map_err(|e| format!("Failed to write JSON: {e}"))?;
}
write_json_row_object(&mut file, &col_names, row)?;
let formatted = crate::temporal_format::format_temporal_export_row(
row,
&column_types,
request.date_time_format.as_deref(),
);
write_json_row_object(&mut file, &col_names, &formatted)?;
is_first_row = false;
}
@@ -1350,7 +1397,12 @@ pub async fn export_table_data_core(
break;
}
let rows_markdown = format_markdown_rows(&result.rows);
let formatted_rows = crate::temporal_format::format_temporal_export_rows(
&result.rows,
&column_types,
request.date_time_format.as_deref(),
);
let rows_markdown = format_markdown_rows(&formatted_rows);
if !rows_markdown.is_empty() {
if wrote_rows {
file.write_all(b"\n").map_err(|e| format!("Failed to write Markdown: {e}"))?;
@@ -1622,6 +1674,7 @@ mod tests {
skip_count: false,
batch_size: Some(500),
row_limit: Some(1000),
date_time_format: None,
};
let sql = table_cursor_sql(
@@ -1669,6 +1722,7 @@ mod tests {
skip_count: false,
batch_size: Some(100),
row_limit: None,
date_time_format: None,
};
let sql = table_cursor_sql(&request, &DatabaseType::Oracle, &columns, &primary_keys);
assert_eq!(sql, "SELECT \"ID\", \"NAME\" FROM \"APP\".\"USERS\"");