diff --git a/database/gdb/gdb_model_select.go b/database/gdb/gdb_model_select.go index c6c3c486d..2fea75fe5 100644 --- a/database/gdb/gdb_model_select.go +++ b/database/gdb/gdb_model_select.go @@ -158,7 +158,7 @@ func (m *Model) Array(fieldsAndWhere ...interface{}) ([]Value, error) { // see Model.Where. // // Note that it returns sql.ErrNoRows if there's no record retrieved with the given conditions -// from table. +// from table and is not nil. // // Eg: // user := new(User) @@ -182,14 +182,14 @@ func (m *Model) Struct(pointer interface{}, where ...interface{}) error { // see Model.Where. // // Note that it returns sql.ErrNoRows if there's no record retrieved with the given conditions -// from table. +// from table and is not empty. // // Eg: // users := ([]User)(nil) -// err := db.Table("user").Structs(&users) +// err := db.Table("user").Structs(&users) // // users := ([]*User)(nil) -// err := db.Table("user").Structs(&users) +// err := db.Table("user").Structs(&users) func (m *Model) Structs(pointer interface{}, where ...interface{}) error { all, err := m.All(where...) if err != nil { @@ -216,10 +216,10 @@ func (m *Model) Structs(pointer interface{}, where ...interface{}) error { // err := db.Table("user").Where("id", 1).Struct(&user) // // users := ([]User)(nil) -// err := db.Table("user").Structs(&users) +// err := db.Table("user").Structs(&users) // // users := ([]*User)(nil) -// err := db.Table("user").Structs(&users) +// err := db.Table("user").Structs(&users) func (m *Model) Scan(pointer interface{}, where ...interface{}) error { t := reflect.TypeOf(pointer) k := t.Kind() diff --git a/database/gdb/gdb_z_mysql_join_test.go b/database/gdb/gdb_z_mysql_join_test.go new file mode 100644 index 000000000..da70728e0 --- /dev/null +++ b/database/gdb/gdb_z_mysql_join_test.go @@ -0,0 +1,59 @@ +// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + +package gdb_test + +import ( + "fmt" + "testing" + + "github.com/gogf/gf/os/gtime" + "github.com/gogf/gf/test/gtest" +) + +func Test_Table_Join(t *testing.T) { + var ( + tableUser = "user_" + gtime.TimestampMicroStr() + tableUserDetail = "user_detail_" + gtime.TimestampMicroStr() + ) + if _, err := db.Exec(fmt.Sprintf(` +CREATE TABLE %s ( + uid int(10) unsigned NOT NULL AUTO_INCREMENT, + name varchar(45) NOT NULL COMMENT, + PRIMARY KEY (uid) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + `, tableUser)); err != nil { + gtest.Error(err) + } + defer dropTable(tableUser) + + if _, err := db.Exec(fmt.Sprintf(` +CREATE TABLE %s ( + uid int(10) unsigned NOT NULL AUTO_INCREMENT, + name varchar(45) NOT NULL COMMENT, + PRIMARY KEY (uid) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + `, tableUserDetail)); err != nil { + gtest.Error(err) + } + defer dropTable(tableUserDetail) + + gtest.C(t, func(t *gtest.T) { + type EntityUser struct { + Uid int + Name string + } + type EntityUserDetail struct { + Uid int + TrueName string + } + type Entity struct { + User *EntityUser + UserDetail *EntityUserDetail + } + + }) +} diff --git a/util/gutil/gutil_map.go b/util/gutil/gutil_map.go index bc014dc1b..45149ea5d 100644 --- a/util/gutil/gutil_map.go +++ b/util/gutil/gutil_map.go @@ -26,13 +26,13 @@ func MapContains(data map[string]interface{}, key string) (ok bool) { return } -// MapDelete deletes all from map . -func MapDelete(data map[string]interface{}, key ...string) { +// MapDelete deletes all from map . +func MapDelete(data map[string]interface{}, keys ...string) { if data == nil { return } - for _, v := range key { - delete(data, v) + for _, key := range keys { + delete(data, key) } } @@ -78,6 +78,8 @@ func MapPossibleItemByKey(data map[string]interface{}, key string) (foundKey str // MapContainsPossibleKey checks if the given is contained in given map . // It checks the key with or without cases or chars '-'/'_'/'.'/' '. +// +// Note that this function might be of low performance. func MapContainsPossibleKey(data map[string]interface{}, key string) bool { if k, _ := MapPossibleItemByKey(data, key); k != "" { return true