Nessuna descrizione
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

get_oracle_tables.go 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package dbs
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "time"
  6. "git.x2erp.com/qdy/go-svc-mcp/internal/mcp"
  7. )
  8. func init() {
  9. mcp.Register("get_oracle_tables", "获取Oracle数据库中的所有表和描述",
  10. map[string]interface{}{
  11. "type": "object",
  12. "properties": map[string]interface{}{
  13. "include_system_tables": map[string]interface{}{
  14. "type": "boolean",
  15. "description": "是否包含系统表",
  16. "default": false,
  17. },
  18. "schema": map[string]interface{}{
  19. "type": "string",
  20. "description": "模式名称(默认为当前用户)",
  21. "default": "",
  22. },
  23. "database_key": map[string]interface{}{
  24. "type": "string",
  25. "description": "数据库配置键名(如:business),可选,默认使用主数据库",
  26. "enum": []string{"warehouse", "business"},
  27. "default": "warehouse",
  28. },
  29. },
  30. "required": []string{},
  31. },
  32. func(input json.RawMessage, deps *mcp.ToolDependencies) (interface{}, error) {
  33. var params struct {
  34. IncludeSystemTables bool `json:"include_system_tables"`
  35. Schema string `json:"schema"`
  36. DatabaseKey string `json:"database_key"`
  37. }
  38. if len(input) > 0 {
  39. if err := json.Unmarshal(input, &params); err != nil {
  40. return nil, err
  41. }
  42. }
  43. // 获取数据库工厂
  44. dbFactory, err := GetDBFactory(params.DatabaseKey, deps)
  45. if err != nil {
  46. return nil, err
  47. }
  48. // 获取数据库类型,确保是Oracle
  49. dbType := dbFactory.GetDBType()
  50. if dbType != "oracle" {
  51. return nil, fmt.Errorf("当前数据库类型为 %s,此工具仅支持Oracle数据库", dbType)
  52. }
  53. // 构建查询SQL
  54. var query string
  55. if params.Schema == "" {
  56. // 查询当前用户下的表
  57. if params.IncludeSystemTables {
  58. query = `
  59. SELECT
  60. table_name,
  61. comments as table_description
  62. FROM user_tab_comments
  63. WHERE table_type = 'TABLE'
  64. ORDER BY table_name`
  65. } else {
  66. query = `
  67. SELECT
  68. table_name,
  69. comments as table_description
  70. FROM user_tab_comments
  71. WHERE table_type = 'TABLE'
  72. AND table_name NOT LIKE 'BIN$%' -- 排除回收站中的表
  73. AND table_name NOT LIKE 'DR$%' -- 排除全文索引表
  74. AND table_name NOT LIKE 'SYS_%' -- 排除系统表
  75. ORDER BY table_name`
  76. }
  77. } else {
  78. // 查询指定模式下的表
  79. if params.IncludeSystemTables {
  80. query = `
  81. SELECT
  82. owner as schema_name,
  83. table_name,
  84. comments as table_description
  85. FROM all_tab_comments
  86. WHERE table_type = 'TABLE'
  87. AND owner = UPPER(:1)
  88. ORDER BY table_name`
  89. } else {
  90. query = `
  91. SELECT
  92. owner as schema_name,
  93. table_name,
  94. comments as table_description
  95. FROM all_tab_comments
  96. WHERE table_type = 'TABLE'
  97. AND owner = UPPER(:1)
  98. AND table_name NOT LIKE 'BIN$%'
  99. AND table_name NOT LIKE 'DR$%'
  100. AND table_name NOT LIKE 'SYS_%'
  101. ORDER BY table_name`
  102. }
  103. }
  104. // 执行查询
  105. var results []map[string]interface{}
  106. if params.Schema == "" {
  107. results, err = dbFactory.QuerySliceMap(query)
  108. } else {
  109. results, err = dbFactory.QuerySliceMapWithParams(query, params.Schema)
  110. }
  111. if err != nil {
  112. return nil, fmt.Errorf("查询表信息失败: %v", err)
  113. }
  114. // 如果没有描述信息,设置为空字符串
  115. for i := range results {
  116. if results[i]["table_description"] == nil {
  117. results[i]["table_description"] = ""
  118. }
  119. }
  120. return map[string]interface{}{
  121. "tenant_id": deps.ReqCtx.TenantID,
  122. "user_id": deps.ReqCtx.UserID,
  123. "database_type": dbType,
  124. "database_name": dbFactory.GetDatabaseName(),
  125. "schema": params.Schema,
  126. "include_system_tables": params.IncludeSystemTables,
  127. "tables": results,
  128. "total_tables": len(results),
  129. "timestamp": time.Now().Format(time.RFC3339),
  130. }, nil
  131. },
  132. )
  133. }