diff --git a/data/view/genfunc/def.go b/data/view/genfunc/def.go index a6db57d..8793b6d 100755 --- a/data/view/genfunc/def.go +++ b/data/view/genfunc/def.go @@ -121,24 +121,40 @@ type Condition struct { list []*conditionInfo } -// And a condition by and .and 一个条件 -func (c *Condition) And(column string, cases string, value interface{}) { - c.list = append(c.list, &conditionInfo{ - andor: "and", - column: column, // 列名 - case_: cases, // 条件(and,or,in,>=,<=) - value: value, - }) +func (c *Condition) AndWithCondition(condition bool,column string, cases string, value interface{}) (*Condition) { + if condition { + c.list = append(c.list, &conditionInfo{ + andor: "and", + column: column, // 列名 + case_: cases, // 条件(and,or,in,>=,<=) + value: value, + }) + } + return c } -// Or a condition by or .or 一个条件 -func (c *Condition) Or(column string, cases string, value interface{}) { - c.list = append(c.list, &conditionInfo{ - andor: "or", - column: column, // 列名 - case_: cases, // 条件(and,or,in,>=,<=) - value: value, - }) + +// And a Condition by and .and 一个条件 +func (c *Condition) And(column string, cases string, value interface{}) (*Condition) { + return c.AndWithCondition(true,column,cases,value) +} + + +func (c *Condition) OrWithCondition(condition bool,column string, cases string, value interface{}) (*Condition) { + if condition { + c.list = append(c.list, &conditionInfo{ + andor: "or", + column: column, // 列名 + case_: cases, // 条件(and,or,in,>=,<=) + value: value, + }) + } + return c +} + +// Or a Condition by or .or 一个条件 +func (c *Condition) Or(column string, cases string, value interface{}) (*Condition) { + return c.OrWithCondition(true,column,cases,value) } func (c *Condition) Get() (where string, out []interface{}) { diff --git a/data/view/genfunc/genfunc_test.go b/data/view/genfunc/genfunc_test.go index b10dc71..93800c5 100644 --- a/data/view/genfunc/genfunc_test.go +++ b/data/view/genfunc/genfunc_test.go @@ -143,6 +143,7 @@ func TestCondition(t *testing.T) { condition := model.Condition{} condition.And(model.AccountColumns.AccountID, ">=", "1") condition.And(model.AccountColumns.UserID, "in", []string{"1", "2", "3"}) + condition.AndWithCondition(false, model.AccountColumns.AccountID, "in", []string{"5"}) condition.Or(model.AccountColumns.Type, "in", []string{"1", "2", "3"}) where, obj := condition.Get() @@ -155,6 +156,6 @@ func TestCondition(t *testing.T) { sqldb.Close() }() - accountMgr := model.AccountMgr(db.Where(condition.Get())) + accountMgr := model.AccountMgr(db.Where(where, obj...)) accountMgr.Gets() } diff --git a/data/view/genfunc/model/gen.base.go b/data/view/genfunc/model/gen.base.go index ce3dde4..89a575c 100644 --- a/data/view/genfunc/model/gen.base.go +++ b/data/view/genfunc/model/gen.base.go @@ -97,24 +97,38 @@ type Condition struct { list []*conditionInfo } -// And a condition by and .and 一个条件 -func (c *Condition) And(column string, cases string, value interface{}) { - c.list = append(c.list, &conditionInfo{ - andor: "and", - column: column, // 列名 - case_: cases, // 条件(and,or,in,>=,<=) - value: value, - }) +func (c *Condition) AndWithCondition(condition bool, column string, cases string, value interface{}) (*Condition) { + if condition { + c.list = append(c.list, &conditionInfo{ + andor: "and", + column: column, // 列名 + case_: cases, // 条件(and,or,in,>=,<=) + value: value, + }) + } + return c } -// Or a condition by or .or 一个条件 -func (c *Condition) Or(column string, cases string, value interface{}) { - c.list = append(c.list, &conditionInfo{ - andor: "or", - column: column, // 列名 - case_: cases, // 条件(and,or,in,>=,<=) - value: value, - }) +// And a Condition by and .and 一个条件 +func (c *Condition) And(column string, cases string, value interface{})(*Condition) { + return c.AndWithCondition(true, column, cases, value) +} + +func (c *Condition) OrWithCondition(condition bool, column string, cases string, value interface{}) (*Condition){ + if condition { + c.list = append(c.list, &conditionInfo{ + andor: "or", + column: column, // 列名 + case_: cases, // 条件(and,or,in,>=,<=) + value: value, + }) + } + return c +} + +// Or a Condition by or .or 一个条件 +func (c *Condition) Or(column string, cases string, value interface{})(*Condition) { + return c.OrWithCondition(true, column, cases, value) } func (c *Condition) Get() (where string, out []interface{}) {