From f54593037b091dedaa4f048220d8a0b5d89b2581 Mon Sep 17 00:00:00 2001 From: Jack Date: Mon, 31 Aug 2020 00:59:42 +0800 Subject: [PATCH] improve CURD functions for package gdb --- database/gdb/gdb_core.go | 61 +++++++++++++++++++++++++--------------- 1 file changed, 39 insertions(+), 22 deletions(-) diff --git a/database/gdb/gdb_core.go b/database/gdb/gdb_core.go index 2da34ed92..91f111eea 100644 --- a/database/gdb/gdb_core.go +++ b/database/gdb/gdb_core.go @@ -334,7 +334,10 @@ func (c *Core) Transaction(f func(tx *TX) error) (err error) { // // The parameter specifies the batch operation count when given data is slice. func (c *Core) Insert(table string, data interface{}, batch ...int) (sql.Result, error) { - return c.DB.DoInsert(nil, table, data, gINSERT_OPTION_DEFAULT, batch...) + if len(batch) > 0 { + return c.Model(table).Data(data).Batch(batch[0]).Insert() + } + return c.Model(table).Data(data).Insert() } // InsertIgnore does "INSERT IGNORE INTO ..." statement for the table. @@ -347,7 +350,10 @@ func (c *Core) Insert(table string, data interface{}, batch ...int) (sql.Result, // // The parameter specifies the batch operation count when given data is slice. func (c *Core) InsertIgnore(table string, data interface{}, batch ...int) (sql.Result, error) { - return c.DB.DoInsert(nil, table, data, gINSERT_OPTION_IGNORE, batch...) + if len(batch) > 0 { + return c.Model(table).Data(data).Batch(batch[0]).InsertIgnore() + } + return c.Model(table).Data(data).InsertIgnore() } // Replace does "REPLACE INTO ..." statement for the table. @@ -363,7 +369,10 @@ func (c *Core) InsertIgnore(table string, data interface{}, batch ...int) (sql.R // If given data is type of slice, it then does batch replacing, and the optional parameter // specifies the batch operation count. func (c *Core) Replace(table string, data interface{}, batch ...int) (sql.Result, error) { - return c.DB.DoInsert(nil, table, data, gINSERT_OPTION_REPLACE, batch...) + if len(batch) > 0 { + return c.Model(table).Data(data).Batch(batch[0]).Replace() + } + return c.Model(table).Data(data).Replace() } // Save does "INSERT INTO ... ON DUPLICATE KEY UPDATE..." statement for the table. @@ -378,11 +387,14 @@ func (c *Core) 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 (c *Core) Save(table string, data interface{}, batch ...int) (sql.Result, error) { - return c.DB.DoInsert(nil, table, data, gINSERT_OPTION_SAVE, batch...) + if len(batch) > 0 { + return c.Model(table).Data(data).Batch(batch[0]).Save() + } + return c.Model(table).Data(data).Save() } // doInsert inserts or updates data for given table. -// +// This function is usually used for custom interface definition, you do not need call it manually. // The parameter can be type of map/gmap/struct/*struct/[]map/[]struct, etc. // Eg: // Data(g.Map{"uid": 10000, "name":"john"}) @@ -465,28 +477,41 @@ func (c *Core) DoInsert(link Link, table string, data interface{}, option int, b // BatchInsert batch inserts data. // The parameter must be type of slice of map or struct. func (c *Core) BatchInsert(table string, list interface{}, batch ...int) (sql.Result, error) { - return c.DB.DoBatchInsert(nil, table, list, gINSERT_OPTION_DEFAULT, batch...) + if len(batch) > 0 { + return c.Model(table).Data(list).Batch(batch[0]).Insert() + } + return c.Model(table).Data(list).Insert() } -// BatchInsert batch inserts data with ignore option. +// BatchInsertIgnore batch inserts data with ignore option. // The parameter must be type of slice of map or struct. func (c *Core) BatchInsertIgnore(table string, list interface{}, batch ...int) (sql.Result, error) { - return c.DB.DoBatchInsert(nil, table, list, gINSERT_OPTION_IGNORE, batch...) + if len(batch) > 0 { + return c.Model(table).Data(list).Batch(batch[0]).InsertIgnore() + } + return c.Model(table).Data(list).InsertIgnore() } // BatchReplace batch replaces data. // The parameter must be type of slice of map or struct. func (c *Core) BatchReplace(table string, list interface{}, batch ...int) (sql.Result, error) { - return c.DB.DoBatchInsert(nil, table, list, gINSERT_OPTION_REPLACE, batch...) + if len(batch) > 0 { + return c.Model(table).Data(list).Batch(batch[0]).Replace() + } + return c.Model(table).Data(list).Replace() } // BatchSave batch replaces data. // The parameter must be type of slice of map or struct. func (c *Core) BatchSave(table string, list interface{}, batch ...int) (sql.Result, error) { - return c.DB.DoBatchInsert(nil, table, list, gINSERT_OPTION_SAVE, batch...) + if len(batch) > 0 { + return c.Model(table).Data(list).Batch(batch[0]).Save() + } + return c.Model(table).Data(list).Save() } // DoBatchInsert batch inserts/replaces/saves data. +// This function is usually used for custom interface definition, you do not need call it manually. func (c *Core) DoBatchInsert(link Link, table string, list interface{}, option int, batch ...int) (result sql.Result, err error) { table = c.DB.QuotePrefixTableName(table) var ( @@ -636,15 +661,11 @@ func (c *Core) DoBatchInsert(link Link, table string, list interface{}, option i // "age IN(?,?)", 18, 50 // User{ Id : 1, UserName : "john"} func (c *Core) Update(table string, data interface{}, condition interface{}, args ...interface{}) (sql.Result, error) { - newWhere, newArgs := formatWhere(c.DB, condition, args, false) - if newWhere != "" { - newWhere = " WHERE " + newWhere - } - return c.DB.DoUpdate(nil, table, data, newWhere, newArgs...) + return c.Model(table).Data(data).Where(condition, args...).Update() } // doUpdate does "UPDATE ... " statement for the table. -// Also see Update. +// This function is usually used for custom interface definition, you do not need call it manually. func (c *Core) DoUpdate(link Link, table string, data interface{}, condition string, args ...interface{}) (result sql.Result, err error) { table = c.DB.QuotePrefixTableName(table) var ( @@ -704,15 +725,11 @@ func (c *Core) DoUpdate(link Link, table string, data interface{}, condition str // "age IN(?,?)", 18, 50 // User{ Id : 1, UserName : "john"} func (c *Core) Delete(table string, condition interface{}, args ...interface{}) (result sql.Result, err error) { - newWhere, newArgs := formatWhere(c.DB, condition, args, false) - if newWhere != "" { - newWhere = " WHERE " + newWhere - } - return c.DB.DoDelete(nil, table, newWhere, newArgs...) + return c.Model(table).Where(condition, args...).Delete() } // DoDelete does "DELETE FROM ... " statement for the table. -// Also see Delete. +// This function is usually used for custom interface definition, you do not need call it manually. func (c *Core) DoDelete(link Link, table string, condition string, args ...interface{}) (result sql.Result, err error) { if link == nil { if link, err = c.DB.Master(); err != nil {