feat(extractor): configure INSERT quoting and portable temporal output

Closes #10559
This commit is contained in:
t8y2 committed 2026-10-03 04:17:17 +00:00
1 parent a95762a536
commit d4b4cf8279
13 files changed
+725 -20

No files matched your search

@@ -106,6 +106,7 @@ function warningText(code: DataGridExtractWarningCode, omittedColumns: string[]
} }
function schedulePreview() { function schedulePreview() {
previewSequence += 1;
if (previewTimer) clearTimeout(previewTimer); if (previewTimer) clearTimeout(previewTimer);
previewTimer = setTimeout(() => void refreshPreview(), 180); previewTimer = setTimeout(() => void refreshPreview(), 180);
} }
@@ -124,6 +125,7 @@ watch(
draftOptions.value = normalizeDataGridExtractorOptions(props.options); draftOptions.value = normalizeDataGridExtractorOptions(props.options);
schedulePreview(); schedulePreview();
}, },
{ immediate: true },
); );
watch( watch(
@@ -212,6 +214,18 @@ function save() {
<label v-if="draftExtractor === 'sql-inserts'" class="flex items-center gap-2 text-sm" <label v-if="draftExtractor === 'sql-inserts'" class="flex items-center gap-2 text-sm"
><input type="checkbox" :checked="draftOptions.sql.includeDatabaseName" @change="updateSql('includeDatabaseName', ($event.target as HTMLInputElement).checked)" />{{ t("grid.copyExtractorIncludeDatabaseName") }}</label ><input type="checkbox" :checked="draftOptions.sql.includeDatabaseName" @change="updateSql('includeDatabaseName', ($event.target as HTMLInputElement).checked)" />{{ t("grid.copyExtractorIncludeDatabaseName") }}</label
> >
<div v-if="draftExtractor === 'sql-inserts'" class="space-y-1.5">
<label class="flex items-center gap-2 text-sm"><input type="checkbox" :checked="draftOptions.sql.quoteIdentifiers" @change="updateSql('quoteIdentifiers', ($event.target as HTMLInputElement).checked)" />{{ t("grid.copyExtractorQuoteIdentifiers") }}</label>
<Label>{{ t("grid.copyExtractorTemporalFormat") }}</Label>
<Select :model-value="draftOptions.sql.temporalFormat" @update:model-value="updateSql('temporalFormat', $event as DataGridExtractorOptions['sql']['temporalFormat'])">
<SelectTrigger><SelectValue /></SelectTrigger>
<SelectContent>
<SelectItem value="native">{{ t("grid.copyExtractorTemporalNative") }}</SelectItem>
<SelectItem value="string">{{ t("grid.copyExtractorTemporalString") }}</SelectItem>
</SelectContent>
</Select>
<p v-if="draftOptions.sql.temporalFormat === 'string'" class="text-xs text-muted-foreground">{{ t("grid.copyExtractorTemporalStringHint") }}</p>
</div>
<div v-if="draftExtractor === 'sql-inserts'" class="space-y-1.5"> <div v-if="draftExtractor === 'sql-inserts'" class="space-y-1.5">
<Label>{{ t("grid.copyExtractorInsertMode") }}</Label> <Label>{{ t("grid.copyExtractorInsertMode") }}</Label>
<Select :model-value="draftOptions.sql.insertMode" @update:model-value="updateSql('insertMode', $event as DataGridExtractorOptions['sql']['insertMode'])"> <Select :model-value="draftOptions.sql.insertMode" @update:model-value="updateSql('insertMode', $event as DataGridExtractorOptions['sql']['insertMode'])">
@@ -0,0 +1,176 @@
// @vitest-environment happy-dom
import { createApp, defineComponent, h, nextTick, reactive, type App } from "vue";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { normalizeDataGridExtractorOptions, type DataGridExtractPreview } from "@/lib/dataGrid/dataGridCopyExtractor";
vi.mock("vue-i18n", () => ({ useI18n: () => ({ t: (key: string) => key }) }));
vi.mock("@/components/ui/dialog", () => {
const passthrough = defineComponent({
setup:
(_, { slots }) =>
() =>
h("div", slots.default?.()),
});
return { Dialog: passthrough, DialogContent: passthrough, DialogFooter: passthrough, DialogHeader: passthrough, DialogTitle: passthrough };
});
vi.mock("@/components/ui/select", () => {
const passthrough = defineComponent({
setup:
(_, { slots }) =>
() =>
h("div", slots.default?.()),
});
return {
Select: defineComponent({
props: ["modelValue"],
emits: ["update:modelValue"],
setup:
(props, { emit, slots }) =>
() =>
h("select", { value: props.modelValue, onChange: (event: Event) => emit("update:modelValue", (event.target as HTMLSelectElement).value) }, slots.default?.()),
}),
SelectItem: defineComponent({
props: ["value"],
setup:
(props, { slots }) =>
() =>
h("option", { value: props.value }, slots.default?.()),
}),
SelectContent: passthrough,
SelectTrigger: passthrough,
SelectValue: passthrough,
};
});
import DataGridExtractorDialog from "../DataGridExtractorDialog.vue";
const mounted: Array<{ app: App; host: HTMLElement }> = [];
const result = (text: string): DataGridExtractPreview => ({ text, mimeType: "text/plain", fileExtension: "sql", rowCount: 1, columnCount: 1, sourceRowCount: 1, truncated: false });
function deferred() {
let resolve!: (value: DataGridExtractPreview) => void;
let reject!: (error: Error) => void;
const promise = new Promise<DataGridExtractPreview>((yes, no) => {
resolve = yes;
reject = no;
});
return { promise, resolve, reject };
}
function mountDialog(preview = vi.fn(async () => result("initial")), open = false) {
const props = reactive({ open, preference: "sql-inserts" as const, options: normalizeDataGridExtractorOptions({}), items: [{ value: "sql-inserts" as const, label: "INSERT" }], preview });
const save = vi.fn();
const host = document.createElement("div");
document.body.appendChild(host);
const app = createApp({ render: () => h(DataGridExtractorDialog, { ...props, onSave: save }) });
app.mount(host);
mounted.push({ app, host });
return { props, host, save, preview };
}
async function advancePreview() {
await nextTick();
await vi.advanceTimersByTimeAsync(180);
await nextTick();
}
async function toggle(host: HTMLElement, key: string) {
const label = Array.from(host.querySelectorAll("label")).find((item) => item.textContent?.includes(key));
const input = label?.querySelector("input");
expect(input).toBeTruthy();
input!.checked = !input!.checked;
input!.dispatchEvent(new Event("change", { bubbles: true }));
await nextTick();
}
beforeEach(() => {
vi.useFakeTimers();
});
afterEach(() => {
for (const { app, host } of mounted.splice(0)) {
app.unmount();
host.remove();
}
vi.useRealTimers();
});
describe("extractor draft preview", () => {
it("previews an initially open dialog", async () => {
const { preview, host } = mountDialog(undefined, true);
await advancePreview();
expect(preview).toHaveBeenCalledOnce();
expect(host.querySelector("pre")?.textContent).toBe("initial");
});
it("previews unsaved quote and temporal options without mutating saved settings", async () => {
const { props, host, preview, save } = mountDialog();
props.open = true;
await advancePreview();
await toggle(host, "copyExtractorQuoteIdentifiers");
const select = Array.from(host.querySelectorAll("select")).find((item) => item.querySelector('option[value="string"]'))!;
expect(select).toBeTruthy();
select.value = "string";
select.dispatchEvent(new Event("change", { bubbles: true }));
await advancePreview();
expect(preview).toHaveBeenLastCalledWith("sql-inserts", expect.objectContaining({ sql: expect.objectContaining({ quoteIdentifiers: false, temporalFormat: "string" }) }));
expect(props.options.sql).toMatchObject({ quoteIdentifiers: true, temporalFormat: "native" });
expect(save).not.toHaveBeenCalled();
const button = Array.from(host.querySelectorAll("button")).find((item) => item.textContent?.includes("common.save"))!;
button.click();
expect(save).toHaveBeenCalledWith(expect.objectContaining({ options: expect.objectContaining({ sql: expect.objectContaining({ quoteIdentifiers: false, temporalFormat: "string" }) }) }));
});
it.each(["resolve", "reject"] as const)("ignores an old %s during the debounce window and after a newer preview", async (settle) => {
const old = deferred();
const newer = deferred();
const preview = vi
.fn(async () => result("latest"))
.mockReturnValueOnce(old.promise)
.mockReturnValueOnce(newer.promise);
const { props, host } = mountDialog(preview);
props.open = true;
await advancePreview();
await toggle(host, "copyExtractorSkipComputed");
if (settle === "resolve") old.resolve(result("stale"));
else old.reject(new Error("stale error"));
await nextTick();
await nextTick();
expect(host.querySelector("pre")?.textContent).not.toContain("stale");
await advancePreview();
await toggle(host, "copyExtractorSkipGenerated");
await advancePreview();
expect(host.querySelector("pre")?.textContent).toBe("latest");
newer.resolve(result("also stale"));
await nextTick();
expect(host.querySelector("pre")?.textContent).toBe("latest");
});
it("shows current errors and resets draft options when reopened", async () => {
const preview = vi.fn(async () => result("reopened")).mockRejectedValueOnce(new Error("preview failed"));
const { props, host } = mountDialog(preview);
props.open = true;
await advancePreview();
expect(host.querySelector("pre")?.textContent).toBe("preview failed");
await toggle(host, "copyExtractorSkipComputed");
props.open = false;
await advancePreview();
expect(preview).toHaveBeenCalledOnce();
props.open = true;
await advancePreview();
expect(preview).toHaveBeenLastCalledWith("sql-inserts", props.options);
expect(host.querySelector("pre")?.textContent).toBe("reopened");
});
it("ignores pending results after the dialog closes", async () => {
const pending = deferred();
const { props, host } = mountDialog(vi.fn(() => pending.promise));
props.open = true;
await advancePreview();
props.open = false;
await nextTick();
pending.resolve(result("closed result"));
await nextTick();
expect(host.querySelector("pre")?.textContent).not.toContain("closed result");
});
});
@@ -1227,6 +1227,19 @@ describe("useDataGridExport prepared row statements", () => {
expect(vi.mocked(extractDataGridSelection).mock.calls.map(([request]) => request.options)).toEqual([extractorOptions, extractorOptions, extractorOptions]); expect(vi.mocked(extractDataGridSelection).mock.calls.map(([request]) => request.options)).toEqual([extractorOptions, extractorOptions, extractorOptions]);
}); });
it("sends unsaved INSERT policies to preview without changing the saved copy options", async () => {
const matrix: CellSelectionMatrix = { rowIndexes: [0], columnIndexes: [0, 1], columns: ["id", "name"], rows: [[1, "Ada"]] };
const saved = structuredClone(DEFAULT_DATA_GRID_EXTRACTOR_OPTIONS);
const draft = { ...saved, sql: { ...saved.sql, quoteIdentifiers: false, temporalFormat: "string" as const } };
const state = createExportState(editableTable, ["id", "name"], matrix, [1, "Ada"], undefined, matrix.rows, [], saved);
await state.previewWithPreference("sql-inserts", draft);
expect(vi.mocked(extractDataGridSelection).mock.lastCall?.[0].options.sql).toEqual(draft.sql);
expect(copyToClipboard).not.toHaveBeenCalled();
await state.copyWithExtractor("sql-inserts");
expect(vi.mocked(extractDataGridSelection).mock.lastCall?.[0].options.sql).toEqual(saved.sql);
});
it("does not report success when an extractor export save is cancelled", async () => { it("does not report success when an extractor export save is cancelled", async () => {
const matrix: CellSelectionMatrix = { rowIndexes: [0], columnIndexes: [1], columns: ["name"], rows: [["Ada"]] }; const matrix: CellSelectionMatrix = { rowIndexes: [0], columnIndexes: [1], columns: ["name"], rows: [["Ada"]] };
vi.mocked(extractDataGridSelection).mockResolvedValueOnce({ text: '[{"name":"Ada"}]', mimeType: "application/json", fileExtension: "json", rowCount: 1, columnCount: 1 }); vi.mocked(extractDataGridSelection).mockResolvedValueOnce({ text: '[{"name":"Ada"}]', mimeType: "application/json", fileExtension: "json", rowCount: 1, columnCount: 1 });
+5
View File
@@ -2250,6 +2250,11 @@ export default {
copyExtractorSkipGenerated: "Skip generated columns", copyExtractorSkipGenerated: "Skip generated columns",
copyExtractorExcludePrimaryKeys: "Exclude primary keys", copyExtractorExcludePrimaryKeys: "Exclude primary keys",
copyExtractorIncludeDatabaseName: "Include database name", copyExtractorIncludeDatabaseName: "Include database name",
copyExtractorQuoteIdentifiers: "Quote table and column names",
copyExtractorTemporalFormat: "Date and time values",
copyExtractorTemporalNative: "Native database format",
copyExtractorTemporalString: "Quoted string (YYYY-MM-DD HH:mm:ss)",
copyExtractorTemporalStringHint: "Keeps the source date and time to seconds, without fractional seconds or a time zone.",
copyExtractorInsertMode: "INSERT statements", copyExtractorInsertMode: "INSERT statements",
copyExtractorInsertMerged: "Merge rows", copyExtractorInsertMerged: "Merge rows",
copyExtractorInsertRowByRow: "One statement per row", copyExtractorInsertRowByRow: "One statement per row",
+5
View File
@@ -2162,6 +2162,11 @@ export default withEnglishFallback({
copyExtractorSkipGenerated: "跳过生成列", copyExtractorSkipGenerated: "跳过生成列",
copyExtractorExcludePrimaryKeys: "排除主键", copyExtractorExcludePrimaryKeys: "排除主键",
copyExtractorIncludeDatabaseName: "包含数据库名称", copyExtractorIncludeDatabaseName: "包含数据库名称",
copyExtractorQuoteIdentifiers: "表名和字段名加引号",
copyExtractorTemporalFormat: "日期和时间值",
copyExtractorTemporalNative: "数据库原生格式",
copyExtractorTemporalString: "带引号字符串(YYYY-MM-DD HH:mm:ss)",
copyExtractorTemporalStringHint: "保留原日期和时间到秒,不包含小数秒和时区。",
copyExtractorInsertMode: "INSERT 语句模式", copyExtractorInsertMode: "INSERT 语句模式",
copyExtractorInsertMerged: "合并多行", copyExtractorInsertMerged: "合并多行",
copyExtractorInsertRowByRow: "每行一条语句", copyExtractorInsertRowByRow: "每行一条语句",
+5
View File
@@ -2688,6 +2688,11 @@ export default withEnglishFallback({
copyExtractorSkipGenerated: "跳過生成列", copyExtractorSkipGenerated: "跳過生成列",
copyExtractorExcludePrimaryKeys: "排除主鍵", copyExtractorExcludePrimaryKeys: "排除主鍵",
copyExtractorIncludeDatabaseName: "包含資料庫名稱", copyExtractorIncludeDatabaseName: "包含資料庫名稱",
copyExtractorQuoteIdentifiers: "資料表名稱和欄位名稱加引號",
copyExtractorTemporalFormat: "日期和時間值",
copyExtractorTemporalNative: "資料庫原生格式",
copyExtractorTemporalString: "帶引號字串(YYYY-MM-DD HH:mm:ss)",
copyExtractorTemporalStringHint: "保留原日期和時間到秒,不包含小數秒和時區。",
copyExtractorInsertMode: "INSERT 語句模式", copyExtractorInsertMode: "INSERT 語句模式",
copyExtractorInsertMerged: "合併多行", copyExtractorInsertMerged: "合併多行",
copyExtractorInsertRowByRow: "每行一條語句", copyExtractorInsertRowByRow: "每行一條語句",
@@ -2,6 +2,16 @@ import { describe, expect, it } from "vitest";
import { DEFAULT_DATA_GRID_EXTRACTOR_OPTIONS, normalizeDataGridCopyPreference, normalizeDataGridExtractorOptions, resolveDataGridCopyPreference, validateDataGridExtractorOptions } from "@/lib/dataGrid/dataGridCopyExtractor"; import { DEFAULT_DATA_GRID_EXTRACTOR_OPTIONS, normalizeDataGridCopyPreference, normalizeDataGridExtractorOptions, resolveDataGridCopyPreference, validateDataGridExtractorOptions } from "@/lib/dataGrid/dataGridCopyExtractor";
describe("data-grid extractor options", () => { describe("data-grid extractor options", () => {
it("preserves native SQL defaults and normalizes portable INSERT options", () => {
for (const legacy of [undefined, {}, { sql: {} }, { sql: { insertMode: "row-by-row" } }]) {
expect(normalizeDataGridExtractorOptions(legacy).sql).toMatchObject({ quoteIdentifiers: true, temporalFormat: "native" });
}
const configured = normalizeDataGridExtractorOptions({ sql: { quoteIdentifiers: false, temporalFormat: "string" } });
expect(configured.sql).toMatchObject({ quoteIdentifiers: false, temporalFormat: "string" });
expect(normalizeDataGridExtractorOptions(JSON.parse(JSON.stringify(configured)))).toEqual(configured);
expect(normalizeDataGridExtractorOptions({ sql: { quoteIdentifiers: "false", temporalFormat: "custom" } }).sql).toMatchObject({ quoteIdentifiers: true, temporalFormat: "native" });
});
it("keeps database qualification for legacy options and persists explicit opt-out", () => { it("keeps database qualification for legacy options and persists explicit opt-out", () => {
expect(normalizeDataGridExtractorOptions({ sql: {} }).sql.includeDatabaseName).toBe(true); expect(normalizeDataGridExtractorOptions({ sql: {} }).sql.includeDatabaseName).toBe(true);
const configured = normalizeDataGridExtractorOptions({ sql: { includeDatabaseName: false } }); const configured = normalizeDataGridExtractorOptions({ sql: { includeDatabaseName: false } });
@@ -89,6 +89,8 @@ export interface DataGridExtractorOptions {
insertMode: DataGridCopyInsertMode; insertMode: DataGridCopyInsertMode;
excludePrimaryKeysFromInsert: boolean; excludePrimaryKeysFromInsert: boolean;
includeDatabaseName: boolean; includeDatabaseName: boolean;
quoteIdentifiers: boolean;
temporalFormat: "native" | "string";
}; };
json: { json: {
pretty: boolean; pretty: boolean;
@@ -150,6 +152,8 @@ export const DEFAULT_DATA_GRID_EXTRACTOR_OPTIONS: DataGridExtractorOptions = {
insertMode: "merged", insertMode: "merged",
excludePrimaryKeysFromInsert: false, excludePrimaryKeysFromInsert: false,
includeDatabaseName: true, includeDatabaseName: true,
quoteIdentifiers: true,
temporalFormat: "native",
}, },
json: { pretty: true, camelCaseFieldNames: false }, json: { pretty: true, camelCaseFieldNames: false },
}; };
@@ -181,6 +185,8 @@ export function normalizeDataGridExtractorOptions(value: unknown): DataGridExtra
insertMode: sql.insertMode === "row-by-row" ? "row-by-row" : "merged", insertMode: sql.insertMode === "row-by-row" ? "row-by-row" : "merged",
excludePrimaryKeysFromInsert: sql.excludePrimaryKeysFromInsert === true, excludePrimaryKeysFromInsert: sql.excludePrimaryKeysFromInsert === true,
includeDatabaseName: sql.includeDatabaseName !== false, includeDatabaseName: sql.includeDatabaseName !== false,
quoteIdentifiers: sql.quoteIdentifiers !== false,
temporalFormat: sql.temporalFormat === "string" ? "string" : "native",
}, },
json: { json: {
pretty: json.pretty !== false, pretty: json.pretty !== false,
@@ -95,6 +95,8 @@ pub struct DataGridSqlExtractorOptions {
pub insert_mode: DataGridCopyInsertMode, pub insert_mode: DataGridCopyInsertMode,
pub exclude_primary_keys_from_insert: bool, pub exclude_primary_keys_from_insert: bool,
pub include_database_name: bool, pub include_database_name: bool,
pub quote_identifiers: bool,
pub temporal_format: DataGridTemporalFormat,
} }
impl Default for DataGridSqlExtractorOptions { impl Default for DataGridSqlExtractorOptions {
@@ -105,10 +107,21 @@ impl Default for DataGridSqlExtractorOptions {
insert_mode: DataGridCopyInsertMode::Merged, insert_mode: DataGridCopyInsertMode::Merged,
exclude_primary_keys_from_insert: false, exclude_primary_keys_from_insert: false,
include_database_name: true, include_database_name: true,
quote_identifiers: true,
temporal_format: DataGridTemporalFormat::Native,
} }
} }
} }
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
#[serde(rename_all = "kebab-case")]
pub enum DataGridTemporalFormat {
#[default]
Native,
String,
}
#[derive(Debug, Clone, Serialize, Deserialize)] #[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))] #[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
#[serde(rename_all = "camelCase", default, deny_unknown_fields)] #[serde(rename_all = "camelCase", default, deny_unknown_fields)]
@@ -1,19 +1,25 @@
use super::{ use super::{
estimated_value_bytes, normalized_name_eq, write_bytes, DataGridExtractError, DataGridExtractErrorCode, estimated_value_bytes, normalized_name_eq, write_bytes, DataGridExtractError, DataGridExtractErrorCode,
DataGridExtractWarning, DataGridExtractWarningCode, ExtractContext, WriteMetadata, DataGridExtractWarning, DataGridExtractWarningCode, DataGridTemporalFormat, ExtractContext, WriteMetadata,
DATA_GRID_EXTRACTOR_MAX_OUTPUT_BYTES, DATA_GRID_EXTRACTOR_MAX_OUTPUT_BYTES,
}; };
use crate::data_grid_sql::{ use crate::data_grid_sql::{
build_column_predicate, build_data_grid_copy_insert_statement, build_data_grid_copy_update_statements, build_column_predicate, build_data_grid_copy_insert_statement_with_formatters,
data_grid_generated_table_name, format_grid_sql_literal_with_identifier_quote, is_auto_generated_column, build_data_grid_copy_update_statements, data_grid_generated_table_name, format_grid_copy_insert_sql_literal,
is_grid_insert_omitted_column, is_non_identity_generated_column, supports_relational_copy_predicates, format_grid_sql_literal_with_identifier_quote, is_auto_generated_column, is_grid_insert_omitted_column,
is_non_identity_generated_column, supports_relational_copy_predicates, DataGridColumnInfo,
DataGridCopyInsertStatementOptions, DataGridCopyUpdateStatementOptions, DataGridTableMeta, DataGridCopyInsertStatementOptions, DataGridCopyUpdateStatementOptions, DataGridTableMeta,
}; };
use crate::models::connection::DatabaseType;
use chrono::{DateTime, NaiveDate, NaiveDateTime, NaiveTime};
use dbx_types::types::is_opaque_aggregate_state_type; use dbx_types::types::is_opaque_aggregate_state_type;
use serde_json::Value; use serde_json::Value;
use std::collections::{hash_map::Entry, HashMap, HashSet}; use std::collections::{hash_map::Entry, HashMap, HashSet};
use std::io::Write; use std::io::Write;
#[path = "sql_identifiers.rs"]
mod identifiers;
pub(super) fn write_sql_in_list( pub(super) fn write_sql_in_list(
context: &ExtractContext<'_>, context: &ExtractContext<'_>,
output: &mut dyn Write, output: &mut dyn Write,
@@ -64,19 +70,40 @@ pub(super) fn write_sql_inserts(
ensure_sql_builder_budget(context)?; ensure_sql_builder_budget(context)?;
reject_selected_opaque_aggregate_states(context)?; reject_selected_opaque_aggregate_states(context)?;
let data = sql_selected_data(context, false)?; let data = sql_selected_data(context, false)?;
let statement = build_data_grid_copy_insert_statement(DataGridCopyInsertStatementOptions { let statement = build_data_grid_copy_insert_statement_with_formatters(
database_type: context.request.database_type, DataGridCopyInsertStatementOptions {
identifier_quote: context.request.identifier_quote.clone(), database_type: context.request.database_type,
table_meta: context.request.table_meta.clone(), identifier_quote: context.request.identifier_quote.clone(),
columns: data.columns, table_meta: context.request.table_meta.clone(),
column_types: Some(data.column_types), columns: data.columns,
source_columns: Some(data.source_columns), column_types: Some(data.column_types),
rows: data.rows, source_columns: Some(data.source_columns),
exclude_primary_keys: context.request.options.sql.exclude_primary_keys_from_insert, rows: data.rows,
include_computed_columns: !context.request.options.sql.skip_computed_columns, exclude_primary_keys: context.request.options.sql.exclude_primary_keys_from_insert,
include_database_name: context.request.options.sql.include_database_name, include_computed_columns: !context.request.options.sql.skip_computed_columns,
insert_mode: context.request.options.sql.insert_mode, include_database_name: context.request.options.sql.include_database_name,
}) insert_mode: context.request.options.sql.insert_mode,
},
|reference| {
if context.request.options.sql.quote_identifiers {
reference
} else {
identifiers::unquote_optional_identifiers(
reference,
context.request.database_type,
context.request.identifier_quote.as_deref(),
)
}
},
|value, database_type, info, identifier_quote| {
if context.request.options.sql.temporal_format == DataGridTemporalFormat::String {
if let Some(literal) = portable_temporal_literal(value, database_type, info) {
return literal;
}
}
format_grid_copy_insert_sql_literal(value, database_type, info, identifier_quote)
},
)
.ok_or_else(|| { .ok_or_else(|| {
DataGridExtractError::new( DataGridExtractError::new(
DataGridExtractErrorCode::NoWritableColumns, DataGridExtractErrorCode::NoWritableColumns,
@@ -87,6 +114,50 @@ pub(super) fn write_sql_inserts(
Ok(sql_metadata(data.omitted_columns)) Ok(sql_metadata(data.omitted_columns))
} }
fn portable_temporal_literal(
value: &Value,
database_type: Option<DatabaseType>,
info: Option<&DataGridColumnInfo>,
) -> Option<String> {
let data_type = info?.data_type.trim().to_ascii_lowercase();
let kind = data_type.split(['(', ':', ' ']).next()?;
if database_type == Some(DatabaseType::SqlServer) && kind == "timestamp" {
return None;
}
if !matches!(
kind,
"date"
| "time"
| "timetz"
| "timestamp"
| "timestamptz"
| "datetime"
| "datetime2"
| "datetimeoffset"
| "smalldatetime"
) {
return None;
}
let text = value.as_str()?;
if database_type == Some(DatabaseType::Iotdb) && kind == "timestamp" && text.parse::<i64>().is_ok() {
return None;
}
let normalized = DateTime::parse_from_rfc3339(text)
.map(|datetime| datetime.naive_local())
.ok()
.or_else(|| NaiveDateTime::parse_from_str(text, "%Y-%m-%d %H:%M:%S%.f").ok())
.or_else(|| NaiveDateTime::parse_from_str(text, "%Y-%m-%dT%H:%M:%S%.f").ok())
.or_else(|| NaiveDate::parse_from_str(text, "%Y-%m-%d").ok().and_then(|date| date.and_hms_opt(0, 0, 0)))
.map(|datetime| {
datetime
.format(if matches!(kind, "time" | "timetz") { "%H:%M:%S" } else { "%Y-%m-%d %H:%M:%S" })
.to_string()
})
.or_else(|| NaiveTime::parse_from_str(text, "%H:%M:%S%.f").ok().map(|time| time.format("%H:%M:%S").to_string()))
.unwrap_or_else(|| text.to_string());
Some(format!("'{}'", normalized.replace('\'', "''")))
}
pub(super) fn write_sql_updates( pub(super) fn write_sql_updates(
context: &ExtractContext<'_>, context: &ExtractContext<'_>,
output: &mut dyn Write, output: &mut dyn Write,
@@ -0,0 +1,143 @@
use crate::models::connection::DatabaseType;
use crate::sql_dialect::quote_table_data_identifier;
use sqlparser::dialect::{Dialect, GenericDialect, MsSqlDialect};
use sqlparser::tokenizer::{Token, Tokenizer};
use std::collections::HashSet;
use std::sync::OnceLock;
pub(super) fn unquote_optional_identifiers(
reference: String,
database_type: Option<DatabaseType>,
identifier_quote: Option<&str>,
) -> String {
let dialect: &dyn Dialect =
if database_type == Some(DatabaseType::SqlServer) { &MsSqlDialect {} } else { &GenericDialect {} };
let Ok(tokens) = Tokenizer::new(dialect, &reference).with_unescape(false).tokenize() else {
return reference;
};
if tokens.iter().any(|token| !matches!(token, Token::Word(_) | Token::Period)) {
return reference;
}
tokens
.into_iter()
.map(|token| match token {
Token::Word(mut word) => {
if word.quote_style.is_some() && can_unquote(&word.value, database_type, identifier_quote) {
word.quote_style = None;
}
word.to_string()
}
_ => token.to_string(),
})
.collect()
}
fn can_unquote(name: &str, database_type: Option<DatabaseType>, identifier_quote: Option<&str>) -> bool {
let mut chars = name.chars();
let Some(first) = chars.next() else {
return false;
};
match database_type {
Some(DatabaseType::Oracle | DatabaseType::OceanbaseOracle) => {
first.is_ascii_uppercase()
&& chars.all(|ch| ch.is_ascii_uppercase() || ch.is_ascii_digit() || matches!(ch, '_' | '$' | '#'))
&& !oracle_reserved(name)
}
Some(DatabaseType::Dameng) => {
(first.is_ascii_uppercase() || first == '_')
&& chars.all(|ch| ch.is_ascii_uppercase() || ch.is_ascii_digit() || ch == '_')
&& !dameng_reserved(name)
}
Some(
DatabaseType::Postgres
| DatabaseType::Gaussdb
| DatabaseType::OpenGauss
| DatabaseType::Kingbase
| DatabaseType::Jdbc,
) if identifier_quote != Some("`") => {
quote_table_data_identifier(Some(DatabaseType::Postgres), name, Some("\"")) == name
}
_ => {
let simple =
|ch: char| ch.is_ascii_alphabetic() || matches!(ch, '_' | '$') || ('\u{80}'..='\u{ffff}').contains(&ch);
let lower = name.to_ascii_lowercase();
simple(first) && chars.all(|ch| simple(ch) || ch.is_ascii_digit()) && !mysql_reserved(&lower)
}
}
}
fn oracle_reserved(name: &str) -> bool {
static KEYWORDS: OnceLock<HashSet<&'static str>> = OnceLock::new();
KEYWORDS
.get_or_init(|| {
concat!(
"ACCESS ADD ALL ALTER AND ANY AS ASC AUDIT BETWEEN BY CHAR CHECK CLUSTER COLUMN COMMENT COMPRESS ",
"CONNECT CREATE CURRENT DATE DECIMAL DEFAULT DELETE DESC DISTINCT DROP ELSE EXCLUSIVE EXISTS FILE ",
"FLOAT FOR FROM GRANT GROUP HAVING IDENTIFIED IMMEDIATE IN INCREMENT INDEX INITIAL INSERT INTEGER ",
"INTERSECT INTO IS LEVEL LIKE LOCK LONG MAXEXTENTS MINUS MLSLABEL MODE MODIFY NOAUDIT NOCOMPRESS NOT ",
"NOWAIT NULL NUMBER OF OFFLINE ON ONLINE OPTION OR ORDER PCTFREE PRIOR PRIVILEGES PUBLIC RAW RENAME ",
"RESOURCE REVOKE ROW ROWID ROWNUM ROWS SELECT SESSION SET SHARE SIZE SMALLINT START SUCCESSFUL ",
"SYNONYM SYSDATE TABLE THEN TO TRIGGER UID UNION UNIQUE UPDATE USER VALIDATE VALUES VARCHAR VARCHAR2 ",
"VIEW WHENEVER WHERE WITH",
)
.split_ascii_whitespace()
.collect()
})
.contains(name)
}
fn dameng_reserved(name: &str) -> bool {
static KEYWORDS: OnceLock<HashSet<&'static str>> = OnceLock::new();
KEYWORDS
.get_or_init(|| {
concat!(
"ABSOLUTE ABSTRACT ADD ADMIN ALL ALTER AND ANY ARRAY ARRAYLEN AS ASC ASSIGN AUDIT AUTHORIZATION ",
"AUTO_INCREMENT BEGIN BETWEEN BIGDATEDIFF BINARY BOOL BOTH BREAK BSTRING BY BYTE CALL CASE CAST CATCH ",
"CHAR CHECK CLASS CLUSTER CLUSTERBTR COLLATION COLLECT COLUMN COMMENT COMMIT COMMITWORK CONNECT ",
"CONNECT_BY_ROOT CONST CONSTRAINT CONTAINS CONTEXT CONTINUE CONVERT CORRESPONDING CREATE CROSS CRYPTO ",
"CUBE CURRENT CURSOR DATEADD DATEDIFF DATEPART DECIMAL DECLARE DECODE DEFAULT DELETE DESC DISABLE ",
"DISKSPACE DISTINCT DISTRIBUTED DO DOMAIN DOUBLE DROP ELSE ELSEIF ELSIF ENABLE END EQU EXCHANGE EXEC ",
"EXECUTE EXISTS EXIT EXPLAIN EXTERN EXTRACT FETCH FINAL FINALLY FIRST FLASHBACK FLOAT FOR FOREIGN ",
"FROM FULL FULLY FUNCTION GET GOTO GRANT GROUP GROUPING HAVING IDENTITY IF IFNULL IMMEDIATE IN INDEX ",
"INLINE INNER INSERT INT INTERSECT INTERVAL INTO IS JOIN KEEP LEADING LEFT LEXER LIKE LIST LNNVL ",
"LOGIN LOOP MEMBER MINUS MULTISET NATURAL NEW NEXT NOCOPY NOCYCLE NOT NULL OBJECT OF ON OR ORDER OUT ",
"OVER OVERLAY OVERRIDE PARTITION PENDANT PERCENT PRIMARY PRINT PRIOR PRIVATE PRIVILEGES PROCEDURE ",
"PROTECTED PUBLIC RAISE RECORD REF REFERENCE REFERENCES REFERENCING RELATIVE REPEAT REPLICATE RETURN ",
"RETURNING REVERSE REVOKE RIGHT ROLLBACK ROLLUP ROW ROWNUM ROWS SAVEPOINT SBYTE SCHEMA SEALED SECTION ",
"SELECT SET SETS SHORT SIZEOF SOME STATIC STRUCT SUBPARTITION SWITCH SYNONYM TABLE THROW TIMESTAMPADD ",
"TIMESTAMPDIFF TO TOP TRAILING TRIGGER TRIM TRUNCATE TRY TYPEDEF TYPEOF UINT ULONG UNION UNIQUE UNTIL ",
"UPDATE USER USHORT USING VALUES VARRAY VERIFY VIEW VIRTUAL VOID WHEN WHENEVER WHERE WHILE WITH ",
"WITHIN XMLAGG XMLPARSE XMLTABLE",
)
.split_ascii_whitespace()
.collect()
})
.contains(name)
}
fn mysql_reserved(name: &str) -> bool {
static KEYWORDS: OnceLock<HashSet<&'static str>> = OnceLock::new();
KEYWORDS
.get_or_init(|| {
concat!(
"all analyse analyze and any array as asc asymmetric authorization binary both case cast check ",
"collate collation column concurrently constraint create cross current_catalog current_date ",
"current_role current_schema current_time current_timestamp current_user default deferrable desc ",
"distinct do else end except false fetch for foreign freeze from full grant group having ilike in ",
"initially inner intersect into is isnull join lateral leading left like limit localtime ",
"localtimestamp natural not notnull null offset on only or order outer overlaps placing primary ",
"references returning right select session_user similar some symmetric system_user table tablesample ",
"then to trailing true union unique user using variadic verbose when where window with accessible ",
"auto_increment change database databases delayed describe div dual enclosed escaped explain force ",
"fulltext high_priority ignore index infile key keys kill linear lines load lock low_priority ",
"master_ssl_verify_server_cert maxvalue mediumint mod no_write_to_binlog optimize optionally outfile ",
"partition purge range read_write regexp release rename replace require rlike schema schemas ",
"separator show spatial sql_big_result sql_calc_found_rows sql_small_result ssl starting ",
"straight_join terminated tinyint unlock unsigned use utc_date utc_time utc_timestamp values ",
"varbinary varchar write xor zerofill",
)
.split_ascii_whitespace()
.collect()
})
.contains(name)
}
@@ -22,6 +22,231 @@ fn request(extractor: DataGridExtractorId) -> DataGridExtractRequest {
} }
} }
fn temporal_insert_request() -> DataGridExtractRequest {
let mut native = request(DataGridExtractorId::SqlInserts);
native.database_type = Some(DatabaseType::Oracle);
native.columns = vec![column("CREATED_AT", 0), column("NOTE", 1), column("AMOUNT", 2)];
native.selected_column_indexes = vec![0, 1, 2];
native.rows = vec![vec![json!("2022-08-25T09:58:43Z"), json!("it's text"), json!(42)]];
native.table_meta = Some(DataGridTableMeta {
catalog: None,
database: None,
schema: Some("APP".into()),
table_name: "EVENTS".into(),
primary_keys: vec![],
columns: Some(
[("CREATED_AT", "DATE"), ("NOTE", "VARCHAR2"), ("AMOUNT", "NUMBER")]
.into_iter()
.map(|(name, data_type)| DataGridColumnInfo {
name: name.into(),
data_type: data_type.into(),
is_nullable: true,
is_primary_key: false,
column_default: None,
extra: None,
})
.collect(),
),
});
native
}
#[test]
fn sql_insert_temporal_format_contract_and_output() {
let native = temporal_insert_request();
assert_eq!(extract_data_grid_selection(native.clone()).unwrap().text,
"INSERT INTO \"APP\".\"EVENTS\" (\"CREATED_AT\", \"NOTE\", \"AMOUNT\") VALUES (TO_DATE('2022-08-25 09:58:43', 'YYYY-MM-DD HH24:MI:SS'), 'it''s text', 42);");
let mut payload = serde_json::to_value(&native).unwrap();
payload["options"]["sql"]["quoteIdentifiers"] = json!(false);
payload["options"]["sql"]["temporalFormat"] = json!("string");
let portable: DataGridExtractRequest = serde_json::from_value(payload).unwrap();
assert_eq!(
extract_data_grid_selection(portable).unwrap().text,
"INSERT INTO APP.EVENTS (CREATED_AT, NOTE, AMOUNT) VALUES ('2022-08-25 09:58:43', 'it''s text', 42);"
);
}
#[test]
fn sql_insert_portable_temporals_preserve_wall_time_and_escape_fallback_text() {
for (data_type, value, native_literal, portable_literal) in [
("DATE", "2022-08-25", "DATE '2022-08-25'", "'2022-08-25 00:00:00'"),
(
"TIMESTAMP(6)",
"2022-08-25 09:58:43.123456",
"TO_TIMESTAMP('2022-08-25 09:58:43.123456', 'YYYY-MM-DD HH24:MI:SS.FF')",
"'2022-08-25 09:58:43'",
),
(
"TIMESTAMP WITH TIME ZONE",
"2022-08-25T09:58:43.123456+08:00",
"TO_TIMESTAMP_TZ('2022-08-25 09:58:43.123456 +08:00', 'YYYY-MM-DD HH24:MI:SS.FF TZH:TZM')",
"'2022-08-25 09:58:43'",
),
(
"TIMESTAMP WITH LOCAL TIME ZONE",
"2022-08-25T09:58:43Z",
"TO_TIMESTAMP_TZ('2022-08-25 09:58:43 +00:00', 'YYYY-MM-DD HH24:MI:SS TZH:TZM')",
"'2022-08-25 09:58:43'",
),
("DATE", "not a date's value", "'not a date''s value'", "'not a date''s value'"),
("VARCHAR2", "2022-08-25T09:58:43Z", "'2022-08-25T09:58:43Z'", "'2022-08-25T09:58:43Z'"),
] {
for database_type in [DatabaseType::Oracle, DatabaseType::OceanbaseOracle] {
let mut request = temporal_insert_request();
request.database_type = Some(database_type);
request.rows[0][0] = json!(value);
request.table_meta.as_mut().unwrap().columns.as_mut().unwrap()[0].data_type = data_type.into();
let native = extract_data_grid_selection(request.clone()).unwrap().text;
request.options.sql.temporal_format = DataGridTemporalFormat::String;
let portable = extract_data_grid_selection(request).unwrap().text;
assert!(native.contains(&format!("VALUES ({native_literal}, 'it''s text', 42)")), "{native}");
assert!(portable.contains(&format!("VALUES ({portable_literal}, 'it''s text', 42)")), "{portable}");
}
}
for value in [Value::Null, json!(42), json!(true), json!("")] {
let mut request = temporal_insert_request();
request.rows[0][0] = value;
let native = extract_data_grid_selection(request.clone()).unwrap().text;
request.options.sql.temporal_format = DataGridTemporalFormat::String;
assert_eq!(extract_data_grid_selection(request).unwrap().text, native);
}
}
#[test]
fn sql_insert_quote_opt_out_keeps_required_delimiters_and_namespaces() {
for (database_type, quote, name, expected) in [
(DatabaseType::Oracle, None, "EVENTS", "EVENTS"),
(DatabaseType::Oracle, None, "Events", "\"Events\""),
(DatabaseType::Oracle, None, "ORDER", "\"ORDER\""),
(DatabaseType::Oracle, None, "A\"B", "\"A\"\"B\""),
(DatabaseType::Oracle, None, "EVENT LOG", "\"EVENT LOG\""),
(DatabaseType::Mysql, None, "events", "events"),
(DatabaseType::Mysql, None, "order", "`order`"),
(DatabaseType::Mysql, None, "a`b", "`a``b`"),
(DatabaseType::Postgres, None, "events", "events"),
(DatabaseType::Postgres, None, "Events", "\"Events\""),
(DatabaseType::SqlServer, None, "events", "events"),
(DatabaseType::SqlServer, None, "a]b", "[a]]b]"),
(DatabaseType::Dameng, None, "EVENTS", "EVENTS"),
(DatabaseType::Dameng, None, "ABSOLUTE", "\"ABSOLUTE\""),
(DatabaseType::Kingbase, Some("`"), "events", "events"),
(DatabaseType::Kingbase, Some("`"), "order", "`order`"),
(DatabaseType::Jdbc, Some("\""), "events", "events"),
(DatabaseType::Jdbc, Some("\""), "Events", "\"Events\""),
(DatabaseType::Spanner, Some("\""), "order", "\"order\""),
] {
for include_database_name in [true, false] {
let mut request = request(DataGridExtractorId::SqlInserts);
request.database_type = Some(database_type);
request.identifier_quote = quote.map(str::to_string);
request.columns = vec![column(name, 0)];
request.selected_column_indexes = vec![0];
request.rows = vec![vec![json!(1)]];
request.table_meta = Some(DataGridTableMeta {
catalog: None,
database: None,
schema: Some("APP".into()),
table_name: name.into(),
primary_keys: vec![],
columns: None,
});
request.options.sql.quote_identifiers = false;
request.options.sql.include_database_name = include_database_name;
let text = extract_data_grid_selection(request).unwrap().text;
assert!(text.contains(&format!("{expected} ({expected}) VALUES (1);")), "{database_type:?}: {text}");
if !include_database_name {
assert_eq!(text, format!("INSERT INTO {expected} ({expected}) VALUES (1);"));
}
}
}
}
#[test]
fn sql_insert_portable_options_keep_insert_modes_and_type_fallbacks() {
for insert_mode in
[crate::data_grid_sql::DataGridCopyInsertMode::Merged, crate::data_grid_sql::DataGridCopyInsertMode::RowByRow]
{
let mut request = temporal_insert_request();
request.database_type = Some(DatabaseType::Mysql);
request.rows.push(vec![Value::Null, json!("O'Reilly"), json!(0)]);
request.options.sql.quote_identifiers = false;
request.options.sql.temporal_format = DataGridTemporalFormat::String;
request.options.sql.insert_mode = insert_mode;
request.options.sql.include_database_name = false;
let result = extract_data_grid_selection(request).unwrap();
assert!(result.text.contains("('2022-08-25 09:58:43', 'it''s text', 42)"));
assert!(result.text.contains("(NULL, 'O''Reilly', 0)"));
assert_eq!(
result.text.matches("INSERT INTO EVENTS").count(),
if insert_mode == crate::data_grid_sql::DataGridCopyInsertMode::Merged { 1 } else { 2 }
);
}
let mut request = temporal_insert_request();
request.options.sql.temporal_format = DataGridTemporalFormat::String;
request.table_meta = None;
let text = extract_data_grid_selection(request).unwrap().text;
assert!(text.contains("'2022-08-25T09:58:43Z'"));
}
#[test]
fn sql_insert_string_format_preserves_binary_timestamps_and_numeric_epochs() {
for (database_type, data_type, value) in [
(DatabaseType::SqlServer, "timestamp", "0x00000000000007D3"),
(DatabaseType::Iotdb, "TIMESTAMP", "1747308643123456789"),
] {
let mut request = temporal_insert_request();
request.database_type = Some(database_type);
request.table_meta.as_mut().unwrap().columns.as_mut().unwrap()[0].data_type = data_type.into();
request.rows[0][0] = json!(value);
let native = extract_data_grid_selection(request.clone()).unwrap().text;
request.options.sql.temporal_format = DataGridTemporalFormat::String;
assert_eq!(extract_data_grid_selection(request).unwrap().text, native);
}
}
#[test]
fn sql_insert_policy_contract_rejects_unknown_modes_and_invalid_boolean_types() {
for invalid in [json!({"sql": {"temporalFormat": "custom"}}), json!({"sql": {"quoteIdentifiers": "false"}})] {
assert!(serde_json::from_value::<DataGridExtractorOptions>(invalid).is_err());
}
let options: DataGridExtractorOptions =
serde_json::from_value(json!({"sql": {"temporalFormat": "string", "quoteIdentifiers": false}})).unwrap();
let roundtrip: DataGridExtractorOptions = serde_json::from_value(serde_json::to_value(options).unwrap()).unwrap();
assert_eq!(roundtrip.sql.temporal_format, DataGridTemporalFormat::String);
assert!(!roundtrip.sql.quote_identifiers);
}
#[test]
fn insert_policies_do_not_change_other_extractors_or_column_errors() {
for extractor in [
DataGridExtractorId::Csv,
DataGridExtractorId::Tsv,
DataGridExtractorId::Json,
DataGridExtractorId::JsonLines,
DataGridExtractorId::Markdown,
DataGridExtractorId::Html,
DataGridExtractorId::Xml,
DataGridExtractorId::SqlUpdates,
DataGridExtractorId::SqlInList,
DataGridExtractorId::WhereClause,
] {
let mut request = temporal_insert_request();
request.extractor = extractor;
request.table_meta.as_mut().unwrap().primary_keys = vec!["AMOUNT".into()];
let native = extract_data_grid_selection(request.clone()).unwrap().text;
request.options.sql.quote_identifiers = false;
request.options.sql.temporal_format = DataGridTemporalFormat::String;
assert_eq!(extract_data_grid_selection(request).unwrap().text, native, "{extractor:?}");
}
let mut request = temporal_insert_request();
request.options.sql.quote_identifiers = false;
request.options.sql.temporal_format = DataGridTemporalFormat::String;
for info in request.table_meta.as_mut().unwrap().columns.as_mut().unwrap() {
info.extra = Some("VIRTUAL GENERATED".into());
}
assert_eq!(extract_data_grid_selection(request).unwrap_err().code, DataGridExtractErrorCode::NoWritableColumns);
}
#[test] #[test]
fn partially_deserialized_options_use_the_canonical_defaults() { fn partially_deserialized_options_use_the_canonical_defaults() {
let options = serde_json::from_value::<DataGridExtractorOptions>(json!({ let options = serde_json::from_value::<DataGridExtractorOptions>(json!({
@@ -43,6 +268,8 @@ fn partially_deserialized_options_use_the_canonical_defaults() {
assert_eq!(options.sql.insert_mode, crate::data_grid_sql::DataGridCopyInsertMode::Merged); assert_eq!(options.sql.insert_mode, crate::data_grid_sql::DataGridCopyInsertMode::Merged);
assert!(!options.sql.exclude_primary_keys_from_insert); assert!(!options.sql.exclude_primary_keys_from_insert);
assert!(options.sql.include_database_name); assert!(options.sql.include_database_name);
assert!(options.sql.quote_identifiers);
assert_eq!(options.sql.temporal_format, DataGridTemporalFormat::Native);
assert!(options.json.pretty); assert!(options.json.pretty);
assert!(!options.json.camel_case_field_names); assert!(!options.json.camel_case_field_names);
} }
+20 -3
View File
@@ -536,6 +536,18 @@ pub fn build_data_grid_copy_update_statements(options: DataGridCopyUpdateStateme
} }
pub fn build_data_grid_copy_insert_statement(options: DataGridCopyInsertStatementOptions) -> Option<String> { pub fn build_data_grid_copy_insert_statement(options: DataGridCopyInsertStatementOptions) -> Option<String> {
build_data_grid_copy_insert_statement_with_formatters(
options,
|reference| reference,
format_grid_copy_insert_sql_literal,
)
}
pub(crate) fn build_data_grid_copy_insert_statement_with_formatters(
options: DataGridCopyInsertStatementOptions,
format_reference: impl Fn(String) -> String,
format_literal: impl Fn(&Value, Option<DatabaseType>, Option<&DataGridColumnInfo>, Option<&str>) -> String,
) -> Option<String> {
let save_columns = effective_copy_columns(options.source_columns.as_deref(), &options.columns); let save_columns = effective_copy_columns(options.source_columns.as_deref(), &options.columns);
let column_info = options.table_meta.as_ref().and_then(|meta| meta.columns.as_deref()).unwrap_or(&[]); let column_info = options.table_meta.as_ref().and_then(|meta| meta.columns.as_deref()).unwrap_or(&[]);
let primary_key_set: Vec<String> = options let primary_key_set: Vec<String> = options
@@ -640,10 +652,15 @@ pub fn build_data_grid_copy_insert_statement(options: DataGridCopyInsertStatemen
) )
}, },
); );
let table = format_reference(table);
let columns = insert_columns let columns = insert_columns
.iter() .iter()
.map(|(_, index, _)| { .map(|(_, index, _)| {
data_grid_identifier(options.database_type, &options.columns[*index], options.identifier_quote.as_deref()) format_reference(data_grid_identifier(
options.database_type,
&options.columns[*index],
options.identifier_quote.as_deref(),
))
}) })
.collect::<Vec<_>>() .collect::<Vec<_>>()
.join(", "); .join(", ");
@@ -656,7 +673,7 @@ pub fn build_data_grid_copy_insert_statement(options: DataGridCopyInsertStatemen
insert_columns insert_columns
.iter() .iter()
.map(|(_, index, info)| { .map(|(_, index, info)| {
format_grid_copy_insert_sql_literal( format_literal(
row.get(*index).unwrap_or(&Value::Null), row.get(*index).unwrap_or(&Value::Null),
options.database_type, options.database_type,
info.as_ref(), info.as_ref(),
@@ -2392,7 +2409,7 @@ pub fn normalize_data_grid_save_error(database_type: Option<DatabaseType>, error
error.to_string() error.to_string()
} }
fn format_grid_copy_insert_sql_literal( pub(crate) fn format_grid_copy_insert_sql_literal(
value: &Value, value: &Value,
database_type: Option<DatabaseType>, database_type: Option<DatabaseType>,
column_info: Option<&DataGridColumnInfo>, column_info: Option<&DataGridColumnInfo>,