o2o公共业务代码
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.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package db
  2. import (
  3. "database/sql"
  4. _ "github.com/go-sql-driver/mysql" //必须导入mysql驱动,否则会panic
  5. "xorm.io/xorm"
  6. )
  7. var Db *xorm.Engine
  8. /********************************************* 公用方法 *********************************************/
  9. // 数据批量插入
  10. func DbInsertBatch(Db *xorm.Engine, m ...interface{}) error {
  11. if len(m) == 0 {
  12. return nil
  13. }
  14. id, err := Db.Insert(m...)
  15. if id == 0 || err != nil {
  16. return err
  17. }
  18. return nil
  19. }
  20. // QueryNativeString 查询原生sql
  21. func QueryNativeString(Db *xorm.Engine, sql string, args ...interface{}) ([]map[string]string, error) {
  22. results, err := Db.SQL(sql, args...).QueryString()
  23. return results, err
  24. }
  25. // UpdateComm common update
  26. func UpdateComm(Db *xorm.Engine, id interface{}, model interface{}) (int64, error) {
  27. row, err := Db.ID(id).Update(model)
  28. return row, err
  29. }
  30. // InsertComm common insert
  31. func InsertComm(Db *xorm.Engine, model interface{}) (int64, error) {
  32. row, err := Db.InsertOne(model)
  33. return row, err
  34. }
  35. // GetComm
  36. // payload *model
  37. // return *model,has,err
  38. func GetComm(Db *xorm.Engine, model interface{}) (interface{}, bool, error) {
  39. has, err := Db.Get(model)
  40. if err != nil {
  41. return nil, false, err
  42. }
  43. return model, has, nil
  44. }
  45. // InsertCommWithSession common insert
  46. func InsertCommWithSession(session *xorm.Session, model interface{}) (int64, error) {
  47. row, err := session.InsertOne(model)
  48. return row, err
  49. }
  50. // ExecuteOriginalSql 执行原生sql
  51. func ExecuteOriginalSql(session *xorm.Session, sql string) (sql.Result, error) {
  52. result, err := session.Exec(sql)
  53. if err != nil {
  54. return nil, err
  55. }
  56. return result, nil
  57. }