Merge pull request #191 from jiang4869/master

给自定义SQL增加了根据条件判断是否生成的功能
master
xxj 3 years ago committed by GitHub
commit 9e62d21919
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -121,24 +121,40 @@ type Condition struct {
list []*conditionInfo list []*conditionInfo
} }
// And a condition by and .and 一个条件 func (c *Condition) AndWithCondition(condition bool,column string, cases string, value interface{}) (*Condition) {
func (c *Condition) And(column string, cases string, value interface{}) { if condition {
c.list = append(c.list, &conditionInfo{ c.list = append(c.list, &conditionInfo{
andor: "and", andor: "and",
column: column, // 列名 column: column, // 列名
case_: cases, // 条件(and,or,in,>=,<=) case_: cases, // 条件(and,or,in,>=,<=)
value: value, 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", // And a Condition by and .and 一个条件
column: column, // 列名 func (c *Condition) And(column string, cases string, value interface{}) (*Condition) {
case_: cases, // 条件(and,or,in,>=,<=) return c.AndWithCondition(true,column,cases,value)
value: 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{}) { func (c *Condition) Get() (where string, out []interface{}) {

@ -143,6 +143,7 @@ func TestCondition(t *testing.T) {
condition := model.Condition{} condition := model.Condition{}
condition.And(model.AccountColumns.AccountID, ">=", "1") condition.And(model.AccountColumns.AccountID, ">=", "1")
condition.And(model.AccountColumns.UserID, "in", []string{"1", "2", "3"}) 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"}) condition.Or(model.AccountColumns.Type, "in", []string{"1", "2", "3"})
where, obj := condition.Get() where, obj := condition.Get()
@ -155,6 +156,6 @@ func TestCondition(t *testing.T) {
sqldb.Close() sqldb.Close()
}() }()
accountMgr := model.AccountMgr(db.Where(condition.Get())) accountMgr := model.AccountMgr(db.Where(where, obj...))
accountMgr.Gets() accountMgr.Gets()
} }

@ -97,24 +97,38 @@ type Condition struct {
list []*conditionInfo list []*conditionInfo
} }
// And a condition by and .and 一个条件 func (c *Condition) AndWithCondition(condition bool, column string, cases string, value interface{}) (*Condition) {
func (c *Condition) And(column string, cases string, value interface{}) { if condition {
c.list = append(c.list, &conditionInfo{ c.list = append(c.list, &conditionInfo{
andor: "and", andor: "and",
column: column, // 列名 column: column, // 列名
case_: cases, // 条件(and,or,in,>=,<=) case_: cases, // 条件(and,or,in,>=,<=)
value: value, 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{ // And a Condition by and .and 一个条件
andor: "or", func (c *Condition) And(column string, cases string, value interface{})(*Condition) {
column: column, // 列名 return c.AndWithCondition(true, column, cases, value)
case_: cases, // 条件(and,or,in,>=,<=) }
value: 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{}) { func (c *Condition) Get() (where string, out []interface{}) {

Loading…
Cancel
Save