广告涉及的mq都放这里
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

127 rindas
3.3 KiB

  1. package db
  2. import (
  3. "applet/app/utils/logx"
  4. "database/sql"
  5. "fmt"
  6. "os"
  7. "time"
  8. _ "github.com/go-sql-driver/mysql" //必须导入mysql驱动,否则会panic
  9. "xorm.io/xorm"
  10. "xorm.io/xorm/log"
  11. "applet/app/cfg"
  12. )
  13. var Db *xorm.Engine
  14. // 根据DB配置文件初始化数据库
  15. func InitDB(c *cfg.DBCfg) error {
  16. var (
  17. err error
  18. f *os.File
  19. )
  20. //创建Orm引擎
  21. if Db, err = xorm.NewEngine("mysql", fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8mb4", c.User, c.Psw, c.Host, c.Name)); err != nil {
  22. return err
  23. }
  24. Db.SetConnMaxLifetime(c.MaxLifetime * time.Second) //设置最长连接时间
  25. Db.SetMaxOpenConns(c.MaxOpenConns) //设置最大打开连接数
  26. Db.SetMaxIdleConns(c.MaxIdleConns) //设置连接池的空闲数大小
  27. if err = Db.Ping(); err != nil { //尝试ping数据库
  28. return err
  29. }
  30. if c.ShowLog { //根据配置文件设置日志
  31. Db.ShowSQL(true) //设置是否打印sql
  32. Db.Logger().SetLevel(0) //设置日志等级
  33. //修改日志文件存放路径文件名是%s.log
  34. path := fmt.Sprintf(c.Path, c.Name)
  35. f, err = os.OpenFile(path, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0777)
  36. if err != nil {
  37. os.RemoveAll(c.Path)
  38. if f, err = os.OpenFile(c.Path, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0777); err != nil {
  39. return err
  40. }
  41. }
  42. logger := log.NewSimpleLogger(f)
  43. logger.ShowSQL(true)
  44. Db.SetLogger(logger)
  45. }
  46. return nil
  47. }
  48. /********************************************* 公用方法 *********************************************/
  49. // 数据批量插入
  50. func DbInsertBatch(Db *xorm.Engine, m ...interface{}) error {
  51. if len(m) == 0 {
  52. return nil
  53. }
  54. id, err := Db.Insert(m...)
  55. if id == 0 || err != nil {
  56. return logx.Warn("cannot insert data :", err)
  57. }
  58. return nil
  59. }
  60. func DbInsertBatchSess(sess *xorm.Session, m ...interface{}) error {
  61. if len(m) == 0 {
  62. return nil
  63. }
  64. id, err := sess.Insert(m...)
  65. if id == 0 || err != nil {
  66. return logx.Warn("cannot insert data :", err)
  67. }
  68. return nil
  69. }
  70. // QueryNativeString 查询原生sql
  71. func QueryNativeString(Db *xorm.Engine, sql string, args ...interface{}) ([]map[string]string, error) {
  72. results, err := Db.SQL(sql, args...).QueryString()
  73. return results, err
  74. }
  75. func QueryNativeStringWithSession(session *xorm.Session, sql string, args ...interface{}) ([]map[string]string, error) {
  76. results, err := session.SQL(sql, args...).QueryString()
  77. return results, err
  78. }
  79. // UpdateComm common update
  80. func UpdateComm(Db *xorm.Engine, id interface{}, model interface{}) (int64, error) {
  81. row, err := Db.ID(id).Update(model)
  82. return row, err
  83. }
  84. // InsertComm common insert
  85. func InsertComm(Db *xorm.Engine, model interface{}) (int64, error) {
  86. row, err := Db.InsertOne(model)
  87. return row, err
  88. }
  89. // GetComm
  90. // payload *model
  91. // return *model,has,err
  92. func GetComm(Db *xorm.Engine, model interface{}) (interface{}, bool, error) {
  93. has, err := Db.Get(model)
  94. if err != nil {
  95. _ = logx.Warn(err)
  96. return nil, false, err
  97. }
  98. return model, has, nil
  99. }
  100. // InsertCommWithSession common insert
  101. func InsertCommWithSession(session *xorm.Session, model interface{}) (int64, error) {
  102. row, err := session.InsertOne(model)
  103. return row, err
  104. }
  105. // ExecuteOriginalSql 执行原生sql
  106. func ExecuteOriginalSql(session *xorm.Session, sql string) (sql.Result, error) {
  107. result, err := session.Exec(sql)
  108. if err != nil {
  109. _ = logx.Warn(err)
  110. return nil, err
  111. }
  112. return result, nil
  113. }