feat(objects): edit database object source
This commit is contained in:
1 parent
70db0aa62c
commit
03bea705e7
11 files changed
+272
-8
No files matched your search
@@ -57,6 +57,7 @@ export const deleteSchemaCachePrefix = forward("deleteSchemaCachePrefix");
|
||||
export const listSchemas = forward("listSchemas");
|
||||
export const listTables = forward("listTables");
|
||||
export const listObjects = forward("listObjects");
|
||||
export const getObjectSource = forward("getObjectSource");
|
||||
export const getColumns = forward("getColumns");
|
||||
export const listIndexes = forward("listIndexes");
|
||||
export const listForeignKeys = forward("listForeignKeys");
|
||||
|
||||
@@ -3,6 +3,8 @@ import type {
|
||||
DatabaseInfo,
|
||||
TableInfo,
|
||||
ObjectInfo,
|
||||
ObjectSource,
|
||||
ObjectSourceKind,
|
||||
ColumnInfo,
|
||||
IndexInfo,
|
||||
ForeignKeyInfo,
|
||||
@@ -175,6 +177,18 @@ export async function listObjects(connectionId: string, database: string, schema
|
||||
return get(`/api/schema/objects?${qs({ connection_id: connectionId, database, schema })}`);
|
||||
}
|
||||
|
||||
export async function getObjectSource(
|
||||
connectionId: string,
|
||||
database: string,
|
||||
schema: string,
|
||||
name: string,
|
||||
objectType: ObjectSourceKind,
|
||||
): Promise<ObjectSource> {
|
||||
return get(
|
||||
`/api/schema/object-source?${qs({ connection_id: connectionId, database, schema, table: name, object_type: objectType })}`,
|
||||
);
|
||||
}
|
||||
|
||||
export async function getColumns(
|
||||
connectionId: string,
|
||||
database: string,
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
import type { DatabaseType, ObjectSourceKind } from "@/types/database";
|
||||
|
||||
type BuildEditableObjectSourceSqlInput = {
|
||||
databaseType: DatabaseType;
|
||||
objectType: ObjectSourceKind;
|
||||
schema?: string | null;
|
||||
name: string;
|
||||
source: string;
|
||||
};
|
||||
|
||||
function quotePostgresIdentifier(value: string) {
|
||||
return `"${value.replaceAll('"', '""')}"`;
|
||||
}
|
||||
|
||||
function ensureSemicolon(sql: string) {
|
||||
const trimmed = sql.trim();
|
||||
return trimmed.endsWith(";") ? trimmed : `${trimmed};`;
|
||||
}
|
||||
|
||||
function postgresQualifiedName(schema: string | null | undefined, name: string) {
|
||||
return [schema, name]
|
||||
.filter(Boolean)
|
||||
.map((part) => quotePostgresIdentifier(part as string))
|
||||
.join(".");
|
||||
}
|
||||
|
||||
export function objectSourceEditTabTitle(schema: string | null | undefined, name: string) {
|
||||
return `Edit source - ${[schema, name].filter(Boolean).join(".")}`;
|
||||
}
|
||||
|
||||
export function buildEditableObjectSourceSql(input: BuildEditableObjectSourceSqlInput) {
|
||||
const source = input.source.trim();
|
||||
if (input.databaseType === "sqlserver") {
|
||||
return source.replace(/^CREATE\s+(?!OR\s+ALTER\b)/i, "CREATE OR ALTER ");
|
||||
}
|
||||
|
||||
if ((input.databaseType === "postgres" || input.databaseType === "gaussdb") && input.objectType === "VIEW") {
|
||||
return `CREATE OR REPLACE VIEW ${postgresQualifiedName(input.schema, input.name)} AS\n${ensureSemicolon(source)}`;
|
||||
}
|
||||
|
||||
return ensureSemicolon(source);
|
||||
}
|
||||
@@ -5,6 +5,8 @@ import type {
|
||||
DatabaseInfo,
|
||||
TableInfo,
|
||||
ObjectInfo,
|
||||
ObjectSource,
|
||||
ObjectSourceKind,
|
||||
ColumnInfo,
|
||||
IndexInfo,
|
||||
ForeignKeyInfo,
|
||||
@@ -144,6 +146,16 @@ export async function listObjects(connectionId: string, database: string, schema
|
||||
return invoke("list_objects", { connectionId, database, schema });
|
||||
}
|
||||
|
||||
export async function getObjectSource(
|
||||
connectionId: string,
|
||||
database: string,
|
||||
schema: string,
|
||||
name: string,
|
||||
objectType: ObjectSourceKind,
|
||||
): Promise<ObjectSource> {
|
||||
return invoke("get_object_source", { connectionId, database, schema, name, objectType });
|
||||
}
|
||||
|
||||
export async function listSchemas(connectionId: string, database: string): Promise<string[]> {
|
||||
return invoke("list_schemas", { connectionId, database });
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user