|
@@ -11,13 +11,8 @@ import (
|
|
|
// 带事务的, 和不带事务的 说明:
|
|
|
// 如果需要支持事务请调aaaTx方法, 并传开启事务的DB
|
|
|
|
|
|
-// OneById 通过ID查询一条
|
|
|
-func OneById(m interface{}, id uint) (err error) {
|
|
|
- return OneByIdTx(DB, m, id)
|
|
|
-}
|
|
|
-
|
|
|
// OneByIdTx 通过ID查询一条
|
|
|
-func OneByIdTx(tx *gorm.DB, m interface{}, id uint) (err error) {
|
|
|
+func OneById(tx *gorm.DB, m interface{}, id uint) (err error) {
|
|
|
if id == 0 {
|
|
|
return errors.New("主键ID不能为空")
|
|
|
}
|
|
@@ -27,13 +22,8 @@ func OneByIdTx(tx *gorm.DB, m interface{}, id uint) (err error) {
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
-// One 查询一条 使用m的有值属性作为条件, m必须是Model结构体. 条件为空时报错.
|
|
|
-func One(m interface{}) (err error) {
|
|
|
- return OneTx(DB, m)
|
|
|
-}
|
|
|
-
|
|
|
// OneTx 查询一条 使用m的有值属性作为条件, m必须是Model结构体. 条件为空时报错.
|
|
|
-func OneTx(tx *gorm.DB, m interface{}) error {
|
|
|
+func One(tx *gorm.DB, m interface{}) error {
|
|
|
return oneTx(tx, m, "")
|
|
|
}
|
|
|
|
|
@@ -59,23 +49,13 @@ func oneTx(tx *gorm.DB, m interface{}, Type string) (err error) {
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
-// First 查询第一条 使用m的有值属性作为条件, m必须是Model结构体. 条件为空时报错.
|
|
|
-func First(m interface{}) (err error) {
|
|
|
- return FirstTx(DB, m)
|
|
|
-}
|
|
|
-
|
|
|
// FirstTx 查询第一条 使用m的有值属性作为条件, m必须是Model结构体. 条件为空时报错.
|
|
|
-func FirstTx(tx *gorm.DB, m interface{}) error {
|
|
|
+func First(tx *gorm.DB, m interface{}) error {
|
|
|
return oneTx(tx, m, "first")
|
|
|
}
|
|
|
|
|
|
-// Last 查询最后一条 使用m的有值属性作为条件, m必须是Model结构体. 条件为空时报错.
|
|
|
-func Last(m interface{}) (err error) {
|
|
|
- return LastTx(DB, m)
|
|
|
-}
|
|
|
-
|
|
|
// LastTx 查询最后一条 使用m的有值属性作为条件, m必须是Model结构体. 条件为空时报错.
|
|
|
-func LastTx(tx *gorm.DB, m interface{}) error {
|
|
|
+func Last(tx *gorm.DB, m interface{}) error {
|
|
|
return oneTx(tx, m, "last")
|
|
|
}
|
|
|
|
|
@@ -88,23 +68,13 @@ func LastTx(tx *gorm.DB, m interface{}) error {
|
|
|
// return nil
|
|
|
//}
|
|
|
|
|
|
-// Create 新增
|
|
|
-func Create(m interface{}) error {
|
|
|
- return CreateTx(DB, m)
|
|
|
-}
|
|
|
-
|
|
|
// CreateTx 带事务的, 新增
|
|
|
-func CreateTx(tx *gorm.DB, m interface{}) error {
|
|
|
+func Create(tx *gorm.DB, m interface{}) error {
|
|
|
return tx.Create(m).Error
|
|
|
}
|
|
|
|
|
|
-// Update 更新一条数据的单个字段, m.ID必须有值
|
|
|
-func Update(m interface{}, field string, value interface{}) error {
|
|
|
- return UpdateTx(DB, m, field, value)
|
|
|
-}
|
|
|
-
|
|
|
// UpdateTx 带事务的, 更新一条数据的单个字段, m.ID必须有值
|
|
|
-func UpdateTx(tx *gorm.DB, m interface{}, field string, value interface{}) error {
|
|
|
+func Update(tx *gorm.DB, m interface{}, field string, value interface{}) error {
|
|
|
db := tx.Model(m).Update(field, value)
|
|
|
if err := db.Error; err != nil {
|
|
|
return err
|
|
@@ -115,17 +85,8 @@ func UpdateTx(tx *gorm.DB, m interface{}, field string, value interface{}) error
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
-// Updates 更新一条数据的多个字段, m.ID必须有值
|
|
|
-// FIXME: 只会更新非零值的字段!!
|
|
|
-// 尽量不要让m和fields是同一个对象, fields只赋值需要更新的字段
|
|
|
-// TODO 支持更新成零值, 支持结构体 map, 放在表结构体里? 不通过ID更新?
|
|
|
-// TODO: fields 里不能ID有值, 否则也更新ID, 这是不行的
|
|
|
-func Updates(m interface{}, fields interface{}) error {
|
|
|
- return UpdatesTx(DB, m, fields)
|
|
|
-}
|
|
|
-
|
|
|
// UpdatesTx 带事务的, 更新一条数据的多个字段, m.ID必须有值
|
|
|
-func UpdatesTx(tx *gorm.DB, m interface{}, fields interface{}) error {
|
|
|
+func Updates(tx *gorm.DB, m interface{}, fields interface{}) error {
|
|
|
db := tx.Model(m).Updates(fields)
|
|
|
if err := db.Error; err != nil {
|
|
|
return err
|
|
@@ -136,15 +97,8 @@ func UpdatesTx(tx *gorm.DB, m interface{}, fields interface{}) error {
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
-// Delete 删除, m.ID必须有值.
|
|
|
-func Delete(m interface{}) error {
|
|
|
- //func Delete(m interface{}, conds ...interface{}) error { // 不做这种支持, 由ma.DB.Delete()去支持
|
|
|
- return DeleteTx(DB, m)
|
|
|
- //return DeleteTx(nil, m, conds...)
|
|
|
-}
|
|
|
-
|
|
|
// DeleteTx 删除, m.ID必须有值. tx不为空就是带事务的
|
|
|
-func DeleteTx(tx *gorm.DB, m interface{}) error {
|
|
|
+func Delete(tx *gorm.DB, m interface{}) error {
|
|
|
//func DeleteTx(tx *gorm.DB, m interface{}, conds ...interface{}) error {
|
|
|
db := tx.Delete(m)
|
|
|
//db := tx.Delete(m, conds...)
|
|
@@ -157,13 +111,8 @@ func DeleteTx(tx *gorm.DB, m interface{}) error {
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
-// List 查询数据列表, m是库表结构体, m的有值属性会作为查询条件, 且必须有条件, list里个体的类型可以与m的类型不同
|
|
|
-func List(m interface{}, list interface{}) (err error) {
|
|
|
- return ListTx(DB, m, list)
|
|
|
-}
|
|
|
-
|
|
|
// ListTx 查询数据列表, m是库表结构体, m的有值属性会作为查询条件, 且必须有条件, list里个体的类型可以与m的类型不同
|
|
|
-func ListTx(tx *gorm.DB, m interface{}, list interface{}) (err error) {
|
|
|
+func List(tx *gorm.DB, m interface{}, list interface{}) (err error) {
|
|
|
if lxUtil.IsZeroOfUnderlyingType(m) {
|
|
|
return errors.New("条件不能为空")
|
|
|
}
|
|
@@ -174,13 +123,8 @@ func ListTx(tx *gorm.DB, m interface{}, list interface{}) (err error) {
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
-// ListAll 查询所有数据, m是库表结构体, m的有值属性不会作为查询条件, list里个体的类型可以与m的类型不同
|
|
|
-func ListAll(m interface{}, list interface{}) (err error) {
|
|
|
- return ListAllTx(DB, m, list)
|
|
|
-}
|
|
|
-
|
|
|
// ListAllTx 查询所有数据, m是库表结构体, m的有值属性不会作为查询条件, list里个体的类型可以与m的类型不同
|
|
|
-func ListAllTx(tx *gorm.DB, m interface{}, list interface{}) (err error) {
|
|
|
+func ListAll(tx *gorm.DB, m interface{}, list interface{}) (err error) {
|
|
|
if tx.Model(m).Find(list).Error != nil {
|
|
|
// if tx.Where(m).Find(list).Error != nil {
|
|
|
return errors.New("查询出现错误")
|
|
@@ -188,14 +132,8 @@ func ListAllTx(tx *gorm.DB, m interface{}, list interface{}) (err error) {
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
-// Query 查询数据列表, 支持分页、总数、汇总, 见PaginationQuery属性. PaginationQuery不可为nil
|
|
|
-func Query(m interface{}, list interface{}, q *PaginationQuery) (err error) {
|
|
|
- QueryTx(DB, m, list, q)
|
|
|
- return
|
|
|
-}
|
|
|
-
|
|
|
// QueryTx 带事务的查询数据列表. tx为空就是不带事务, 否则认为是开启了事务. 你应该避免使用次方法, 而是使用Query
|
|
|
-func QueryTx(tx *gorm.DB, m interface{}, list interface{}, q *PaginationQuery) (err error) {
|
|
|
+func Query(tx *gorm.DB, m interface{}, list interface{}, q *PaginationQuery) (err error) {
|
|
|
// !! 关于会话 新的Statement实例 及复用 ref: https://gorm.io/zh_CN/docs/method_chaining.html
|
|
|
|
|
|
if q == nil {
|
|
@@ -268,12 +206,7 @@ func QueryTx(tx *gorm.DB, m interface{}, list interface{}, q *PaginationQuery) (
|
|
|
//
|
|
|
//}
|
|
|
|
|
|
-// SqlQuery 原生SQL查询列表, 支持分页. PaginationQuery可为nil
|
|
|
-func SqlQuery(sql string, list interface{}, q *PaginationQuery, params ...interface{}) (err error) {
|
|
|
- return SqlQueryTx(DB, sql, list, q, params...)
|
|
|
-}
|
|
|
-
|
|
|
-func SqlQueryTx(tx *gorm.DB, sql string, list interface{}, q *PaginationQuery, params ...interface{}) (err error) {
|
|
|
+func SqlQuery(tx *gorm.DB, sql string, list interface{}, q *PaginationQuery, params ...interface{}) (err error) {
|
|
|
var builder strings.Builder
|
|
|
builder.WriteString(sql)
|
|
|
|