// 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 mysql_test import ( "fmt" "testing" "time" "github.com/gogf/gf/v2/database/gdb" "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/os/gtime" "github.com/gogf/gf/v2/test/gtest" "github.com/gogf/gf/v2/text/gregex" "github.com/gogf/gf/v2/text/gstr" "github.com/gogf/gf/v2/util/gmeta" "github.com/gogf/gf/v2/util/guid" ) // https://github.com/gogf/gf/issues/1934 func Test_Issue1934(t *testing.T) { table := createInitTable() defer dropTable(table) gtest.C(t, func(t *gtest.T) { one, err := db.Model(table).Where(" id ", 1).One() t.AssertNil(err) t.Assert(one["id"], 1) }) } // https://github.com/gogf/gf/issues/1570 func Test_Issue1570(t *testing.T) { var ( tableUser = "user_" + gtime.TimestampMicroStr() tableUserDetail = "user_detail_" + gtime.TimestampMicroStr() tableUserScores = "user_scores_" + gtime.TimestampMicroStr() ) if _, err := db.Exec(ctx, fmt.Sprintf(` CREATE TABLE %s ( uid int(10) unsigned NOT NULL AUTO_INCREMENT, name varchar(45) NOT NULL, PRIMARY KEY (uid) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; `, tableUser)); err != nil { gtest.Error(err) } defer dropTable(tableUser) if _, err := db.Exec(ctx, fmt.Sprintf(` CREATE TABLE %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(ctx, fmt.Sprintf(` CREATE TABLE %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 EntityUser struct { Uid int `json:"uid"` Name string `json:"name"` } type EntityUserDetail struct { Uid int `json:"uid"` Address string `json:"address"` } type EntityUserScores struct { Id int `json:"id"` Uid int `json:"uid"` Score int `json:"score"` } type Entity struct { User *EntityUser UserDetail *EntityUserDetail UserScores []*EntityUserScores } // Initialize the data. gtest.C(t, func(t *gtest.T) { var err error for i := 1; i <= 5; i++ { // User. _, err = db.Insert(ctx, tableUser, g.Map{ "uid": i, "name": fmt.Sprintf(`name_%d`, i), }) t.AssertNil(err) // Detail. _, err = db.Insert(ctx, tableUserDetail, g.Map{ "uid": i, "address": fmt.Sprintf(`address_%d`, i), }) t.AssertNil(err) // Scores. for j := 1; j <= 5; j++ { _, err = db.Insert(ctx, tableUserScores, g.Map{ "uid": i, "score": j, }) t.AssertNil(err) } } }) // Result ScanList with struct elements and pointer attributes. gtest.C(t, func(t *gtest.T) { var users []Entity // User err := db.Model(tableUser). Where("uid", g.Slice{3, 4}). Fields("uid"). Order("uid asc"). ScanList(&users, "User") t.AssertNil(err) t.AssertNil(err) t.Assert(len(users), 2) t.Assert(users[0].User, &EntityUser{3, ""}) t.Assert(users[1].User, &EntityUser{4, ""}) // Detail err = db.Model(tableUserDetail). Where("uid", gdb.ListItemValues(users, "User", "Uid")). Order("uid asc"). ScanList(&users, "UserDetail", "User", "uid:Uid") t.AssertNil(err) t.AssertNil(err) t.Assert(users[0].UserDetail, &EntityUserDetail{3, "address_3"}) t.Assert(users[1].UserDetail, &EntityUserDetail{4, "address_4"}) // Scores err = db.Model(tableUserScores). Where("uid", gdb.ListItemValues(users, "User", "Uid")). Order("id asc"). ScanList(&users, "UserScores", "User", "uid:Uid") t.AssertNil(err) t.AssertNil(err) t.Assert(len(users[0].UserScores), 5) t.Assert(len(users[1].UserScores), 5) t.Assert(users[0].UserScores[0].Uid, 3) t.Assert(users[0].UserScores[0].Score, 1) t.Assert(users[0].UserScores[4].Score, 5) t.Assert(users[1].UserScores[0].Uid, 4) t.Assert(users[1].UserScores[0].Score, 1) t.Assert(users[1].UserScores[4].Score, 5) }) } // https://github.com/gogf/gf/issues/1401 func Test_Issue1401(t *testing.T) { var ( table1 = "parcels" table2 = "parcel_items" ) array := gstr.SplitAndTrim(gtest.DataContent(`issue1401.sql`), ";") for _, v := range array { if _, err := db.Exec(ctx, v); err != nil { gtest.Error(err) } } defer dropTable(table1) defer dropTable(table2) gtest.C(t, func(t *gtest.T) { type NItem struct { Id int `json:"id"` ParcelId int `json:"parcel_id"` } type ParcelItem struct { gmeta.Meta `orm:"table:parcel_items"` NItem } type ParcelRsp struct { gmeta.Meta `orm:"table:parcels"` Id int `json:"id"` Items []*ParcelItem `json:"items" orm:"with:parcel_id=Id"` } parcelDetail := &ParcelRsp{} err := db.Model(table1).With(parcelDetail.Items).Where("id", 3).Scan(&parcelDetail) t.AssertNil(err) t.Assert(parcelDetail.Id, 3) t.Assert(len(parcelDetail.Items), 1) t.Assert(parcelDetail.Items[0].Id, 2) t.Assert(parcelDetail.Items[0].ParcelId, 3) }) } // https://github.com/gogf/gf/issues/1412 func Test_Issue1412(t *testing.T) { var ( table1 = "parcels" table2 = "items" ) array := gstr.SplitAndTrim(gtest.DataContent(`issue1412.sql`), ";") for _, v := range array { if _, err := db.Exec(ctx, v); err != nil { gtest.Error(err) } } defer dropTable(table1) defer dropTable(table2) gtest.C(t, func(t *gtest.T) { type Items struct { gmeta.Meta `orm:"table:items"` Id int `json:"id"` Name string `json:"name"` } type ParcelRsp struct { gmeta.Meta `orm:"table:parcels"` Id int `json:"id"` ItemId int `json:"item_id"` Items Items `json:"items" orm:"with:Id=ItemId"` } entity := &ParcelRsp{} err := db.Model("parcels").With(Items{}).Where("id=3").Scan(&entity) t.AssertNil(err) t.Assert(entity.Id, 3) t.Assert(entity.ItemId, 0) t.Assert(entity.Items.Id, 0) t.Assert(entity.Items.Name, "") }) gtest.C(t, func(t *gtest.T) { type Items struct { gmeta.Meta `orm:"table:items"` Id int `json:"id"` Name string `json:"name"` } type ParcelRsp struct { gmeta.Meta `orm:"table:parcels"` Id int `json:"id"` ItemId int `json:"item_id"` Items Items `json:"items" orm:"with:Id=ItemId"` } entity := &ParcelRsp{} err := db.Model("parcels").With(Items{}).Where("id=30000").Scan(&entity) t.AssertNE(err, nil) t.Assert(entity.Id, 0) t.Assert(entity.ItemId, 0) t.Assert(entity.Items.Id, 0) t.Assert(entity.Items.Name, "") }) } // https://github.com/gogf/gf/issues/1002 func Test_Issue1002(t *testing.T) { table := createTable() defer dropTable(table) result, err := db.Model(table).Data(g.Map{ "id": 1, "passport": "port_1", "password": "pass_1", "nickname": "name_2", "create_time": "2020-10-27 19:03:33", }).Insert() gtest.AssertNil(err) n, _ := result.RowsAffected() gtest.Assert(n, 1) // where + string. gtest.C(t, func(t *gtest.T) { v, err := db.Model(table).Fields("id").Where("create_time>'2020-10-27 19:03:32' and create_time<'2020-10-27 19:03:34'").Value() t.AssertNil(err) t.Assert(v.Int(), 1) }) gtest.C(t, func(t *gtest.T) { v, err := db.Model(table).Fields("id").Where("create_time>'2020-10-27 19:03:32' and create_time<'2020-10-27 19:03:34'").Value() t.AssertNil(err) t.Assert(v.Int(), 1) }) // where + string arguments. gtest.C(t, func(t *gtest.T) { v, err := db.Model(table).Fields("id").Where("create_time>? and create_time? and create_time? and create_time? and create_time? and create_time? and create_time