sys_auto_code_mysql.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package system
  2. import (
  3. "server/global"
  4. "server/model/system/response"
  5. )
  6. var AutoCodeMysql = new(autoCodeMysql)
  7. type autoCodeMysql struct{}
  8. // GetDB 获取数据库的所有数据库名
  9. // Author [piexlmax](https://github.com/piexlmax)
  10. // Author [SliverHorn](https://github.com/SliverHorn)
  11. func (s *autoCodeMysql) GetDB(businessDB string) (data []response.Db, err error) {
  12. var entities []response.Db
  13. sql := "SELECT SCHEMA_NAME AS `database` FROM INFORMATION_SCHEMA.SCHEMATA;"
  14. if businessDB == "" {
  15. err = global.GVA_DB.Raw(sql).Scan(&entities).Error
  16. } else {
  17. err = global.GVA_DBList[businessDB].Raw(sql).Scan(&entities).Error
  18. }
  19. return entities, err
  20. }
  21. // GetTables 获取数据库的所有表名
  22. // Author [piexlmax](https://github.com/piexlmax)
  23. // Author [SliverHorn](https://github.com/SliverHorn)
  24. func (s *autoCodeMysql) GetTables(businessDB string, dbName string) (data []response.Table, err error) {
  25. var entities []response.Table
  26. sql := `select table_name as table_name from information_schema.tables where table_schema = ?`
  27. if businessDB == "" {
  28. err = global.GVA_DB.Raw(sql, dbName).Scan(&entities).Error
  29. } else {
  30. err = global.GVA_DBList[businessDB].Raw(sql, dbName).Scan(&entities).Error
  31. }
  32. return entities, err
  33. }
  34. // GetColumn 获取指定数据库和指定数据表的所有字段名,类型值等
  35. // Author [piexlmax](https://github.com/piexlmax)
  36. // Author [SliverHorn](https://github.com/SliverHorn)
  37. func (s *autoCodeMysql) GetColumn(businessDB string, tableName string, dbName string) (data []response.Column, err error) {
  38. var entities []response.Column
  39. sql := `
  40. SELECT
  41. c.COLUMN_NAME column_name,
  42. c.DATA_TYPE data_type,
  43. CASE c.DATA_TYPE
  44. WHEN 'longtext' THEN c.CHARACTER_MAXIMUM_LENGTH
  45. WHEN 'varchar' THEN c.CHARACTER_MAXIMUM_LENGTH
  46. WHEN 'double' THEN CONCAT_WS(',', c.NUMERIC_PRECISION, c.NUMERIC_SCALE)
  47. WHEN 'decimal' THEN CONCAT_WS(',', c.NUMERIC_PRECISION, c.NUMERIC_SCALE)
  48. WHEN 'int' THEN c.NUMERIC_PRECISION
  49. WHEN 'bigint' THEN c.NUMERIC_PRECISION
  50. ELSE ''
  51. END AS data_type_long,
  52. c.COLUMN_COMMENT column_comment,
  53. CASE WHEN kcu.COLUMN_NAME IS NOT NULL THEN 1 ELSE 0 END AS primary_key
  54. FROM
  55. INFORMATION_SCHEMA.COLUMNS c
  56. LEFT JOIN
  57. INFORMATION_SCHEMA.KEY_COLUMN_USAGE kcu
  58. ON
  59. c.TABLE_SCHEMA = kcu.TABLE_SCHEMA
  60. AND c.TABLE_NAME = kcu.TABLE_NAME
  61. AND c.COLUMN_NAME = kcu.COLUMN_NAME
  62. AND kcu.CONSTRAINT_NAME = 'PRIMARY'
  63. WHERE
  64. c.TABLE_NAME = ?
  65. AND c.TABLE_SCHEMA = ?;`
  66. if businessDB == "" {
  67. err = global.GVA_DB.Raw(sql, tableName, dbName).Scan(&entities).Error
  68. } else {
  69. err = global.GVA_DBList[businessDB].Raw(sql, tableName, dbName).Scan(&entities).Error
  70. }
  71. return entities, err
  72. }