蛋蛋星球 后台端
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 2.9 KiB

2 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package db
  2. import (
  3. "database/sql"
  4. "fmt"
  5. "os"
  6. "time"
  7. _ "github.com/go-sql-driver/mysql" //必须导入mysql驱动,否则会panic
  8. "xorm.io/xorm"
  9. "xorm.io/xorm/log"
  10. "applet/app/cfg"
  11. "applet/app/utils/logx"
  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. // UpdateComm common update
  66. func UpdateComm(Db *xorm.Engine, id interface{}, model interface{}) (int64, error) {
  67. row, err := Db.ID(id).Update(model)
  68. return row, err
  69. }
  70. // InsertComm common insert
  71. func InsertComm(Db *xorm.Engine, model interface{}) (int64, error) {
  72. row, err := Db.InsertOne(model)
  73. return row, err
  74. }
  75. // ExecuteOriginalSql 执行原生sql
  76. func ExecuteOriginalSql(session *xorm.Session, sql string) (sql.Result, error) {
  77. result, err := session.Exec(sql)
  78. if err != nil {
  79. _ = logx.Warn(err)
  80. return nil, err
  81. }
  82. return result, nil
  83. }
  84. // GetComm
  85. // payload *model
  86. // return *model,has,err
  87. func GetComm(Db *xorm.Engine, model interface{}) (interface{}, bool, error) {
  88. has, err := Db.Get(model)
  89. if err != nil {
  90. _ = logx.Warn(err)
  91. return nil, false, err
  92. }
  93. return model, has, nil
  94. }
  95. // InsertCommWithSession common insert
  96. func InsertCommWithSession(session *xorm.Session, model interface{}) (int64, error) {
  97. row, err := session.InsertOne(model)
  98. return row, err
  99. }