dbtables.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package tools
  2. import (
  3. "errors"
  4. "gorm.io/gorm"
  5. orm "device-manage/common/global"
  6. "device-manage/tools"
  7. config2 "device-manage/tools/config"
  8. )
  9. type DBTables struct {
  10. TableName string `gorm:"column:TABLE_NAME" json:"tableName"`
  11. Engine string `gorm:"column:ENGINE" json:"engine"`
  12. TableRows string `gorm:"column:TABLE_ROWS" json:"tableRows"`
  13. TableCollation string `gorm:"column:TABLE_COLLATION" json:"tableCollation"`
  14. CreateTime string `gorm:"column:CREATE_TIME" json:"createTime"`
  15. UpdateTime string `gorm:"column:UPDATE_TIME" json:"updateTime"`
  16. TableComment string `gorm:"column:TABLE_COMMENT" json:"tableComment"`
  17. }
  18. func (e *DBTables) GetPage(pageSize int, pageIndex int) ([]DBTables, int, error) {
  19. var doc []DBTables
  20. table := new(gorm.DB)
  21. var count int64
  22. if config2.DatabaseConfig.Driver == "mysql" {
  23. table = orm.Eloquent.Table("information_schema.tables")
  24. table = table.Where("TABLE_NAME not in (select table_name from " + config2.GenConfig.DBName + ".sys_tables) ")
  25. table = table.Where("table_schema= ? ", config2.GenConfig.DBName)
  26. if e.TableName != "" {
  27. table = table.Where("TABLE_NAME = ?", e.TableName)
  28. }
  29. if err := table.Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Offset(-1).Limit(-1).Count(&count).Error; err != nil {
  30. return nil, 0, err
  31. }
  32. } else {
  33. tools.Assert(true, "目前只支持mysql数据库", 500)
  34. }
  35. //table.Count(&count)
  36. return doc, int(count), nil
  37. }
  38. func (e *DBTables) Get() (DBTables, error) {
  39. var doc DBTables
  40. table := new(gorm.DB)
  41. if config2.DatabaseConfig.Driver == "mysql" {
  42. table = orm.Eloquent.Table("information_schema.tables")
  43. table = table.Where("table_schema= ? ", config2.GenConfig.DBName)
  44. if e.TableName == "" {
  45. return doc, errors.New("table name cannot be empty!")
  46. }
  47. table = table.Where("TABLE_NAME = ?", e.TableName)
  48. } else {
  49. tools.Assert(true, "目前只支持mysql数据库", 500)
  50. }
  51. if err := table.First(&doc).Error; err != nil {
  52. return doc, err
  53. }
  54. return doc, nil
  55. }