修改类名,增加链式调用
This commit is contained in:
@@ -117,45 +117,47 @@ func CloseRelated() {
|
|||||||
|
|
||||||
|
|
||||||
// 自定义sql查询
|
// 自定义sql查询
|
||||||
type Query struct {
|
type Condition struct {
|
||||||
list []*queryInfo
|
list []*conditionInfo
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Query) AndOnCondition(condition bool,column string, cases string, value interface{}) {
|
func (c *Condition) AndWithCondition(condition bool,column string, cases string, value interface{}) (*Condition) {
|
||||||
if condition {
|
if condition {
|
||||||
c.list = append(c.list, &queryInfo{
|
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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// And a Condition by and .and 一个条件
|
// And a Condition by and .and 一个条件
|
||||||
func (c *Query) And(column string, cases string, value interface{}) {
|
func (c *Condition) And(column string, cases string, value interface{}) (*Condition) {
|
||||||
c.AndOnCondition(true,column,cases,value)
|
return c.AndWithCondition(true,column,cases,value)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func (c *Query) OrOnCondition(condition bool,column string, cases string, value interface{}) {
|
func (c *Condition) OrWithCondition(condition bool,column string, cases string, value interface{}) (*Condition) {
|
||||||
if condition {
|
if condition {
|
||||||
c.list = append(c.list, &queryInfo{
|
c.list = append(c.list, &conditionInfo{
|
||||||
andor: "or",
|
andor: "or",
|
||||||
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 一个条件
|
// Or a Condition by or .or 一个条件
|
||||||
func (c *Query) Or(column string, cases string, value interface{}) {
|
func (c *Condition) Or(column string, cases string, value interface{}) (*Condition) {
|
||||||
c.OrOnCondition(true,column,cases,value)
|
return c.OrWithCondition(true,column,cases,value)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Query) Get() (where string, out []interface{}) {
|
func (c *Condition) 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" {
|
||||||
@@ -182,7 +184,7 @@ func (c *Query) Get() (where string, out []interface{}) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
type queryInfo struct {
|
type conditionInfo struct {
|
||||||
andor string
|
andor string
|
||||||
column string // 列名
|
column string // 列名
|
||||||
case_ string // 条件(in,>=,<=)
|
case_ string // 条件(in,>=,<=)
|
||||||
|
|||||||
@@ -140,13 +140,13 @@ func TestFuncFetchBy(t *testing.T) {
|
|||||||
|
|
||||||
// TestCondition 测试sql构建
|
// TestCondition 测试sql构建
|
||||||
func TestCondition(t *testing.T) {
|
func TestCondition(t *testing.T) {
|
||||||
query := model.Query{}
|
condition := model.Condition{}
|
||||||
query.And(model.AccountColumns.AccountID, ">=", "1")
|
condition.And(model.AccountColumns.AccountID, ">=", "1")
|
||||||
query.And(model.AccountColumns.UserID, "in", []string{"1", "2", "3"})
|
condition.And(model.AccountColumns.UserID, "in", []string{"1", "2", "3"})
|
||||||
query.AndOnCondition(false, model.AccountColumns.AccountID, "in", []string{"5"})
|
condition.AndWithCondition(false, model.AccountColumns.AccountID, "in", []string{"5"})
|
||||||
query.Or(model.AccountColumns.Type, "in", []string{"1", "2", "3"})
|
condition.Or(model.AccountColumns.Type, "in", []string{"1", "2", "3"})
|
||||||
|
|
||||||
where, obj := query.Get()
|
where, obj := condition.Get()
|
||||||
fmt.Println(where)
|
fmt.Println(where)
|
||||||
fmt.Println(obj...)
|
fmt.Println(obj...)
|
||||||
|
|
||||||
|
|||||||
@@ -93,43 +93,45 @@ func CloseRelated() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 自定义sql查询
|
// 自定义sql查询
|
||||||
type Query struct {
|
type Condition struct {
|
||||||
list []*queryInfo
|
list []*conditionInfo
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Query) AndOnCondition(condition bool, column string, cases string, value interface{}) {
|
func (c *Condition) AndWithCondition(condition bool, column string, cases string, value interface{}) (*Condition) {
|
||||||
if condition {
|
if condition {
|
||||||
c.list = append(c.list, &queryInfo{
|
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
|
||||||
}
|
}
|
||||||
|
|
||||||
// And a Condition by and .and 一个条件
|
// And a Condition by and .and 一个条件
|
||||||
func (c *Query) And(column string, cases string, value interface{}) {
|
func (c *Condition) And(column string, cases string, value interface{})(*Condition) {
|
||||||
c.AndOnCondition(true, column, cases, value)
|
return c.AndWithCondition(true, column, cases, value)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Query) OrOnCondition(condition bool, column string, cases string, value interface{}) {
|
func (c *Condition) OrWithCondition(condition bool, column string, cases string, value interface{}) (*Condition){
|
||||||
if condition {
|
if condition {
|
||||||
c.list = append(c.list, &queryInfo{
|
c.list = append(c.list, &conditionInfo{
|
||||||
andor: "or",
|
andor: "or",
|
||||||
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 一个条件
|
// Or a Condition by or .or 一个条件
|
||||||
func (c *Query) Or(column string, cases string, value interface{}) {
|
func (c *Condition) Or(column string, cases string, value interface{})(*Condition) {
|
||||||
c.OrOnCondition(true, column, cases, value)
|
return c.OrWithCondition(true, column, cases, value)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Query) Get() (where string, out []interface{}) {
|
func (c *Condition) 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" {
|
||||||
@@ -156,7 +158,7 @@ func (c *Query) Get() (where string, out []interface{}) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
type queryInfo struct {
|
type conditionInfo struct {
|
||||||
andor string
|
andor string
|
||||||
column string // 列名
|
column string // 列名
|
||||||
case_ string // 条件(in,>=,<=)
|
case_ string // 条件(in,>=,<=)
|
||||||
|
|||||||
Reference in New Issue
Block a user