mirror of
https://gitee.com/johng/gf
synced 2026-06-06 16:21:40 +08:00
add automatic adding OmitNil option for data and where filtering if given struct name is defined like 'xxxForDao'
This commit is contained in:
@ -275,6 +275,7 @@ const (
|
||||
sqlTypeQueryContext = `DB.QueryContext`
|
||||
sqlTypeExecContext = `DB.ExecContext`
|
||||
sqlTypePrepareContext = `DB.PrepareContext`
|
||||
modelForDaoSuffix = `ForDao`
|
||||
)
|
||||
|
||||
var (
|
||||
|
||||
@ -414,6 +414,11 @@ func formatWhere(db DB, in formatWhereInput) (newWhere string, newArgs []interfa
|
||||
}
|
||||
|
||||
case reflect.Struct:
|
||||
// If the `where` parameter is defined like `xxxForDao`, it then adds `OmitNil` option for this condition,
|
||||
// which will filter all nil parameters in `where`.
|
||||
if gstr.HasSuffix(reflect.TypeOf(in.Where).String(), modelForDaoSuffix) {
|
||||
in.OmitNil = true
|
||||
}
|
||||
// If `where` struct implements iIterator interface,
|
||||
// it then uses its Iterate function to iterate its key-value pairs.
|
||||
// For example, ListMap and TreeMap are ordered map,
|
||||
|
||||
@ -51,26 +51,26 @@ func (m *Model) Data(data ...interface{}) *Model {
|
||||
model.data = m
|
||||
}
|
||||
} else {
|
||||
switch params := data[0].(type) {
|
||||
switch value := data[0].(type) {
|
||||
case Result:
|
||||
model.data = params.List()
|
||||
model.data = value.List()
|
||||
|
||||
case Record:
|
||||
model.data = params.Map()
|
||||
model.data = value.Map()
|
||||
|
||||
case List:
|
||||
list := make(List, len(params))
|
||||
for k, v := range params {
|
||||
list := make(List, len(value))
|
||||
for k, v := range value {
|
||||
list[k] = gutil.MapCopy(v)
|
||||
}
|
||||
model.data = list
|
||||
|
||||
case Map:
|
||||
model.data = gutil.MapCopy(params)
|
||||
model.data = gutil.MapCopy(value)
|
||||
|
||||
default:
|
||||
var (
|
||||
reflectInfo = utils.OriginValueAndKind(params)
|
||||
reflectInfo = utils.OriginValueAndKind(value)
|
||||
)
|
||||
switch reflectInfo.OriginKind {
|
||||
case reflect.Slice, reflect.Array:
|
||||
@ -84,6 +84,12 @@ func (m *Model) Data(data ...interface{}) *Model {
|
||||
model.data = ConvertDataForTableRecord(data[0])
|
||||
|
||||
case reflect.Struct:
|
||||
// If the `data` parameter is defined like `xxxForDao`,
|
||||
// it then adds `OmitNilData` option for this condition,
|
||||
// which will filter all nil parameters in `data`.
|
||||
if gstr.HasSuffix(reflect.TypeOf(value).String(), modelForDaoSuffix) {
|
||||
model = model.OmitNilData()
|
||||
}
|
||||
if v, ok := data[0].(iInterfaces); ok {
|
||||
var (
|
||||
array = v.Interfaces()
|
||||
|
||||
99
database/gdb/gdb_z_mysql_model_for_dao_test.go
Normal file
99
database/gdb/gdb_z_mysql_model_for_dao_test.go
Normal file
@ -0,0 +1,99 @@
|
||||
// Copyright GoFrame Author(https://goframe.org). 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 (
|
||||
"github.com/gogf/gf/v2/test/gtest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_Model_Insert_Data_ForDao(t *testing.T) {
|
||||
table := createTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
type UserForDao struct {
|
||||
Id interface{}
|
||||
Passport interface{}
|
||||
Password interface{}
|
||||
Nickname interface{}
|
||||
CreateTime interface{}
|
||||
}
|
||||
data := UserForDao{
|
||||
Id: 1,
|
||||
Passport: "user_1",
|
||||
Password: "pass_1",
|
||||
}
|
||||
result, err := db.Model(table).Data(data).Insert()
|
||||
t.AssertNil(err)
|
||||
n, _ := result.LastInsertId()
|
||||
t.Assert(n, 1)
|
||||
|
||||
one, err := db.Model(table).WherePri(1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one[`id`], `1`)
|
||||
t.Assert(one[`passport`], `user_1`)
|
||||
t.Assert(one[`password`], `pass_1`)
|
||||
t.Assert(one[`nickname`], ``)
|
||||
t.Assert(one[`create_time`], ``)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Model_Update_Data_ForDao(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
type UserForDao struct {
|
||||
Id interface{}
|
||||
Passport interface{}
|
||||
Password interface{}
|
||||
Nickname interface{}
|
||||
CreateTime interface{}
|
||||
}
|
||||
data := UserForDao{
|
||||
Id: 1,
|
||||
Passport: "user_100",
|
||||
Password: "pass_100",
|
||||
}
|
||||
_, err := db.Model(table).Data(data).WherePri(1).Update()
|
||||
t.AssertNil(err)
|
||||
|
||||
one, err := db.Model(table).WherePri(1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one[`id`], `1`)
|
||||
t.Assert(one[`passport`], `user_100`)
|
||||
t.Assert(one[`password`], `pass_100`)
|
||||
t.Assert(one[`nickname`], `name_1`)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Model_Where_ForDao(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
type UserForDao struct {
|
||||
Id interface{}
|
||||
Passport interface{}
|
||||
Password interface{}
|
||||
Nickname interface{}
|
||||
CreateTime interface{}
|
||||
}
|
||||
where := UserForDao{
|
||||
Id: 1,
|
||||
Passport: "user_1",
|
||||
Password: "pass_1",
|
||||
}
|
||||
one, err := db.Model(table).Where(where).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one[`id`], `1`)
|
||||
t.Assert(one[`passport`], `user_1`)
|
||||
t.Assert(one[`password`], `pass_1`)
|
||||
t.Assert(one[`nickname`], `name_1`)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user