golang 的 rabbitmq 消费项目
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.

db.go 3.1 KiB

2 years ago
2 years ago
2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. // QueryNativeString 查询原生sql
  61. func QueryNativeString(Db *xorm.Engine, sql string, args ...interface{}) ([]map[string]string, error) {
  62. results, err := Db.SQL(sql, args...).QueryString()
  63. return results, err
  64. }
  65. func QueryNativeStringWithSession(session *xorm.Session, sql string, args ...interface{}) ([]map[string]string, error) {
  66. results, err := session.SQL(sql, args...).QueryString()
  67. return results, err
  68. }
  69. // UpdateComm common update
  70. func UpdateComm(Db *xorm.Engine, id interface{}, model interface{}) (int64, error) {
  71. row, err := Db.ID(id).Update(model)
  72. return row, err
  73. }
  74. // InsertComm common insert
  75. func InsertComm(Db *xorm.Engine, model interface{}) (int64, error) {
  76. row, err := Db.InsertOne(model)
  77. return row, err
  78. }
  79. // GetComm
  80. // payload *model
  81. // return *model,has,err
  82. func GetComm(Db *xorm.Engine, model interface{}) (interface{}, bool, error) {
  83. has, err := Db.Get(model)
  84. if err != nil {
  85. _ = logx.Warn(err)
  86. return nil, false, err
  87. }
  88. return model, has, nil
  89. }
  90. // InsertCommWithSession common insert
  91. func InsertCommWithSession(session *xorm.Session, model interface{}) (int64, error) {
  92. row, err := session.InsertOne(model)
  93. return row, err
  94. }
  95. // ExecuteOriginalSql 执行原生sql
  96. func ExecuteOriginalSql(session *xorm.Session, sql string) (sql.Result, error) {
  97. result, err := session.Exec(sql)
  98. if err != nil {
  99. _ = logx.Warn(err)
  100. return nil, err
  101. }
  102. return result, nil
  103. }