Skip to content

Commit

Permalink
v3.2
Browse files Browse the repository at this point in the history
* Add range clauses ([NOT] BETWEEN) support - #25 - @denisvm
* Readmefix #26- @tiagopotencia
  • Loading branch information
doug-martin committed Sep 14, 2016
1 parent e051d5c commit da68992
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 37 deletions.
10 changes: 10 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
65 changes: 53 additions & 12 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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))
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down
51 changes: 26 additions & 25 deletions expressions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -1218,6 +1217,7 @@ type (
SqlFunctionExpression interface {
Expression
AliasMethods
RangeMethods
ComparisonMethods
//The function name
Name() string
Expand Down Expand Up @@ -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 (
Expand All @@ -1319,6 +1319,7 @@ type (
BooleanMethods
OrderedMethods
DistinctMethods
RangeMethods
//The exression being casted
Casted() Expression
//The the SQL type to cast the expression to
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit da68992

Please sign in to comment.