附近小店
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

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