|
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- package db
-
- import (
- "database/sql"
- _ "github.com/go-sql-driver/mysql" //必须导入mysql驱动,否则会panic
- "xorm.io/xorm"
- )
-
- var Db *xorm.Engine
-
- /********************************************* 公用方法 *********************************************/
-
- // 数据批量插入
- func DbInsertBatch(Db *xorm.Engine, m ...interface{}) error {
- if len(m) == 0 {
- return nil
- }
- id, err := Db.Insert(m...)
- if id == 0 || err != nil {
- return err
- }
- return nil
- }
-
- // QueryNativeString 查询原生sql
- func QueryNativeString(Db *xorm.Engine, sql string, args ...interface{}) ([]map[string]string, error) {
- results, err := Db.SQL(sql, args...).QueryString()
- return results, err
- }
-
- // UpdateComm common update
- func UpdateComm(Db *xorm.Engine, id interface{}, model interface{}) (int64, error) {
- row, err := Db.ID(id).Update(model)
- return row, err
- }
-
- // InsertComm common insert
- func InsertComm(Db *xorm.Engine, model interface{}) (int64, error) {
- row, err := Db.InsertOne(model)
- return row, err
- }
-
- // GetComm
- // payload *model
- // return *model,has,err
- func GetComm(Db *xorm.Engine, model interface{}) (interface{}, bool, error) {
- has, err := Db.Get(model)
- if err != nil {
- return nil, false, err
- }
- return model, has, nil
- }
-
- // InsertCommWithSession common insert
- func InsertCommWithSession(session *xorm.Session, model interface{}) (int64, error) {
- row, err := session.InsertOne(model)
- return row, err
- }
-
- // ExecuteOriginalSql 执行原生sql
- func ExecuteOriginalSql(session *xorm.Session, sql string) (sql.Result, error) {
- result, err := session.Exec(sql)
- if err != nil {
- return nil, err
- }
- return result, nil
- }
|