informationschemamodel.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package model
  2. import (
  3. "github.com/tal-tech/go-zero/core/stores/sqlx"
  4. )
  5. type (
  6. InformationSchemaModel struct {
  7. conn sqlx.SqlConn
  8. }
  9. Column struct {
  10. Name string `db:"COLUMN_NAME"`
  11. DataType string `db:"DATA_TYPE"`
  12. Key string `db:"COLUMN_KEY"`
  13. Extra string `db:"EXTRA"`
  14. Comment string `db:"COLUMN_COMMENT"`
  15. }
  16. )
  17. func NewInformationSchemaModel(conn sqlx.SqlConn) *InformationSchemaModel {
  18. return &InformationSchemaModel{conn: conn}
  19. }
  20. func (m *InformationSchemaModel) GetAllTables(database string) ([]string, error) {
  21. query := `select TABLE_NAME from TABLES where TABLE_SCHEMA = ?`
  22. var tables []string
  23. err := m.conn.QueryRows(&tables, query, database)
  24. if err != nil {
  25. return nil, err
  26. }
  27. return tables, nil
  28. }
  29. func (m *InformationSchemaModel) FindByTableName(db, table string) ([]*Column, error) {
  30. querySql := `select COLUMN_NAME,DATA_TYPE,COLUMN_KEY,EXTRA,COLUMN_COMMENT from COLUMNS where TABLE_SCHEMA = ? and TABLE_NAME = ?`
  31. var reply []*Column
  32. err := m.conn.QueryRows(&reply, querySql, db, table)
  33. return reply, err
  34. }