type Filter interface { // FilterWithColumn SELECT * FROM @@table WHERE @@column=@value FilterWithColumn(column string, value string) ([]*gen.T, error)
// FilterWithObject // // SELECT * FROM @@table where id > 0 // {{if user != nil}} // {{if user.ID > 0}} // AND id = @user.ID // {{else if user.Username != ""}} // AND username=@user.Username // {{else if user.Email != ""}} // AND email=@user.Email // {{end}} // {{end}} FilterWithObject(user *gen.T) ([]*gen.T, error) }
func Test_userDo_FilterWithColumn(t *testing.T) { result, err := User.FilterWithColumn("username", "test") if err != nil { fmt.Println("filter user fail") } for _, each := range result { fmt.Println(each) } }
func Test_userDo_FilterWithObject(t *testing.T) { user := model.User{ ID: 3, Username: "test2", Email: "test2@test", } result, err := User.FilterWithObject(&user) if err != nil { fmt.Println("filter user fail") } for _, each := range result { fmt.Println(each) } }
其中FilterWithObject函数的User对象的ID不为空,且大于的时候,执行的SQL如下
1 2
SELECT * FROM user where id > 0 AND id = 3
ID为空的时候,执行的SQL如下
1 2
SELECT * FROM user where id > 0 AND username='test2'
ID和Username都为空的时候,执行的SQL如下
1 2
SELECT * FROM user where id > 0 AND email='test2@test'