Skip to content

armgit / dbx

Public Imported from GitHub
0

25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。

9.6 KB
Raw Blame
import type { ColumnInfo, DatabaseType, ForeignKeyInfo, IndexInfo, TriggerInfo } from "@/types/database.ts";

export interface ColumnIdentity {
  generation?: "BY DEFAULT" | "ALWAYS";
  seed?: number;
  increment?: number;
}

export interface ColumnGenerated {
  /** Generation expression without the surrounding parentheses; empty means "not generated". */
  expression: string;
  storage?: "VIRTUAL" | "STORED";
}

export interface ColumnExtra {
  autoIncrement?: boolean;
  onUpdateCurrentTimestamp?: boolean;
  identity?: ColumnIdentity;
  generated?: ColumnGenerated;
  manticoreIndexed?: boolean;
  manticoreStored?: boolean;
  manticoreAttribute?: boolean;
  manticoreSecondaryIndex?: boolean;
}

export interface EditableStructureColumn {
  id: string;
  name: string;
  dataType: string;
  enumValues?: string[];
  isNullable: boolean;
  defaultValue: string;
  comment: string;
  isPrimaryKey: boolean;
  extra: ColumnExtra;
  characterSet?: string;
  collation?: string;
  original?: ColumnInfo;
  originalPosition?: number;
  markedForDrop: boolean;
}

export interface EditableStructureIndex {
  id: string;
  name: string;
  columns: string[];
  nameEdited?: boolean;
  /** First selected field, retained when a composite index's members change. */
  autoNameColumn?: string;
  isUnique: boolean;
  isPrimary: boolean;
  filter: string;
  indexType: string;
  includedColumns: string[];
  comment: string;
  /** Build the index with PostgreSQL `CREATE INDEX CONCURRENTLY` (PostgreSQL only, default off). */
  concurrently?: boolean;
  /** Parallel to `columns`: operator class for each key column (PostgreSQL). `null` means default. */
  columnOpclasses?: (string | null)[];
  original?: IndexInfo;
  /** Oracle-family recovery only: the original definition remains the metadata source, but its DROP already committed. */
  oracleOriginalMissing?: boolean;
  markedForDrop: boolean;
}

export interface EditableStructureForeignKey {
  id: string;
  name: string;
  column: string;
  refSchema: string;
  refTable: string;
  refColumn: string;
  /** Oracle-family pairs are never reconstructed from the display strings. */
  columns?: string[];
  refColumns?: string[];
  oracleState?: OracleForeignKeyState;
  oracleOriginal?: OracleForeignKeyDefinition;
  onUpdate: string;
  onDelete: string;
  original?: ForeignKeyInfo;
  markedForDrop: boolean;
}

export interface OracleForeignKeyState {
  enabled: boolean;
  validated: boolean;
  deferrable: boolean;
  initiallyDeferred: boolean;
  rely: boolean;
}

export interface OracleForeignKeyDefinition {
  name: string;
  columns: string[];
  refSchema: string;
  refTable: string;
  refColumns: string[];
  onDelete: "NO ACTION" | "CASCADE" | "SET NULL";
  state: OracleForeignKeyState;
}

export interface OracleForeignKeyChange {
  original: OracleForeignKeyDefinition | null;
  target: OracleForeignKeyDefinition | null;
}

export type OracleConstraintChange = {
  original: OracleConstraintDefinition | null;
  target: OracleConstraintDefinition | null;
  indexPolicy?: "keep" | "drop" | "none";
};

export type OracleConstraintDefinition = { kind: "check"; definition: OracleCheckConstraint } | { kind: "unique"; definition: OracleUniqueConstraint };

export interface OracleCheckConstraint {
  name: string;
  expression: string;
  state: OracleForeignKeyState;
}

export interface OracleUniqueConstraint {
  name: string;
  columns: string[];
  state: OracleForeignKeyState;
  /** Exact source dictionary value: NULL, RELY, or NORELY. */
  relyRaw: string;
  index: {
    owner: string;
    name: string;
    tableOwner: string;
    tableName: string;
    sharing: string;
    metadataComplete: boolean;
    indexType: string;
    uniqueness: string;
    status: string;
    partitioned: boolean;
    visibility: string;
    secondary: string | null;
    columns: string[];
    directions: string[];
    keyIsExpression: boolean[];
    includedColumns: string[];
    newNameAvailable: boolean;
    newName?: string;
  };
}

export interface EditableStructureTrigger {
  id: string;
  name: string;
  timing: string;
  event: string;
  statement: string;
  enabled?: boolean;
  /** Explicit exact trigger workflow; omitted callers keep the existing SQL contract. */
  oracleExact?: { owner: string; parentType: "TABLE" | "VIEW" };
  /** Full authoritative source captured before a structured edit; retained with a failed draft. */
  loadedOracleSource?: string;
  loadedOracleStructured?: boolean;
  original?: TriggerInfo;
  markedForDrop: boolean;
}

