智慧食堂
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

há 1 ano
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package db
  2. import (
  3. "database/sql"
  4. "fmt"
  5. "os"
  6. _ "github.com/go-sql-driver/mysql"
  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. func InitDB(c *cfg.DBCfg) error {
  14. var err error
  15. 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 {
  16. return err
  17. }
  18. Db.SetConnMaxLifetime(c.MaxLifetime)
  19. Db.SetMaxOpenConns(c.MaxOpenConns)
  20. Db.SetMaxIdleConns(c.MaxIdleConns)
  21. if err = Db.Ping(); err != nil {
  22. return err
  23. }
  24. if c.ShowLog {
  25. Db.ShowSQL(true)
  26. Db.Logger().SetLevel(0)
  27. f, err := os.OpenFile(c.Path, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0777)
  28. if err != nil {
  29. os.RemoveAll(c.Path)
  30. if f, err = os.OpenFile(c.Path, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0777); err != nil {
  31. return err
  32. }
  33. }
  34. logger := log.NewSimpleLogger(f)
  35. logger.ShowSQL(true)
  36. Db.SetLogger(logger)
  37. }
  38. return nil
  39. }
  40. /********************************************* 公用方法 *********************************************/
  41. // DbInsertBatch 数据批量插入
  42. func DbInsertBatch(Db *xorm.Engine, m ...interface{}) error {
  43. if len(m) == 0 {
  44. return nil
  45. }
  46. id, err := Db.Insert(m...)
  47. if id == 0 || err != nil {
  48. return logx.Warn("cannot insert data :", err)
  49. }
  50. return nil
  51. }
  52. // QueryNativeString 查询原生sql
  53. func QueryNativeString(Db *xorm.Engine, sql string, args ...interface{}) ([]map[string]string, error) {
  54. results, err := Db.SQL(sql, args...).QueryString()
  55. return results, err
  56. }
  57. func QueryNativeStringSess(sess *xorm.Session, sql string, args ...interface{}) ([]map[string]string, error) {
  58. results, err := sess.SQL(sql, args...).QueryString()
  59. return results, err
  60. }
  61. // CommonInsert 插入一条或多条数据
  62. func CommonInsert(Db *xorm.Engine, data interface{}) (int64, error) {
  63. row, err := Db.Insert(data)
  64. return row, err
  65. }
  66. // UpdateComm 根据主键更新
  67. func UpdateComm(Db *xorm.Engine, id interface{}, model interface{}) (int64, error) {
  68. row, err := Db.ID(id).Update(model)
  69. return row, err
  70. }
  71. // InsertOneComm 插入一条数据
  72. func InsertOneComm(Db *xorm.Engine, model interface{}) (int64, error) {
  73. row, err := Db.InsertOne(model)
  74. return row, err
  75. }
  76. // GetComm 获取一条数据
  77. // payload *model
  78. // return *model,has,err
  79. func GetComm(Db *xorm.Engine, model interface{}) (interface{}, bool, error) {
  80. has, err := Db.Get(model)
  81. if err != nil {
  82. _ = logx.Warn(err)
  83. return nil, false, err
  84. }
  85. return model, has, nil
  86. }
  87. // ExecuteOriginalSql 执行原生sql
  88. func ExecuteOriginalSql(Db *xorm.Engine, sql string) (sql.Result, error) {
  89. result, err := Db.Exec(sql)
  90. if err != nil {
  91. _ = logx.Warn(err)
  92. return nil, err
  93. }
  94. return result, nil
  95. }
  96. // InsertCommWithSession common insert
  97. func InsertCommWithSession(session *xorm.Session, model interface{}) (int64, error) {
  98. row, err := session.InsertOne(model)
  99. return row, err
  100. }