工具
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

53 lines
1021 B

4 years ago
  1. package hasaki_{{.app_name}}
  2. import (
  3. "git.ouxuan.net/hasaki-service/hasaki-sdk/hskdb"
  4. "git.ouxuan.net/hasaki-service/hasaki-service-{{.app_name}}/hasaki_{{.app_name}}_options"
  5. "xorm.io/xorm"
  6. )
  7. type Api struct {
  8. db *xorm.Engine
  9. config interface{}
  10. session *xorm.Session
  11. }
  12. func NewApi(dbs ...*xorm.Engine) *Api {
  13. var db *xorm.Engine
  14. if len(dbs) > 0 {
  15. db = dbs[0]
  16. } else {
  17. db = hskdb.GetXormAuto()
  18. }
  19. return &Api{
  20. db: db,
  21. session: db.NewSession(),
  22. }
  23. }
  24. func (api *Api) SetSession(s *xorm.Session) {
  25. api.session = s
  26. }
  27. func (api *Api) NewSession() *xorm.Session {
  28. return api.session
  29. }
  30. func GetApi(ctx interface{}) *Api {
  31. config := hasaki_value_card_options.GetMysqlConfig(ctx)
  32. if config == nil {
  33. api := NewApi(hskdb.GetXormAuto())
  34. return api
  35. } else {
  36. api := NewApi(hskdb.GetXormAuto(config))
  37. api.config = config
  38. return api
  39. }
  40. }
  41. func (api *Api) InitDb() (err error) {
  42. return InitDb(api.config)
  43. }
  44. func InitDbByConfig(config interface{}) (err error) {
  45. return InitDb(config)
  46. }