feat(import): add SQL script import with dialect-aware parsing
This commit is contained in:
1 parent
ab4e747bf9
commit
dfd4d3b7ff
15 files changed
+809
-12
No files matched your search
@@ -115,6 +115,26 @@ describe("tableImport", () => {
|
||||
expect(buildTableImportParseOptions({ ...baseSettings, format: "csv", sheetName: "Second" }).sheetName).toBeNull();
|
||||
});
|
||||
|
||||
it("passes the text encoding for SQL script sources like delimited text", () => {
|
||||
const settings = {
|
||||
delimiter: ",",
|
||||
textEncoding: "gbk" as const,
|
||||
titleRow: 1,
|
||||
dataStartRow: 2,
|
||||
lastDataRow: 0,
|
||||
trimValues: false,
|
||||
emptyStringAsNull: true,
|
||||
jsonShape: "auto" as const,
|
||||
databaseType: "mysql" as const,
|
||||
};
|
||||
|
||||
expect(buildTableImportParseOptions({ ...settings, format: "sql" }).encoding).toBe("gbk");
|
||||
expect(buildTableImportParseOptions({ ...settings, format: "sql" }).sqlDialect).toBe("mysql");
|
||||
expect(buildTableImportParseOptions({ ...settings, format: "delimited" }).encoding).toBe("gbk");
|
||||
expect(buildTableImportParseOptions({ ...settings, format: "delimited" }).sqlDialect).toBeNull();
|
||||
expect(buildTableImportParseOptions({ ...settings, format: "excel" }).encoding).toBeNull();
|
||||
});
|
||||
|
||||
it("suggests create-table data types from preview rows", () => {
|
||||
expect(
|
||||
suggestImportTargetDataTypes(
|
||||
|
||||
@@ -4477,7 +4477,7 @@ export async function sortTablesByFkDependency(options: SortTablesByFkOptions):
|
||||
export type TableImportMode = "append" | "truncate";
|
||||
export type TableImportStatus = "running" | "done" | "error" | "cancelled";
|
||||
export type TableImportPhase = "preparing" | "detectingEncoding" | "reading" | "writing" | "finalizing" | "done";
|
||||
export type TableImportSourceFormat = "csv" | "tsv" | "delimited" | "json" | "excel";
|
||||
export type TableImportSourceFormat = "csv" | "tsv" | "delimited" | "json" | "excel" | "sql";
|
||||
export type TableImportJsonShape = "auto" | "objects" | "arrays";
|
||||
export type TableImportTextEncoding = "auto" | "utf8" | "gbk" | "utf16Le" | "utf16Be";
|
||||
|
||||
@@ -4499,6 +4499,7 @@ export interface TableImportParseOptions {
|
||||
sheetName?: string | null;
|
||||
sheetIndex?: number | null;
|
||||
jsonShape?: TableImportJsonShape | null;
|
||||
sqlDialect?: DatabaseType | null;
|
||||
}
|
||||
|
||||
export interface TableImportPreviewRequest {
|
||||
|
||||
@@ -86,6 +86,7 @@ export interface TableImportParseSettings {
|
||||
emptyStringAsNull: boolean;
|
||||
sheetName?: string;
|
||||
jsonShape: TableImportJsonShape;
|
||||
databaseType?: DatabaseType | null;
|
||||
}
|
||||
|
||||
export function defaultTableImportEmptyStringAsNull(format: TableImportSourceFormat): boolean {
|
||||
@@ -94,9 +95,11 @@ export function defaultTableImportEmptyStringAsNull(format: TableImportSourceFor
|
||||
|
||||
export function buildTableImportParseOptions(settings: TableImportParseSettings): TableImportParseOptions {
|
||||
const isDelimited = settings.format === "csv" || settings.format === "tsv" || settings.format === "delimited";
|
||||
// SQL 脚本与分隔文本一样是纯文本源,需要传递编码供后端解码(支持 GBK 等常见转储编码)
|
||||
const isTextSource = isDelimited || settings.format === "sql";
|
||||
return {
|
||||
delimiter: settings.format === "tsv" ? "\\t" : settings.format === "csv" ? "," : settings.delimiter,
|
||||
encoding: isDelimited ? settings.textEncoding : null,
|
||||
encoding: isTextSource ? settings.textEncoding : null,
|
||||
titleRow: settings.titleRow,
|
||||
dataStartRow: settings.dataStartRow,
|
||||
lastDataRow: settings.lastDataRow,
|
||||
@@ -104,6 +107,8 @@ export function buildTableImportParseOptions(settings: TableImportParseSettings)
|
||||
emptyStringAsNull: settings.emptyStringAsNull,
|
||||
sheetName: settings.format === "excel" ? settings.sheetName || null : null,
|
||||
jsonShape: settings.format === "json" ? settings.jsonShape : null,
|
||||
// SQL 脚本需要按源方言解析字符串转义与标识符大小写(取目标连接的数据库类型)
|
||||
sqlDialect: settings.format === "sql" ? (settings.databaseType ?? null) : null,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user