Files
MyGoNavi/internal/app/export_options.go
Syngnat 15fba1d89f feat(import-export): 优化数据表导入导出能力
- 修复表格格式导出时空值显示为 null 的问题
- 支持按列导出并保持字段顺序
- 支持导入列与数据库字段映射及冲突校验
- 隔离并发导入任务进度并完善元数据回退

Fixes #646
2026-07-18 15:17:54 +08:00

92 lines
3.1 KiB
Go

package app
import (
"errors"
"strings"
)
const (
maxXLSXRowsPerSheet = 1048575
defaultXLSXRowsPerSheet = maxXLSXRowsPerSheet
)
type ExportFileOptions struct {
Format string `json:"format"`
Columns []string `json:"columns,omitempty"`
XLSXMaxRowsPerSheet int `json:"xlsxMaxRowsPerSheet,omitempty"`
JobID string `json:"jobId,omitempty"`
TotalRowsHint int64 `json:"totalRowsHint,omitempty"`
TotalRowsKnown bool `json:"totalRowsKnown,omitempty"`
InsertSQLDialect string `json:"insertSQLDialect,omitempty"`
InsertSQLTargetTable string `json:"insertSQLTargetTable,omitempty"`
InsertSQLColumnTypes map[string]string `json:"insertSQLColumnTypes,omitempty"`
InsertSQLTargetColumns map[string]string `json:"insertSQLTargetColumns,omitempty"`
InsertSQLAllowEmptyTargetTable bool `json:"insertSQLAllowEmptyTargetTable,omitempty"`
}
func normalizeExportFileOptions(format string, options ExportFileOptions) ExportFileOptions {
resolvedFormat := strings.ToLower(strings.TrimSpace(format))
if explicitFormat := strings.ToLower(strings.TrimSpace(options.Format)); explicitFormat != "" {
resolvedFormat = explicitFormat
}
return ExportFileOptions{
Format: resolvedFormat,
Columns: normalizeExportColumns(options.Columns),
XLSXMaxRowsPerSheet: normalizeXLSXRowsPerSheet(options.XLSXMaxRowsPerSheet),
JobID: strings.TrimSpace(options.JobID),
TotalRowsHint: normalizeExportTotalRowsHint(options.TotalRowsHint, options.TotalRowsKnown),
TotalRowsKnown: options.TotalRowsKnown,
InsertSQLDialect: strings.ToLower(strings.TrimSpace(options.InsertSQLDialect)),
InsertSQLTargetTable: strings.TrimSpace(options.InsertSQLTargetTable),
InsertSQLColumnTypes: options.InsertSQLColumnTypes,
InsertSQLTargetColumns: options.InsertSQLTargetColumns,
InsertSQLAllowEmptyTargetTable: options.InsertSQLAllowEmptyTargetTable,
}
}
func normalizeExportColumns(columns []string) []string {
if columns == nil {
return nil
}
result := make([]string, 0, len(columns))
seen := make(map[string]struct{}, len(columns))
for _, column := range columns {
if strings.TrimSpace(column) == "" {
continue
}
if _, exists := seen[column]; exists {
continue
}
seen[column] = struct{}{}
result = append(result, column)
}
return result
}
func validateExportColumnsSelection(options ExportFileOptions) error {
if options.Columns != nil && len(options.Columns) == 0 {
return errors.New("at least one export column must be selected")
}
return nil
}
func normalizeXLSXRowsPerSheet(value int) int {
if value <= 0 {
return defaultXLSXRowsPerSheet
}
if value > maxXLSXRowsPerSheet {
return maxXLSXRowsPerSheet
}
return value
}
func normalizeExportTotalRowsHint(value int64, known bool) int64 {
if !known {
return 0
}
if value < 0 {
return 0
}
return value
}