From d4091a4826042fc8f2f5a24f8a176cb62e035ea6 Mon Sep 17 00:00:00 2001 From: Jack Date: Mon, 31 Aug 2020 15:57:04 +0800 Subject: [PATCH] improve some transaction operations by directly calling model operations, making their implements logic the same --- database/gdb/gdb_transaction.go | 53 +++++++++++++++++++++------------ 1 file changed, 34 insertions(+), 19 deletions(-) diff --git a/database/gdb/gdb_transaction.go b/database/gdb/gdb_transaction.go index cdae88386..c2d2a9a0d 100644 --- a/database/gdb/gdb_transaction.go +++ b/database/gdb/gdb_transaction.go @@ -115,7 +115,6 @@ func (tx *TX) GetScan(objPointer interface{}, sql string, args ...interface{}) e default: return fmt.Errorf("element type should be type of struct/slice, unsupported: %v", k) } - return nil } // GetValue queries and returns the field value from database. @@ -154,7 +153,10 @@ func (tx *TX) GetCount(sql string, args ...interface{}) (int, error) { // // The parameter specifies the batch operation count when given data is slice. func (tx *TX) Insert(table string, data interface{}, batch ...int) (sql.Result, error) { - return tx.db.DoInsert(tx.tx, table, data, gINSERT_OPTION_DEFAULT, batch...) + if len(batch) > 0 { + return tx.Model(table).Data(data).Batch(batch[0]).Insert() + } + return tx.Model(table).Data(data).Insert() } // InsertIgnore does "INSERT IGNORE INTO ..." statement for the table. @@ -167,7 +169,10 @@ func (tx *TX) Insert(table string, data interface{}, batch ...int) (sql.Result, // // The parameter specifies the batch operation count when given data is slice. func (tx *TX) InsertIgnore(table string, data interface{}, batch ...int) (sql.Result, error) { - return tx.db.DoInsert(tx.tx, table, data, gINSERT_OPTION_IGNORE, batch...) + if len(batch) > 0 { + return tx.Model(table).Data(data).Batch(batch[0]).InsertIgnore() + } + return tx.Model(table).Data(data).InsertIgnore() } // Replace does "REPLACE INTO ..." statement for the table. @@ -183,7 +188,10 @@ func (tx *TX) InsertIgnore(table string, data interface{}, batch ...int) (sql.Re // If given data is type of slice, it then does batch replacing, and the optional parameter // specifies the batch operation count. func (tx *TX) Replace(table string, data interface{}, batch ...int) (sql.Result, error) { - return tx.db.DoInsert(tx.tx, table, data, gINSERT_OPTION_REPLACE, batch...) + if len(batch) > 0 { + return tx.Model(table).Data(data).Batch(batch[0]).Replace() + } + return tx.Model(table).Data(data).Replace() } // Save does "INSERT INTO ... ON DUPLICATE KEY UPDATE..." statement for the table. @@ -198,31 +206,46 @@ func (tx *TX) Replace(table string, data interface{}, batch ...int) (sql.Result, // If given data is type of slice, it then does batch saving, and the optional parameter // specifies the batch operation count. func (tx *TX) Save(table string, data interface{}, batch ...int) (sql.Result, error) { - return tx.db.DoInsert(tx.tx, table, data, gINSERT_OPTION_SAVE, batch...) + if len(batch) > 0 { + return tx.Model(table).Data(data).Batch(batch[0]).Save() + } + return tx.Model(table).Data(data).Save() } // BatchInsert batch inserts data. // The parameter must be type of slice of map or struct. func (tx *TX) BatchInsert(table string, list interface{}, batch ...int) (sql.Result, error) { - return tx.db.DoBatchInsert(tx.tx, table, list, gINSERT_OPTION_DEFAULT, batch...) + if len(batch) > 0 { + return tx.Model(table).Data(list).Batch(batch[0]).Insert() + } + return tx.Model(table).Data(list).Insert() } // BatchInsert batch inserts data with ignore option. // The parameter must be type of slice of map or struct. func (tx *TX) BatchInsertIgnore(table string, list interface{}, batch ...int) (sql.Result, error) { - return tx.db.DoBatchInsert(tx.tx, table, list, gINSERT_OPTION_IGNORE, batch...) + if len(batch) > 0 { + return tx.Model(table).Data(list).Batch(batch[0]).InsertIgnore() + } + return tx.Model(table).Data(list).InsertIgnore() } // BatchReplace batch replaces data. // The parameter must be type of slice of map or struct. func (tx *TX) BatchReplace(table string, list interface{}, batch ...int) (sql.Result, error) { - return tx.db.DoBatchInsert(tx.tx, table, list, gINSERT_OPTION_REPLACE, batch...) + if len(batch) > 0 { + return tx.Model(table).Data(list).Batch(batch[0]).Replace() + } + return tx.Model(table).Data(list).Replace() } // BatchSave batch replaces data. // The parameter must be type of slice of map or struct. func (tx *TX) BatchSave(table string, list interface{}, batch ...int) (sql.Result, error) { - return tx.db.DoBatchInsert(tx.tx, table, list, gINSERT_OPTION_SAVE, batch...) + if len(batch) > 0 { + return tx.Model(table).Data(list).Batch(batch[0]).Save() + } + return tx.Model(table).Data(list).Save() } // Update does "UPDATE ... " statement for the table. @@ -240,11 +263,7 @@ func (tx *TX) BatchSave(table string, list interface{}, batch ...int) (sql.Resul // "age IN(?,?)", 18, 50 // User{ Id : 1, UserName : "john"} func (tx *TX) Update(table string, data interface{}, condition interface{}, args ...interface{}) (sql.Result, error) { - newWhere, newArgs := formatWhere(tx.db, condition, args, false) - if newWhere != "" { - newWhere = " WHERE " + newWhere - } - return tx.db.DoUpdate(tx.tx, table, data, newWhere, newArgs...) + return tx.Model(table).Data(data).Where(condition, args...).Update() } // Delete does "DELETE FROM ... " statement for the table. @@ -259,9 +278,5 @@ func (tx *TX) Update(table string, data interface{}, condition interface{}, args // "age IN(?,?)", 18, 50 // User{ Id : 1, UserName : "john"} func (tx *TX) Delete(table string, condition interface{}, args ...interface{}) (sql.Result, error) { - newWhere, newArgs := formatWhere(tx.db, condition, args, false) - if newWhere != "" { - newWhere = " WHERE " + newWhere - } - return tx.db.DoDelete(tx.tx, table, newWhere, newArgs...) + return tx.Model(table).Where(condition, args...).Delete() }