2020-01-29 14:22:44 +03:00
|
|
|
package gorm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
2020-05-28 08:12:56 +03:00
|
|
|
"errors"
|
2020-03-08 08:24:08 +03:00
|
|
|
"reflect"
|
2020-02-22 15:57:29 +03:00
|
|
|
"strings"
|
2020-02-04 03:56:15 +03:00
|
|
|
|
|
|
|
"github.com/jinzhu/gorm/clause"
|
2020-01-29 14:22:44 +03:00
|
|
|
)
|
|
|
|
|
2020-02-03 05:40:03 +03:00
|
|
|
// Create insert the value into database
|
2020-03-09 15:37:01 +03:00
|
|
|
func (db *DB) Create(value interface{}) (tx *DB) {
|
2020-02-03 05:40:03 +03:00
|
|
|
tx = db.getInstance()
|
|
|
|
tx.Statement.Dest = value
|
2020-03-09 15:37:01 +03:00
|
|
|
tx.callbacks.Create().Execute(tx)
|
2020-02-03 05:40:03 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save update value in database, if the value doesn't have primary key, will insert it
|
2020-03-09 15:37:01 +03:00
|
|
|
func (db *DB) Save(value interface{}) (tx *DB) {
|
2020-01-29 14:22:44 +03:00
|
|
|
tx = db.getInstance()
|
2020-03-08 08:24:08 +03:00
|
|
|
tx.Statement.Dest = value
|
|
|
|
|
2020-04-16 05:29:18 +03:00
|
|
|
if err := tx.Statement.Parse(value); err == nil && tx.Statement.Schema != nil {
|
2020-03-08 08:24:08 +03:00
|
|
|
where := clause.Where{Exprs: make([]clause.Expression, len(tx.Statement.Schema.PrimaryFields))}
|
2020-05-24 15:44:37 +03:00
|
|
|
reflectValue := reflect.Indirect(reflect.ValueOf(value))
|
|
|
|
switch reflectValue.Kind() {
|
|
|
|
case reflect.Slice, reflect.Array:
|
|
|
|
tx.AddError(ErrPtrStructSupported)
|
|
|
|
case reflect.Struct:
|
|
|
|
for idx, pf := range tx.Statement.Schema.PrimaryFields {
|
|
|
|
if pv, isZero := pf.ValueOf(reflectValue); isZero {
|
|
|
|
tx.callbacks.Create().Execute(tx)
|
|
|
|
where.Exprs[idx] = clause.Eq{Column: pf.DBName, Value: pv}
|
|
|
|
return
|
|
|
|
}
|
2020-03-08 08:24:08 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tx.Statement.AddClause(where)
|
|
|
|
}
|
|
|
|
|
2020-05-23 18:50:48 +03:00
|
|
|
if len(tx.Statement.Selects) == 0 {
|
|
|
|
tx.Statement.Selects = append(tx.Statement.Selects, "*")
|
|
|
|
}
|
2020-03-09 15:37:01 +03:00
|
|
|
tx.callbacks.Update().Execute(tx)
|
2020-01-29 14:22:44 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// First find first record that match given conditions, order by primary key
|
2020-05-26 16:30:17 +03:00
|
|
|
func (db *DB) First(dest interface{}, conds ...interface{}) (tx *DB) {
|
2020-02-07 18:45:35 +03:00
|
|
|
tx = db.getInstance().Limit(1).Order(clause.OrderByColumn{
|
2020-02-04 03:56:15 +03:00
|
|
|
Column: clause.Column{Table: clause.CurrentTable, Name: clause.PrimaryKey},
|
|
|
|
})
|
2020-03-07 08:43:20 +03:00
|
|
|
if len(conds) > 0 {
|
|
|
|
tx.Statement.AddClause(clause.Where{Exprs: tx.Statement.BuildCondtion(conds[0], conds[1:]...)})
|
|
|
|
}
|
2020-03-03 09:18:12 +03:00
|
|
|
tx.Statement.RaiseErrorOnNotFound = true
|
2020-05-26 16:30:17 +03:00
|
|
|
tx.Statement.Dest = dest
|
2020-03-09 15:37:01 +03:00
|
|
|
tx.callbacks.Query().Execute(tx)
|
2020-01-29 14:22:44 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Take return a record that match given conditions, the order will depend on the database implementation
|
2020-05-26 16:30:17 +03:00
|
|
|
func (db *DB) Take(dest interface{}, conds ...interface{}) (tx *DB) {
|
2020-03-03 09:18:12 +03:00
|
|
|
tx = db.getInstance().Limit(1)
|
2020-03-07 08:43:20 +03:00
|
|
|
if len(conds) > 0 {
|
|
|
|
tx.Statement.AddClause(clause.Where{Exprs: tx.Statement.BuildCondtion(conds[0], conds[1:]...)})
|
|
|
|
}
|
2020-03-03 09:18:12 +03:00
|
|
|
tx.Statement.RaiseErrorOnNotFound = true
|
2020-05-26 16:30:17 +03:00
|
|
|
tx.Statement.Dest = dest
|
2020-03-09 15:37:01 +03:00
|
|
|
tx.callbacks.Query().Execute(tx)
|
2020-01-29 14:22:44 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Last find last record that match given conditions, order by primary key
|
2020-05-26 16:30:17 +03:00
|
|
|
func (db *DB) Last(dest interface{}, conds ...interface{}) (tx *DB) {
|
2020-02-23 18:28:35 +03:00
|
|
|
tx = db.getInstance().Limit(1).Order(clause.OrderByColumn{
|
|
|
|
Column: clause.Column{Table: clause.CurrentTable, Name: clause.PrimaryKey},
|
2020-03-04 06:32:36 +03:00
|
|
|
Desc: true,
|
2020-02-23 18:28:35 +03:00
|
|
|
})
|
2020-03-07 08:43:20 +03:00
|
|
|
if len(conds) > 0 {
|
|
|
|
tx.Statement.AddClause(clause.Where{Exprs: tx.Statement.BuildCondtion(conds[0], conds[1:]...)})
|
|
|
|
}
|
2020-03-03 09:18:12 +03:00
|
|
|
tx.Statement.RaiseErrorOnNotFound = true
|
2020-05-26 16:30:17 +03:00
|
|
|
tx.Statement.Dest = dest
|
2020-03-09 15:37:01 +03:00
|
|
|
tx.callbacks.Query().Execute(tx)
|
2020-01-29 14:22:44 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find find records that match given conditions
|
2020-05-26 16:30:17 +03:00
|
|
|
func (db *DB) Find(dest interface{}, conds ...interface{}) (tx *DB) {
|
2020-01-29 14:22:44 +03:00
|
|
|
tx = db.getInstance()
|
2020-03-07 08:43:20 +03:00
|
|
|
if len(conds) > 0 {
|
|
|
|
tx.Statement.AddClause(clause.Where{Exprs: tx.Statement.BuildCondtion(conds[0], conds[1:]...)})
|
|
|
|
}
|
2020-05-26 16:30:17 +03:00
|
|
|
tx.Statement.Dest = dest
|
2020-03-09 15:37:01 +03:00
|
|
|
tx.callbacks.Query().Execute(tx)
|
2020-01-29 14:22:44 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-28 08:12:56 +03:00
|
|
|
func (tx *DB) assignExprsToValue(exprs []clause.Expression) {
|
|
|
|
for _, expr := range exprs {
|
|
|
|
if eq, ok := expr.(clause.Eq); ok {
|
|
|
|
switch column := eq.Column.(type) {
|
|
|
|
case string:
|
|
|
|
if field := tx.Statement.Schema.LookUpField(column); field != nil {
|
|
|
|
field.Set(tx.Statement.ReflectValue, eq.Value)
|
|
|
|
}
|
|
|
|
case clause.Column:
|
|
|
|
if field := tx.Statement.Schema.LookUpField(column.Name); field != nil {
|
|
|
|
field.Set(tx.Statement.ReflectValue, eq.Value)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *DB) FirstOrInit(dest interface{}, conds ...interface{}) (tx *DB) {
|
2020-01-29 14:22:44 +03:00
|
|
|
tx = db.getInstance()
|
2020-05-28 08:12:56 +03:00
|
|
|
if tx = tx.First(dest, conds...); errors.Is(tx.Error, ErrRecordNotFound) {
|
|
|
|
if c, ok := tx.Statement.Clauses["WHERE"]; ok {
|
|
|
|
if where, ok := c.Expression.(clause.Where); ok {
|
|
|
|
tx.assignExprsToValue(where.Exprs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// initialize with attrs, conds
|
|
|
|
if len(tx.Statement.attrs) > 0 {
|
2020-05-28 11:10:10 +03:00
|
|
|
exprs := tx.Statement.BuildCondtion(tx.Statement.attrs[0], tx.Statement.attrs[1:]...)
|
2020-05-28 08:12:56 +03:00
|
|
|
tx.assignExprsToValue(exprs)
|
|
|
|
}
|
|
|
|
tx.Error = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// initialize with attrs, conds
|
|
|
|
if len(tx.Statement.assigns) > 0 {
|
2020-05-28 11:10:10 +03:00
|
|
|
exprs := tx.Statement.BuildCondtion(tx.Statement.assigns[0], tx.Statement.assigns[1:]...)
|
2020-05-28 08:12:56 +03:00
|
|
|
tx.assignExprsToValue(exprs)
|
|
|
|
}
|
2020-01-29 14:22:44 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-28 11:10:10 +03:00
|
|
|
func (db *DB) FirstOrCreate(dest interface{}, conds ...interface{}) (tx *DB) {
|
2020-01-29 14:22:44 +03:00
|
|
|
tx = db.getInstance()
|
2020-05-28 11:10:10 +03:00
|
|
|
if err := tx.First(dest, conds...).Error; errors.Is(err, ErrRecordNotFound) {
|
|
|
|
tx.Error = nil
|
|
|
|
|
|
|
|
if c, ok := tx.Statement.Clauses["WHERE"]; ok {
|
|
|
|
if where, ok := c.Expression.(clause.Where); ok {
|
|
|
|
tx.assignExprsToValue(where.Exprs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// initialize with attrs, conds
|
|
|
|
if len(tx.Statement.attrs) > 0 {
|
|
|
|
exprs := tx.Statement.BuildCondtion(tx.Statement.attrs[0], tx.Statement.attrs[1:]...)
|
|
|
|
tx.assignExprsToValue(exprs)
|
|
|
|
}
|
|
|
|
|
|
|
|
// initialize with attrs, conds
|
|
|
|
if len(tx.Statement.assigns) > 0 {
|
|
|
|
exprs := tx.Statement.BuildCondtion(tx.Statement.assigns[0], tx.Statement.assigns[1:]...)
|
|
|
|
tx.assignExprsToValue(exprs)
|
|
|
|
}
|
|
|
|
|
|
|
|
return tx.Create(dest)
|
|
|
|
} else if len(tx.Statement.assigns) > 0 {
|
|
|
|
exprs := tx.Statement.BuildCondtion(tx.Statement.assigns[0], tx.Statement.assigns[1:])
|
|
|
|
assigns := map[string]interface{}{}
|
|
|
|
for _, expr := range exprs {
|
|
|
|
if eq, ok := expr.(clause.Eq); ok {
|
|
|
|
switch column := eq.Column.(type) {
|
|
|
|
case string:
|
|
|
|
assigns[column] = eq.Value
|
|
|
|
case clause.Column:
|
|
|
|
assigns[column.Name] = eq.Value
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return tx.Model(dest).Updates(assigns)
|
|
|
|
}
|
2020-05-28 08:12:56 +03:00
|
|
|
|
2020-01-29 14:22:44 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update update attributes with callbacks, refer: https://jinzhu.github.io/gorm/crud.html#update
|
2020-03-09 15:37:01 +03:00
|
|
|
func (db *DB) Update(column string, value interface{}) (tx *DB) {
|
2020-01-29 14:22:44 +03:00
|
|
|
tx = db.getInstance()
|
2020-03-07 08:43:20 +03:00
|
|
|
tx.Statement.Dest = map[string]interface{}{column: value}
|
2020-03-09 15:37:01 +03:00
|
|
|
tx.callbacks.Update().Execute(tx)
|
2020-01-29 14:22:44 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Updates update attributes with callbacks, refer: https://jinzhu.github.io/gorm/crud.html#update
|
2020-03-09 15:37:01 +03:00
|
|
|
func (db *DB) Updates(values interface{}) (tx *DB) {
|
2020-01-29 14:22:44 +03:00
|
|
|
tx = db.getInstance()
|
2020-03-07 08:43:20 +03:00
|
|
|
tx.Statement.Dest = values
|
2020-03-09 15:37:01 +03:00
|
|
|
tx.callbacks.Update().Execute(tx)
|
2020-01-29 14:22:44 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-03-09 15:37:01 +03:00
|
|
|
func (db *DB) UpdateColumn(column string, value interface{}) (tx *DB) {
|
2020-01-29 14:22:44 +03:00
|
|
|
tx = db.getInstance()
|
2020-03-07 08:43:20 +03:00
|
|
|
tx.Statement.Dest = map[string]interface{}{column: value}
|
2020-05-30 12:34:22 +03:00
|
|
|
tx.Statement.DisableUpdateTime = true
|
2020-03-09 15:37:01 +03:00
|
|
|
tx.callbacks.Update().Execute(tx)
|
2020-01-29 14:22:44 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-03-09 15:37:01 +03:00
|
|
|
func (db *DB) UpdateColumns(values interface{}) (tx *DB) {
|
2020-01-29 14:22:44 +03:00
|
|
|
tx = db.getInstance()
|
2020-03-07 08:43:20 +03:00
|
|
|
tx.Statement.Dest = values
|
2020-05-30 12:34:22 +03:00
|
|
|
tx.Statement.DisableUpdateTime = true
|
2020-03-09 15:37:01 +03:00
|
|
|
tx.callbacks.Update().Execute(tx)
|
2020-01-29 14:22:44 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-03 05:40:03 +03:00
|
|
|
// Delete delete value match given conditions, if the value has primary key, then will including the primary key as condition
|
2020-03-09 15:37:01 +03:00
|
|
|
func (db *DB) Delete(value interface{}, conds ...interface{}) (tx *DB) {
|
2020-01-29 14:22:44 +03:00
|
|
|
tx = db.getInstance()
|
2020-03-08 09:51:52 +03:00
|
|
|
if len(conds) > 0 {
|
|
|
|
tx.Statement.AddClause(clause.Where{Exprs: tx.Statement.BuildCondtion(conds[0], conds[1:]...)})
|
|
|
|
}
|
|
|
|
tx.Statement.Dest = value
|
2020-03-09 15:37:01 +03:00
|
|
|
tx.callbacks.Delete().Execute(tx)
|
2020-01-29 14:22:44 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-24 06:32:59 +03:00
|
|
|
func (db *DB) Count(count *int64) (tx *DB) {
|
2020-01-29 14:22:44 +03:00
|
|
|
tx = db.getInstance()
|
2020-05-24 06:32:59 +03:00
|
|
|
if len(tx.Statement.Selects) == 0 {
|
|
|
|
tx.Statement.Selects = []string{"count(1)"}
|
|
|
|
}
|
|
|
|
if tx.Statement.Model == nil {
|
|
|
|
tx.Statement.Model = tx.Statement.Dest
|
|
|
|
}
|
|
|
|
tx.Statement.Dest = count
|
|
|
|
tx.callbacks.Query().Execute(tx)
|
|
|
|
if db.RowsAffected != 1 {
|
|
|
|
*count = db.RowsAffected
|
|
|
|
}
|
2020-01-29 14:22:44 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-03-09 15:37:01 +03:00
|
|
|
func (db *DB) Row() *sql.Row {
|
2020-02-22 14:41:01 +03:00
|
|
|
tx := db.getInstance()
|
2020-03-09 15:37:01 +03:00
|
|
|
tx.callbacks.Row().Execute(tx)
|
2020-02-22 14:41:01 +03:00
|
|
|
return tx.Statement.Dest.(*sql.Row)
|
2020-02-03 05:40:03 +03:00
|
|
|
}
|
|
|
|
|
2020-03-09 15:37:01 +03:00
|
|
|
func (db *DB) Rows() (*sql.Rows, error) {
|
2020-02-22 14:41:01 +03:00
|
|
|
tx := db.Set("rows", true)
|
2020-03-09 15:37:01 +03:00
|
|
|
tx.callbacks.Row().Execute(tx)
|
2020-02-22 14:41:01 +03:00
|
|
|
return tx.Statement.Dest.(*sql.Rows), tx.Error
|
2020-02-03 05:40:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Scan scan value to a struct
|
2020-03-09 15:37:01 +03:00
|
|
|
func (db *DB) Scan(dest interface{}) (tx *DB) {
|
2020-01-30 10:14:48 +03:00
|
|
|
tx = db.getInstance()
|
2020-05-26 16:30:17 +03:00
|
|
|
tx.Statement.Dest = dest
|
|
|
|
tx.callbacks.Query().Execute(tx)
|
2020-01-30 10:14:48 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-31 13:51:43 +03:00
|
|
|
// Pluck used to query single column from a model as a map
|
|
|
|
// var ages []int64
|
|
|
|
// db.Find(&users).Pluck("age", &ages)
|
|
|
|
func (db *DB) Pluck(column string, dest interface{}) (tx *DB) {
|
|
|
|
tx = db.getInstance()
|
|
|
|
tx.Statement.Dest = dest
|
|
|
|
tx.callbacks.Query().Execute(tx)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-26 18:13:05 +03:00
|
|
|
func (db *DB) ScanRows(rows *sql.Rows, dest interface{}) error {
|
|
|
|
tx := db.getInstance()
|
|
|
|
tx.Error = tx.Statement.Parse(dest)
|
|
|
|
tx.Statement.Dest = dest
|
|
|
|
tx.Statement.ReflectValue = reflect.Indirect(reflect.ValueOf(dest))
|
|
|
|
Scan(rows, tx, true)
|
|
|
|
return tx.Error
|
2020-01-30 10:14:48 +03:00
|
|
|
}
|
|
|
|
|
2020-02-23 18:28:35 +03:00
|
|
|
// Transaction start a transaction as a block, return error will rollback, otherwise to commit.
|
2020-03-09 15:37:01 +03:00
|
|
|
func (db *DB) Transaction(fc func(tx *DB) error, opts ...*sql.TxOptions) (err error) {
|
2020-01-29 14:22:44 +03:00
|
|
|
panicked := true
|
|
|
|
tx := db.Begin(opts...)
|
|
|
|
defer func() {
|
|
|
|
// Make sure to rollback when panic, Block error or Commit error
|
|
|
|
if panicked || err != nil {
|
|
|
|
tx.Rollback()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2020-02-22 18:08:20 +03:00
|
|
|
err = fc(tx.Session(&Session{}))
|
2020-01-29 14:22:44 +03:00
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
err = tx.Commit().Error
|
|
|
|
}
|
|
|
|
|
|
|
|
panicked = false
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-23 18:28:35 +03:00
|
|
|
// Begin begins a transaction
|
2020-03-09 15:37:01 +03:00
|
|
|
func (db *DB) Begin(opts ...*sql.TxOptions) (tx *DB) {
|
2020-01-29 14:22:44 +03:00
|
|
|
tx = db.getInstance()
|
2020-03-09 08:10:48 +03:00
|
|
|
if beginner, ok := tx.Statement.ConnPool.(TxBeginner); ok {
|
2020-02-23 18:28:35 +03:00
|
|
|
var opt *sql.TxOptions
|
|
|
|
var err error
|
|
|
|
if len(opts) > 0 {
|
|
|
|
opt = opts[0]
|
|
|
|
}
|
|
|
|
|
2020-05-31 13:51:43 +03:00
|
|
|
if tx.Statement.ConnPool, err = beginner.BeginTx(tx.Statement.Context, opt); err != nil {
|
2020-02-23 18:28:35 +03:00
|
|
|
tx.AddError(err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
tx.AddError(ErrInvalidTransaction)
|
|
|
|
}
|
2020-01-29 14:22:44 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-23 18:28:35 +03:00
|
|
|
// Commit commit a transaction
|
2020-03-09 15:37:01 +03:00
|
|
|
func (db *DB) Commit() *DB {
|
2020-03-09 08:10:48 +03:00
|
|
|
if comminter, ok := db.Statement.ConnPool.(TxCommiter); ok && comminter != nil {
|
2020-02-23 18:28:35 +03:00
|
|
|
db.AddError(comminter.Commit())
|
|
|
|
} else {
|
|
|
|
db.AddError(ErrInvalidTransaction)
|
|
|
|
}
|
|
|
|
return db
|
2020-01-29 14:22:44 +03:00
|
|
|
}
|
|
|
|
|
2020-02-23 18:28:35 +03:00
|
|
|
// Rollback rollback a transaction
|
2020-03-09 15:37:01 +03:00
|
|
|
func (db *DB) Rollback() *DB {
|
2020-03-09 08:10:48 +03:00
|
|
|
if comminter, ok := db.Statement.ConnPool.(TxCommiter); ok && comminter != nil {
|
2020-02-23 18:28:35 +03:00
|
|
|
db.AddError(comminter.Rollback())
|
|
|
|
} else {
|
|
|
|
db.AddError(ErrInvalidTransaction)
|
|
|
|
}
|
|
|
|
return db
|
2020-01-29 14:22:44 +03:00
|
|
|
}
|
|
|
|
|
2020-02-23 18:28:35 +03:00
|
|
|
// Exec execute raw sql
|
2020-03-09 15:37:01 +03:00
|
|
|
func (db *DB) Exec(sql string, values ...interface{}) (tx *DB) {
|
2020-01-29 14:22:44 +03:00
|
|
|
tx = db.getInstance()
|
2020-02-22 15:57:29 +03:00
|
|
|
tx.Statement.SQL = strings.Builder{}
|
|
|
|
clause.Expr{SQL: sql, Vars: values}.Build(tx.Statement)
|
2020-03-09 15:37:01 +03:00
|
|
|
tx.callbacks.Raw().Execute(tx)
|
2020-01-29 14:22:44 +03:00
|
|
|
return
|
|
|
|
}
|
2020-05-28 11:10:10 +03:00
|
|
|
|
|
|
|
func (db *DB) RecordNotFound() bool {
|
|
|
|
return errors.Is(db.Error, ErrRecordNotFound)
|
|
|
|
}
|