Skip to content

按条件判断拼接查询bug #72

@ColorDreams

Description

@ColorDreams

伪代码如下:

        // 变量 user 为入参
	query, u := gplus.NewQuery[User]()
	query.Eq(&u.Password, "12345678").Or(func(q *gplus.QueryCond[User]) {
		q.EqCond(user.Id > 0,&u.Id, user.Id).
			Or().EqCond(len(user.Account) > 0, &u.Account, user.Account).
			Or().EqCond(len(user.Email) > 0, &u.Email, user.Email).
			Or().EqCond(len(user.Phone) > 0, &u.Phone, user.Phone)
	}).OrderByAsc(&u.Id)

当条件为false时,EqCond的条件不会加入到sql中,但是因为前面用了一个Or()作为条件关系,会出现这种情况:

SELECT * FROM `user` WHERE password = '12345678' OR ( id = 0 OR account = 'admin' OR )  ORDER BY id ASC LIMIT 1

应该所有的*Cond条件函数,在使用Or()或者And()之后都会出现这种情况,需要做特殊的适配,AndCond()和OrCond()没办法用在这种条件分组的情况

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions