| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- package dbs
-
- import (
- "encoding/json"
- "fmt"
- "time"
-
- "git.x2erp.com/qdy/go-svc-mcp/internal/mcp"
- )
-
- func init() {
- mcp.Register("get_oracle_tables", "获取Oracle数据库中的所有表和描述",
- map[string]interface{}{
- "type": "object",
- "properties": map[string]interface{}{
- "include_system_tables": map[string]interface{}{
- "type": "boolean",
- "description": "是否包含系统表",
- "default": false,
- },
- "schema": map[string]interface{}{
- "type": "string",
- "description": "模式名称(默认为当前用户)",
- "default": "",
- },
- "database_key": map[string]interface{}{
- "type": "string",
- "description": "数据库配置键名(如:business),可选,默认使用主数据库",
- "enum": []string{"warehouse", "business"},
- "default": "warehouse",
- },
- },
- "required": []string{},
- },
- func(input json.RawMessage, deps *mcp.ToolDependencies) (interface{}, error) {
- var params struct {
- IncludeSystemTables bool `json:"include_system_tables"`
- Schema string `json:"schema"`
- DatabaseKey string `json:"database_key"`
- }
-
- if len(input) > 0 {
- if err := json.Unmarshal(input, ¶ms); err != nil {
- return nil, err
- }
- }
-
- // 获取数据库工厂
- dbFactory, err := GetDBFactory(params.DatabaseKey, deps)
- if err != nil {
- return nil, err
- }
-
- // 获取数据库类型,确保是Oracle
- dbType := dbFactory.GetDBType()
- if dbType != "oracle" {
- return nil, fmt.Errorf("当前数据库类型为 %s,此工具仅支持Oracle数据库", dbType)
- }
-
- // 构建查询SQL
- var query string
- if params.Schema == "" {
- // 查询当前用户下的表
- if params.IncludeSystemTables {
- query = `
- SELECT
- table_name,
- comments as table_description
- FROM user_tab_comments
- WHERE table_type = 'TABLE'
- ORDER BY table_name`
- } else {
- query = `
- SELECT
- table_name,
- comments as table_description
- FROM user_tab_comments
- WHERE table_type = 'TABLE'
- AND table_name NOT LIKE 'BIN$%' -- 排除回收站中的表
- AND table_name NOT LIKE 'DR$%' -- 排除全文索引表
- AND table_name NOT LIKE 'SYS_%' -- 排除系统表
- ORDER BY table_name`
- }
- } else {
- // 查询指定模式下的表
- if params.IncludeSystemTables {
- query = `
- SELECT
- owner as schema_name,
- table_name,
- comments as table_description
- FROM all_tab_comments
- WHERE table_type = 'TABLE'
- AND owner = UPPER(:1)
- ORDER BY table_name`
- } else {
- query = `
- SELECT
- owner as schema_name,
- table_name,
- comments as table_description
- FROM all_tab_comments
- WHERE table_type = 'TABLE'
- AND owner = UPPER(:1)
- AND table_name NOT LIKE 'BIN$%'
- AND table_name NOT LIKE 'DR$%'
- AND table_name NOT LIKE 'SYS_%'
- ORDER BY table_name`
- }
- }
-
- // 执行查询
- var results []map[string]interface{}
-
- if params.Schema == "" {
- results, err = dbFactory.QuerySliceMap(query)
- } else {
- results, err = dbFactory.QuerySliceMapWithParams(query, params.Schema)
- }
-
- if err != nil {
- return nil, fmt.Errorf("查询表信息失败: %v", err)
- }
-
- // 如果没有描述信息,设置为空字符串
- for i := range results {
- if results[i]["table_description"] == nil {
- results[i]["table_description"] = ""
- }
- }
-
- return map[string]interface{}{
- "tenant_id": deps.ReqCtx.TenantID,
- "user_id": deps.ReqCtx.UserID,
- "database_type": dbType,
- "database_name": dbFactory.GetDatabaseName(),
- "schema": params.Schema,
- "include_system_tables": params.IncludeSystemTables,
- "tables": results,
- "total_tables": len(results),
- "timestamp": time.Now().Format(time.RFC3339),
- }, nil
- },
- )
- }
|