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.

90 lines
3.9 KiB

3 years ago
  1. package system
  2. import (
  3. "strings"
  4. "github.com/flipped-aurora/gin-vue-admin/server/global"
  5. "github.com/flipped-aurora/gin-vue-admin/server/model/system/response"
  6. "github.com/pkg/errors"
  7. "gorm.io/driver/postgres"
  8. "gorm.io/gorm"
  9. "gorm.io/gorm/logger"
  10. )
  11. var AutoCodePgsql = new(autoCodePgsql)
  12. type autoCodePgsql struct{}
  13. // GetDB 获取数据库的所有数据库名
  14. // Author [piexlmax](https://github.com/piexlmax)
  15. // Author [SliverHorn](https://github.com/SliverHorn)
  16. func (a *autoCodePgsql) GetDB() (data []response.Db, err error) {
  17. var entities []response.Db
  18. sql := `SELECT datname as database FROM pg_database WHERE datistemplate = false`
  19. err = global.GVA_DB.Raw(sql).Scan(&entities).Error
  20. return entities, err
  21. }
  22. // GetTables 获取数据库的所有表名
  23. // Author [piexlmax](https://github.com/piexlmax)
  24. // Author [SliverHorn](https://github.com/SliverHorn)
  25. func (a *autoCodePgsql) GetTables(dbName string) (data []response.Table, err error) {
  26. var entities []response.Table
  27. sql := `select table_name as table_name from information_schema.tables where table_catalog = ? and table_schema = ?`
  28. db, _err := gorm.Open(postgres.Open(global.GVA_CONFIG.Pgsql.LinkDsn(dbName)), &gorm.Config{Logger: logger.Default.LogMode(logger.Info)})
  29. if _err != nil {
  30. return nil, errors.Wrapf(err, "[pgsql] 连接 数据库(%s)的表失败!", dbName)
  31. }
  32. err = db.Raw(sql, dbName, "public").Scan(&entities).Error
  33. return entities, err
  34. }
  35. // GetColumn 获取指定数据库和指定数据表的所有字段名,类型值等
  36. // Author [piexlmax](https://github.com/piexlmax)
  37. // Author [SliverHorn](https://github.com/SliverHorn)
  38. func (a *autoCodePgsql) GetColumn(tableName string, dbName string) (data []response.Column, err error) {
  39. // todo 数据获取不全, 待完善sql
  40. sql := `
  41. SELECT columns.COLUMN_NAME as column_name,
  42. columns.DATA_TYPE as data_type,
  43. CASE
  44. columns.DATA_TYPE
  45. WHEN 'text' THEN
  46. concat_ws('', '', columns.CHARACTER_MAXIMUM_LENGTH)
  47. WHEN 'varchar' THEN
  48. concat_ws('', '', columns.CHARACTER_MAXIMUM_LENGTH)
  49. WHEN 'smallint' THEN
  50. concat_ws(',', columns.NUMERIC_PRECISION, columns.NUMERIC_SCALE)
  51. WHEN 'decimal' THEN
  52. concat_ws(',', columns.NUMERIC_PRECISION, columns.NUMERIC_SCALE)
  53. WHEN 'integer' THEN
  54. concat_ws('', '', columns.NUMERIC_PRECISION)
  55. WHEN 'bigint' THEN
  56. concat_ws('', '', columns.NUMERIC_PRECISION)
  57. ELSE ''
  58. END AS data_type_long,
  59. (select description.description
  60. from pg_description description
  61. where description.objoid = (select attribute.attrelid
  62. from pg_attribute attribute
  63. where attribute.attrelid =
  64. (select oid from pg_class class where class.relname = '@table_name') and attname =columns.COLUMN_NAME )
  65. and description.objsubid = (select attribute.attnum
  66. from pg_attribute attribute
  67. where attribute.attrelid =
  68. (select oid from pg_class class where class.relname = '@table_name') and attname =columns.COLUMN_NAME )) as column_comment
  69. FROM INFORMATION_SCHEMA.COLUMNS columns
  70. WHERE table_catalog = '@table_catalog'
  71. and table_schema = 'public'
  72. and table_name = '@table_name';
  73. `
  74. var entities []response.Column
  75. db, _err := gorm.Open(postgres.Open(global.GVA_CONFIG.Pgsql.LinkDsn(dbName)), &gorm.Config{Logger: logger.Default.LogMode(logger.Info)})
  76. if _err != nil {
  77. return nil, errors.Wrapf(err, "[pgsql] 连接 数据库(%s)的表(%s)失败!", dbName, tableName)
  78. }
  79. sql = strings.ReplaceAll(sql, "@table_catalog", dbName)
  80. sql = strings.ReplaceAll(sql, "@table_name", tableName)
  81. err = db.Raw(sql).Scan(&entities).Error
  82. return entities, err
  83. }