github.com/RevenueMonster/sqlike@v1.0.6/sqlike/replace.go (about)

     1  package sqlike
     2  
     3  import (
     4  	"context"
     5  	"database/sql"
     6  	"reflect"
     7  
     8  	"github.com/RevenueMonster/sqlike/reflext"
     9  	"github.com/RevenueMonster/sqlike/sqlike/options"
    10  )
    11  
    12  // ReplaceOne :
    13  func (tb *Table) ReplaceOne(ctx context.Context, src interface{}, opts ...*options.InsertOneOptions) (sql.Result, error) {
    14  	opt := new(options.InsertOneOptions)
    15  	if len(opts) > 0 && opts[0] != nil {
    16  		opt = opts[0]
    17  	}
    18  	v := reflect.ValueOf(src)
    19  	if !v.IsValid() {
    20  		return nil, ErrInvalidInput
    21  	}
    22  	t := v.Type()
    23  	if !reflext.IsKind(t, reflect.Ptr) {
    24  		return nil, ErrUnaddressableEntity
    25  	}
    26  
    27  	if v.IsNil() {
    28  		return nil, ErrNilEntity
    29  	}
    30  
    31  	arr := reflect.MakeSlice(reflect.SliceOf(t), 0, 1)
    32  	arr = reflect.Append(arr, v)
    33  	return insertMany(
    34  		ctx,
    35  		tb.dbName,
    36  		tb.name,
    37  		tb.pk,
    38  		tb.client.cache,
    39  		tb.codec,
    40  		tb.driver,
    41  		tb.dialect,
    42  		tb.logger,
    43  		arr.Interface(),
    44  		&opt.InsertOptions,
    45  	)
    46  }