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。
TypeScript aicliclickhousedatabasedatabase-clientdatabase-managementdockerguimcpmongodbmysqlpostgresqlredisrustsql-serversqlitetaurivue
fix(oceanbase): support viewing package body implementation in oracle mode (#13716)
lxk955134bcd560436
diff --git a/crates/dbx-core/src/schema/mod.rs b/crates/dbx-core/src/schema/mod.rsindex 8a2e0633a9..e80c0ff49a 100644--- a/crates/dbx-core/src/schema/mod.rs+++ b/crates/dbx-core/src/schema/mod.rs@@ -12545,16 +12545,11 @@ async fn get_object_source_once(}} else {match pool_handle.as_ref().ok_or("Pool not found")? {- PoolKind::Mysql(pool, MysqlMode::OceanBaseOracle)- if matches!(object_type, db::ObjectSourceKind::Trigger) =>- {- if options.is_exact() {+ PoolKind::Mysql(pool, MysqlMode::OceanBaseOracle) => {+ if options.is_exact() && matches!(object_type, db::ObjectSourceKind::Trigger) {return oracle_triggers::pool_reader(pool.clone()).await.source(schema, name).await;}- first_string_cell(- db::mysql::execute_query(pool, &oracle_object_source_sql(schema, name, &object_type), true)- .await?,- )?+ db::ob_oracle::get_object_source(pool, schema, name, &object_type).await?}PoolKind::Mysql(pool, _) => {mysql_object_source(pool, mysql_table_metadata_catalog(database, schema), name, &object_type)diff --git a/crates/dbx-core/src/schema/providers/native.rs b/crates/dbx-core/src/schema/providers/native.rsindex 0b15965fac..ffeefe8d59 100644--- a/crates/dbx-core/src/schema/providers/native.rs+++ b/crates/dbx-core/src/schema/providers/native.rs@@ -293,6 +293,9 @@ pub(in crate::schema) async fn object_source(object_type: &db::ObjectSourceKind,) -> Result<Option<String>, String> {match pool {+ PoolKind::Mysql(pool, mode) if *mode == MysqlMode::OceanBaseOracle => {+ super::super::db::ob_oracle::get_object_source(pool, schema, name, object_type).await.map(Some)+ }PoolKind::Mysql(pool, _) => super::super::mysql_object_source(pool,super::super::mysql_table_metadata_catalog(database, schema),diff --git a/crates/dbx-driver-mysql/src/ob_oracle.rs b/crates/dbx-driver-mysql/src/ob_oracle.rsindex 258188bb08..30b55d28b2 100644--- a/crates/dbx-driver-mysql/src/ob_oracle.rs+++ b/crates/dbx-driver-mysql/src/ob_oracle.rs@@ -2,7 +2,9 @@ use std::collections::BTreeMap;use mysql_async::prelude::*;-use crate::types::{ColumnInfo, DatabaseInfo, ForeignKeyInfo, IndexInfo, ObjectInfo, TableInfo, TriggerInfo};+use crate::types::{+ ColumnInfo, DatabaseInfo, ForeignKeyInfo, IndexInfo, ObjectInfo, ObjectSourceKind, TableInfo, TriggerInfo,+};const CURRENT_SCHEMA_EXPRESSION: &str = "SYS_CONTEXT('USERENV', 'CURRENT_SCHEMA')";@@ -371,6 +373,153 @@ pub async fn list_triggers(pool: &mysql_async::Pool, schema: &str, table: &str).collect())}+pub fn all_source_type(kind: &ObjectSourceKind) -> Option<&'static str> {+ match kind {+ ObjectSourceKind::Procedure => Some("PROCEDURE"),+ ObjectSourceKind::Function => Some("FUNCTION"),+ ObjectSourceKind::Package => Some("PACKAGE"),+ ObjectSourceKind::PackageBody => Some("PACKAGE BODY"),+ ObjectSourceKind::Trigger => Some("TRIGGER"),+ ObjectSourceKind::Type => Some("TYPE"),+ ObjectSourceKind::TypeBody => Some("TYPE BODY"),+ _ => None,+ }+}++pub fn metadata_ddl_type(kind: &ObjectSourceKind) -> Option<&'static str> {+ match kind {+ ObjectSourceKind::View => Some("VIEW"),+ ObjectSourceKind::MaterializedView => Some("MATERIALIZED_VIEW"),+ ObjectSourceKind::Procedure => Some("PROCEDURE"),+ ObjectSourceKind::Function => Some("FUNCTION"),+ ObjectSourceKind::Trigger => Some("TRIGGER"),+ ObjectSourceKind::Sequence => Some("SEQUENCE"),+ ObjectSourceKind::Synonym => Some("SYNONYM"),+ ObjectSourceKind::Package => Some("PACKAGE"),+ ObjectSourceKind::PackageBody => Some("PACKAGE_BODY"),+ ObjectSourceKind::Type => Some("TYPE"),+ ObjectSourceKind::TypeBody => Some("TYPE_BODY"),+ _ => None,+ }+}++pub fn all_source_sql(schema: &str, name: &str, source_type: &str) -> String {+ format!(+ "SELECT TEXT FROM ALL_SOURCE WHERE OWNER = {s} AND NAME = {n} AND TYPE = {t} ORDER BY LINE",+ s = metadata_owner_sql(schema),+ n = quote_value(name),+ t = quote_value(source_type),+ )+}++pub fn all_views_sql(schema: &str, name: &str) -> String {+ format!(+ "SELECT TEXT FROM ALL_VIEWS WHERE OWNER = {s} AND VIEW_NAME = {n}",+ s = metadata_owner_sql(schema),+ n = quote_value(name),+ )+}++pub fn dbms_metadata_ddl_sql(schema: &str, name: &str, ddl_type: &str) -> String {+ if schema.trim().is_empty() {+ format!(+ "SELECT DBMS_METADATA.GET_DDL({ddl}, {name}) FROM DUAL",+ ddl = quote_value(ddl_type),+ name = quote_value(name),+ )+ } else {+ format!(+ "SELECT DBMS_METADATA.GET_DDL({ddl}, {name}, {schema}) FROM DUAL",+ ddl = quote_value(ddl_type),+ name = quote_value(name),+ schema = quote_value(schema),+ )+ }+}++pub fn editable_oracle_source(source: &str) -> String {+ let trimmed = source.trim();+ if trimmed.is_empty() {+ return String::new();+ }+ if trimmed.get(..6).is_some_and(|head| head.eq_ignore_ascii_case("CREATE"))+ && (trimmed.len() == 6 || trimmed.as_bytes()[6].is_ascii_whitespace())+ {+ trimmed.to_string()+ } else {+ format!("CREATE OR REPLACE {trimmed}")+ }+}++pub async fn get_object_source(+ pool: &mysql_async::Pool,+ schema: &str,+ name: &str,+ kind: &ObjectSourceKind,+) -> Result<String, String> {+ let mut conn = pool.get_conn().await.map_err(|e| e.to_string())?;++ // For routine and package types (PACKAGE, PACKAGE_BODY, PROCEDURE, FUNCTION, TRIGGER, TYPE, TYPE_BODY),+ // query ALL_SOURCE first because DBMS_METADATA.GET_DDL can block or fail on OceanBase Oracle mode (#10212, #13628).+ if let Some(source_type) = all_source_type(kind) {+ let sql = all_source_sql(schema, name, source_type);+ if let Ok(result) = conn.query_iter(&sql).await {+ if let Ok(rows) = result.collect_and_drop::<mysql_async::Row>().await {+ let mut full_source = String::new();+ for row in rows {+ let line = get_str(&row, 0);+ full_source.push_str(&line);+ }+ if !full_source.trim().is_empty() {+ return Ok(editable_oracle_source(&full_source));+ }+ }+ }+ }++ // For VIEW, DBMS_METADATA produces the complete CREATE OR REPLACE VIEW declaration.+ // Fall back to ALL_VIEWS if DBMS_METADATA is unavailable or empty.+ if matches!(kind, ObjectSourceKind::View) {+ let metadata_sql = dbms_metadata_ddl_sql(schema, name, "VIEW");+ if let Ok(result) = conn.query_iter(&metadata_sql).await {+ if let Ok(rows) = result.collect_and_drop::<mysql_async::Row>().await {+ if let Some(row) = rows.first() {+ let source = get_str(row, 0);+ if !source.trim().is_empty() {+ return Ok(editable_oracle_source(&source));+ }+ }+ }+ }++ let views_sql = all_views_sql(schema, name);+ let result = conn.query_iter(&views_sql).await.map_err(|e| e.to_string())?;+ let rows: Vec<mysql_async::Row> = result.collect_and_drop().await.map_err(|e| e.to_string())?;+ if let Some(row) = rows.first() {+ let source = get_str(row, 0);+ if !source.trim().is_empty() {+ return Ok(source);+ }+ }+ return Err("Object source not found".to_string());+ }++ // For other types or when ALL_SOURCE yielded nothing, fall back to DBMS_METADATA.GET_DDL.+ if let Some(ddl_type) = metadata_ddl_type(kind) {+ let sql = dbms_metadata_ddl_sql(schema, name, ddl_type);+ let result = conn.query_iter(&sql).await.map_err(|e| e.to_string())?;+ let rows: Vec<mysql_async::Row> = result.collect_and_drop().await.map_err(|e| e.to_string())?;+ if let Some(row) = rows.first() {+ let source = get_str(row, 0);+ if !source.trim().is_empty() {+ return Ok(editable_oracle_source(&source));+ }+ }+ }++ Err("Object source not found".to_string())+}+#[cfg(test)]mod tests {use super::*;@@ -433,6 +582,8 @@ mod tests {list_indexes_sql("", "PERF_PROV_CONFIG"),list_foreign_keys_sql("", "PERF_PROV_CONFIG"),list_triggers_sql("", "PERF_PROV_CONFIG"),+ all_source_sql("", "PKG", "PACKAGE BODY"),+ all_views_sql("", "V"),];for sql in statements {@@ -451,6 +602,8 @@ mod tests {list_indexes_sql("SYS", "PERF_PROV_CONFIG"),list_foreign_keys_sql("SYS", "PERF_PROV_CONFIG"),list_triggers_sql("SYS", "PERF_PROV_CONFIG"),+ all_source_sql("SYS", "PKG", "PACKAGE BODY"),+ all_views_sql("SYS", "V"),];for sql in statements {@@ -458,4 +611,48 @@ mod tests {assert!(!sql.contains(CURRENT_SCHEMA_EXPRESSION), "unexpected current schema lookup in: {sql}");}}++ #[test]+ fn ob_oracle_all_source_sql_for_package_body() {+ let sql = all_source_sql("HR", "PAYROLL_PKG", "PACKAGE BODY");+ assert_eq!(+ sql,+ "SELECT TEXT FROM ALL_SOURCE WHERE OWNER = 'HR' AND NAME = 'PAYROLL_PKG' AND TYPE = 'PACKAGE BODY' ORDER BY LINE"+ );+ }++ #[test]+ fn ob_oracle_dbms_metadata_ddl_sql() {+ assert_eq!(+ dbms_metadata_ddl_sql("HR", "PAYROLL_PKG", "PACKAGE_BODY"),+ "SELECT DBMS_METADATA.GET_DDL('PACKAGE_BODY', 'PAYROLL_PKG', 'HR') FROM DUAL"+ );+ assert_eq!(+ dbms_metadata_ddl_sql("", "PAYROLL_PKG", "PACKAGE_BODY"),+ "SELECT DBMS_METADATA.GET_DDL('PACKAGE_BODY', 'PAYROLL_PKG') FROM DUAL"+ );+ }++ #[test]+ fn ob_oracle_editable_oracle_source_wraps_raw_source() {+ assert_eq!(+ editable_oracle_source("PACKAGE BODY pkg IS\nBEGIN\n NULL;\nEND;"),+ "CREATE OR REPLACE PACKAGE BODY pkg IS\nBEGIN\n NULL;\nEND;"+ );+ assert_eq!(+ editable_oracle_source("CREATE PACKAGE BODY pkg IS\nBEGIN\n NULL;\nEND;"),+ "CREATE PACKAGE BODY pkg IS\nBEGIN\n NULL;\nEND;"+ );+ assert_eq!(+ editable_oracle_source("CREATE OR REPLACE PACKAGE BODY pkg IS\nBEGIN\n NULL;\nEND;"),+ "CREATE OR REPLACE PACKAGE BODY pkg IS\nBEGIN\n NULL;\nEND;"+ );+ assert_eq!(+ editable_oracle_source(" create or replace package pkg is end; "),+ "create or replace package pkg is end;"+ );+ let commented = "--注释\nPACKAGE BODY pkg IS BEGIN NULL; END;";+ assert_eq!(editable_oracle_source(commented), format!("CREATE OR REPLACE {commented}"));+ assert_eq!(editable_oracle_source(""), "");+ }}diff --git a/crates/dbx-sql-core/src/object_source_sql.rs b/crates/dbx-sql-core/src/object_source_sql.rsindex 295b9af730..8e2812fbab 100644--- a/crates/dbx-sql-core/src/object_source_sql.rs+++ b/crates/dbx-sql-core/src/object_source_sql.rs@@ -593,8 +593,16 @@ fn build_executable_object_source_statements_with_mysql_validation_name(return Ok(vec![executable_oracle_view_ddl(input.schema.as_deref(), &input.name, source)]);}- if is_oracle_like(input.database_type)- && matches!(input.object_type, ObjectSourceKind::Function | ObjectSourceKind::Procedure)+ if (is_oracle_like(input.database_type) || input.database_type == DatabaseType::OceanbaseOracle)+ && matches!(+ input.object_type,+ ObjectSourceKind::Function+ | ObjectSourceKind::Procedure+ | ObjectSourceKind::Package+ | ObjectSourceKind::PackageBody+ | ObjectSourceKind::Type+ | ObjectSourceKind::TypeBody+ ){return Ok(vec![executable_oracle_routine_ddl(source)]);}@@ -736,7 +744,12 @@ fn validate_oracle_object_source(source.to_string()} else if matches!(object_type,- ObjectSourceKind::Procedure | ObjectSourceKind::Function | ObjectSourceKind::Type | ObjectSourceKind::TypeBody+ ObjectSourceKind::Procedure+ | ObjectSourceKind::Function+ | ObjectSourceKind::Package+ | ObjectSourceKind::PackageBody+ | ObjectSourceKind::Type+ | ObjectSourceKind::TypeBody) {format!("CREATE OR REPLACE {}", source.trim())} else {@@ -1458,7 +1471,7 @@ fn executable_oracle_view_ddl(schema: Option<&str>, name: &str, source: &str) ->fn executable_oracle_routine_ddl(source: &str) -> String {let trimmed = source.trim();- if Regex::new(r"(?i)^(?:PROCEDURE|FUNCTION)\b").unwrap().is_match(trimmed) {+ if Regex::new(r"(?i)^(?:PROCEDURE|FUNCTION|PACKAGE(?:\s+BODY)?|TYPE(?:\s+BODY)?)\b").unwrap().is_match(trimmed) {return ensure_semicolon(&format!("CREATE OR REPLACE {trimmed}"));}ensure_semicolon(trimmed)@@ -3500,6 +3513,30 @@ mod tests {}}+ #[test]+ fn oceanbase_oracle_bare_package_body_source_saves_as_create_or_replace() {+ let source = "PACKAGE BODY PAYROLL AS\n PROCEDURE PROCESS IS BEGIN NULL; END;\nEND PAYROLL;";++ for database_type in [DatabaseType::Oracle, DatabaseType::OceanbaseOracle, DatabaseType::Dameng] {+ let statements =+ build_executable_object_source_statements(input(database_type, ObjectSourceKind::PackageBody, source))+ .unwrap();++ assert_eq!(+ statements,+ vec![+ "CREATE OR REPLACE PACKAGE BODY PAYROLL AS\n PROCEDURE PROCESS IS BEGIN NULL; END;\nEND PAYROLL;"+ ]+ );++ let editable = build_editable_object_source(input(database_type, ObjectSourceKind::PackageBody, source));+ assert_eq!(+ editable,+ "CREATE OR REPLACE PACKAGE BODY PAYROLL AS\n PROCEDURE PROCESS IS BEGIN NULL; END;\nEND PAYROLL;"+ );+ }+ }+#[test]fn oracle_routine_create_prefix_is_not_duplicated() {for source in [