export interface BuildTableStructureChangeSqlOptions {
  databaseType?: DatabaseType;
  /** Driver profile reported by the connection (e.g. `"gbase8s"`). GBase 8s is
   * Informix-compatible rather than MySQL-compatible like the rest of the
   * `Gbase` family, so this disambiguates which dialect the backend generates. */
  driverProfile?: string | null;
  schema?: string;
  tableName: string;
  columns: EditableStructureColumn[];
  indexes: EditableStructureIndex[];
  foreignKeys?: EditableStructureForeignKey[];
  triggers?: EditableStructureTrigger[];
  tableComment?: string;
  originalTableComment?: string;
  mysqlEngine?: string;
  mysqlAutoIncrementValue?: string;
  transwarpCreate?: TranswarpCreateTableOptions;
  /** MySQL only: the table's current default collation. Columns whose collation merely
   * matches it inherit the table default, so the backend leaves their redundant
   * `CHARACTER SET`/`COLLATE` clauses out of the generated DDL. */
  tableCollation?: string;
  /** The target table is a PostgreSQL partitioned parent (`relkind = 'p'`);
   * the backend rejects `CREATE INDEX CONCURRENTLY` on such tables (fail
   * closed) instead of downgrading to a blocking `CREATE INDEX`. */
  partitioned?: boolean;
  /** The target table is a PostgreSQL foreign table (`relkind = 'f'`);
   * PostgreSQL rejects `COMMENT ON TABLE` for these, so the backend must
   * generate `COMMENT ON FOREIGN TABLE`. */
  foreignTable?: boolean;
  /** When true, the connection is GaussDB M-mode which uses MySQL-compatible
   * SQL dialect with backtick quoting. */
  isGaussdbMMode?: boolean;
  /** Optional constraint name for a newly created primary key. Empty or
   * omitted keeps the anonymous `PRIMARY KEY (...)` form and lets the server
   * auto-name the constraint. Ignored when the draft only replaces an
   * existing primary key (renaming is out of scope). */
  primaryKeyName?: string;
  oracleForeignKeyChanges?: OracleForeignKeyChange[];
  oracleConstraintChanges?: OracleConstraintChange[];
  /** Ordered change to an existing Oracle-family primary key; an empty target removes it. */
  primaryKeyChange?: {
    name: string;
    originalColumns: string[];
    columns: string[];
    oracleIndex?: { policy: "keep" | "drop" | "none"; owner: string; name: string; newName?: string };
    oracleState?: { enabled: boolean; validated: boolean; deferrable: boolean; initiallyDeferred: boolean; rely: boolean };
  };
}

/** Change only an existing MySQL table's defaults; existing columns are left untouched. */
export interface MysqlTableCharsetChange {
  charset: string;
  collation?: string;
}

export interface TranswarpCreateTableOptions {
  partitionColumns: string[];
  bucketColumns: string[];
  bucketCount?: number;
  storageFormat?: string;
  transactional: boolean;
}

export interface TableStructureChangeSql {
  statements: string[];
  warnings: string[];
  triggerOperations?: OracleTriggerStructureOperation[];
  oracleTriggerContract?: 1;
}

export interface OracleTriggerStructureOperation {
  draftId: string;
  action: "create" | "replace" | "delete" | "enable" | "disable";
  owner: string;
  name: string;
  statementIndex: number;
  statementCount: number;
  enabled?: boolean;
}

/** Explicit partition maintenance operations (PostgreSQL only). */
export type TablePartitionOperationKind = "create" | "attach" | "detach" | "drop";

/** Partition bound as entered in the UI; values are SQL literal text. */
export type TablePartitionBoundDraft = { kind: "range"; from: string[]; to: string[] } | { kind: "list"; values: string[] } | { kind: "hash"; modulus: number; remainder: number } | { kind: "default" };

export interface TablePartitionOperation {
  id: string;
  kind: TablePartitionOperationKind;
  /** Partitioned parent's schema; empty falls back to the edited table's schema. */
  parentSchema: string;
  /** Partitioned parent; empty falls back to the edited table. */
  parentTable: string;
  /** Schema of the partition relation; empty falls back to the parent's schema. */
  schema: string;
  name: string;
  /** Required for `create`/`attach`; must be absent for `detach`/`drop`. */
  bound?: TablePartitionBoundDraft;
  /** Emit `DETACH PARTITION ... CONCURRENTLY` (PostgreSQL 14+). */
  concurrently: boolean;
}

export interface TablePartitionSqlOptions {
  databaseType?: DatabaseType;
  driverProfile?: string | null;
  schema?: string;
  tableName: string;
  operations: TablePartitionOperation[];
}

/** Declarative partitioning for a table being created. */
export interface TablePartitionDefinition {
  kind: "range" | "list" | "hash";
  columns: string[];
  expression: string;
}

export interface BuildCreatePartitionedTableSqlOptions {
  options: BuildTableStructureChangeSqlOptions;
  partitioning: TablePartitionDefinition;
}

export interface BuildTableOwnerChangeSqlOptions {
  databaseType?: DatabaseType;
  schema?: string;
  tableName: string;
  owner: string;
  originalOwner: string;
}

export interface SqliteTableStructureChangePreview extends TableStructureChangeSql {
  schemaRevision: string;
}

export interface BuildSingleColumnAlterSqlOptions {
  databaseType?: DatabaseType;
  driverProfile?: string | null;
  schema?: string;
  tableName: string;
  column: EditableStructureColumn;
}