feat(data-generate): prefer column default values

This commit is contained in:
t8y2 committed 2026-07-05 00:49:13 +08:00
1 parent 67a1843faf
commit 91855de832
3 files changed
+264 -14

No files matched your search

@@ -4,7 +4,7 @@ import { useI18n } from "vue-i18n";
import { useConnectionStore } from "@/stores/connectionStore";
import * as api from "@/lib/backend/api";
import type { TableGenerateConfig } from "@/lib/dataGrid/dataGenerate";
import { findGeneratorKey, generateTableData, defaultGeneratorParams } from "@/lib/dataGrid/dataGenerate";
import { displayGeneratedValue, findGeneratorKey, formatGeneratedValue, generateTableData, defaultGeneratorParams } from "@/lib/dataGrid/dataGenerate";
import { quoteTableIdentifier } from "@/lib/table/tableSelectSql";
import { isTauriRuntime } from "@/lib/backend/tauriRuntime";
import { effectiveDatabaseTypeForConnection } from "@/lib/database/jdbcDialect";
@@ -113,7 +113,7 @@ async function loadSchemas() {
database: db,
rowCount: 1000,
columns: cols.map((c: ColumnInfo) => {
const isAI = c.extra === "auto_increment" || (c.column_default?.includes("nextval") ?? false);
const isAI = c.extra === "auto_increment" || (c.column_default?.toLowerCase().includes("nextval") ?? false);
const gKey = findGeneratorKey(c.name, c.data_type, isAI);
return {
columnName: c.name,
@@ -133,6 +133,7 @@ async function loadSchemas() {
gKey,
),
isAutoIncrement: isAI,
columnDefault: c.column_default,
};
}),
};
@@ -205,7 +206,7 @@ async function loadColumns(schema: string, table: string) {
database: props.prefillDatabase,
rowCount: 1000,
columns: cols.map((c: ColumnInfo) => {
const isAI = c.extra === "auto_increment" || (c.column_default?.includes("nextval") ?? false);
const isAI = c.extra === "auto_increment" || (c.column_default?.toLowerCase().includes("nextval") ?? false);
const gKey = findGeneratorKey(c.name, c.data_type, isAI);
return {
columnName: c.name,
@@ -225,6 +226,7 @@ async function loadColumns(schema: string, table: string) {
gKey,
),
isAutoIncrement: isAI,
columnDefault: c.column_default,
};
}),
};
@@ -324,6 +326,10 @@ function onPreviewColResizeStart(ci: number, event: MouseEvent) {
}
const currentPreview = computed(() => generatedResults.value[previewTableIndex.value] ?? { tableName: "", columns: [], rows: [], sql: "" });
function displayPreviewCell(cell: unknown): string {
return displayGeneratedValue(cell);
}
async function fetchMaxValues(cfg: TableGenerateConfig): Promise<Record<string, number>> {
const starts: Record<string, number> = {};
const aiCols = cfg.columns.filter((c) => {
@@ -428,13 +434,7 @@ function sqlStatementsForTable(r: { tableName: string; columns: string[]; rows:
} else {
const colList = r.columns.map((c) => quoteTableIdentifier(dbType.value, c)).join(", ");
for (const row of r.rows) {
const vals = row
.map((v) => {
if (v === null || v === undefined) return "NULL";
if (typeof v === "number") return String(v);
return `'${String(v).replace(/'/g, "''")}'`;
})
.join(", ");
const vals = row.map(formatGeneratedValue).join(", ");
stmts.push(`INSERT INTO ${quoteTableIdentifier(dbType.value, r.tableName)} (${colList}) VALUES (${vals});`);
}
}
@@ -868,7 +868,7 @@ async function onFileSelected(event: Event) {
:style="{ maxWidth: (previewColWidths[ci] ?? 120) + 'px' }"
:class="{ 'text-muted-foreground italic': cell === null || cell === undefined }"
>
{{ cell === null || cell === undefined ? "NULL" : String(cell) }}
{{ displayPreviewCell(cell) }}
</td>
</tr>
</tbody>