sys_auto_code_oracle.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 AutoCodeOracle = new(autoCodeOracle)
  7. type autoCodeOracle struct{}
  8. // GetDB 获取数据库的所有数据库名
  9. // Author [piexlmax](https://github.com/piexlmax)
  10. // Author [SliverHorn](https://github.com/SliverHorn)
  11. func (s *autoCodeOracle) GetDB(businessDB string) (data []response.Db, err error) {
  12. var entities []response.Db
  13. sql := `SELECT lower(username) AS "database" FROM all_users`
  14. err = global.GVA_DBList[businessDB].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 *autoCodeOracle) GetTables(businessDB string, dbName string) (data []response.Table, err error) {
  21. var entities []response.Table
  22. sql := `select lower(table_name) as "table_name" from all_tables where lower(owner) = ?`
  23. err = global.GVA_DBList[businessDB].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 *autoCodeOracle) GetColumn(businessDB string, tableName string, dbName string) (data []response.Column, err error) {
  30. var entities []response.Column
  31. sql := `
  32. SELECT
  33. lower(a.COLUMN_NAME) as "column_name",
  34. (CASE WHEN a.DATA_TYPE = 'NUMBER' AND a.DATA_SCALE=0 THEN 'int' else lower(a.DATA_TYPE) end) as "data_type",
  35. (CASE WHEN a.DATA_TYPE = 'NUMBER' THEN a.DATA_PRECISION else a.DATA_LENGTH end) as "data_type_long",
  36. b.COMMENTS as "column_comment",
  37. (CASE WHEN pk.COLUMN_NAME IS NOT NULL THEN 1 ELSE 0 END) as "primary_key",
  38. a.COLUMN_ID
  39. FROM
  40. all_tab_columns a
  41. JOIN
  42. all_col_comments b ON a.OWNER = b.OWNER AND a.TABLE_NAME = b.TABLE_NAME AND a.COLUMN_NAME = b.COLUMN_NAME
  43. LEFT JOIN
  44. (
  45. SELECT
  46. acc.OWNER,
  47. acc.TABLE_NAME,
  48. acc.COLUMN_NAME
  49. FROM
  50. all_cons_columns acc
  51. JOIN
  52. all_constraints ac ON acc.OWNER = ac.OWNER AND acc.CONSTRAINT_NAME = ac.CONSTRAINT_NAME
  53. WHERE
  54. ac.CONSTRAINT_TYPE = 'P'
  55. ) pk ON a.OWNER = pk.OWNER AND a.TABLE_NAME = pk.TABLE_NAME AND a.COLUMN_NAME = pk.COLUMN_NAME
  56. WHERE
  57. lower(a.table_name) = ?
  58. AND lower(a.OWNER) = ?
  59. ORDER BY
  60. a.COLUMN_ID;
  61. `
  62. err = global.GVA_DBList[businessDB].Raw(sql, tableName, dbName).Scan(&entities).Error
  63. return entities, err
  64. }