db.go 754 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package orm
  2. import (
  3. "github.com/go-xorm/xorm"
  4. )
  5. const (
  6. tableNameKey = "table_name"
  7. )
  8. const (
  9. tableNamesSql = `select table_name from information_schema.tables where table_schema = ? and table_type = 'base table';`
  10. )
  11. type mysqlMeta struct {
  12. Db *xorm.Engine
  13. DbName string
  14. }
  15. func NewMysqlMeta(dbName string, db *xorm.Engine) DbMeta {
  16. return &mysqlMeta{
  17. Db: db,
  18. DbName: dbName,
  19. }
  20. }
  21. func (this *mysqlMeta) GetTables() ([]string, error) {
  22. list, err := this.Db.SQL(tableNamesSql, this.DbName).QueryString()
  23. if err != nil {
  24. return nil, err
  25. }
  26. result := make([]string, 0, len(list))
  27. for _, e := range list {
  28. tableName, isExist := e[tableNameKey]
  29. if isExist {
  30. result = append(result, tableName)
  31. }
  32. }
  33. return result, nil
  34. }