diff --git a/database/gdb/gdb.go b/database/gdb/gdb.go index e4f88f45b..6b701d668 100644 --- a/database/gdb/gdb.go +++ b/database/gdb/gdb.go @@ -184,7 +184,7 @@ var ( // The parameter specifies the configuration group name, // which is DEFAULT_GROUP_NAME in default. func New(name ...string) (db DB, err error) { - group := configs.defaultGroup + group := configs.group if len(name) > 0 && name[0] != "" { group = name[0] } @@ -232,7 +232,7 @@ func New(name ...string) (db DB, err error) { // The parameter specifies the configuration group name, // which is DEFAULT_GROUP_NAME in default. func Instance(name ...string) (db DB, err error) { - group := configs.defaultGroup + group := configs.group if len(name) > 0 && name[0] != "" { group = name[0] } diff --git a/database/gdb/gdb_base.go b/database/gdb/gdb_base.go index 581addacb..cbefda532 100644 --- a/database/gdb/gdb_base.go +++ b/database/gdb/gdb_base.go @@ -192,7 +192,9 @@ func (bs *dbBase) GetStructs(pointer interface{}, query string, args ...interfac return all.Structs(pointer) } -// GetScan queries one or more records from database and converts them to given struct. +// GetScan queries one or more records from database and converts them to given struct or +// struct array. +// // If parameter is type of struct pointer, it calls GetStruct internally for // the conversion. If parameter is type of slice, it calls GetStructs internally // for conversion. @@ -240,7 +242,7 @@ func (bs *dbBase) GetCount(query string, args ...interface{}) (int, error) { return value.Int(), nil } -// ping一下,判断或保持数据库链接(master) +// PingMaster pings the master node to check authentication or keeps the connection alive. func (bs *dbBase) PingMaster() error { if master, err := bs.db.Master(); err != nil { return err @@ -249,7 +251,7 @@ func (bs *dbBase) PingMaster() error { } } -// ping一下,判断或保持数据库链接(slave) +// PingSlave pings the slave node to check authentication or keeps the connection alive. func (bs *dbBase) PingSlave() error { if slave, err := bs.db.Slave(); err != nil { return err @@ -258,8 +260,10 @@ func (bs *dbBase) PingSlave() error { } } -// 事务操作,开启,会返回一个底层的事务操作对象链接如需要嵌套事务,那么可以使用该对象,否则请忽略 -// 只有在tx.Commit/tx.Rollback时,链接会自动Close +// Begin starts and returns the transaction object. +// You should call Commit or Rollback functions of the transaction object +// if you no longer use the transaction. Commit or Rollback functions will also +// close the transaction automatically. func (bs *dbBase) Begin() (*TX, error) { if master, err := bs.db.Master(); err != nil { return nil, err @@ -276,46 +280,76 @@ func (bs *dbBase) Begin() (*TX, error) { } } -// CURD操作:单条数据写入, 仅仅执行写入操作,如果存在冲突的主键或者唯一索引,那么报错返回。 -// 参数data支持map/struct/*struct/slice类型, -// 当为slice(例如[]map/[]struct/[]*struct)类型时,batch参数生效,并自动切换为批量操作。 +// Insert does "INSERT INTO ..." statement for the table. +// If there's already one unique record of the data in the table, it returns error. +// +// The parameter can be type of map/gmap/struct/*struct/[]map/[]struct, etc. +// Eg: +// Data(g.Map{"uid": 10000, "name":"john"}) +// Data(g.Slice{g.Map{"uid": 10000, "name":"john"}, g.Map{"uid": 20000, "name":"smith"}) +// +// The parameter specifies the batch operation count when given data is slice. func (bs *dbBase) Insert(table string, data interface{}, batch ...int) (sql.Result, error) { return bs.db.doInsert(nil, table, data, gINSERT_OPTION_DEFAULT, batch...) } +// InsertIgnore does "INSERT IGNORE INTO ..." statement for the table. +// If there's already one unique record of the data in the table, it ignores the inserting. +// +// The parameter can be type of map/gmap/struct/*struct/[]map/[]struct, etc. +// Eg: +// Data(g.Map{"uid": 10000, "name":"john"}) +// Data(g.Slice{g.Map{"uid": 10000, "name":"john"}, g.Map{"uid": 20000, "name":"smith"}) +// +// The parameter specifies the batch operation count when given data is slice. func (bs *dbBase) InsertIgnore(table string, data interface{}, batch ...int) (sql.Result, error) { return bs.db.doInsert(nil, table, data, gINSERT_OPTION_IGNORE, batch...) } -// CURD操作:单条数据写入, 如果数据存在(主键或者唯一索引),那么删除后重新写入一条。 -// 参数data支持map/struct/*struct/slice类型, -// 当为slice(例如[]map/[]struct/[]*struct)类型时,batch参数生效,并自动切换为批量操作。 +// Replace does "REPLACE INTO ..." statement for the table. +// If there's already one unique record of the data in the table, it deletes the record +// and inserts a new one. +// +// The parameter can be type of map/gmap/struct/*struct/[]map/[]struct, etc. +// Eg: +// Data(g.Map{"uid": 10000, "name":"john"}) +// Data(g.Slice{g.Map{"uid": 10000, "name":"john"}, g.Map{"uid": 20000, "name":"smith"}) +// +// The parameter can be type of map/gmap/struct/*struct/[]map/[]struct, etc. +// If given data is type of slice, it then does batch replacing, and the optional parameter +// specifies the batch operation count. func (bs *dbBase) Replace(table string, data interface{}, batch ...int) (sql.Result, error) { return bs.db.doInsert(nil, table, data, gINSERT_OPTION_REPLACE, batch...) } -// CURD操作:单条数据写入, 如果数据存在(主键或者唯一索引),那么更新,否则写入一条新数据。 -// 参数data支持map/struct/*struct/slice类型, -// 当为slice(例如[]map/[]struct/[]*struct)类型时,batch参数生效,并自动切换为批量操作。 +// Save does "INSERT INTO ... ON DUPLICATE KEY UPDATE..." statement for the table. +// It updates the record if there's primary or unique index in the saving data, +// or else it inserts a new record into the table. +// +// The parameter can be type of map/gmap/struct/*struct/[]map/[]struct, etc. +// Eg: +// Data(g.Map{"uid": 10000, "name":"john"}) +// Data(g.Slice{g.Map{"uid": 10000, "name":"john"}, g.Map{"uid": 20000, "name":"smith"}) +// +// If given data is type of slice, it then does batch saving, and the optional parameter +// specifies the batch operation count. func (bs *dbBase) Save(table string, data interface{}, batch ...int) (sql.Result, error) { return bs.db.doInsert(nil, table, data, gINSERT_OPTION_SAVE, batch...) } -// 支持insert、replace, save, ignore操作。 -// 0: insert: 仅仅执行写入操作,如果存在冲突的主键或者唯一索引,那么报错返回; -// 1: replace: 如果数据存在(主键或者唯一索引),那么删除后重新写入一条; -// 2: save: 如果数据存在(主键或者唯一索引),那么更新,否则写入一条新数据; -// 3: ignore: 如果数据存在(主键或者唯一索引),那么什么也不做; +// doInsert inserts or updates data for given table. // -// 参数data支持map/struct/*struct/slice类型, -// 当为slice(例如[]map/[]struct/[]*struct)类型时,batch参数生效,并自动切换为批量操作。 +// The parameter