Files
dbx/apps/desktop/src/lib/database/createDatabaseSql.ts
T

52 lines
2.1 KiB
TypeScript

import type { DatabaseType } from "@/types/database";
import * as api from "@/lib/backend/api";
const MYSQL_COMPATIBLE_PROFILES = new Set(["mysql", "mariadb", "tidb", "oceanbase", "doris", "starrocks", "custom_mysql"]);
const MYSQL_COMPATIBLE_TYPES = new Set<DatabaseType>(["mysql", "doris", "starrocks", "goldendb"]);
export interface CreateDatabaseSqlOptions {
databaseType?: DatabaseType;
driverProfile?: string | null;
target?: "database" | "schema" | "catalog" | "namespace";
parent?: string | null;
name: string;
charset?: string;
collation?: string;
}
export function supportsCreateDatabaseCharset(databaseType?: DatabaseType, driverProfile?: string | null): boolean {
return MYSQL_COMPATIBLE_TYPES.has(databaseType as DatabaseType) || (!!driverProfile && MYSQL_COMPATIBLE_PROFILES.has(driverProfile.toLowerCase()));
}
export function buildCreateDatabaseSql(options: CreateDatabaseSqlOptions): Promise<string> {
return api.buildCreateDatabaseSql(options);
}
export function buildDuckDbAttachDatabaseSql(path: string, name: string): Promise<string> {
return api.buildDuckDbAttachDatabaseSql(path, name);
}
export function buildSqliteAttachDatabaseSql(path: string, name: string): Promise<string> {
return api.buildSqliteAttachDatabaseSql(path, name);
}
export function attachedDatabaseNameFromPath(path: string, fallbackName = "database"): string {
const fileName = path.split(/[\\/]/).pop() ?? "";
const withoutExtension = fileName.replace(/\.[^.\\/]+$/, "");
const normalized = withoutExtension
.trim()
.replace(/[^\p{L}\p{N}_]+/gu, "_")
.replace(/^_+|_+$/g, "");
return normalized || fallbackName;
}
export function uniqueAttachedDatabaseName(baseName: string, existingNames: string[], reservedNames: string[] = []): string {
const existing = new Set([...existingNames, ...reservedNames].map((name) => name.toLowerCase()));
if (!existing.has(baseName.toLowerCase())) return baseName;
for (let index = 2; index < Number.MAX_SAFE_INTEGER; index++) {
const candidate = `${baseName}_${index}`;
if (!existing.has(candidate.toLowerCase())) return candidate;
}
return `${baseName}_${Date.now()}`;
}