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_oracle_data.go 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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_oracle_data", "通过表名称获取Oracle表数据:只能接收表名称和分页查询参数分页参数数据获取数据不能超过10条",
  11. map[string]interface{}{
  12. "type": "object",
  13. "properties": map[string]interface{}{
  14. "table_name": map[string]interface{}{
  15. "type": "string",
  16. "description": "表名称(支持大小写敏感)",
  17. },
  18. "schema": map[string]interface{}{
  19. "type": "string",
  20. "description": "模式名称(默认为当前用户)",
  21. "default": "",
  22. },
  23. "page": map[string]interface{}{
  24. "type": "integer",
  25. "description": "页码(从1开始)",
  26. "default": 1,
  27. "minimum": 1,
  28. },
  29. "page_size": map[string]interface{}{
  30. "type": "integer",
  31. "description": "每页记录数(最大10条)",
  32. "default": 10,
  33. "minimum": 1,
  34. "maximum": 10,
  35. },
  36. "order_by": map[string]interface{}{
  37. "type": "string",
  38. "description": "排序字段(例如:column1 ASC, column2 DESC)",
  39. "default": "",
  40. },
  41. "where_clause": map[string]interface{}{
  42. "type": "string",
  43. "description": "WHERE条件(例如:status = 'ACTIVE')",
  44. "default": "",
  45. },
  46. "database_key": map[string]interface{}{
  47. "type": "string",
  48. "description": "数据库配置键名(如:business),可选,默认使用主数据库",
  49. "enum": []string{"warehouse", "business"},
  50. "default": "warehouse",
  51. },
  52. },
  53. "required": []string{"table_name"},
  54. },
  55. func(input json.RawMessage, deps *mcp.ToolDependencies) (interface{}, error) {
  56. var params struct {
  57. TableName string `json:"table_name"`
  58. Schema string `json:"schema"`
  59. Page int `json:"page"`
  60. PageSize int `json:"page_size"`
  61. OrderBy string `json:"order_by"`
  62. WhereClause string `json:"where_clause"`
  63. DatabaseKey string `json:"database_key"`
  64. }
  65. if len(input) > 0 {
  66. if err := json.Unmarshal(input, &params); err != nil {
  67. return nil, err
  68. }
  69. }
  70. // 设置默认值
  71. if params.Page == 0 {
  72. params.Page = 1
  73. }
  74. if params.PageSize == 0 {
  75. params.PageSize = 10
  76. }
  77. // 确保每页记录数不超过10条
  78. if params.PageSize > 10 {
  79. params.PageSize = 10
  80. }
  81. // 获取数据库工厂
  82. dbFactory, err := GetDBFactory(params.DatabaseKey, deps)
  83. if err != nil {
  84. return nil, err
  85. }
  86. // 获取数据库类型,确保是Oracle
  87. dbType := dbFactory.GetDBType()
  88. if dbType != "oracle" {
  89. return nil, fmt.Errorf("当前数据库类型为 %s,此工具仅支持Oracle数据库", dbType)
  90. }
  91. // 清理表名和模式名
  92. tableName := strings.TrimSpace(params.TableName)
  93. schema := strings.TrimSpace(params.Schema)
  94. if tableName == "" {
  95. return nil, fmt.Errorf("表名称不能为空")
  96. }
  97. // 验证表是否存在
  98. tableExists := false
  99. var tableExistsQuery string
  100. if schema == "" {
  101. tableExistsQuery = `SELECT COUNT(*) as table_count FROM user_tables WHERE table_name = UPPER(:1)`
  102. } else {
  103. tableExistsQuery = `SELECT COUNT(*) as table_count FROM all_tables WHERE table_name = UPPER(:1) AND owner = UPPER(:2)`
  104. }
  105. var tableCheckResults []map[string]interface{}
  106. if schema == "" {
  107. tableCheckResults, err = dbFactory.QuerySliceMapWithParams(tableExistsQuery, tableName)
  108. } else {
  109. tableCheckResults, err = dbFactory.QuerySliceMapWithParams(tableExistsQuery, tableName, schema)
  110. }
  111. if err != nil {
  112. return nil, fmt.Errorf("检查表是否存在失败: %v", err)
  113. }
  114. if len(tableCheckResults) > 0 {
  115. if count, ok := tableCheckResults[0]["table_count"].(int64); ok && count > 0 {
  116. tableExists = true
  117. }
  118. }
  119. if !tableExists {
  120. return nil, fmt.Errorf("表 '%s' 不存在", tableName)
  121. }
  122. // 构建带模式前缀的表名
  123. fullTableName := tableName
  124. if schema != "" {
  125. fullTableName = fmt.Sprintf("%s.%s", schema, tableName)
  126. }
  127. // 构建WHERE子句
  128. whereClause := ""
  129. if params.WhereClause != "" {
  130. whereClause = fmt.Sprintf("WHERE %s", params.WhereClause)
  131. }
  132. // 构建ORDER BY子句
  133. orderByClause := ""
  134. if params.OrderBy != "" {
  135. orderByClause = fmt.Sprintf("ORDER BY %s", params.OrderBy)
  136. }
  137. // 计算分页偏移量
  138. offset := (params.Page - 1) * params.PageSize
  139. // Oracle分页查询(使用ROWNUM)
  140. // 先获取总记录数
  141. countQuery := fmt.Sprintf("SELECT COUNT(*) as total_count FROM %s %s", fullTableName, whereClause)
  142. countResults, err := dbFactory.QuerySliceMap(countQuery)
  143. if err != nil {
  144. return nil, fmt.Errorf("查询总记录数失败: %v", err)
  145. }
  146. totalCount := int64(0)
  147. if len(countResults) > 0 {
  148. if count, ok := countResults[0]["total_count"].(int64); ok {
  149. totalCount = count
  150. }
  151. }
  152. // 计算总页数
  153. totalPages := 0
  154. if totalCount > 0 {
  155. totalPages = int((totalCount + int64(params.PageSize) - 1) / int64(params.PageSize))
  156. }
  157. // 如果请求的页码大于总页数,返回空结果
  158. if params.Page > totalPages && totalPages > 0 {
  159. return map[string]interface{}{
  160. "tenant_id": deps.ReqCtx.TenantID,
  161. "user_id": deps.ReqCtx.UserID,
  162. "database_type": dbType,
  163. "database_name": dbFactory.GetDatabaseName(),
  164. "schema": schema,
  165. "table_name": tableName,
  166. "full_table_name": fullTableName,
  167. "page": params.Page,
  168. "page_size": params.PageSize,
  169. "total_count": totalCount,
  170. "total_pages": totalPages,
  171. "where_clause": params.WhereClause,
  172. "order_by": params.OrderBy,
  173. "data": []map[string]interface{}{},
  174. "data_count": 0,
  175. "has_more": false,
  176. "timestamp": time.Now().Format(time.RFC3339),
  177. }, nil
  178. }
  179. // 构建数据查询(Oracle分页查询)
  180. dataQuery := ""
  181. if whereClause == "" && orderByClause == "" {
  182. // 简单查询,使用ROWNUM分页
  183. dataQuery = fmt.Sprintf(`
  184. SELECT * FROM (
  185. SELECT t.*, ROWNUM rn FROM %s t
  186. WHERE ROWNUM <= %d
  187. ) WHERE rn > %d`,
  188. fullTableName, offset+params.PageSize, offset)
  189. } else {
  190. // 复杂查询,需要子查询
  191. if orderByClause == "" {
  192. // 有WHERE条件但没有ORDER BY
  193. dataQuery = fmt.Sprintf(`
  194. SELECT * FROM (
  195. SELECT t.*, ROWNUM rn FROM %s t
  196. %s
  197. AND ROWNUM <= %d
  198. ) WHERE rn > %d`,
  199. fullTableName, whereClause, offset+params.PageSize, offset)
  200. } else {
  201. // 有WHERE条件和ORDER BY
  202. dataQuery = fmt.Sprintf(`
  203. SELECT * FROM (
  204. SELECT inner.*, ROWNUM rn FROM (
  205. SELECT * FROM %s
  206. %s
  207. %s
  208. ) inner
  209. WHERE ROWNUM <= %d
  210. ) WHERE rn > %d`,
  211. fullTableName, whereClause, orderByClause, offset+params.PageSize, offset)
  212. }
  213. }
  214. // 执行数据查询
  215. dataResults, err := dbFactory.QuerySliceMap(dataQuery)
  216. if err != nil {
  217. return nil, fmt.Errorf("查询表数据失败: %v", err)
  218. }
  219. // 移除Oracle分页添加的rn字段
  220. for i := range dataResults {
  221. delete(dataResults[i], "rn")
  222. }
  223. // 检查是否有更多数据
  224. hasMore := (int64(offset+params.PageSize) < totalCount)
  225. return map[string]interface{}{
  226. "tenant_id": deps.ReqCtx.TenantID,
  227. "user_id": deps.ReqCtx.UserID,
  228. "database_type": dbType,
  229. "database_name": dbFactory.GetDatabaseName(),
  230. "schema": schema,
  231. "table_name": tableName,
  232. "full_table_name": fullTableName,
  233. "page": params.Page,
  234. "page_size": params.PageSize,
  235. "total_count": totalCount,
  236. "total_pages": totalPages,
  237. "where_clause": params.WhereClause,
  238. "order_by": params.OrderBy,
  239. "data": dataResults,
  240. "data_count": len(dataResults),
  241. "has_more": hasMore,
  242. "timestamp": time.Now().Format(time.RFC3339),
  243. "note": "数据获取限制:最多返回10条记录",
  244. }, nil
  245. },
  246. )
  247. }