models.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package models
  2. import (
  3. "fmt"
  4. "log"
  5. "time"
  6. "../pkg/setting"
  7. "../routers/restful"
  8. "github.com/astaxie/beego/logs"
  9. "github.com/xormplus/xorm"
  10. )
  11. var Engine *xorm.Engine
  12. var SqlServerEngine *xorm.Engine
  13. func Setup() {
  14. var err error
  15. // restful 接口的 数据库初始化
  16. if err := restful.SQLInit("mysql", fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8&parseTime=True&loc=Local",
  17. setting.DatabaseSetting.User,
  18. setting.DatabaseSetting.Password,
  19. setting.DatabaseSetting.Host,
  20. setting.DatabaseSetting.Name), 1000, 10, setting.DatabaseSetting.ShowXormlog); err != nil {
  21. log.Fatal(err.Error())
  22. fmt.Println("dbs 数据库初始化失败 ")
  23. }
  24. Engine = restful.Engine
  25. if err != nil {
  26. log.Fatalf("models.Setup err: %v", err)
  27. }
  28. Engine.SetConnMaxLifetime(time.Second * 30)
  29. Engine.ShowSQL(true)
  30. if err = Engine.Ping(); err != nil {
  31. logs.Error(err)
  32. panic(err)
  33. }
  34. }
  35. func SetupSqlserver() {
  36. // if setting.SQlserverSetting.Host != "" {
  37. // var err error
  38. // // restful 接口的 数据库初始化
  39. // if err := restful.SQLInit("odbc", setting.SQlserverSetting.Host, 1000, 10, setting.DatabaseSetting.ShowXormlog); err != nil {
  40. // log.Fatal(err.Error())
  41. // fmt.Println("dbs 数据库初始化失败 ")
  42. // }
  43. // SqlServerEngine = restful.Engine
  44. // if err != nil {
  45. // log.Fatalf("models.Setup err: %v", err)
  46. // }
  47. // SqlServerEngine.SetConnMaxLifetime(time.Second * 30)
  48. // SqlServerEngine.ShowSQL(true)
  49. // if err = SqlServerEngine.Ping(); err != nil {
  50. // logs.Error(err)
  51. // panic(err)
  52. // }
  53. // }
  54. }