dbcolumns.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 DBColumns struct {
  10. TableSchema string `gorm:"column:TABLE_SCHEMA" json:"tableSchema"`
  11. TableName string `gorm:"column:TABLE_NAME" json:"tableName"`
  12. ColumnName string `gorm:"column:COLUMN_NAME" json:"columnName"`
  13. ColumnDefault string `gorm:"column:COLUMN_DEFAULT" json:"columnDefault"`
  14. IsNullable string `gorm:"column:IS_NULLABLE" json:"isNullable"`
  15. DataType string `gorm:"column:DATA_TYPE" json:"dataType"`
  16. CharacterMaximumLength string `gorm:"column:CHARACTER_MAXIMUM_LENGTH" json:"characterMaximumLength"`
  17. CharacterSetName string `gorm:"column:CHARACTER_SET_NAME" json:"characterSetName"`
  18. ColumnType string `gorm:"column:COLUMN_TYPE" json:"columnType"`
  19. ColumnKey string `gorm:"column:COLUMN_KEY" json:"columnKey"`
  20. Extra string `gorm:"column:EXTRA" json:"extra"`
  21. ColumnComment string `gorm:"column:COLUMN_COMMENT" json:"columnComment"`
  22. }
  23. func (e *DBColumns) GetPage(pageSize int, pageIndex int) ([]DBColumns, int, error) {
  24. var doc []DBColumns
  25. var count int64
  26. table := new(gorm.DB)
  27. if config2.DatabaseConfig.Driver == "mysql" {
  28. table = orm.Eloquent.Table("information_schema.`COLUMNS`")
  29. table = table.Where("table_schema= ? ", config2.GenConfig.DBName)
  30. if e.TableName != "" {
  31. return nil, 0, errors.New("table name cannot be empty!")
  32. }
  33. table = table.Where("TABLE_NAME = ?", e.TableName)
  34. }
  35. if err := table.Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Offset(-1).Limit(-1).Count(&count).Error; err != nil {
  36. return nil, 0, err
  37. }
  38. //table.Count(&count)
  39. return doc, int(count), nil
  40. }
  41. func (e *DBColumns) GetList() ([]DBColumns, error) {
  42. var doc []DBColumns
  43. table := new(gorm.DB)
  44. if e.TableName == "" {
  45. return nil, errors.New("table name cannot be empty!")
  46. }
  47. if config2.DatabaseConfig.Driver == "mysql" {
  48. table = orm.Eloquent.Table("information_schema.columns")
  49. table = table.Where("table_schema= ? ", config2.GenConfig.DBName)
  50. table = table.Where("TABLE_NAME = ?", e.TableName).Order("ORDINAL_POSITION asc")
  51. } else {
  52. tools.Assert(true, "目前只支持mysql数据库", 500)
  53. }
  54. if err := table.Find(&doc).Error; err != nil {
  55. return doc, err
  56. }
  57. return doc, nil
  58. }