Feature/driver-dm fix something is invalid in dm (#2158)

* fix core.hasfield error index out of range & fix GroupConcat is invalid in dm

* add unit test

Co-authored-by: Xu <zhenghao.xu>
This commit is contained in:
zhonghuaxunGM 2022-09-28 10:02:48 +08:00 committed by GitHub
parent 66aa0c7050
commit cd5bf7c504
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 126 additions and 3 deletions

View File

@ -139,7 +139,7 @@ func (d *Driver) TableFields(
return nil, err
}
fields = make(map[string]*gdb.TableField)
for _, m := range result {
for i, m := range result {
// m[NULLABLE] returns "N" "Y"
// "N" means not null
// "Y" means could be null
@ -148,7 +148,7 @@ func (d *Driver) TableFields(
nullable = true
}
fields[m["COLUMN_NAME"].String()] = &gdb.TableField{
Index: m["COLUMN_ID"].Int(),
Index: i,
Name: m["COLUMN_NAME"].String(),
Type: m["DATA_TYPE"].String(),
Null: nullable,
@ -167,7 +167,8 @@ func (d *Driver) DoFilter(ctx context.Context, link gdb.Link, sql string, args [
newSql, newArgs, err = d.Core.DoFilter(ctx, link, newSql, newArgs)
}()
// There should be no need to capitalize, because it has been done from field processing before
newSql, _ = gregex.ReplaceString(`["\n\t]`, "", sql)
newSql, err = gregex.ReplaceString(`["\n\t]`, "", sql)
newSql = gstr.ReplaceI(newSql, "GROUP_CONCAT", "WM_CONCAT")
// g.Dump("Driver.DoFilter()::newSql", newSql)
newArgs = args
// g.Dump("Driver.DoFilter()::newArgs", newArgs)

View File

@ -582,3 +582,125 @@ func Test_Empty_Slice_Argument(t *testing.T) {
t.Assert(len(result), 0)
})
}
// func Test_GROUP_CONCAT(t *testing.T) {
// gtest.C(t, func(t *gtest.T) {
// type GroupIdAndUserIDsInfo struct {
// GroupID int64
// UserIDs string
// }
// result := make([]GroupIdAndUserIDsInfo, 0)
// model := db.Model("t_inf_group", "groupinfo").Fields("groupinfo.id as group_id", "GROUP_CONCAT(userinfo.id) as user_ids")
// model.InnerJoin("t_lin_user_group", "lin", "groupinfo.id = lin.group_id")
// model.InnerJoin("t_inf_user", "userinfo", "lin.user_id = userinfo.id")
// model.Where("groupinfo.enabled", 1).Where("groupinfo.deleted", 0)
// model.Where("userinfo.enabled", 1).Where("userinfo.deleted", 0)
// model.Group("groupinfo.id")
// err := model.Scan(&result)
// gtest.Assert(err, nil)
// g.Dump(result)
// })
// }
// func TestGroup(t *testing.T) {
// gtest.C(t, func(t *gtest.T) {
// type GroupListResult struct {
// ID int64 `json:"group_id"`
// GroupName string `json:"group_name"`
// CategoryName string `json:"category_name"`
// Description string `json:"description"`
// RoleName string `json:"role_name"`
// UserIDs []string `json:"user_ids"`
// Enabled int64 `json:"enabled"`
// CreatedTime string `json:"created_time"`
// UpdateTime string `json:"updated_time"`
// }
// result := make([]GroupListResult, 0)
// model := db.Model("t_inf_group", "groupinfo")
// model.LeftJoin("t_inf_group_category", "category", "groupinfo.category_id=category.id and (category.enabled = 1) and (category.deleted = 0)").
// Where("groupinfo.deleted", 0).
// Where("groupinfo.enabled", 1)
// total, err := model.Count()
// gtest.Assert(err, nil)
// model.Fields("distinct groupinfo.id, groupinfo.group_name, groupinfo.enabled, ifnull(category.category_name,'') as category_name", "groupinfo.created_time", "groupinfo.updated_time", "groupinfo.description")
// err = model.Order("groupinfo.updated_time desc").Page(1, 100).Scan(&result)
// gtest.Assert(err, nil)
// g.Dump(result)
// g.Dump(total)
// })
// gtest.C(t, func(t *gtest.T) {
// type GroupListByUserIdResult struct {
// ID int64 `json:"group_id"`
// GroupName string `json:"group_name"`
// CategoryName string `json:"category_name"`
// RoleName string `json:"role_name"`
// }
// result := make([]*GroupListByUserIdResult, 0)
// model := db.Model("t_inf_group", "groupinfo").Fields("distinct groupinfo.id, groupinfo.group_name, groupinfo.enabled, category.category_name,groupinfo.updated_time")
// model.LeftJoin("t_inf_group_category", "category", "groupinfo.category_id=category.id and (category.enabled = 1) and (category.deleted = 0)")
// // if userId != 0 {
// // model.InnerJoin(grouptype.TLINUSERGROUP, "ug", "groupinfo.id=ug.group_id")
// // model.InnerJoin(grouptype.TINFUSER, "u", "u.id=ug.user_id")
// // model.Where("u.id = ?", userId).Where("u.deleted", consts.DataDeletedFalse)
// // }
// model.Where("groupinfo.enabled", 1).Where("groupinfo.deleted", 0)
// err := model.Order("groupinfo.updated_time desc").Scan(&result)
// //
// gtest.Assert(err, nil)
// g.Dump(result)
// })
// gtest.C(t, func(t *gtest.T) {
// model := db.Model("t_inf_role", "role").Fields("role.role_name", "role.id")
// model.RightJoin("t_lin_group_role", "link", "link.role_id=role.id")
// // model.Where("link.group_id", gid)
// model.Where("role.deleted", 0)
// record, err := model.One()
// gtest.Assert(err, nil)
// g.Dump(record)
// })
// gtest.C(t, func(t *gtest.T) {
// type GroupInfos struct {
// RoleName string `orm:"role_name"`
// RoleID int64 `orm:"id"`
// GroupID int64 `orm:"group_id"`
// }
// result := make([]GroupInfos, 0)
// model := db.Model("t_inf_role", "role").Fields("role.id", "role.role_name", "link.group_id")
// model.RightJoin("t_lin_group_role", "link", "link.role_id=role.id")
// model.Where("role.enabled", 1).Where("role.deleted", 0)
// err := model.Scan(&result)
// gtest.Assert(err, nil)
// g.Dump(result)
// })
// gtest.C(t, func(t *gtest.T) {
// type GroupIdAndRoleNameInfo struct {
// GroupID int64
// RoleID int64
// RoleName string
// }
// result := make([]GroupIdAndRoleNameInfo, 0)
// model := db.Model("t_inf_group", "groupinfo").Fields("groupinfo.id as group_id", "lin.role_id", "role.role_name")
// model.InnerJoin("t_lin_group_role", "lin", "groupinfo.id = lin.group_id")
// model.InnerJoin("t_inf_role", "role", "lin.role_id = role.id")
// model.Where("groupinfo.enabled", 1).Where("groupinfo.deleted", 0)
// model.Where("role.enabled", 1).Where("role.deleted", 0)
// err2 := model.Scan(&result)
// gtest.Assert(err2, nil)
// g.Dump(result)
// })
// }