export_controller.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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(outputDir 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. outputDir = "./GTool" + "/" + dbName + "_" + dbIp
  35. if err := utils.CreateDirectory(outputDir); err != nil {
  36. fmt.Println(fmt.Errorf("创建文件夹失败,可能已存在同名文件夹 %s, err: %v", outputDir, err))
  37. return
  38. }
  39. PrintDatabaseInfo(dbInfo)
  40. //存放文件夹名字为库名
  41. dbInfoFile := outputDir + "/" + "db_info.txt"
  42. WriteDatabaseInfoToFile(dbInfo, dbInfoFile)
  43. // !导出
  44. // // 创建一个空的 tableNames 数组,用于存放表名
  45. // tableNames := make([]string, 0, len(dbInfo.Tables)+len(dbInfo.LobTables))
  46. // // 将 tableInfoMap 中的表名添加到 tableNames 中
  47. // for tableName := range dbInfo.Tables {
  48. // tableNames = append(tableNames, tableName)
  49. // }
  50. // // 将 lobTableInfoMap 中的表名添加到 tableNames 中
  51. // for tableName := range dbInfo.LobTables {
  52. // tableNames = append(tableNames, tableName)
  53. // }
  54. // 创建一个新的 map,将普通表和 LOB 表的信息都放进去
  55. allTables := make(map[string]models.TableInfo, len(dbInfo.Tables)+len(dbInfo.LobTables))
  56. // 将 tableInfoMap 中的表信息添加到 allTables 中
  57. for tableName, tableInfo := range dbInfo.Tables {
  58. allTables[tableName] = tableInfo
  59. }
  60. // 将 lobTableInfoMap 中的表信息添加到 allTables 中
  61. for tableName, tableInfo := range dbInfo.LobTables {
  62. allTables[tableName] = tableInfo
  63. }
  64. services.WriteMain(executor, allTables, outputDir, delimiter, maxFileSize, pageSize)
  65. }
  66. // 打印 DatabaseInfo 的函数
  67. func PrintDatabaseInfo(dbInfo *models.DatabaseInfo) {
  68. if dbInfo == nil {
  69. fmt.Println("数据库信息为空")
  70. return
  71. }
  72. fmt.Printf("数据库总大小: %.2f MB\n", dbInfo.SizeMB)
  73. fmt.Printf("表的数量: %d\n\n", dbInfo.TableCount)
  74. fmt.Println("普通表信息:")
  75. for _, table := range dbInfo.Tables {
  76. fmt.Printf("表名: %s\n", table.Name)
  77. fmt.Printf("大小: %.2f MB\n", table.Size)
  78. fmt.Printf("行数: %d\n", table.Count)
  79. fmt.Printf("DDL:\n%s\n\n", table.DDL)
  80. }
  81. fmt.Println("LOB 表信息:")
  82. for _, table := range dbInfo.LobTables {
  83. fmt.Printf("表名: %s\n", table.Name)
  84. fmt.Printf("大小: %.2f MB\n", table.Size)
  85. fmt.Printf("行数: %d\n", table.Count)
  86. fmt.Printf("DDL:\n%s\n\n", table.DDL)
  87. }
  88. fmt.Println("存储过程和函数:")
  89. for _, function := range dbInfo.Functions {
  90. fmt.Printf("名称: %s\n", function.Name)
  91. fmt.Printf("类型: %s\n", function.Type)
  92. fmt.Printf("定义:\n%s\n\n", function.Definition)
  93. }
  94. fmt.Println("触发器:")
  95. for _, trigger := range dbInfo.Triggers {
  96. fmt.Printf("名称: %s\n", trigger.Name)
  97. fmt.Printf("事件: %s\n", trigger.Event)
  98. fmt.Printf("表: %s\n", trigger.Table)
  99. fmt.Printf("时机: %s\n", trigger.Timing)
  100. fmt.Printf("语句:\n%s\n\n", trigger.Statement)
  101. }
  102. }
  103. // WriteDatabaseInfoToFile 将 DatabaseInfo 写入文件,并格式化划分信息
  104. func WriteDatabaseInfoToFile(dbInfo *models.DatabaseInfo, filePath string) error {
  105. if dbInfo == nil {
  106. return fmt.Errorf("数据库信息为空")
  107. }
  108. // 打开或创建文件
  109. file, err := os.Create(filePath)
  110. if err != nil {
  111. return fmt.Errorf("无法创建文件: %v", err)
  112. }
  113. defer file.Close()
  114. // 计算表的数量(普通表和 LOB 表)
  115. normalTableCount := len(dbInfo.Tables)
  116. lobTableCount := len(dbInfo.LobTables)
  117. functionCount := len(dbInfo.Functions)
  118. triggerCount := len(dbInfo.Triggers)
  119. // 写入数据库总大小、表数量以及各类表和对象的数量
  120. fmt.Fprintf(file, "--======== 数据库信息 ========\n")
  121. fmt.Fprintf(file, "数据库总大小: %.2f MB\n", dbInfo.SizeMB)
  122. fmt.Fprintf(file, "表的总数量: %d\n", dbInfo.TableCount)
  123. fmt.Fprintf(file, "普通表的数量: %d\n", normalTableCount)
  124. fmt.Fprintf(file, "LOB 表的数量: %d\n", lobTableCount)
  125. fmt.Fprintf(file, "存储过程和函数的数量: %d\n", functionCount)
  126. fmt.Fprintf(file, "触发器的数量: %d\n\n", triggerCount)
  127. // 写入普通表信息
  128. fmt.Fprintf(file, "--======== 普通表信息 ========\n")
  129. for _, table := range dbInfo.Tables {
  130. fmt.Fprintf(file, "-------- 表名: %s --------\n", table.Name)
  131. fmt.Fprintf(file, "大小: %.2f MB\n", table.Size)
  132. fmt.Fprintf(file, "行数: %d\n", table.Count)
  133. fmt.Fprintf(file, "DDL:\n%s\n\n", table.DDL)
  134. }
  135. // 写入 LOB 表信息
  136. fmt.Fprintf(file, "--======== LOB 表信息 ========\n")
  137. for _, table := range dbInfo.LobTables {
  138. fmt.Fprintf(file, "-------- 表名: %s --------\n", table.Name)
  139. fmt.Fprintf(file, "大小: %.2f MB\n", table.Size)
  140. fmt.Fprintf(file, "行数: %d\n", table.Count)
  141. fmt.Fprintf(file, "DDL:\n%s\n\n", table.DDL)
  142. }
  143. // 写入存储过程和函数信息
  144. fmt.Fprintf(file, "--======== 存储过程和函数 ========\n")
  145. for _, function := range dbInfo.Functions {
  146. fmt.Fprintf(file, "-------- 名称: %s --------\n", function.Name)
  147. fmt.Fprintf(file, "类型: %s\n", function.Type)
  148. fmt.Fprintf(file, "定义:\n%s\n\n", function.Definition)
  149. }
  150. // 写入触发器信息
  151. fmt.Fprintf(file, "--======== 触发器 ========\n")
  152. for _, trigger := range dbInfo.Triggers {
  153. fmt.Fprintf(file, "-------- 名称: %s --------\n", trigger.Name)
  154. fmt.Fprintf(file, "事件: %s\n", trigger.Event)
  155. fmt.Fprintf(file, "表: %s\n", trigger.Table)
  156. fmt.Fprintf(file, "时机: %s\n", trigger.Timing)
  157. fmt.Fprintf(file, "语句:\n%s\n\n", trigger.Statement)
  158. }
  159. fmt.Fprintf(file, "--============================\n")
  160. return nil
  161. }