From da68992068cb434c98c1ea28149cda402fa59704 Mon Sep 17 00:00:00 2001 From: Doug Martin Date: Wed, 14 Sep 2016 18:08:27 -0500 Subject: [PATCH] v3.2 * Add range clauses ([NOT] BETWEEN) support - #25 - @denisvm * Readmefix #26- @tiagopotencia --- HISTORY.md | 10 ++++++++ example_test.go | 65 ++++++++++++++++++++++++++++++++++++++++--------- expressions.go | 51 +++++++++++++++++++------------------- 3 files changed, 89 insertions(+), 37 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index cfd13bc7..015e9caf 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,3 +1,13 @@ +## v3.2 + +* Add range clauses ([NOT] BETWEEN) support - [#25](https://github.com/doug-martin/goqu/pull/25) - [@denisvm](https://github.com/denisvm) +* Readmefix [#26](https://github.com/doug-martin/goqu/pull/26) - [@tiagopotencia](https://github.com/tiagopotencia) + +## v3.1.3 + +* Bugfix for chained Where() [#20](https://github.com/doug-martin/goqu/pull/20) - [@Emreu](https://github.com/Emreu) + + ## v3.1.2 * Fixing ScanStruct issue with embedded pointers in crud_exec [#20](https://github.com/doug-martin/goqu/pull/20) - [@ruzz311](https://github.com/ruzz311) diff --git a/example_test.go b/example_test.go index 72f8c8e1..5a0bfdac 100644 --- a/example_test.go +++ b/example_test.go @@ -185,12 +185,6 @@ func ExampleComparisonMethods() { sql, _, _ = db.From("test").Where(goqu.L("(a + b)").Lte(10)).ToSql() fmt.Println(sql) - sql, _, _ = db.From("test").Where(goqu.L("(a + b)").Between(goqu.RangeVal{Start:10,End:100})).ToSql() - fmt.Println(sql) - - sql, _, _ = db.From("test").Where(goqu.L("(a + b)").NotBetween(goqu.RangeVal{Start:10,End:100})).ToSql() - fmt.Println(sql) - //used with Ex expression map sql, _, _ = db.From("test").Where(goqu.Ex{ "a": 10, @@ -214,8 +208,6 @@ func ExampleComparisonMethods() { // SELECT * FROM "test" WHERE ((a + b) >= 10) // SELECT * FROM "test" WHERE ((a + b) < 10) // SELECT * FROM "test" WHERE ((a + b) <= 10) - // SELECT * FROM "test" WHERE ((a + b) BETWEEN 10 AND 100) - // SELECT * FROM "test" WHERE ((a + b) NOT BETWEEN 10 AND 100) // SELECT * FROM "test" WHERE (("a" = 10) AND ("b" != 10) AND ("c" >= 10) AND ("d" < 10) AND ("e" <= 10)) } @@ -275,6 +267,55 @@ func ExampleOrderedMethods() { // SELECT * FROM "test" ORDER BY "a" DESC NULLS LAST } +func ExampleRangeMethods() { + db := goqu.New("default", driver) + sql, _, _ := db.From("test").Where(goqu.I("name").Between(goqu.RangeVal{Start: "a", End: "b"})).ToSql() + fmt.Println(sql) + + sql, _, _ = db.From("test").Where(goqu.I("name").NotBetween(goqu.RangeVal{Start: "a", End: "b"})).ToSql() + fmt.Println(sql) + + sql, _, _ = db.From("test").Where(goqu.I("x").Between(goqu.RangeVal{Start: goqu.I("y"), End: goqu.I("z")})).ToSql() + fmt.Println(sql) + + sql, _, _ = db.From("test").Where(goqu.I("x").NotBetween(goqu.RangeVal{Start: goqu.I("y"), End: goqu.I("z")})).ToSql() + fmt.Println(sql) + + sql, _, _ = db.From("test").Where(goqu.L("(a + b)").Between(goqu.RangeVal{Start: 10, End: 100})).ToSql() + fmt.Println(sql) + + sql, _, _ = db.From("test").Where(goqu.L("(a + b)").NotBetween(goqu.RangeVal{Start: 10, End: 100})).ToSql() + fmt.Println(sql) + // Output: + // SELECT * FROM "test" WHERE ("name" BETWEEN 'a' AND 'b') + // SELECT * FROM "test" WHERE ("name" NOT BETWEEN 'a' AND 'b') + // SELECT * FROM "test" WHERE ("x" BETWEEN "y" AND "z") + // SELECT * FROM "test" WHERE ("x" NOT BETWEEN "y" AND "z") + // SELECT * FROM "test" WHERE ((a + b) BETWEEN 10 AND 100) + // SELECT * FROM "test" WHERE ((a + b) NOT BETWEEN 10 AND 100) +} + +func ExampleRangeMethods_Ex() { + db := goqu.New("default", driver) + sql, _, _ := db.From("test").Where(goqu.Ex{"name": goqu.Op{"between": goqu.RangeVal{Start: "a", End: "b"}}}).ToSql() + fmt.Println(sql) + + sql, _, _ = db.From("test").Where(goqu.Ex{"name": goqu.Op{"notBetween": goqu.RangeVal{Start: "a", End: "b"}}}).ToSql() + fmt.Println(sql) + + sql, _, _ = db.From("test").Where(goqu.Ex{"x": goqu.Op{"between": goqu.RangeVal{Start: goqu.I("y"), End: goqu.I("z")}}}).ToSql() + fmt.Println(sql) + + sql, _, _ = db.From("test").Where(goqu.Ex{"x": goqu.Op{"notBetween": goqu.RangeVal{Start: goqu.I("y"), End: goqu.I("z")}}}).ToSql() + fmt.Println(sql) + + // Output: + // SELECT * FROM "test" WHERE ("name" BETWEEN 'a' AND 'b') + // SELECT * FROM "test" WHERE ("name" NOT BETWEEN 'a' AND 'b') + // SELECT * FROM "test" WHERE ("x" BETWEEN "y" AND "z") + // SELECT * FROM "test" WHERE ("x" NOT BETWEEN "y" AND "z") +} + func ExampleStringMethods() { db := goqu.New("default", driver) //using identifiers @@ -1520,8 +1561,8 @@ func ExampleEx_withOpPrepared() { fmt.Println(sql, args) sql, args, _ = db.From("items").Prepared(true).Where(goqu.Ex{ - "col1": goqu.Op{"between": goqu.RangeVal{Start:1,End:10}}, - "col2": goqu.Op{"notbetween": goqu.RangeVal{Start:1,End:10}}, + "col1": goqu.Op{"between": goqu.RangeVal{Start: 1, End: 10}}, + "col2": goqu.Op{"notbetween": goqu.RangeVal{Start: 1, End: 10}}, }).ToSql() fmt.Println(sql, args) @@ -1567,8 +1608,8 @@ func ExampleOp() { fmt.Println(sql) sql, _, _ = db.From("items").Where(goqu.Ex{ - "col1": goqu.Op{"between": goqu.RangeVal{Start:1,End:10}}, - "col2": goqu.Op{"notbetween": goqu.RangeVal{Start:1,End:10}}, + "col1": goqu.Op{"between": goqu.RangeVal{Start: 1, End: 10}}, + "col2": goqu.Op{"notbetween": goqu.RangeVal{Start: 1, End: 10}}, }).ToSql() fmt.Println(sql) diff --git a/expressions.go b/expressions.go index c3254136..cf63607e 100644 --- a/expressions.go +++ b/expressions.go @@ -767,17 +767,17 @@ func (me literal) Args() []interface{} { return me.args } -func (me literal) Expression() Expression { return me } -func (me literal) As(val interface{}) AliasedExpression { return aliased(me, val) } -func (me literal) Eq(val interface{}) BooleanExpression { return eq(me, val) } -func (me literal) Neq(val interface{}) BooleanExpression { return neq(me, val) } -func (me literal) Gt(val interface{}) BooleanExpression { return gt(me, val) } -func (me literal) Gte(val interface{}) BooleanExpression { return gte(me, val) } -func (me literal) Lt(val interface{}) BooleanExpression { return lt(me, val) } -func (me literal) Lte(val interface{}) BooleanExpression { return lte(me, val) } -func (me literal) Asc() OrderedExpression { return asc(me) } -func (me literal) Desc() OrderedExpression { return desc(me) } -func (me literal) Between(val RangeVal) RangeExpression { return between(me, val) } +func (me literal) Expression() Expression { return me } +func (me literal) As(val interface{}) AliasedExpression { return aliased(me, val) } +func (me literal) Eq(val interface{}) BooleanExpression { return eq(me, val) } +func (me literal) Neq(val interface{}) BooleanExpression { return neq(me, val) } +func (me literal) Gt(val interface{}) BooleanExpression { return gt(me, val) } +func (me literal) Gte(val interface{}) BooleanExpression { return gte(me, val) } +func (me literal) Lt(val interface{}) BooleanExpression { return lt(me, val) } +func (me literal) Lte(val interface{}) BooleanExpression { return lte(me, val) } +func (me literal) Asc() OrderedExpression { return asc(me) } +func (me literal) Desc() OrderedExpression { return desc(me) } +func (me literal) Between(val RangeVal) RangeExpression { return between(me, val) } func (me literal) NotBetween(val RangeVal) RangeExpression { return notBetween(me, val) } type ( @@ -1032,7 +1032,6 @@ func checkBoolExpType(op BooleanOperation, lhs Expression, rhs interface{}, inve return boolean{op: op, lhs: lhs, rhs: rhs} } - type ( RangeOperation int RangeExpression interface { @@ -1218,6 +1217,7 @@ type ( SqlFunctionExpression interface { Expression AliasMethods + RangeMethods ComparisonMethods //The function name Name() string @@ -1295,17 +1295,17 @@ func (me sqlFunctionExpression) Clone() Expression { return sqlFunctionExpression{name: me.name, args: me.args} } -func (me sqlFunctionExpression) Expression() Expression { return me } -func (me sqlFunctionExpression) Args() []interface{} { return me.args } -func (me sqlFunctionExpression) Name() string { return me.name } -func (me sqlFunctionExpression) As(val interface{}) AliasedExpression { return aliased(me, val) } -func (me sqlFunctionExpression) Eq(val interface{}) BooleanExpression { return eq(me, val) } -func (me sqlFunctionExpression) Neq(val interface{}) BooleanExpression { return neq(me, val) } -func (me sqlFunctionExpression) Gt(val interface{}) BooleanExpression { return gt(me, val) } -func (me sqlFunctionExpression) Gte(val interface{}) BooleanExpression { return gte(me, val) } -func (me sqlFunctionExpression) Lt(val interface{}) BooleanExpression { return lt(me, val) } -func (me sqlFunctionExpression) Lte(val interface{}) BooleanExpression { return lte(me, val) } -func (me sqlFunctionExpression) Between(val RangeVal) RangeExpression { return between(me, val) } +func (me sqlFunctionExpression) Expression() Expression { return me } +func (me sqlFunctionExpression) Args() []interface{} { return me.args } +func (me sqlFunctionExpression) Name() string { return me.name } +func (me sqlFunctionExpression) As(val interface{}) AliasedExpression { return aliased(me, val) } +func (me sqlFunctionExpression) Eq(val interface{}) BooleanExpression { return eq(me, val) } +func (me sqlFunctionExpression) Neq(val interface{}) BooleanExpression { return neq(me, val) } +func (me sqlFunctionExpression) Gt(val interface{}) BooleanExpression { return gt(me, val) } +func (me sqlFunctionExpression) Gte(val interface{}) BooleanExpression { return gte(me, val) } +func (me sqlFunctionExpression) Lt(val interface{}) BooleanExpression { return lt(me, val) } +func (me sqlFunctionExpression) Lte(val interface{}) BooleanExpression { return lte(me, val) } +func (me sqlFunctionExpression) Between(val RangeVal) RangeExpression { return between(me, val) } func (me sqlFunctionExpression) NotBetween(val RangeVal) RangeExpression { return notBetween(me, val) } type ( @@ -1319,6 +1319,7 @@ type ( BooleanMethods OrderedMethods DistinctMethods + RangeMethods //The exression being casted Casted() Expression //The the SQL type to cast the expression to @@ -1373,8 +1374,8 @@ func (me cast) IsNotTrue() BooleanExpression { return isNot(me, true func (me cast) IsFalse() BooleanExpression { return is(me, false) } func (me cast) IsNotFalse() BooleanExpression { return isNot(me, nil) } func (me cast) Distinct() SqlFunctionExpression { return DISTINCT(me) } -func (me cast) Between(val RangeVal) RangeExpression { return between(me, val) } -func (me cast) NotBetween(val RangeVal) RangeExpression{ return notBetween(me, val) } +func (me cast) Between(val RangeVal) RangeExpression { return between(me, val) } +func (me cast) NotBetween(val RangeVal) RangeExpression { return notBetween(me, val) } type ( compoundType int