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.

55 lines
2.1 KiB

  1. package system
  2. import (
  3. "github.com/flipped-aurora/gin-vue-admin/server/global"
  4. "github.com/flipped-aurora/gin-vue-admin/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() (data []response.Db, err error) {
  12. var entities []response.Db
  13. sql := "SELECT SCHEMA_NAME AS `database` FROM INFORMATION_SCHEMA.SCHEMATA;"
  14. err = global.GVA_DB.Raw(sql).Scan(&entities).Error
  15. return entities, err
  16. }
  17. // GetTables 获取数据库的所有表名
  18. // Author [piexlmax](https://github.com/piexlmax)
  19. // Author [SliverHorn](https://github.com/SliverHorn)
  20. func (s *autoCodeMysql) GetTables(dbName string) (data []response.Table, err error) {
  21. var entities []response.Table
  22. sql := `select table_name as table_name from information_schema.tables where table_schema = ?`
  23. err = global.GVA_DB.Raw(sql, dbName).Scan(&entities).Error
  24. return entities, err
  25. }
  26. // GetColumn 获取指定数据库和指定数据表的所有字段名,类型值等
  27. // Author [piexlmax](https://github.com/piexlmax)
  28. // Author [SliverHorn](https://github.com/SliverHorn)
  29. func (s *autoCodeMysql) GetColumn(tableName string, dbName string) (data []response.Column, err error) {
  30. var entities []response.Column
  31. sql := `
  32. SELECT COLUMN_NAME column_name,
  33. DATA_TYPE data_type,
  34. CASE DATA_TYPE
  35. WHEN 'longtext' THEN c.CHARACTER_MAXIMUM_LENGTH
  36. WHEN 'varchar' THEN c.CHARACTER_MAXIMUM_LENGTH
  37. WHEN 'double' THEN CONCAT_WS(',', c.NUMERIC_PRECISION, c.NUMERIC_SCALE)
  38. WHEN 'decimal' THEN CONCAT_WS(',', c.NUMERIC_PRECISION, c.NUMERIC_SCALE)
  39. WHEN 'int' THEN c.NUMERIC_PRECISION
  40. WHEN 'bigint' THEN c.NUMERIC_PRECISION
  41. ELSE '' END AS data_type_long,
  42. COLUMN_COMMENT column_comment
  43. FROM INFORMATION_SCHEMA.COLUMNS c
  44. WHERE table_name = ?
  45. AND table_schema = ?
  46. `
  47. err = global.GVA_DB.Raw(sql, tableName, dbName).Scan(&entities).Error
  48. return entities, err
  49. }