Skip to content

Commit

Permalink
Merge pull request #191 from jiang4869/master
Browse files Browse the repository at this point in the history
给自定义SQL增加了根据条件判断是否生成的功能
  • Loading branch information
xxjwxc authored Jan 7, 2022
2 parents f79e1be + 819706e commit 9e62d21
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 37 deletions.
52 changes: 34 additions & 18 deletions data/view/genfunc/def.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
}
// 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,
})
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
}
// 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{}) {
Expand Down
3 changes: 2 additions & 1 deletion data/view/genfunc/genfunc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
}
50 changes: 32 additions & 18 deletions data/view/genfunc/model/gen.base.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
}

// 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,
})
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
}

// 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{}) {
Expand Down

0 comments on commit 9e62d21

Please sign in to comment.