No Description
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_sqlserver_tables.go 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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_sqlserver_tables", "获取SQL Server数据库中的所有表和描述",
  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": "模式名称(默认为dbo)",
  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. // 获取数据库类型,确保是SQL Server
  50. dbType := dbFactory.GetDBType()
  51. if dbType != "sqlserver" {
  52. return nil, fmt.Errorf("当前数据库类型为 %s,此工具仅支持SQL Server数据库", dbType)
  53. }
  54. // 设置默认模式
  55. schema := strings.TrimSpace(params.Schema)
  56. if schema == "" {
  57. schema = "dbo"
  58. }
  59. // 构建查询SQL
  60. var query string
  61. if params.IncludeSystemTables {
  62. query = `
  63. SELECT
  64. TABLE_SCHEMA as schema_name,
  65. TABLE_NAME as table_name,
  66. '' as table_description -- SQL Server没有内置的表描述字段
  67. FROM INFORMATION_SCHEMA.TABLES
  68. WHERE TABLE_TYPE = 'BASE TABLE'
  69. AND TABLE_SCHEMA = @p1
  70. ORDER BY TABLE_NAME`
  71. } else {
  72. query = `
  73. SELECT
  74. TABLE_SCHEMA as schema_name,
  75. TABLE_NAME as table_name,
  76. '' as table_description -- SQL Server没有内置的表描述字段
  77. FROM INFORMATION_SCHEMA.TABLES
  78. WHERE TABLE_TYPE = 'BASE TABLE'
  79. AND TABLE_SCHEMA = @p1
  80. AND TABLE_NAME NOT LIKE 'sys%'
  81. AND TABLE_NAME NOT LIKE 'MS%'
  82. ORDER BY TABLE_NAME`
  83. }
  84. // 执行查询
  85. results, err := dbFactory.QuerySliceMapWithParams(query, schema)
  86. if err != nil {
  87. return nil, fmt.Errorf("查询表信息失败: %v", err)
  88. }
  89. // SQL Server没有内置表描述,尝试从扩展属性获取
  90. for i := range results {
  91. tableName := results[i]["table_name"].(string)
  92. descQuery := `
  93. SELECT value as table_description
  94. FROM fn_listextendedproperty ('MS_Description', 'SCHEMA', @p1, 'TABLE', @p2, NULL, NULL)`
  95. descResults, err := dbFactory.QuerySliceMapWithParams(descQuery, schema, tableName)
  96. if err == nil && len(descResults) > 0 {
  97. if desc, ok := descResults[0]["table_description"].(string); ok && desc != "" {
  98. results[i]["table_description"] = desc
  99. }
  100. }
  101. }
  102. return map[string]interface{}{
  103. "tenant_id": deps.ReqCtx.TenantID,
  104. "user_id": deps.ReqCtx.UserID,
  105. "database_type": dbType,
  106. "database_name": dbFactory.GetDatabaseName(),
  107. "schema": schema,
  108. "include_system_tables": params.IncludeSystemTables,
  109. "tables": results,
  110. "total_tables": len(results),
  111. "timestamp": time.Now().Format(time.RFC3339),
  112. "note": "SQL Server表描述需要手动通过扩展属性设置",
  113. }, nil
  114. },
  115. )
  116. }