feat(types): add database type help

This commit is contained in:
二丫讲梵 authored and GitHub committed 2026-07-12 13:28:46 +08:00
1 parent fdb33ef477
commit 4f1997d8eb
30 files changed
+2006 -79

No files matched your search

@@ -0,0 +1,199 @@
export type MysqlDataTypeHelpKey =
| "tinyint"
| "smallint"
| "mediumint"
| "int"
| "bigint"
| "decimal"
| "float"
| "double"
| "real"
| "bit"
| "boolean"
| "serial"
| "date"
| "datetime"
| "timestamp"
| "time"
| "year"
| "char"
| "varchar"
| "tinytext"
| "text"
| "mediumtext"
| "longtext"
| "binary"
| "varbinary"
| "tinyblob"
| "blob"
| "mediumblob"
| "longblob"
| "enum"
| "set"
| "json"
| "jsonMysql"
| "jsonMariaDb"
| "nationalCharacter"
| "geometry"
| "point"
| "linestring"
| "polygon"
| "multipoint"
| "multilinestring"
| "multipolygon"
| "geometrycollection"
| "inet4"
| "inet6"
| "uuid"
| "vector"
| "xmltype"
| "integerDisplayWidth"
| "zerofill"
| "unsignedNumeric"
| "floatPrecision"
| "floatingPointDisplay";
export interface MysqlDataTypeHelp {
key: MysqlDataTypeHelpKey;
warningKeys?: MysqlDataTypeHelpKey[];
}
export interface MysqlDataTypeHelpOptions {
/** Set only when the selected connection profile identifies the server product. */
product?: "mysql" | "mariadb";
}
const MYSQL_TYPE_HELP_KEYS: Readonly<Record<string, MysqlDataTypeHelpKey>> = {
tinyint: "tinyint",
smallint: "smallint",
mediumint: "mediumint",
int: "int",
bigint: "bigint",
decimal: "decimal",
float: "float",
double: "double",
real: "real",
bit: "bit",
boolean: "boolean",
nationalCharacter: "nationalCharacter",
serial: "serial",
date: "date",
datetime: "datetime",
timestamp: "timestamp",
time: "time",
year: "year",
char: "char",
varchar: "varchar",
tinytext: "tinytext",
text: "text",
mediumtext: "mediumtext",
longtext: "longtext",
binary: "binary",
varbinary: "varbinary",
tinyblob: "tinyblob",
blob: "blob",
mediumblob: "mediumblob",
longblob: "longblob",
enum: "enum",
set: "set",
json: "json",
geometry: "geometry",
point: "point",
linestring: "linestring",
polygon: "polygon",
multipoint: "multipoint",
multilinestring: "multilinestring",
multipolygon: "multipolygon",
geometrycollection: "geometrycollection",
inet4: "inet4",
inet6: "inet6",
uuid: "uuid",
vector: "vector",
xmltype: "xmltype",
};
const MYSQL_TYPE_ALIASES: Readonly<Record<string, string>> = {
integer: "int",
numeric: "decimal",
dec: "decimal",
fixed: "decimal",
bool: "boolean",
"character varying": "varchar",
character: "char",
"char varying": "varchar",
"char byte": "char",
nchar: "nationalCharacter",
nvarchar: "nationalCharacter",
"nchar varying": "nationalCharacter",
"national char": "nationalCharacter",
"national character": "nationalCharacter",
"national varchar": "nationalCharacter",
"national char varying": "nationalCharacter",
"national character varying": "nationalCharacter",
"double precision": "double",
float4: "float",
float8: "double",
int1: "tinyint",
int2: "smallint",
int3: "mediumint",
int4: "int",
int8: "bigint",
middleint: "mediumint",
long: "mediumtext",
"long varchar": "mediumtext",
"long varbinary": "mediumblob",
geomcollection: "geometrycollection",
};
/**
* Returns help for a MySQL-family type name without making a database request.
* The caller is responsible for limiting this to connections that use MySQL
* semantics, since several compatible databases accept similarly named types
* with different limits.
*/
export function getMysqlDataTypeHelp(rawType: string, options: MysqlDataTypeHelpOptions = {}): MysqlDataTypeHelp | undefined {
const normalized = normalizeMysqlType(rawType);
const resolvedType = MYSQL_TYPE_ALIASES[normalized] ?? normalized;
const baseKey = MYSQL_TYPE_HELP_KEYS[resolvedType];
if (!baseKey) return undefined;
const key = resolvedType === "json" ? jsonHelpKey(options.product) : baseKey;
const warningKeys = mysqlDataTypeWarningKeys(rawType, resolvedType);
return warningKeys.length > 0 ? { key, warningKeys } : { key };
}
function jsonHelpKey(product: MysqlDataTypeHelpOptions["product"]): MysqlDataTypeHelpKey {
if (product === "mysql") return "jsonMysql";
if (product === "mariadb") return "jsonMariaDb";
return "json";
}
function mysqlDataTypeWarningKeys(rawType: string, resolvedType: string): MysqlDataTypeHelpKey[] {
const lowered = rawType.trim().toLowerCase().replace(/\s+/g, " ");
const warnings: MysqlDataTypeHelpKey[] = [];
const isInteger = ["tinyint", "smallint", "mediumint", "int", "bigint"].includes(resolvedType);
const isNumeric = isInteger || ["decimal", "float", "double", "real"].includes(resolvedType);
if (isInteger && /\(\s*\d+\s*\)/.test(lowered)) warnings.push("integerDisplayWidth");
if (isNumeric && /\bzerofill\b/.test(lowered)) warnings.push("zerofill");
if (["decimal", "float", "double", "real"].includes(resolvedType) && /\bunsigned\b/.test(lowered)) warnings.push("unsignedNumeric");
const parameters = lowered.match(/\(([^)]*)\)/)?.[1]?.trim() ?? "";
if (resolvedType === "float" && /^\d+$/.test(parameters)) {
const precision = Number(parameters);
if (precision >= 25 && precision <= 53) warnings.push("floatPrecision");
}
if (["float", "double"].includes(resolvedType) && /^\d+\s*,\s*\d+$/.test(parameters)) warnings.push("floatingPointDisplay");
return warnings;
}
function normalizeMysqlType(rawType: string): string {
return rawType
.trim()
.toLowerCase()
.replace(/\([^)]*\)/g, " ")
.replace(/\b(?:unsigned|signed|zerofill)\b/g, " ")
.replace(/\s+/g, " ")
.trim();
}
@@ -0,0 +1,155 @@
export type PostgresDataTypeHelpKey =
| "smallint"
| "integer"
| "bigint"
| "serial"
| "numeric"
| "real"
| "double"
| "money"
| "boolean"
| "char"
| "varchar"
| "text"
| "bytea"
| "date"
| "time"
| "timetz"
| "timestamp"
| "timestamptz"
| "interval"
| "uuid"
| "json"
| "jsonb"
| "xml"
| "bit"
| "tsvector"
| "tsquery"
| "cidr"
| "inet"
| "macaddr"
| "geometry"
| "range"
| "oid"
| "array"
| "multirange"
| "jsonpath"
| "pgLsn"
| "pgSnapshot";
export interface PostgresDataTypeHelp {
key: PostgresDataTypeHelpKey;
}
const POSTGRES_TYPE_HELP_KEYS: Readonly<Record<string, PostgresDataTypeHelpKey>> = {
smallint: "smallint",
integer: "integer",
bigint: "bigint",
smallserial: "serial",
serial: "serial",
bigserial: "serial",
numeric: "numeric",
real: "real",
"double precision": "double",
money: "money",
boolean: "boolean",
char: "char",
varchar: "varchar",
text: "text",
bytea: "bytea",
date: "date",
time: "time",
timetz: "timetz",
timestamp: "timestamp",
timestamptz: "timestamptz",
interval: "interval",
uuid: "uuid",
json: "json",
jsonb: "jsonb",
xml: "xml",
bit: "bit",
"bit varying": "bit",
tsvector: "tsvector",
tsquery: "tsquery",
cidr: "cidr",
inet: "inet",
macaddr: "macaddr",
macaddr8: "macaddr",
point: "geometry",
line: "geometry",
lseg: "geometry",
box: "geometry",
path: "geometry",
polygon: "geometry",
circle: "geometry",
int4range: "range",
int8range: "range",
numrange: "range",
tsrange: "range",
tstzrange: "range",
daterange: "range",
oid: "oid",
jsonpath: "jsonpath",
pg_lsn: "pgLsn",
pg_snapshot: "pgSnapshot",
};
const POSTGRES_TYPE_ALIASES: Readonly<Record<string, string>> = {
int2: "smallint",
int: "integer",
int4: "integer",
int8: "bigint",
decimal: "numeric",
float4: "real",
float8: "double precision",
bool: "boolean",
character: "char",
"character varying": "varchar",
"time without time zone": "time",
"time with time zone": "timetz",
"timestamp without time zone": "timestamp",
"timestamp with time zone": "timestamptz",
varbit: "bit varying",
};
const POSTGRES_MULTIRANGE_TYPES = new Set(["int4multirange", "int8multirange", "nummultirange", "tsmultirange", "tstzmultirange", "datemultirange"]);
/**
* Returns static PostgreSQL type help without querying the server. Extension
* types and user-defined types intentionally remain undocumented.
*/
export function getPostgresDataTypeHelp(rawType: string): PostgresDataTypeHelp | undefined {
const normalized = normalizePostgresType(rawType);
if (isPostgresArrayType(normalized)) return { key: "array" };
const baseType = normalized
.replace(/\([^)]*\)/g, " ")
.replace(/\s+/g, " ")
.trim();
if (baseType === "float") {
const key = floatHelpKey(normalized);
return key ? { key } : undefined;
}
if (POSTGRES_MULTIRANGE_TYPES.has(baseType)) return { key: "multirange" };
const canonicalType = POSTGRES_TYPE_ALIASES[baseType] ?? baseType;
const key = POSTGRES_TYPE_HELP_KEYS[canonicalType];
return key ? { key } : undefined;
}
function normalizePostgresType(rawType: string): string {
return rawType.trim().toLowerCase().replace(/\s+/g, " ");
}
function isPostgresArrayType(normalizedType: string): boolean {
return /(?:\[\s*\]\s*)+$/.test(normalizedType);
}
function floatHelpKey(normalizedType: string): "real" | "double" | undefined {
if (normalizedType === "float") return "double";
const precision = normalizedType.match(/^float\(\s*(\d+)\s*\)$/)?.[1];
if (!precision) return undefined;
const value = Number(precision);
if (value >= 1 && value <= 24) return "real";
return value >= 25 && value <= 53 ? "double" : undefined;
}
@@ -0,0 +1,24 @@
export type SqliteDataTypeHelpKey = "integer" | "real" | "text" | "blob" | "numeric";
export interface SqliteDataTypeHelp {
key: SqliteDataTypeHelpKey;
}
/**
* Maps declared types using SQLite's ordered affinity rules. The order matters:
* for example, FLOATING POINT has INTEGER affinity because it contains "INT".
*/
export function getSqliteDataTypeHelp(rawType: string): SqliteDataTypeHelp | undefined {
const normalized = rawType
.trim()
.toLowerCase()
.replace(/\([^)]*\)/g, " ")
.replace(/\s+/g, " ")
.trim();
if (!normalized) return { key: "blob" };
if (normalized.includes("int")) return { key: "integer" };
if (normalized.includes("char") || normalized.includes("clob") || normalized.includes("text")) return { key: "text" };
if (normalized.includes("blob")) return { key: "blob" };
if (normalized.includes("real") || normalized.includes("floa") || normalized.includes("doub")) return { key: "real" };
return { key: "numeric" };
}
@@ -965,8 +965,17 @@ function isValidTemporalPrecision(dbType: DatabaseType | undefined, params: stri
return Number.isInteger(value) && value >= 0 && value <= max && String(value) === params;
}
export function getDefaultLengthForType(_dbType: DatabaseType | undefined, baseType: string): string {
export interface DataTypeDefaultOptions {
/**
* Native MySQL profiles use MySQL 8-safe defaults. Compatibility profiles
* retain their existing DDL defaults because their server/version is unknown.
*/
omitMysqlDeprecatedDefaults?: boolean;
}
export function getDefaultLengthForType(_dbType: DatabaseType | undefined, baseType: string, options: DataTypeDefaultOptions = {}): string {
const key = baseType.trim().toLowerCase();
if (_dbType === "mysql" && options.omitMysqlDeprecatedDefaults && isMysqlDeprecatedDefaultParameterType(key)) return "";
if (_dbType === "sqlite" || _dbType === "rqlite" || _dbType === "turso") {
return "";
} else if (_dbType === "questdb") {
@@ -978,6 +987,20 @@ export function getDefaultLengthForType(_dbType: DatabaseType | undefined, baseT
}
}
/** Default data type for a newly added structure-editor column. */
export function defaultNewColumnDataType(dbType: DatabaseType | undefined, dataTypeOptions: readonly string[] = []): string {
if (dbType === "manticoresearch") {
const baseType = dataTypeOptions[0] ?? "text";
return combineDataTypeForDatabase(dbType, baseType, getDefaultLengthForType(dbType, baseType));
}
return dbType === "sqlite" ? "text" : "varchar(255)";
}
function isMysqlDeprecatedDefaultParameterType(baseType: string): boolean {
const typeName = baseType.split(/\s+/)[0];
return ["tinyint", "smallint", "mediumint", "int", "integer", "bigint", "float", "double", "real"].includes(typeName ?? "");
}
export function isDataTypeLengthDisabled(_dbType: DatabaseType | undefined, baseType: string): boolean {
const key = baseType.trim().toLowerCase();
if (_dbType === "questdb") {