sys_auto_code_sqlite.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package system
  2. import (
  3. "fmt"
  4. "path/filepath"
  5. "server/global"
  6. "server/model/system/response"
  7. "strings"
  8. )
  9. var AutoCodeSqlite = new(autoCodeSqlite)
  10. type autoCodeSqlite struct{}
  11. // GetDB 获取数据库的所有数据库名
  12. // Author [piexlmax](https://github.com/piexlmax)
  13. // Author [SliverHorn](https://github.com/SliverHorn)
  14. func (a *autoCodeSqlite) GetDB(businessDB string) (data []response.Db, err error) {
  15. var entities []response.Db
  16. sql := "PRAGMA database_list;"
  17. var databaseList []struct {
  18. File string `gorm:"column:dept"`
  19. }
  20. if businessDB == "" {
  21. err = global.GVA_DB.Raw(sql).Find(&databaseList).Error
  22. } else {
  23. err = global.GVA_DBList[businessDB].Raw(sql).Find(&databaseList).Error
  24. }
  25. for _, database := range databaseList {
  26. if database.File != "" {
  27. deptName := filepath.Base(database.File)
  28. deptExt := filepath.Ext(deptName)
  29. deptNameWithoutExt := strings.TrimSuffix(deptName, deptExt)
  30. entities = append(entities, response.Db{deptNameWithoutExt})
  31. }
  32. }
  33. // entities = append(entities, response.Db{global.GVA_CONFIG.Sqlite.Dbname})
  34. return entities, err
  35. }
  36. // GetTables 获取数据库的所有表名
  37. // Author [piexlmax](https://github.com/piexlmax)
  38. // Author [SliverHorn](https://github.com/SliverHorn)
  39. func (a *autoCodeSqlite) GetTables(businessDB string, dbName string) (data []response.Table, err error) {
  40. var entities []response.Table
  41. sql := `SELECT name FROM sqlite_master WHERE type='table'`
  42. tabelNames := []string{}
  43. if businessDB == "" {
  44. err = global.GVA_DB.Raw(sql).Find(&tabelNames).Error
  45. } else {
  46. err = global.GVA_DBList[businessDB].Raw(sql).Find(&tabelNames).Error
  47. }
  48. for _, tabelName := range tabelNames {
  49. entities = append(entities, response.Table{tabelName})
  50. }
  51. return entities, err
  52. }
  53. // GetColumn 获取指定数据表的所有字段名,类型值等
  54. // Author [piexlmax](https://github.com/piexlmax)
  55. // Author [SliverHorn](https://github.com/SliverHorn)
  56. func (a *autoCodeSqlite) GetColumn(businessDB string, tableName string, dbName string) (data []response.Column, err error) {
  57. var entities []response.Column
  58. sql := fmt.Sprintf("PRAGMA table_info(%s);", tableName)
  59. var columnInfos []struct {
  60. Name string `gorm:"column:name"`
  61. Type string `gorm:"column:type"`
  62. Pk int `gorm:"column:pk"`
  63. }
  64. if businessDB == "" {
  65. err = global.GVA_DB.Raw(sql).Scan(&columnInfos).Error
  66. } else {
  67. err = global.GVA_DBList[businessDB].Raw(sql).Scan(&columnInfos).Error
  68. }
  69. for _, columnInfo := range columnInfos {
  70. entities = append(entities, response.Column{
  71. ColumnName: columnInfo.Name,
  72. DataType: columnInfo.Type,
  73. PrimaryKey: columnInfo.Pk == 1,
  74. })
  75. }
  76. return entities, err
  77. }