@ -13,7 +13,7 @@ import (
"gorm.io/gorm/utils"
"gorm.io/gorm/utils"
)
)
// Create insert the value into database
// Create insert s value, returning the inserted data's primary key in value's id
func ( db * DB ) Create ( value interface { } ) ( tx * DB ) {
func ( db * DB ) Create ( value interface { } ) ( tx * DB ) {
if db . CreateBatchSize > 0 {
if db . CreateBatchSize > 0 {
return db . CreateInBatches ( value , db . CreateBatchSize )
return db . CreateInBatches ( value , db . CreateBatchSize )
@ -24,7 +24,7 @@ func (db *DB) Create(value interface{}) (tx *DB) {
return tx . callbacks . Create ( ) . Execute ( tx )
return tx . callbacks . Create ( ) . Execute ( tx )
}
}
// CreateInBatches insert the value in batches into databas e
// CreateInBatches insert s value in batches of batchSiz e
func ( db * DB ) CreateInBatches ( value interface { } , batchSize int ) ( tx * DB ) {
func ( db * DB ) CreateInBatches ( value interface { } , batchSize int ) ( tx * DB ) {
reflectValue := reflect . Indirect ( reflect . ValueOf ( value ) )
reflectValue := reflect . Indirect ( reflect . ValueOf ( value ) )
@ -68,7 +68,7 @@ func (db *DB) CreateInBatches(value interface{}, batchSize int) (tx *DB) {
return
return
}
}
// Save update value in database, if the value doesn't have primary key, will insert it
// Save update s value in database. If value doesn't contain a matching primary key, value is inserted.
func ( db * DB ) Save ( value interface { } ) ( tx * DB ) {
func ( db * DB ) Save ( value interface { } ) ( tx * DB ) {
tx = db . getInstance ( )
tx = db . getInstance ( )
tx . Statement . Dest = value
tx . Statement . Dest = value
@ -114,7 +114,7 @@ func (db *DB) Save(value interface{}) (tx *DB) {
return
return
}
}
// First find first record that match given conditions, order by primary key
// First find s the first record ordered by primary key, matching given conditions conds
func ( db * DB ) First ( dest interface { } , conds ... interface { } ) ( tx * DB ) {
func ( db * DB ) First ( dest interface { } , conds ... interface { } ) ( tx * DB ) {
tx = db . Limit ( 1 ) . Order ( clause . OrderByColumn {
tx = db . Limit ( 1 ) . Order ( clause . OrderByColumn {
Column : clause . Column { Table : clause . CurrentTable , Name : clause . PrimaryKey } ,
Column : clause . Column { Table : clause . CurrentTable , Name : clause . PrimaryKey } ,
@ -129,7 +129,7 @@ func (db *DB) First(dest interface{}, conds ...interface{}) (tx *DB) {
return tx . callbacks . Query ( ) . Execute ( tx )
return tx . callbacks . Query ( ) . Execute ( tx )
}
}
// Take return a record that match given conditions, the order will depend on the database implementation
// Take finds the first record returned by the database in no specified order, matching given conditions conds
func ( db * DB ) Take ( dest interface { } , conds ... interface { } ) ( tx * DB ) {
func ( db * DB ) Take ( dest interface { } , conds ... interface { } ) ( tx * DB ) {
tx = db . Limit ( 1 )
tx = db . Limit ( 1 )
if len ( conds ) > 0 {
if len ( conds ) > 0 {
@ -142,7 +142,7 @@ func (db *DB) Take(dest interface{}, conds ...interface{}) (tx *DB) {
return tx . callbacks . Query ( ) . Execute ( tx )
return tx . callbacks . Query ( ) . Execute ( tx )
}
}
// Last find last record that match given conditions, order by primary key
// Last find s the last record ordered by primary key, matching given conditions conds
func ( db * DB ) Last ( dest interface { } , conds ... interface { } ) ( tx * DB ) {
func ( db * DB ) Last ( dest interface { } , conds ... interface { } ) ( tx * DB ) {
tx = db . Limit ( 1 ) . Order ( clause . OrderByColumn {
tx = db . Limit ( 1 ) . Order ( clause . OrderByColumn {
Column : clause . Column { Table : clause . CurrentTable , Name : clause . PrimaryKey } ,
Column : clause . Column { Table : clause . CurrentTable , Name : clause . PrimaryKey } ,
@ -158,7 +158,7 @@ func (db *DB) Last(dest interface{}, conds ...interface{}) (tx *DB) {
return tx . callbacks . Query ( ) . Execute ( tx )
return tx . callbacks . Query ( ) . Execute ( tx )
}
}
// Find find records that match given conditions
// Find find s all records matching given conditions cond s
func ( db * DB ) Find ( dest interface { } , conds ... interface { } ) ( tx * DB ) {
func ( db * DB ) Find ( dest interface { } , conds ... interface { } ) ( tx * DB ) {
tx = db . getInstance ( )
tx = db . getInstance ( )
if len ( conds ) > 0 {
if len ( conds ) > 0 {
@ -170,7 +170,7 @@ func (db *DB) Find(dest interface{}, conds ...interface{}) (tx *DB) {
return tx . callbacks . Query ( ) . Execute ( tx )
return tx . callbacks . Query ( ) . Execute ( tx )
}
}
// FindInBatches find records in batches
// FindInBatches find s all records in batches of batchSize
func ( db * DB ) FindInBatches ( dest interface { } , batchSize int , fc func ( tx * DB , batch int ) error ) * DB {
func ( db * DB ) FindInBatches ( dest interface { } , batchSize int , fc func ( tx * DB , batch int ) error ) * DB {
var (
var (
tx = db . Order ( clause . OrderByColumn {
tx = db . Order ( clause . OrderByColumn {
@ -202,7 +202,9 @@ func (db *DB) FindInBatches(dest interface{}, batchSize int, fc func(tx *DB, bat
batch ++
batch ++
if result . Error == nil && result . RowsAffected != 0 {
if result . Error == nil && result . RowsAffected != 0 {
tx . AddError ( fc ( result , batch ) )
fcTx := result . Session ( & Session { NewDB : true } )
fcTx . RowsAffected = result . RowsAffected
tx . AddError ( fc ( fcTx , batch ) )
} else if result . Error != nil {
} else if result . Error != nil {
tx . AddError ( result . Error )
tx . AddError ( result . Error )
}
}
@ -284,7 +286,8 @@ func (db *DB) assignInterfacesToValue(values ...interface{}) {
}
}
}
}
// FirstOrInit gets the first matched record or initialize a new instance with given conditions (only works with struct or map conditions)
// FirstOrInit finds the first matching record, otherwise if not found initializes a new instance with given conds.
// Each conds must be a struct or map.
func ( db * DB ) FirstOrInit ( dest interface { } , conds ... interface { } ) ( tx * DB ) {
func ( db * DB ) FirstOrInit ( dest interface { } , conds ... interface { } ) ( tx * DB ) {
queryTx := db . Limit ( 1 ) . Order ( clause . OrderByColumn {
queryTx := db . Limit ( 1 ) . Order ( clause . OrderByColumn {
Column : clause . Column { Table : clause . CurrentTable , Name : clause . PrimaryKey } ,
Column : clause . Column { Table : clause . CurrentTable , Name : clause . PrimaryKey } ,
@ -310,7 +313,8 @@ func (db *DB) FirstOrInit(dest interface{}, conds ...interface{}) (tx *DB) {
return
return
}
}
// FirstOrCreate gets the first matched record or create a new one with given conditions (only works with struct, map conditions)
// FirstOrCreate finds the first matching record, otherwise if not found creates a new instance with given conds.
// Each conds must be a struct or map.
func ( db * DB ) FirstOrCreate ( dest interface { } , conds ... interface { } ) ( tx * DB ) {
func ( db * DB ) FirstOrCreate ( dest interface { } , conds ... interface { } ) ( tx * DB ) {
tx = db . getInstance ( )
tx = db . getInstance ( )
queryTx := db . Session ( & Session { } ) . Limit ( 1 ) . Order ( clause . OrderByColumn {
queryTx := db . Session ( & Session { } ) . Limit ( 1 ) . Order ( clause . OrderByColumn {
@ -358,14 +362,14 @@ func (db *DB) FirstOrCreate(dest interface{}, conds ...interface{}) (tx *DB) {
return tx
return tx
}
}
// Update update attributes with callbacks, refer : https://gorm.io/docs/update.html#Update-Changed-Fields
// Update update s column with value using callbacks. Reference : https://gorm.io/docs/update.html#Update-Changed-Fields
func ( db * DB ) Update ( column string , value interface { } ) ( tx * DB ) {
func ( db * DB ) Update ( column string , value interface { } ) ( tx * DB ) {
tx = db . getInstance ( )
tx = db . getInstance ( )
tx . Statement . Dest = map [ string ] interface { } { column : value }
tx . Statement . Dest = map [ string ] interface { } { column : value }
return tx . callbacks . Update ( ) . Execute ( tx )
return tx . callbacks . Update ( ) . Execute ( tx )
}
}
// Updates update attributes with callbacks, refer : https://gorm.io/docs/update.html#Update-Changed-Fields
// Updates update s attributes using callbacks. values must be a struct or map. Reference : https://gorm.io/docs/update.html#Update-Changed-Fields
func ( db * DB ) Updates ( values interface { } ) ( tx * DB ) {
func ( db * DB ) Updates ( values interface { } ) ( tx * DB ) {
tx = db . getInstance ( )
tx = db . getInstance ( )
tx . Statement . Dest = values
tx . Statement . Dest = values
@ -386,7 +390,9 @@ func (db *DB) UpdateColumns(values interface{}) (tx *DB) {
return tx . callbacks . Update ( ) . Execute ( tx )
return tx . callbacks . Update ( ) . Execute ( tx )
}
}
// Delete delete value match given conditions, if the value has primary key, then will including the primary key as condition
// Delete deletes value matching given conditions. If value contains primary key it is included in the conditions. If
// value includes a deleted_at field, then Delete performs a soft delete instead by setting deleted_at with the current
// time if null.
func ( db * DB ) Delete ( value interface { } , conds ... interface { } ) ( tx * DB ) {
func ( db * DB ) Delete ( value interface { } , conds ... interface { } ) ( tx * DB ) {
tx = db . getInstance ( )
tx = db . getInstance ( )
if len ( conds ) > 0 {
if len ( conds ) > 0 {
@ -480,7 +486,7 @@ func (db *DB) Rows() (*sql.Rows, error) {
return rows , tx . Error
return rows , tx . Error
}
}
// Scan scan value to a struc t
// Scan scan s selected value to the struct des t
func ( db * DB ) Scan ( dest interface { } ) ( tx * DB ) {
func ( db * DB ) Scan ( dest interface { } ) ( tx * DB ) {
config := * db . Config
config := * db . Config
currentLogger , newLogger := config . Logger , logger . Recorder . New ( )
currentLogger , newLogger := config . Logger , logger . Recorder . New ( )
@ -505,7 +511,7 @@ func (db *DB) Scan(dest interface{}) (tx *DB) {
return
return
}
}
// Pluck used to query single column from a model as a map
// Pluck queries a single column from a model, returning in the slice dest. E.g.:
// var ages []int64
// var ages []int64
// db.Model(&users).Pluck("age", &ages)
// db.Model(&users).Pluck("age", &ages)
func ( db * DB ) Pluck ( column string , dest interface { } ) ( tx * DB ) {
func ( db * DB ) Pluck ( column string , dest interface { } ) ( tx * DB ) {
@ -548,7 +554,8 @@ func (db *DB) ScanRows(rows *sql.Rows, dest interface{}) error {
return tx . Error
return tx . Error
}
}
// Connection use a db conn to execute Multiple commands,this conn will put conn pool after it is executed.
// Connection uses a db connection to execute an arbitrary number of commands in fc. When finished, the connection is
// returned to the connection pool.
func ( db * DB ) Connection ( fc func ( tx * DB ) error ) ( err error ) {
func ( db * DB ) Connection ( fc func ( tx * DB ) error ) ( err error ) {
if db . Error != nil {
if db . Error != nil {
return db . Error
return db . Error
@ -570,7 +577,9 @@ func (db *DB) Connection(fc func(tx *DB) error) (err error) {
return fc ( tx )
return fc ( tx )
}
}
// Transaction start a transaction as a block, return error will rollback, otherwise to commit.
// Transaction start a transaction as a block, return error will rollback, otherwise to commit. Transaction executes an
// arbitrary number of commands in fc within a transaction. On success the changes are committed; if an error occurs
// they are rolled back.
func ( db * DB ) Transaction ( fc func ( tx * DB ) error , opts ... * sql . TxOptions ) ( err error ) {
func ( db * DB ) Transaction ( fc func ( tx * DB ) error , opts ... * sql . TxOptions ) ( err error ) {
panicked := true
panicked := true
@ -613,7 +622,7 @@ func (db *DB) Transaction(fc func(tx *DB) error, opts ...*sql.TxOptions) (err er
return
return
}
}
// Begin begins a transaction
// Begin begins a transaction with any transaction options opts
func ( db * DB ) Begin ( opts ... * sql . TxOptions ) * DB {
func ( db * DB ) Begin ( opts ... * sql . TxOptions ) * DB {
var (
var (
// clone statement
// clone statement
@ -642,7 +651,7 @@ func (db *DB) Begin(opts ...*sql.TxOptions) *DB {
return tx
return tx
}
}
// Commit commit a transaction
// Commit commit s the changes in a transaction
func ( db * DB ) Commit ( ) * DB {
func ( db * DB ) Commit ( ) * DB {
if committer , ok := db . Statement . ConnPool . ( TxCommitter ) ; ok && committer != nil && ! reflect . ValueOf ( committer ) . IsNil ( ) {
if committer , ok := db . Statement . ConnPool . ( TxCommitter ) ; ok && committer != nil && ! reflect . ValueOf ( committer ) . IsNil ( ) {
db . AddError ( committer . Commit ( ) )
db . AddError ( committer . Commit ( ) )
@ -652,7 +661,7 @@ func (db *DB) Commit() *DB {
return db
return db
}
}
// Rollback rollback a transaction
// Rollback rollback s the changes in a transaction
func ( db * DB ) Rollback ( ) * DB {
func ( db * DB ) Rollback ( ) * DB {
if committer , ok := db . Statement . ConnPool . ( TxCommitter ) ; ok && committer != nil {
if committer , ok := db . Statement . ConnPool . ( TxCommitter ) ; ok && committer != nil {
if ! reflect . ValueOf ( committer ) . IsNil ( ) {
if ! reflect . ValueOf ( committer ) . IsNil ( ) {
@ -682,7 +691,7 @@ func (db *DB) RollbackTo(name string) *DB {
return db
return db
}
}
// Exec execute raw sql
// Exec execute s raw sql
func ( db * DB ) Exec ( sql string , values ... interface { } ) ( tx * DB ) {
func ( db * DB ) Exec ( sql string , values ... interface { } ) ( tx * DB ) {
tx = db . getInstance ( )
tx = db . getInstance ( )
tx . Statement . SQL = strings . Builder { }
tx . Statement . SQL = strings . Builder { }