mirror of
https://gitee.com/johng/gf
synced 2026-07-07 06:15:15 +08:00
improve Model.Data function for package gdb
This commit is contained in:
@ -1,12 +1,25 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/gogf/gf/database/gdb"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/os/glog"
|
||||
"github.com/gogf/gf/util/gcoime"
|
||||
)
|
||||
|
||||
func main() {
|
||||
l := glog.New()
|
||||
l.Info("info1")
|
||||
l.SetLevelStr("notice")
|
||||
l.Info("info2")
|
||||
g.Sliceg.Sli
|
||||
for _, line := range lines {
|
||||
gl := gdb.Map{}
|
||||
r := NewMysqlTableLogger()
|
||||
if err := json.Unmarshal([]byte(line), &r); err != nil || r == nil { //写入日志表数据错误
|
||||
glog.Errorf("%s[%s] SaveToDB json.DecodeTo : %v, log=%s", c.LogHead, c.LogStatus(), err, GetSubString(line, 1024))
|
||||
continue
|
||||
}
|
||||
if tableName == "" {
|
||||
tableName = r.GetTableName()
|
||||
}
|
||||
gls = append(gls, r)
|
||||
}
|
||||
}
|
||||
|
||||
@ -40,6 +40,11 @@ type apiInterfaces interface {
|
||||
Interfaces() []interface{}
|
||||
}
|
||||
|
||||
// apiMapStrAny is the interface support for converting struct parameter to map.
|
||||
type apiMapStrAny interface {
|
||||
MapStrAny() map[string]interface{}
|
||||
}
|
||||
|
||||
const (
|
||||
ORM_TAG_FOR_STRUCT = "orm"
|
||||
ORM_TAG_FOR_UNIQUE = "unique"
|
||||
|
||||
@ -67,8 +67,21 @@ func (m *Model) Data(data ...interface{}) *Model {
|
||||
list[i] = DataToMapDeep(rv.Index(i).Interface())
|
||||
}
|
||||
model.data = list
|
||||
case reflect.Map, reflect.Struct:
|
||||
case reflect.Map:
|
||||
model.data = DataToMapDeep(data[0])
|
||||
case reflect.Struct:
|
||||
if v, ok := data[0].(apiMapStrAny); ok {
|
||||
model.data = v.MapStrAny()
|
||||
} else if v, ok := data[0].(apiInterfaces); ok {
|
||||
array := v.Interfaces()
|
||||
list := make(List, len(array))
|
||||
for i := 0; i < len(array); i++ {
|
||||
list[i] = DataToMapDeep(array[i])
|
||||
}
|
||||
model.data = list
|
||||
} else {
|
||||
model.data = DataToMapDeep(data[0])
|
||||
}
|
||||
default:
|
||||
model.data = data[0]
|
||||
}
|
||||
|
||||
@ -9,6 +9,7 @@ package gdb_test
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/container/garray"
|
||||
"github.com/gogf/gf/container/gmap"
|
||||
"github.com/gogf/gf/util/gutil"
|
||||
"testing"
|
||||
@ -937,15 +938,48 @@ func Test_Model_GroupBy(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_Model_Data(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
result, err := db.Table(table).Data("nickname=?", "test").Where("id=?", 3).Update()
|
||||
t.Assert(err, nil)
|
||||
n, _ := result.RowsAffected()
|
||||
t.Assert(n, 1)
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
table := createTable()
|
||||
defer dropTable(table)
|
||||
users := make([]g.MapStrAny, 0)
|
||||
for i := 1; i <= 10; i++ {
|
||||
users = append(users, g.MapStrAny{
|
||||
"id": i,
|
||||
"passport": fmt.Sprintf(`passport_%d`, i),
|
||||
"password": fmt.Sprintf(`password_%d`, i),
|
||||
"nickname": fmt.Sprintf(`nickname_%d`, i),
|
||||
})
|
||||
}
|
||||
result, err := db.Table(table).Data(users).Batch(2).Insert()
|
||||
t.Assert(err, nil)
|
||||
n, _ := result.RowsAffected()
|
||||
t.Assert(n, 10)
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
table := createTable()
|
||||
defer dropTable(table)
|
||||
users := garray.New()
|
||||
for i := 1; i <= 10; i++ {
|
||||
users.Append(g.MapStrAny{
|
||||
"id": i,
|
||||
"passport": fmt.Sprintf(`passport_%d`, i),
|
||||
"password": fmt.Sprintf(`password_%d`, i),
|
||||
"nickname": fmt.Sprintf(`nickname_%d`, i),
|
||||
})
|
||||
}
|
||||
result, err := db.Table(table).Data(users).Batch(2).Insert()
|
||||
t.Assert(err, nil)
|
||||
n, _ := result.RowsAffected()
|
||||
t.Assert(n, 10)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Model_Where(t *testing.T) {
|
||||
@ -2002,3 +2036,53 @@ func Test_Model_Schema2(t *testing.T) {
|
||||
t.Assert(v.String(), "")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Model_FieldsExStruct(t *testing.T) {
|
||||
table := createTable()
|
||||
defer dropTable(table)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
type User struct {
|
||||
Id int `orm:"id" json:"id"`
|
||||
Passport string `orm:"password" json:"pass_port"`
|
||||
Password string `orm:"password" json:"password"`
|
||||
NickName string `orm:"nickname" json:"nick__name"`
|
||||
}
|
||||
user := &User{
|
||||
Id: 1,
|
||||
Passport: "111",
|
||||
Password: "222",
|
||||
NickName: "333",
|
||||
}
|
||||
r, err := db.Table(table).FieldsEx("create_time, password").OmitEmpty().Data(user).Insert()
|
||||
t.Assert(err, nil)
|
||||
n, err := r.RowsAffected()
|
||||
t.Assert(err, nil)
|
||||
t.Assert(n, 1)
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
type User struct {
|
||||
Id int `orm:"id" json:"id"`
|
||||
Passport string `orm:"password" json:"pass_port"`
|
||||
Password string `orm:"password" json:"password"`
|
||||
NickName string `orm:"nickname" json:"nick__name"`
|
||||
}
|
||||
users := make([]*User, 0)
|
||||
for i := 100; i < 110; i++ {
|
||||
users = append(users, &User{
|
||||
Id: i,
|
||||
Passport: fmt.Sprintf(`passport_%d`, i),
|
||||
Password: fmt.Sprintf(`password_%d`, i),
|
||||
NickName: fmt.Sprintf(`nickname_%d`, i),
|
||||
})
|
||||
}
|
||||
r, err := db.Table(table).FieldsEx("create_time, password").
|
||||
OmitEmpty().
|
||||
Batch(2).
|
||||
Data(users).
|
||||
Insert()
|
||||
t.Assert(err, nil)
|
||||
n, err := r.RowsAffected()
|
||||
t.Assert(err, nil)
|
||||
t.Assert(n, 10)
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user