Açıklama Yok
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_mysql_tables.go 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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_mysql_tables", "获取MySQL数据库中的所有表和描述",
  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": "数据库名称(默认为当前数据库)",
  22. "default": "",
  23. },
  24. "database_key": map[string]interface{}{
  25. "type": "string",
  26. "description": "数据库配置键名(如:business),可选,默认使用主数据库",
  27. "enum": []string{"warehouse", "business"},
  28. "default": "",
  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. // 获取数据库类型,确保是MySQL
  50. dbType := dbFactory.GetDBType()
  51. if dbType != "mysql" {
  52. return nil, fmt.Errorf("数据库类型为 %s,此工具仅支持MySQL数据库", dbType)
  53. }
  54. // 获取当前数据库名称
  55. currentDatabase := dbFactory.GetDatabaseName()
  56. schema := strings.TrimSpace(params.Schema)
  57. if schema == "" {
  58. schema = currentDatabase
  59. }
  60. // 构建查询SQL
  61. var query string
  62. if params.IncludeSystemTables {
  63. query = `
  64. SELECT
  65. TABLE_NAME as table_name,
  66. TABLE_COMMENT as table_description
  67. FROM information_schema.TABLES
  68. WHERE TABLE_SCHEMA = ?
  69. AND TABLE_TYPE = 'BASE TABLE'
  70. ORDER BY TABLE_NAME`
  71. } else {
  72. query = `
  73. SELECT
  74. TABLE_NAME as table_name,
  75. TABLE_COMMENT as table_description
  76. FROM information_schema.TABLES
  77. WHERE TABLE_SCHEMA = ?
  78. AND TABLE_TYPE = 'BASE TABLE'
  79. AND TABLE_NAME NOT LIKE 'mysql%'
  80. AND TABLE_NAME NOT LIKE 'sys%'
  81. AND TABLE_NAME NOT LIKE 'performance_schema%'
  82. AND TABLE_NAME NOT LIKE 'information_schema%'
  83. ORDER BY TABLE_NAME`
  84. }
  85. // 执行查询
  86. results, err := dbFactory.QuerySliceMapWithParams(query, schema)
  87. if err != nil {
  88. return nil, fmt.Errorf("查询表信息失败: %v", err)
  89. }
  90. // 如果没有描述信息,设置为空字符串
  91. for i := range results {
  92. if results[i]["table_description"] == nil {
  93. results[i]["table_description"] = ""
  94. }
  95. }
  96. return map[string]interface{}{
  97. "tenant_id": deps.ReqCtx.TenantID,
  98. "user_id": deps.ReqCtx.UserID,
  99. "database_type": dbType,
  100. "database_name": dbFactory.GetDatabaseName(),
  101. "schema": schema,
  102. "include_system_tables": params.IncludeSystemTables,
  103. "tables": results,
  104. "total_tables": len(results),
  105. "timestamp": time.Now().Format(time.RFC3339),
  106. }, nil
  107. },
  108. )
  109. }