diff --git a/.example/util/gvalid/gvalid_i18n_http.go b/.example/util/gvalid/gvalid_i18n_http.go new file mode 100644 index 000000000..cba5062fc --- /dev/null +++ b/.example/util/gvalid/gvalid_i18n_http.go @@ -0,0 +1,35 @@ +package main + +import ( + "github.com/gogf/gf/net/ghttp" + + "github.com/gogf/gf/frame/g" + "github.com/gogf/gf/i18n/gi18n" +) + +func main() { + type User struct { + Name string `v:"required#ReuiredUserName"` + Type int `v:"required#ReuiredUserType"` + Project string `v:"size:10#MustSize"` + } + s := g.Server() + s.Group("/", func(group *ghttp.RouterGroup) { + group.Middleware(func(r *ghttp.Request) { + lang := r.GetString("lang", "zh-CN") + r.SetCtx(gi18n.WithLanguage(r.Context(), lang)) + r.Middleware.Next() + }) + group.GET("/validate", func(r *ghttp.Request) { + var ( + err error + user = User{} + ) + if err = r.Parse(&user); err != nil { + r.Response.WriteExit(err) + } + r.Response.WriteExit(user) + }) + }) + s.SetPort(8199) +} diff --git a/database/gdb/gdb_z_mysql_association_with_test.go b/database/gdb/gdb_z_mysql_association_with_test.go index 94827ac43..3ea1c19f1 100644 --- a/database/gdb/gdb_z_mysql_association_with_test.go +++ b/database/gdb/gdb_z_mysql_association_with_test.go @@ -17,6 +17,74 @@ import ( "testing" ) +/* +mysql> show tables; ++----------------+ +| Tables_in_test | ++----------------+ +| user | +| user_detail | +| user_score | ++----------------+ +3 rows in set (0.01 sec) + +mysql> select * from `user`; ++----+--------+ +| id | name | ++----+--------+ +| 1 | name_1 | +| 2 | name_2 | +| 3 | name_3 | +| 4 | name_4 | +| 5 | name_5 | ++----+--------+ +5 rows in set (0.01 sec) + +mysql> select * from `user_detail`; ++-----+-----------+ +| uid | address | ++-----+-----------+ +| 1 | address_1 | +| 2 | address_2 | +| 3 | address_3 | +| 4 | address_4 | +| 5 | address_5 | ++-----+-----------+ +5 rows in set (0.00 sec) + +mysql> select * from `user_score`; ++----+-----+-------+ +| id | uid | score | ++----+-----+-------+ +| 1 | 1 | 1 | +| 2 | 1 | 2 | +| 3 | 1 | 3 | +| 4 | 1 | 4 | +| 5 | 1 | 5 | +| 6 | 2 | 1 | +| 7 | 2 | 2 | +| 8 | 2 | 3 | +| 9 | 2 | 4 | +| 10 | 2 | 5 | +| 11 | 3 | 1 | +| 12 | 3 | 2 | +| 13 | 3 | 3 | +| 14 | 3 | 4 | +| 15 | 3 | 5 | +| 16 | 4 | 1 | +| 17 | 4 | 2 | +| 18 | 4 | 3 | +| 19 | 4 | 4 | +| 20 | 4 | 5 | +| 21 | 5 | 1 | +| 22 | 5 | 2 | +| 23 | 5 | 3 | +| 24 | 5 | 4 | +| 25 | 5 | 5 | ++----+-----+-------+ +25 rows in set (0.00 sec) +*/ + func Test_Table_Relation_With_Scan(t *testing.T) { var ( tableUser = "user" @@ -670,6 +738,151 @@ PRIMARY KEY (id) }) } +//func Test_Table_Relation_WithAllCondition_List(t *testing.T) { +// var ( +// tableUser = "user" +// tableUserDetail = "user_detail" +// tableUserScores = "user_scores" +// ) +// if _, err := db.Exec(fmt.Sprintf(` +//CREATE TABLE IF NOT EXISTS %s ( +//id int(10) unsigned NOT NULL AUTO_INCREMENT, +//name varchar(45) NOT NULL, +//PRIMARY KEY (id) +//) ENGINE=InnoDB DEFAULT CHARSET=utf8; +// `, tableUser)); err != nil { +// gtest.Error(err) +// } +// defer dropTable(tableUser) +// +// if _, err := db.Exec(fmt.Sprintf(` +//CREATE TABLE IF NOT EXISTS %s ( +//uid int(10) unsigned NOT NULL AUTO_INCREMENT, +//address varchar(45) NOT NULL, +//PRIMARY KEY (uid) +//) ENGINE=InnoDB DEFAULT CHARSET=utf8; +// `, tableUserDetail)); err != nil { +// gtest.Error(err) +// } +// defer dropTable(tableUserDetail) +// +// if _, err := db.Exec(fmt.Sprintf(` +//CREATE TABLE IF NOT EXISTS %s ( +//id int(10) unsigned NOT NULL AUTO_INCREMENT, +//uid int(10) unsigned NOT NULL, +//score int(10) unsigned NOT NULL, +//PRIMARY KEY (id) +//) ENGINE=InnoDB DEFAULT CHARSET=utf8; +// `, tableUserScores)); err != nil { +// gtest.Error(err) +// } +// defer dropTable(tableUserScores) +// +// type UserDetail struct { +// gmeta.Meta `orm:"table:user_detail"` +// Uid int `json:"uid"` +// Address string `json:"address"` +// } +// +// type UserScores struct { +// gmeta.Meta `orm:"table:user_scores"` +// Id int `json:"id"` +// Uid int `json:"uid"` +// Score int `json:"score"` +// } +// +// type User struct { +// gmeta.Meta `orm:"table:user"` +// Id int `json:"id"` +// Name string `json:"name"` +// UserDetail *UserDetail `orm:"with:uid=id"` +// UserScores []*UserScores `orm:"with:uid=id, score>1 and score<5"` +// } +// +// // Initialize the data. +// var err error +// for i := 1; i <= 5; i++ { +// // User. +// _, err = db.Insert(tableUser, g.Map{ +// "id": i, +// "name": fmt.Sprintf(`name_%d`, i), +// }) +// gtest.Assert(err, nil) +// // Detail. +// _, err = db.Insert(tableUserDetail, g.Map{ +// "uid": i, +// "address": fmt.Sprintf(`address_%d`, i), +// }) +// gtest.Assert(err, nil) +// // Scores. +// for j := 1; j <= 5; j++ { +// _, err = db.Insert(tableUserScores, g.Map{ +// "uid": i, +// "score": j, +// }) +// gtest.Assert(err, nil) +// } +// } +// +// db.SetDebug(true) +// defer db.SetDebug(false) +// +// gtest.C(t, func(t *gtest.T) { +// var users []*User +// err := db.Model(tableUser).WithAll().Where("id", []int{3, 4}).Scan(&users) +// t.AssertNil(err) +// t.Assert(len(users), 2) +// t.Assert(users[0].Id, 3) +// t.Assert(users[0].Name, "name_3") +// t.AssertNE(users[0].UserDetail, nil) +// t.Assert(users[0].UserDetail.Uid, 3) +// t.Assert(users[0].UserDetail.Address, "address_3") +// t.Assert(len(users[0].UserScores), 5) +// t.Assert(users[0].UserScores[0].Uid, 3) +// t.Assert(users[0].UserScores[0].Score, 1) +// t.Assert(users[0].UserScores[4].Uid, 3) +// t.Assert(users[0].UserScores[4].Score, 5) +// +// t.Assert(users[1].Id, 4) +// t.Assert(users[1].Name, "name_4") +// t.AssertNE(users[1].UserDetail, nil) +// t.Assert(users[1].UserDetail.Uid, 4) +// t.Assert(users[1].UserDetail.Address, "address_4") +// t.Assert(len(users[1].UserScores), 5) +// t.Assert(users[1].UserScores[0].Uid, 4) +// t.Assert(users[1].UserScores[0].Score, 1) +// t.Assert(users[1].UserScores[4].Uid, 4) +// t.Assert(users[1].UserScores[4].Score, 5) +// }) +// gtest.C(t, func(t *gtest.T) { +// var users []User +// err := db.Model(tableUser).WithAll().Where("id", []int{3, 4}).Scan(&users) +// t.AssertNil(err) +// t.Assert(len(users), 2) +// t.Assert(users[0].Id, 3) +// t.Assert(users[0].Name, "name_3") +// t.AssertNE(users[0].UserDetail, nil) +// t.Assert(users[0].UserDetail.Uid, 3) +// t.Assert(users[0].UserDetail.Address, "address_3") +// t.Assert(len(users[0].UserScores), 5) +// t.Assert(users[0].UserScores[0].Uid, 3) +// t.Assert(users[0].UserScores[0].Score, 1) +// t.Assert(users[0].UserScores[4].Uid, 3) +// t.Assert(users[0].UserScores[4].Score, 5) +// +// t.Assert(users[1].Id, 4) +// t.Assert(users[1].Name, "name_4") +// t.AssertNE(users[1].UserDetail, nil) +// t.Assert(users[1].UserDetail.Uid, 4) +// t.Assert(users[1].UserDetail.Address, "address_4") +// t.Assert(len(users[1].UserScores), 5) +// t.Assert(users[1].UserScores[0].Uid, 4) +// t.Assert(users[1].UserScores[0].Score, 1) +// t.Assert(users[1].UserScores[4].Uid, 4) +// t.Assert(users[1].UserScores[4].Score, 5) +// }) +//} + func Test_Table_Relation_WithAll_Embedded(t *testing.T) { var ( tableUser = "user" @@ -1380,6 +1593,7 @@ func Test_Table_Relation_With_MultipleDepends1(t *testing.T) { t.Assert(tableA[1].TableB.TableC.Id, 300) }) } + func Test_Table_Relation_With_MultipleDepends2(t *testing.T) { defer func() { dropTable("table_a") @@ -1466,6 +1680,7 @@ func Test_Table_Relation_With_MultipleDepends2(t *testing.T) { t.Assert(tableA[1].TableB[1].TableC, nil) }) } + func Test_Table_Relation_With_MultipleDepends_Embedded(t *testing.T) { defer func() { dropTable("table_a")