Nav apraksta
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

get_postgresql_tables.go 3.6KB

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