给自定义SQL增加了根据条件判断是否生成的功能

master
jiang4869 3 years ago
parent f79e1becc7
commit deb37930bd

@ -117,31 +117,45 @@ func CloseRelated() {
// 自定义sql查询 // 自定义sql查询
type Condition struct { type Query struct {
list []*conditionInfo list []*queryInfo
}
func (c *Query) AndOnCondition(condition bool,column string, cases string, value interface{}) {
if condition {
c.list = append(c.list, &queryInfo{
andor: "and",
column: column, // 列名
case_: cases, // 条件(and,or,in,>=,<=)
value: value,
})
}
}
// And a Condition by and .and 一个条件
func (c *Query) And(column string, cases string, value interface{}) {
c.AndOnCondition(true,column,cases,value)
} }
// And a condition by and .and 一个条件
func (c *Condition) And(column string, cases string, value interface{}) { func (c *Query) OrOnCondition(condition bool,column string, cases string, value interface{}) {
c.list = append(c.list, &conditionInfo{ if condition {
andor: "and", c.list = append(c.list, &queryInfo{
column: column, // 列名 andor: "or",
case_: cases, // 条件(and,or,in,>=,<=) column: column, // 列名
value: value, case_: cases, // 条件(and,or,in,>=,<=)
}) value: value,
})
}
} }
// Or a condition by or .or 一个条件 // Or a Condition by or .or 一个条件
func (c *Condition) Or(column string, cases string, value interface{}) { func (c *Query) Or(column string, cases string, value interface{}) {
c.list = append(c.list, &conditionInfo{ c.OrOnCondition(true,column,cases,value)
andor: "or",
column: column, // 列名
case_: cases, // 条件(and,or,in,>=,<=)
value: value,
})
} }
func (c *Condition) Get() (where string, out []interface{}) { func (c *Query) Get() (where string, out []interface{}) {
firstAnd := -1 firstAnd := -1
for i := 0; i < len(c.list); i++ { // 查找第一个and for i := 0; i < len(c.list); i++ { // 查找第一个and
if c.list[i].andor == "and" { if c.list[i].andor == "and" {
@ -168,7 +182,7 @@ func (c *Condition) Get() (where string, out []interface{}) {
return return
} }
type conditionInfo struct { type queryInfo struct {
andor string andor string
column string // 列名 column string // 列名
case_ string // 条件(in,>=,<=) case_ string // 条件(in,>=,<=)

@ -140,12 +140,13 @@ func TestFuncFetchBy(t *testing.T) {
// TestCondition 测试sql构建 // TestCondition 测试sql构建
func TestCondition(t *testing.T) { func TestCondition(t *testing.T) {
condition := model.Condition{} query := model.Query{}
condition.And(model.AccountColumns.AccountID, ">=", "1") query.And(model.AccountColumns.AccountID, ">=", "1")
condition.And(model.AccountColumns.UserID, "in", []string{"1", "2", "3"}) query.And(model.AccountColumns.UserID, "in", []string{"1", "2", "3"})
condition.Or(model.AccountColumns.Type, "in", []string{"1", "2", "3"}) query.AndOnCondition(false, model.AccountColumns.AccountID, "in", []string{"5"})
query.Or(model.AccountColumns.Type, "in", []string{"1", "2", "3"})
where, obj := condition.Get() where, obj := query.Get()
fmt.Println(where) fmt.Println(where)
fmt.Println(obj...) fmt.Println(obj...)
@ -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()
} }

@ -93,31 +93,43 @@ func CloseRelated() {
} }
// 自定义sql查询 // 自定义sql查询
type Condition struct { type Query struct {
list []*conditionInfo list []*queryInfo
}
func (c *Query) AndOnCondition(condition bool, column string, cases string, value interface{}) {
if condition {
c.list = append(c.list, &queryInfo{
andor: "and",
column: column, // 列名
case_: cases, // 条件(and,or,in,>=,<=)
value: value,
})
}
}
// And a Condition by and .and 一个条件
func (c *Query) And(column string, cases string, value interface{}) {
c.AndOnCondition(true, column, cases, value)
} }
// And a condition by and .and 一个条件 func (c *Query) OrOnCondition(condition bool, column string, cases string, value interface{}) {
func (c *Condition) And(column string, cases string, value interface{}) { if condition {
c.list = append(c.list, &conditionInfo{ c.list = append(c.list, &queryInfo{
andor: "and", andor: "or",
column: column, // 列名 column: column, // 列名
case_: cases, // 条件(and,or,in,>=,<=) case_: cases, // 条件(and,or,in,>=,<=)
value: value, value: value,
}) })
}
} }
// Or a condition by or .or 一个条件 // Or a Condition by or .or 一个条件
func (c *Condition) Or(column string, cases string, value interface{}) { func (c *Query) Or(column string, cases string, value interface{}) {
c.list = append(c.list, &conditionInfo{ c.OrOnCondition(true, column, cases, value)
andor: "or",
column: column, // 列名
case_: cases, // 条件(and,or,in,>=,<=)
value: value,
})
} }
func (c *Condition) Get() (where string, out []interface{}) { func (c *Query) Get() (where string, out []interface{}) {
firstAnd := -1 firstAnd := -1
for i := 0; i < len(c.list); i++ { // 查找第一个and for i := 0; i < len(c.list); i++ { // 查找第一个and
if c.list[i].andor == "and" { if c.list[i].andor == "and" {
@ -144,7 +156,7 @@ func (c *Condition) Get() (where string, out []interface{}) {
return return
} }
type conditionInfo struct { type queryInfo struct {
andor string andor string
column string // 列名 column string // 列名
case_ string // 条件(in,>=,<=) case_ string // 条件(in,>=,<=)

Loading…
Cancel
Save