Skip to content

Commit

Permalink
improve package gdb for association feature
Browse files Browse the repository at this point in the history
  • Loading branch information
zcool321 committed Feb 7, 2021
1 parent 9bb5536 commit 397e11e
Show file tree
Hide file tree
Showing 16 changed files with 1,278 additions and 691 deletions.
50 changes: 47 additions & 3 deletions database/gdb/gdb_type_result_scanlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/gogf/gf/errors/gerror"
"github.com/gogf/gf/text/gstr"
"github.com/gogf/gf/util/gconv"
"github.com/gogf/gf/util/gutil"
"reflect"
)

Expand Down Expand Up @@ -41,6 +42,9 @@ import (
//
// See the example or unit testing cases for clear understanding for this function.
func (r Result) ScanList(listPointer interface{}, bindToAttrName string, relationKV ...string) (err error) {
if r.IsEmpty() {
return nil
}
// Necessary checks for parameters.
if bindToAttrName == "" {
return gerror.New(`bindToAttrName should not be empty`)
Expand Down Expand Up @@ -112,14 +116,29 @@ func (r Result) ScanList(listPointer interface{}, bindToAttrName string, relatio
relationFromAttrName = relationKV[0]
relationKVStr = relationKV[1]
}
array := gstr.SplitAndTrim(relationKVStr, ":")
// The relation key string of table filed name and attribute name
// can be joined with char '=' or ':'.
array := gstr.SplitAndTrim(relationKVStr, "=")
if len(array) == 1 {
// Compatible with old splitting char ':'.
array = gstr.SplitAndTrim(relationKVStr, ":")
}
if len(array) == 2 {
// Defined table field to relation attribute name.
// Like:
// uid:Uid
// uid:UserId
relationResultFieldName = array[0]
relationBindToSubAttrName = array[1]
if key, _ := gutil.MapPossibleItemByKey(r[0].Map(), relationResultFieldName); key == "" {
return gerror.Newf(
`cannot find possible related table field name "%s" from given relation key "%s"`,
relationResultFieldName,
relationKVStr,
)
} else {
relationResultFieldName = key
}
} else {
return gerror.New(`parameter relationKV should be format of "ResultFieldName:BindToAttrName"`)
}
Expand Down Expand Up @@ -152,8 +171,9 @@ func (r Result) ScanList(listPointer interface{}, bindToAttrName string, relatio

// Bind to relation conditions.
var (
relationFromAttrValue reflect.Value
relationFromAttrField reflect.Value
relationFromAttrValue reflect.Value
relationFromAttrField reflect.Value
relationBindToSubAttrNameChecked bool
)
for i := 0; i < arrayValue.Len(); i++ {
arrayElemValue := arrayValue.Index(i)
Expand Down Expand Up @@ -187,6 +207,30 @@ func (r Result) ScanList(listPointer interface{}, bindToAttrName string, relatio
if len(relationDataMap) > 0 && !relationFromAttrValue.IsValid() {
return gerror.Newf(`invalid relation specified: "%v"`, relationKV)
}
// Check and find possible bind to attribute name.
if relationKVStr != "" && !relationBindToSubAttrNameChecked {
relationFromAttrField = relationFromAttrValue.FieldByName(relationBindToSubAttrName)
if !relationFromAttrField.IsValid() {
var (
relationFromAttrType = relationFromAttrValue.Type()
filedMap = make(map[string]interface{})
)
for i := 0; i < relationFromAttrType.NumField(); i++ {
filedMap[relationFromAttrType.Field(i).Name] = struct{}{}
}
if key, _ := gutil.MapPossibleItemByKey(filedMap, relationBindToSubAttrName); key == "" {
return gerror.Newf(
`cannot find possible related attribute name "%s" from given relation key "%s"`,
relationBindToSubAttrName,
relationKVStr,
)
} else {
relationBindToSubAttrName = key

}
}
relationBindToSubAttrNameChecked = true
}
switch bindToAttrKind {
case reflect.Array, reflect.Slice:
if len(relationDataMap) > 0 {
Expand Down
2 changes: 1 addition & 1 deletion database/gdb/gdb_z_driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func Test_Custom_Driver(t *testing.T) {
t.Assert(latestSqlString.Val(), "")
sqlString := "select 10000"
value, err := g.DB("driver-test").GetValue(sqlString)
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(value, 10000)
t.Assert(latestSqlString.Val(), sqlString)
})
Expand Down
4 changes: 2 additions & 2 deletions database/gdb/gdb_z_init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func init() {
"name": true,
"type": true,
}, false)
gtest.Assert(err, nil)
gtest.AssertNil(err)
configNode = gdb.ConfigNode{
Host: "127.0.0.1",
Port: "3306",
Expand Down Expand Up @@ -195,7 +195,7 @@ func createInitTableWithDb(db gdb.DB, table ...string) (name string) {
}

result, err := db.BatchInsert(name, array.Slice())
gtest.Assert(err, nil)
gtest.AssertNil(err)

n, e := result.RowsAffected()
gtest.Assert(e, nil)
Expand Down
Loading

0 comments on commit 397e11e

Please sign in to comment.