fix: convert source to editable form when opening view/procedure editor

This commit is contained in:
t8y2
2026-06-17 15:20:21 +08:00
parent 4888e637c7
commit 4dbd1c7cc8
9 changed files with 72 additions and 2 deletions
@@ -354,8 +354,15 @@ async function openSource(row: ObjectBrowserRow) {
sourceLoading.value = true;
try {
const result = await api.getObjectSource(props.connection.id, props.database, row.schema || selectedSchema.value || props.database, row.name, row.type as ObjectSourceKind);
sourceContent.value = result.source;
sourceDraft.value = result.source;
const editable = await api.buildEditableObjectSource({
databaseType: effectiveDatabaseType.value,
objectType: row.type as ObjectSourceKind,
schema: row.schema || selectedSchema.value || props.database,
name: row.name,
source: result.source,
});
sourceContent.value = editable;
sourceDraft.value = editable;
sourceEditing.value = row.type !== "SEQUENCE";
} catch (e: any) {
sourceError.value = e?.message || String(e);
+1
View File
@@ -156,6 +156,7 @@ export const buildDropSchemaSql = forward("buildDropSchemaSql");
export const buildDuplicateTableStructureSql = forward("buildDuplicateTableStructureSql");
export const buildExecutableObjectSourceStatements = forward("buildExecutableObjectSourceStatements");
export const buildExecutableObjectSourceSql = forward("buildExecutableObjectSourceSql");
export const buildEditableObjectSource = forward("buildEditableObjectSource");
export const buildRoutineRenameObjectSourceStatements = forward("buildRoutineRenameObjectSourceStatements");
export const buildViewDdlSql = forward("buildViewDdlSql");
export const buildTableStructureChangeSql = forward("buildTableStructureChangeSql");
+4
View File
@@ -655,6 +655,10 @@ export async function buildExecutableObjectSourceSql(input: BuildEditableObjectS
return post("/api/query/build-executable-object-source-sql", { input });
}
export async function buildEditableObjectSource(input: BuildEditableObjectSourceSqlInput): Promise<string> {
return post("/api/query/build-editable-object-source", { input });
}
export async function buildRoutineRenameObjectSourceStatements(input: BuildRoutineRenameObjectSourceInput): Promise<string[]> {
return post("/api/query/build-routine-rename-object-source-statements", { input });
}
+4
View File
@@ -681,6 +681,10 @@ export async function buildExecutableObjectSourceSql(input: BuildEditableObjectS
return invoke("build_executable_object_source_sql", { input });
}
export async function buildEditableObjectSource(input: BuildEditableObjectSourceSqlInput): Promise<string> {
return invoke("build_editable_object_source", { input });
}
export async function buildRoutineRenameObjectSourceStatements(input: BuildRoutineRenameObjectSourceInput): Promise<string[]> {
return invoke("build_routine_rename_object_source_statements", { input });
}
+41
View File
@@ -150,6 +150,23 @@ pub fn build_executable_object_source_sql(input: EditableObjectSourceSqlInput) -
Ok(build_executable_object_source_statements(input)?.join("\n"))
}
/// Convert a raw database object source into a form suitable for the source editor.
///
/// This is the *editable* presentation shown to the user when they open a view,
/// procedure, or function for editing. For SQL Server the raw `CREATE VIEW` /
/// `CREATE PROCEDURE` is rewritten to `CREATE OR ALTER` so the user doesn't see
/// a mismatched CREATE statement for an already-existing object. Callers that
/// only need the first statement should use this instead of calling
/// `build_executable_object_source_statements` and discarding rename-cleanup
/// statements.
pub fn build_editable_object_source(input: EditableObjectSourceSqlInput) -> String {
let source = input.source.clone();
match build_executable_object_source_statements(input) {
Ok(statements) => statements.into_iter().next().unwrap_or_default(),
Err(_) => ensure_semicolon(source.trim()),
}
}
pub fn build_view_ddl_sql(input: BuildViewDdlInput) -> String {
let source = input.source.trim();
if Regex::new(r"(?i)^(?:CREATE|ALTER)\s+").unwrap().is_match(source) {
@@ -568,6 +585,30 @@ mod tests {
);
}
#[test]
fn sqlserver_view_source_opened_for_editing_shows_create_or_alter() {
let sql = build_editable_object_source(EditableObjectSourceSqlInput {
database_type: DatabaseType::SqlServer,
object_type: ObjectSourceKind::View,
schema: Some("dbo".to_string()),
name: "v_active_users".to_string(),
source: "CREATE VIEW dbo.v_active_users AS SELECT id, name FROM users WHERE active = 1;".to_string(),
});
assert_eq!(sql, "CREATE OR ALTER VIEW dbo.v_active_users AS SELECT id, name FROM users WHERE active = 1;");
}
#[test]
fn sqlserver_procedure_source_opened_for_editing_shows_create_or_alter() {
let sql = build_editable_object_source(EditableObjectSourceSqlInput {
database_type: DatabaseType::SqlServer,
object_type: ObjectSourceKind::Procedure,
schema: Some("dbo".to_string()),
name: "usp_demo".to_string(),
source: "CREATE PROCEDURE dbo.usp_demo AS SELECT 1;".to_string(),
});
assert_eq!(sql, "CREATE OR ALTER PROCEDURE dbo.usp_demo AS SELECT 1;");
}
#[test]
fn oracle_family_routine_rename_rewrites_source_and_drops_original() {
let statements = build_routine_rename_object_source_statements(RoutineRenameObjectSourceInput {
+1
View File
@@ -254,6 +254,7 @@ async fn main() {
post(routes::query::build_executable_object_source_statements),
)
.route("/query/build-executable-object-source-sql", post(routes::query::build_executable_object_source_sql))
.route("/query/build-editable-object-source", post(routes::query::build_editable_object_source))
.route(
"/query/build-routine-rename-object-source-statements",
post(routes::query::build_routine_rename_object_source_statements),
+4
View File
@@ -615,6 +615,10 @@ pub async fn build_executable_object_source_sql(
dbx_core::object_source_sql::build_executable_object_source_sql(req.input).map(Json).map_err(AppError)
}
pub async fn build_editable_object_source(Json(req): Json<BuildExecutableObjectSourceRequest>) -> Json<String> {
Json(dbx_core::object_source_sql::build_editable_object_source(req.input))
}
pub async fn build_routine_rename_object_source_statements(
Json(req): Json<BuildRoutineRenameObjectSourceRequest>,
) -> Result<Json<Vec<String>>, AppError> {
+7
View File
@@ -347,6 +347,13 @@ pub fn build_executable_object_source_sql(
dbx_core::object_source_sql::build_executable_object_source_sql(input)
}
#[tauri::command]
pub fn build_editable_object_source(
input: dbx_core::object_source_sql::EditableObjectSourceSqlInput,
) -> Result<String, String> {
Ok(dbx_core::object_source_sql::build_editable_object_source(input))
}
#[tauri::command]
pub fn build_routine_rename_object_source_statements(
input: dbx_core::object_source_sql::RoutineRenameObjectSourceInput,
+1
View File
@@ -430,6 +430,7 @@ pub fn run() {
commands::query::build_duplicate_table_structure_sql,
commands::query::build_executable_object_source_statements,
commands::query::build_executable_object_source_sql,
commands::query::build_editable_object_source,
commands::query::build_routine_rename_object_source_statements,
commands::query::build_view_ddl_sql,
commands::query::build_table_structure_change_sql,