export_controller.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. package controllers
  2. import (
  3. "fmt"
  4. "os"
  5. "xg_fetl/internal/db_executor"
  6. "xg_fetl/internal/global"
  7. "xg_fetl/internal/models"
  8. "xg_fetl/internal/services"
  9. "xg_fetl/internal/utils"
  10. )
  11. func ExportController(tableName []string) {
  12. dbType := global.Cfg.ExportConfig.DBConfig.DBType
  13. dsn := global.Cfg.ExportConfig.DBConfig.GetDSN()
  14. dbName := global.Cfg.ExportConfig.DBConfig.DBName
  15. dbIp := global.Cfg.ExportConfig.DBConfig.IP
  16. // baseFilePath := global.Cfg.ExportConfig.BaseFilePath
  17. delimiter := global.Cfg.ExportConfig.Delimiter
  18. maxFileSize := global.Cfg.ExportConfig.MaxFileSize
  19. pageSize := global.Cfg.ExportConfig.PageSize
  20. // !创建数据库链接
  21. executor, err := db_executor.NewDBExecutor(dbType, dsn)
  22. if err != nil {
  23. fmt.Println(fmt.Errorf("create db executor failed, err: %v", err))
  24. panic(err)
  25. }
  26. defer executor.Close()
  27. // ! 获取数据库元信息
  28. dbInfo, err := services.GetDBInfoService(dbName, executor)
  29. if err != nil {
  30. fmt.Println(fmt.Errorf("get db info failed, err: %v", err))
  31. panic(err)
  32. }
  33. // !创建存放路径
  34. var outputDir string
  35. if global.Cfg.ExportConfig.BaseFilePath == "" {
  36. outputDir = global.Cfg.ExportConfig.BaseFilePath + "/" + dbName + "_" + dbIp
  37. } else {
  38. outputDir = "./GTool" + "/" + dbName + "_" + dbIp
  39. }
  40. if err := utils.CreateDirectory(outputDir); err != nil {
  41. fmt.Println(fmt.Errorf("创建文件夹失败,可能已存在同名文件夹 %s, err: %v", outputDir, err))
  42. return
  43. }
  44. PrintDatabaseInfo(dbInfo)
  45. //存放文件夹名字为库名
  46. dbInfoFile := outputDir + "/" + "db_info.txt"
  47. WriteDatabaseInfoToFile(dbInfo, dbInfoFile)
  48. // !导出
  49. // 创建一个新的 map,将普通表和 LOB 表的信息都放进去
  50. allTables := make(map[string]models.TableInfo, len(dbInfo.Tables)+len(dbInfo.LobTables))
  51. // 将 tableInfoMap 中的表信息添加到 allTables 中
  52. for tableName, tableInfo := range dbInfo.Tables {
  53. allTables[tableName] = tableInfo
  54. }
  55. // 将 lobTableInfoMap 中的表信息添加到 allTables 中
  56. for tableName, tableInfo := range dbInfo.LobTables {
  57. allTables[tableName] = tableInfo
  58. }
  59. if len(tableName) != 0 {
  60. // 创建一个新的map来存储根据tableName筛选后的表信息
  61. selectedTables := make(map[string]models.TableInfo)
  62. for _, name := range tableName {
  63. if tableInfo, ok := allTables[name]; ok {
  64. selectedTables[name] = tableInfo
  65. }
  66. }
  67. services.WriteMain(executor, selectedTables, outputDir, delimiter, maxFileSize, pageSize)
  68. } else {
  69. services.WriteMain(executor, allTables, outputDir, delimiter, maxFileSize, pageSize)
  70. }
  71. }
  72. // 打印 DatabaseInfo 的函数
  73. func PrintDatabaseInfo(dbInfo *models.DatabaseInfo) {
  74. if dbInfo == nil {
  75. fmt.Println("数据库信息为空")
  76. return
  77. }
  78. fmt.Printf("数据库总大小: %.2f MB\n", dbInfo.SizeMB)
  79. fmt.Printf("表的数量: %d\n\n", dbInfo.TableCount)
  80. fmt.Println("普通表信息:")
  81. for _, table := range dbInfo.Tables {
  82. fmt.Printf("表名: %s\n", table.Name)
  83. fmt.Printf("大小: %.2f MB\n", table.Size)
  84. fmt.Printf("行数: %d\n", table.Count)
  85. fmt.Printf("DDL:\n%s\n\n", table.DDL)
  86. }
  87. fmt.Println("LOB 表信息:")
  88. for _, table := range dbInfo.LobTables {
  89. fmt.Printf("表名: %s\n", table.Name)
  90. fmt.Printf("大小: %.2f MB\n", table.Size)
  91. fmt.Printf("行数: %d\n", table.Count)
  92. fmt.Printf("DDL:\n%s\n\n", table.DDL)
  93. }
  94. fmt.Println("存储过程和函数:")
  95. for _, function := range dbInfo.Functions {
  96. fmt.Printf("名称: %s\n", function.Name)
  97. fmt.Printf("类型: %s\n", function.Type)
  98. fmt.Printf("定义:\n%s\n\n", function.Definition)
  99. }
  100. fmt.Println("触发器:")
  101. for _, trigger := range dbInfo.Triggers {
  102. fmt.Printf("名称: %s\n", trigger.Name)
  103. fmt.Printf("事件: %s\n", trigger.Event)
  104. fmt.Printf("表: %s\n", trigger.Table)
  105. fmt.Printf("时机: %s\n", trigger.Timing)
  106. fmt.Printf("语句:\n%s\n\n", trigger.Statement)
  107. }
  108. }
  109. // WriteDatabaseInfoToFile 将 DatabaseInfo 写入文件,并格式化划分信息
  110. func WriteDatabaseInfoToFile(dbInfo *models.DatabaseInfo, filePath string) error {
  111. if dbInfo == nil {
  112. return fmt.Errorf("数据库信息为空")
  113. }
  114. // 打开或创建文件
  115. file, err := os.Create(filePath)
  116. if err != nil {
  117. return fmt.Errorf("无法创建文件: %v", err)
  118. }
  119. defer file.Close()
  120. // 计算表的数量(普通表和 LOB 表)
  121. normalTableCount := len(dbInfo.Tables)
  122. lobTableCount := len(dbInfo.LobTables)
  123. functionCount := len(dbInfo.Functions)
  124. triggerCount := len(dbInfo.Triggers)
  125. // 写入数据库总大小、表数量以及各类表和对象的数量
  126. fmt.Fprintf(file, "--======== 数据库信息 ========\n")
  127. fmt.Fprintf(file, "数据库总大小: %.2f MB\n", dbInfo.SizeMB)
  128. fmt.Fprintf(file, "表的总数量: %d\n", dbInfo.TableCount)
  129. fmt.Fprintf(file, "普通表的数量: %d\n", normalTableCount)
  130. fmt.Fprintf(file, "LOB 表的数量: %d\n", lobTableCount)
  131. fmt.Fprintf(file, "存储过程和函数的数量: %d\n", functionCount)
  132. fmt.Fprintf(file, "触发器的数量: %d\n\n", triggerCount)
  133. // 写入普通表信息
  134. fmt.Fprintf(file, "--======== 普通表信息 ========\n")
  135. for _, table := range dbInfo.Tables {
  136. fmt.Fprintf(file, "-------- 表名: %s --------\n", table.Name)
  137. fmt.Fprintf(file, "大小: %.2f MB\n", table.Size)
  138. fmt.Fprintf(file, "行数: %d\n", table.Count)
  139. fmt.Fprintf(file, "DDL:\n%s\n\n", table.DDL)
  140. }
  141. // 写入 LOB 表信息
  142. fmt.Fprintf(file, "--======== LOB 表信息 ========\n")
  143. for _, table := range dbInfo.LobTables {
  144. fmt.Fprintf(file, "-------- 表名: %s --------\n", table.Name)
  145. fmt.Fprintf(file, "大小: %.2f MB\n", table.Size)
  146. fmt.Fprintf(file, "行数: %d\n", table.Count)
  147. fmt.Fprintf(file, "DDL:\n%s\n\n", table.DDL)
  148. }
  149. // 写入存储过程和函数信息
  150. fmt.Fprintf(file, "--======== 存储过程和函数 ========\n")
  151. for _, function := range dbInfo.Functions {
  152. fmt.Fprintf(file, "-------- 名称: %s --------\n", function.Name)
  153. fmt.Fprintf(file, "类型: %s\n", function.Type)
  154. fmt.Fprintf(file, "定义:\n%s\n\n", function.Definition)
  155. }
  156. // 写入触发器信息
  157. fmt.Fprintf(file, "--======== 触发器 ========\n")
  158. for _, trigger := range dbInfo.Triggers {
  159. fmt.Fprintf(file, "-------- 名称: %s --------\n", trigger.Name)
  160. fmt.Fprintf(file, "事件: %s\n", trigger.Event)
  161. fmt.Fprintf(file, "表: %s\n", trigger.Table)
  162. fmt.Fprintf(file, "时机: %s\n", trigger.Timing)
  163. fmt.Fprintf(file, "语句:\n%s\n\n", trigger.Statement)
  164. }
  165. fmt.Fprintf(file, "--============================\n")
  166. return nil
  167. }