From deb37930bd94f65fceb98081937d615e3a0a4dae Mon Sep 17 00:00:00 2001 From: jiang4869 <1121429190@qq.com> Date: Thu, 6 Jan 2022 23:38:48 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E7=BB=99=E8=87=AA=E5=AE=9A=E4=B9=89SQL?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BA=86=E6=A0=B9=E6=8D=AE=E6=9D=A1=E4=BB=B6?= =?UTF-8?q?=E5=88=A4=E6=96=AD=E6=98=AF=E5=90=A6=E7=94=9F=E6=88=90=E7=9A=84?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- data/view/genfunc/def.go | 54 ++++++++++++++++++----------- data/view/genfunc/genfunc_test.go | 13 +++---- data/view/genfunc/model/gen.base.go | 52 ++++++++++++++++----------- 3 files changed, 73 insertions(+), 46 deletions(-) diff --git a/data/view/genfunc/def.go b/data/view/genfunc/def.go index a6db57d..aeb9e8e 100755 --- a/data/view/genfunc/def.go +++ b/data/view/genfunc/def.go @@ -117,31 +117,45 @@ func CloseRelated() { // 自定义sql查询 -type Condition struct { - list []*conditionInfo +type Query struct { + list []*queryInfo } -// 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 *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, + }) + } } -// 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 *Query) And(column string, cases string, value interface{}) { + c.AndOnCondition(true,column,cases,value) } -func (c *Condition) Get() (where string, out []interface{}) { + +func (c *Query) OrOnCondition(condition bool,column string, cases string, value interface{}) { + if condition { + c.list = append(c.list, &queryInfo{ + andor: "or", + column: column, // 列名 + case_: cases, // 条件(and,or,in,>=,<=) + value: value, + }) + } +} + +// Or a Condition by or .or 一个条件 +func (c *Query) Or(column string, cases string, value interface{}) { + c.OrOnCondition(true,column,cases,value) +} + +func (c *Query) Get() (where string, out []interface{}) { firstAnd := -1 for i := 0; i < len(c.list); i++ { // 查找第一个and if c.list[i].andor == "and" { @@ -168,7 +182,7 @@ func (c *Condition) Get() (where string, out []interface{}) { return } -type conditionInfo struct { +type queryInfo struct { andor string column string // 列名 case_ string // 条件(in,>=,<=) diff --git a/data/view/genfunc/genfunc_test.go b/data/view/genfunc/genfunc_test.go index b10dc71..ba25a09 100644 --- a/data/view/genfunc/genfunc_test.go +++ b/data/view/genfunc/genfunc_test.go @@ -140,12 +140,13 @@ func TestFuncFetchBy(t *testing.T) { // TestCondition 测试sql构建 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.Or(model.AccountColumns.Type, "in", []string{"1", "2", "3"}) + query := model.Query{} + query.And(model.AccountColumns.AccountID, ">=", "1") + query.And(model.AccountColumns.UserID, "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(obj...) @@ -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..617387f 100644 --- a/data/view/genfunc/model/gen.base.go +++ b/data/view/genfunc/model/gen.base.go @@ -93,31 +93,43 @@ func CloseRelated() { } // 自定义sql查询 -type Condition struct { - list []*conditionInfo +type Query struct { + list []*queryInfo } -// 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 *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, + }) + } } -// 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 *Query) And(column string, cases string, value interface{}) { + c.AndOnCondition(true, column, cases, value) } -func (c *Condition) Get() (where string, out []interface{}) { +func (c *Query) OrOnCondition(condition bool, column string, cases string, value interface{}) { + if condition { + c.list = append(c.list, &queryInfo{ + andor: "or", + column: column, // 列名 + case_: cases, // 条件(and,or,in,>=,<=) + value: value, + }) + } +} + +// Or a Condition by or .or 一个条件 +func (c *Query) Or(column string, cases string, value interface{}) { + c.OrOnCondition(true, column, cases, value) +} + +func (c *Query) Get() (where string, out []interface{}) { firstAnd := -1 for i := 0; i < len(c.list); i++ { // 查找第一个and if c.list[i].andor == "and" { @@ -144,7 +156,7 @@ func (c *Condition) Get() (where string, out []interface{}) { return } -type conditionInfo struct { +type queryInfo struct { andor string column string // 列名 case_ string // 条件(in,>=,<=) From 8fe4d6f8b3e11ee4062d4710fb3eacc3fe5adca8 Mon Sep 17 00:00:00 2001 From: jiang4869 <1121429190@qq.com> Date: Fri, 7 Jan 2022 00:21:02 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E6=8A=8Aobj=E6=94=B9=E6=88=90obj...?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- data/view/genfunc/genfunc_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data/view/genfunc/genfunc_test.go b/data/view/genfunc/genfunc_test.go index ba25a09..876d5f5 100644 --- a/data/view/genfunc/genfunc_test.go +++ b/data/view/genfunc/genfunc_test.go @@ -156,6 +156,6 @@ func TestCondition(t *testing.T) { sqldb.Close() }() - accountMgr := model.AccountMgr(db.Where(where, obj)) + accountMgr := model.AccountMgr(db.Where(where, obj...)) accountMgr.Gets() } From 819706ee2b41a44e13dfd69d8957c615575c0772 Mon Sep 17 00:00:00 2001 From: jiang4869 <1121429190@qq.com> Date: Fri, 7 Jan 2022 16:43:47 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E7=B1=BB=E5=90=8D?= =?UTF-8?q?=EF=BC=8C=E5=A2=9E=E5=8A=A0=E9=93=BE=E5=BC=8F=E8=B0=83=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- data/view/genfunc/def.go | 26 ++++++++++++++------------ data/view/genfunc/genfunc_test.go | 12 ++++++------ data/view/genfunc/model/gen.base.go | 26 ++++++++++++++------------ 3 files changed, 34 insertions(+), 30 deletions(-) diff --git a/data/view/genfunc/def.go b/data/view/genfunc/def.go index aeb9e8e..8793b6d 100755 --- a/data/view/genfunc/def.go +++ b/data/view/genfunc/def.go @@ -117,45 +117,47 @@ func CloseRelated() { // 自定义sql查询 -type Query struct { - list []*queryInfo +type Condition struct { + 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 { - c.list = append(c.list, &queryInfo{ + 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 *Query) And(column string, cases string, value interface{}) { - c.AndOnCondition(true,column,cases,value) +func (c *Condition) And(column string, cases string, value interface{}) (*Condition) { + 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 { - c.list = append(c.list, &queryInfo{ + 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 *Query) Or(column string, cases string, value interface{}) { - c.OrOnCondition(true,column,cases,value) +func (c *Condition) Or(column string, cases string, value interface{}) (*Condition) { + 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 for i := 0; i < len(c.list); i++ { // 查找第一个and if c.list[i].andor == "and" { @@ -182,7 +184,7 @@ func (c *Query) Get() (where string, out []interface{}) { return } -type queryInfo struct { +type conditionInfo struct { andor string column string // 列名 case_ string // 条件(in,>=,<=) diff --git a/data/view/genfunc/genfunc_test.go b/data/view/genfunc/genfunc_test.go index 876d5f5..93800c5 100644 --- a/data/view/genfunc/genfunc_test.go +++ b/data/view/genfunc/genfunc_test.go @@ -140,13 +140,13 @@ func TestFuncFetchBy(t *testing.T) { // TestCondition 测试sql构建 func TestCondition(t *testing.T) { - query := model.Query{} - query.And(model.AccountColumns.AccountID, ">=", "1") - query.And(model.AccountColumns.UserID, "in", []string{"1", "2", "3"}) - query.AndOnCondition(false, model.AccountColumns.AccountID, "in", []string{"5"}) - query.Or(model.AccountColumns.Type, "in", []string{"1", "2", "3"}) + 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 := query.Get() + where, obj := condition.Get() fmt.Println(where) fmt.Println(obj...) diff --git a/data/view/genfunc/model/gen.base.go b/data/view/genfunc/model/gen.base.go index 617387f..89a575c 100644 --- a/data/view/genfunc/model/gen.base.go +++ b/data/view/genfunc/model/gen.base.go @@ -93,43 +93,45 @@ func CloseRelated() { } // 自定义sql查询 -type Query struct { - list []*queryInfo +type Condition struct { + 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 { - c.list = append(c.list, &queryInfo{ + 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 *Query) And(column string, cases string, value interface{}) { - c.AndOnCondition(true, column, cases, value) +func (c *Condition) And(column string, cases string, value interface{})(*Condition) { + 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 { - c.list = append(c.list, &queryInfo{ + 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 *Query) Or(column string, cases string, value interface{}) { - c.OrOnCondition(true, column, cases, value) +func (c *Condition) Or(column string, cases string, value interface{})(*Condition) { + 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 for i := 0; i < len(c.list); i++ { // 查找第一个and if c.list[i].andor == "and" { @@ -156,7 +158,7 @@ func (c *Query) Get() (where string, out []interface{}) { return } -type queryInfo struct { +type conditionInfo struct { andor string column string // 列名 case_ string // 条件(in,>=,<=)