sys_auto_code_pgsql.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package system
  2. import (
  3. "server/global"
  4. "server/model/system/response"
  5. )
  6. var AutoCodePgsql = new(autoCodePgsql)
  7. type autoCodePgsql struct{}
  8. // GetDB 获取数据库的所有数据库名
  9. // Author [piexlmax](https://github.com/piexlmax)
  10. // Author [SliverHorn](https://github.com/SliverHorn)
  11. func (a *autoCodePgsql) GetDB(businessDB string) (data []response.Db, err error) {
  12. var entities []response.Db
  13. sql := `SELECT datname as database FROM pg_database WHERE datistemplate = false`
  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 (a *autoCodePgsql) 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_catalog = ? and table_schema = ?`
  27. db := global.GVA_DB
  28. if businessDB != "" {
  29. db = global.GVA_DBList[businessDB]
  30. }
  31. err = db.Raw(sql, dbName, "public").Scan(&entities).Error
  32. return entities, err
  33. }
  34. // GetColumn 获取指定数据库和指定数据表的所有字段名,类型值等
  35. // Author [piexlmax](https://github.com/piexlmax)
  36. // Author [SliverHorn](https://github.com/SliverHorn)
  37. func (a *autoCodePgsql) GetColumn(businessDB string, tableName string, dbName string) (data []response.Column, err error) {
  38. // todo 数据获取不全, 待完善sql
  39. sql := `
  40. SELECT
  41. psc.COLUMN_NAME AS COLUMN_NAME,
  42. psc.udt_name AS data_type,
  43. CASE
  44. psc.udt_name
  45. WHEN 'text' THEN
  46. concat_ws ( '', '', psc.CHARACTER_MAXIMUM_LENGTH )
  47. WHEN 'varchar' THEN
  48. concat_ws ( '', '', psc.CHARACTER_MAXIMUM_LENGTH )
  49. WHEN 'smallint' THEN
  50. concat_ws ( ',', psc.NUMERIC_PRECISION, psc.NUMERIC_SCALE )
  51. WHEN 'decimal' THEN
  52. concat_ws ( ',', psc.NUMERIC_PRECISION, psc.NUMERIC_SCALE )
  53. WHEN 'integer' THEN
  54. concat_ws ( '', '', psc.NUMERIC_PRECISION )
  55. WHEN 'int4' THEN
  56. concat_ws ( '', '', psc.NUMERIC_PRECISION )
  57. WHEN 'int8' THEN
  58. concat_ws ( '', '', psc.NUMERIC_PRECISION )
  59. WHEN 'bigint' THEN
  60. concat_ws ( '', '', psc.NUMERIC_PRECISION )
  61. WHEN 'timestamp' THEN
  62. concat_ws ( '', '', psc.datetime_precision )
  63. ELSE ''
  64. END AS data_type_long,
  65. (
  66. SELECT
  67. pd.description
  68. FROM
  69. pg_description pd
  70. WHERE
  71. (pd.objoid,pd.objsubid) in (
  72. SELECT pa.attrelid,pa.attnum
  73. FROM
  74. pg_attribute pa
  75. WHERE pa.attrelid = ( SELECT oid FROM pg_class pc WHERE
  76. pc.relname = psc.table_name
  77. )
  78. and attname = psc.column_name
  79. )
  80. ) AS column_comment,
  81. (
  82. SELECT
  83. COUNT(*)
  84. FROM
  85. pg_constraint
  86. WHERE
  87. contype = 'p'
  88. AND conrelid = (
  89. SELECT
  90. oid
  91. FROM
  92. pg_class
  93. WHERE
  94. relname = psc.table_name
  95. )
  96. AND conkey::int[] @> ARRAY[(
  97. SELECT
  98. attnum::integer
  99. FROM
  100. pg_attribute
  101. WHERE
  102. attrelid = conrelid
  103. AND attname = psc.column_name
  104. )]
  105. ) > 0 AS primary_key
  106. FROM
  107. INFORMATION_SCHEMA.COLUMNS psc
  108. WHERE
  109. table_catalog = ?
  110. AND table_schema = 'public'
  111. AND TABLE_NAME = ?;
  112. `
  113. var entities []response.Column
  114. //sql = strings.ReplaceAll(sql, "@table_catalog", dbName)
  115. //sql = strings.ReplaceAll(sql, "@table_name", tableName)
  116. db := global.GVA_DB
  117. if businessDB != "" {
  118. db = global.GVA_DBList[businessDB]
  119. }
  120. err = db.Raw(sql, dbName, tableName).Scan(&entities).Error
  121. return entities, err
  122. }