sys_auto_code_oracle.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package system
  2. import (
  3. "server/global"
  4. "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. FROM
  39. all_tab_columns a
  40. JOIN
  41. all_col_comments b ON a.OWNER = b.OWNER AND a.TABLE_NAME = b.TABLE_NAME AND a.COLUMN_NAME = b.COLUMN_NAME
  42. LEFT JOIN
  43. (
  44. SELECT
  45. acc.OWNER,
  46. acc.TABLE_NAME,
  47. acc.COLUMN_NAME
  48. FROM
  49. all_cons_columns acc
  50. JOIN
  51. all_constraints ac ON acc.OWNER = ac.OWNER AND acc.CONSTRAINT_NAME = ac.CONSTRAINT_NAME
  52. WHERE
  53. ac.CONSTRAINT_TYPE = 'P'
  54. ) pk ON a.OWNER = pk.OWNER AND a.TABLE_NAME = pk.TABLE_NAME AND a.COLUMN_NAME = pk.COLUMN_NAME
  55. WHERE
  56. lower(a.table_name) = ?
  57. AND lower(a.OWNER) = ?;
  58. `
  59. err = global.GVA_DBList[businessDB].Raw(sql, tableName, dbName).Scan(&entities).Error
  60. return entities, err
  61. }