db_tables.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package tools
  2. import (
  3. "IotAdmin/core/sdk/pkg"
  4. "errors"
  5. "gorm.io/gorm"
  6. config2 "IotAdmin/core/sdk/config"
  7. )
  8. type DBTables struct {
  9. TableName string `gorm:"column:TABLE_NAME" json:"tableName"`
  10. Engine string `gorm:"column:ENGINE" json:"engine"`
  11. TableRows string `gorm:"column:TABLE_ROWS" json:"tableRows"`
  12. TableCollation string `gorm:"column:TABLE_COLLATION" json:"tableCollation"`
  13. CreateTime string `gorm:"column:CREATE_TIME" json:"createTime"`
  14. UpdateTime string `gorm:"column:UPDATE_TIME" json:"updateTime"`
  15. TableComment string `gorm:"column:TABLE_COMMENT" json:"tableComment"`
  16. }
  17. func (e *DBTables) GetPage(tx *gorm.DB, pageSize int, pageIndex int) ([]DBTables, int, error) {
  18. var doc []DBTables
  19. table := new(gorm.DB)
  20. var count int64
  21. if config2.DatabaseConfig.Driver == "mysql" {
  22. table = tx.Table("information_schema.tables")
  23. table = table.Where("TABLE_NAME not in (select table_name from `" + config2.GenConfig.DBName + "`." + (&SysTables{}).TableName() + " )")
  24. table = table.Where("table_schema= ? ", config2.GenConfig.DBName)
  25. table = table.Where("table_name NOT LIKE '/_/_%' ESCAPE '/'")
  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. pkg.Assert(true, "目前只支持mysql数据库", 500)
  34. }
  35. //table.Count(&count)
  36. return doc, int(count), nil
  37. }
  38. func (e *DBTables) Get(tx *gorm.DB) (DBTables, error) {
  39. var doc DBTables
  40. if config2.DatabaseConfig.Driver == "mysql" {
  41. table := tx.Table("information_schema.tables")
  42. table = table.Where("table_schema= ? ", config2.GenConfig.DBName)
  43. if e.TableName == "" {
  44. return doc, errors.New("table name cannot be empty!")
  45. }
  46. table = table.Where("TABLE_NAME = ?", e.TableName)
  47. if err := table.First(&doc).Error; err != nil {
  48. return doc, err
  49. }
  50. } else {
  51. pkg.Assert(true, "目前只支持mysql数据库", 500)
  52. }
  53. return doc, nil
  54. }