Skip to content

Commit

Permalink
Replace both rhs1 and rhs2 for a single RangeVal struct with explicit…
Browse files Browse the repository at this point in the history
… fields
  • Loading branch information
denisvm committed Sep 14, 2016
1 parent 3781086 commit bd7d9bf
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 43 deletions.
20 changes: 10 additions & 10 deletions dataset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -565,21 +565,21 @@ func (me *datasetTest) TestRangeExpression() {
t := me.T()
buf := NewSqlBuilder(false)
ds := From("test")
assert.NoError(t, ds.Literal(me.Truncate(buf), I("a").Between(1,2)))
assert.NoError(t, ds.Literal(me.Truncate(buf), I("a").Between(RangeVal{Start:1,End:2})))
assert.Equal(t, buf.String(), `("a" BETWEEN 1 AND 2)`)
assert.NoError(t, ds.Literal(me.Truncate(buf), I("a").NotBetween(1,2)))
assert.NoError(t, ds.Literal(me.Truncate(buf), I("a").NotBetween(RangeVal{Start:1,End:2})))
assert.Equal(t, buf.String(), `("a" NOT BETWEEN 1 AND 2)`)
assert.NoError(t, ds.Literal(me.Truncate(buf), I("a").Between("aaa","zzz")))
assert.NoError(t, ds.Literal(me.Truncate(buf), I("a").Between(RangeVal{Start:"aaa",End:"zzz"})))
assert.Equal(t, buf.String(), `("a" BETWEEN 'aaa' AND 'zzz')`)

buf = NewSqlBuilder(true)
assert.NoError(t, ds.Literal(me.Truncate(buf), I("a").Between(1,2)))
assert.NoError(t, ds.Literal(me.Truncate(buf), I("a").Between(RangeVal{Start:1,End:2})))
assert.Equal(t, buf.args, []interface{}{1, 2})
assert.Equal(t, buf.String(), `("a" BETWEEN ? AND ?)`)
assert.NoError(t, ds.Literal(me.Truncate(buf), I("a").NotBetween(1,2)))
assert.NoError(t, ds.Literal(me.Truncate(buf), I("a").NotBetween(RangeVal{Start:1,End:2})))
assert.Equal(t, buf.args, []interface{}{1, 2})
assert.Equal(t, buf.String(), `("a" NOT BETWEEN ? AND ?)`)
assert.NoError(t, ds.Literal(me.Truncate(buf), I("a").Between("aaa","zzz")))
assert.NoError(t, ds.Literal(me.Truncate(buf), I("a").Between(RangeVal{Start:"aaa",End:"zzz"})))
assert.Equal(t, buf.args, []interface{}{"aaa", "zzz"})
assert.Equal(t, buf.String(), `("a" BETWEEN ? AND ?)`)
}
Expand Down Expand Up @@ -770,9 +770,9 @@ func (me *datasetTest) TestLiteralExpressionMap() {
assert.Equal(t, buf.String(), `("a" NOT IN ('a', 'b', 'c'))`)
assert.NoError(t, ds.Literal(me.Truncate(buf), Ex{"a": Op{"is": nil, "eq": 10}}))
assert.Equal(t, buf.String(), `(("a" = 10) OR ("a" IS NULL))`)
assert.NoError(t, ds.Literal(me.Truncate(buf), Ex{"a": Op{"between": RangeVal{1,10}}}))
assert.NoError(t, ds.Literal(me.Truncate(buf), Ex{"a": Op{"between": RangeVal{Start:1,End:10}}}))
assert.Equal(t, buf.String(), `("a" BETWEEN 1 AND 10)`)
assert.NoError(t, ds.Literal(me.Truncate(buf), Ex{"a": Op{"notbetween": RangeVal{1,10}}}))
assert.NoError(t, ds.Literal(me.Truncate(buf), Ex{"a": Op{"notbetween": RangeVal{Start:1,End:10}}}))
assert.Equal(t, buf.String(), `("a" NOT BETWEEN 1 AND 10)`)

buf = NewSqlBuilder(true)
Expand Down Expand Up @@ -822,10 +822,10 @@ func (me *datasetTest) TestLiteralExpressionMap() {
assert.NoError(t, ds.Literal(me.Truncate(buf), Ex{"a": Op{"is": nil, "eq": 10}}))
assert.Equal(t, buf.args, []interface{}{10, nil})
assert.Equal(t, buf.String(), `(("a" = ?) OR ("a" IS ?))`)
assert.NoError(t, ds.Literal(me.Truncate(buf), Ex{"a": Op{"between": RangeVal{1,10}}}))
assert.NoError(t, ds.Literal(me.Truncate(buf), Ex{"a": Op{"between": RangeVal{Start:1,End:10}}}))
assert.Equal(t, buf.args, []interface{}{1, 10})
assert.Equal(t, buf.String(), `("a" BETWEEN ? AND ?)`)
assert.NoError(t, ds.Literal(me.Truncate(buf), Ex{"a": Op{"notbetween": RangeVal{1,10}}}))
assert.NoError(t, ds.Literal(me.Truncate(buf), Ex{"a": Op{"notbetween": RangeVal{Start:1,End:10}}}))
assert.Equal(t, buf.args, []interface{}{1, 10})
assert.Equal(t, buf.String(), `("a" NOT BETWEEN ? AND ?)`)
}
Expand Down
7 changes: 3 additions & 4 deletions default_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -746,14 +746,13 @@ func (me *DefaultAdapter) RangeExpressionSql(buf *SqlBuilder, operator RangeExpr
} else {
return NewGoquError("Range operator %+v not supported", operatorOp)
}
rhs1 := operator.Rhs1()
rhs2 := operator.Rhs2()
rhs := operator.Rhs()
buf.WriteRune(space_rune)
if err := me.Literal(buf, rhs1); err != nil {
if err := me.Literal(buf, rhs.Start); err != nil {
return err
}
buf.Write(default_and_fragment)
if err := me.Literal(buf, rhs2); err != nil {
if err := me.Literal(buf, rhs.End); err != nil {
return err
}
buf.WriteRune(right_paren_rune)
Expand Down
4 changes: 2 additions & 2 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,10 @@ 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(10,100)).ToSql()
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(10,100)).ToSql()
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
Expand Down
48 changes: 21 additions & 27 deletions expressions.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,12 @@ func mapToExpressionList(ex map[string]interface{}, eType ExpressionListType) (E
case "between":
rangeVal, ok := op[opKey].(RangeVal)
if ok {
ored = lhs.Between(rangeVal.Start, rangeVal.End)
ored = lhs.Between(rangeVal)
}
case "notbetween":
rangeVal, ok := op[opKey].(RangeVal)
if ok {
ored = lhs.NotBetween(rangeVal.Start, rangeVal.End)
ored = lhs.NotBetween(rangeVal)
}
default:
return nil, NewGoquError("Unsupported expression type %s", op)
Expand Down Expand Up @@ -456,10 +456,10 @@ type (
RangeMethods interface {
//Creates a Range expression for between comparisons
// I("col").Between(1, 10) //("col" BETWEEN 1 AND 10)
Between(interface{}, interface{}) RangeExpression
Between(RangeVal) RangeExpression
//Creates a Range expression for between comparisons
// I("col").NotBetween(1, 10) //("col" NOT BETWEEN 1 AND 10)
NotBetween(interface{}, interface{}) RangeExpression
NotBetween(RangeVal) RangeExpression
}
//Interface that an expression should implement if it can be used in an IN expression
InMethods interface {
Expand Down Expand Up @@ -702,10 +702,10 @@ func (me identifier) Distinct() SqlFunctionExpression { return DISTI
func (me identifier) Cast(t string) CastExpression { return Cast(me, t) }

//Returns a RangeExpression for checking that a identifier is between two values (e.g "my_col" BETWEEN 1 AND 10)
func (me identifier) Between(val1 interface{}, val2 interface{}) RangeExpression { return between(me, val1, val2) }
func (me identifier) Between(val RangeVal) RangeExpression { return between(me, val) }

//Returns a RangeExpression for checking that a identifier is between two values (e.g "my_col" BETWEEN 1 AND 10)
func (me identifier) NotBetween(val1 interface{}, val2 interface{}) RangeExpression { return notBetween(me, val1, val2) }
func (me identifier) NotBetween(val RangeVal) RangeExpression { return notBetween(me, val) }

type (
//Expression for representing "literal" sql.
Expand Down Expand Up @@ -776,8 +776,8 @@ 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(val1 interface{}, val2 interface{}) RangeExpression { return between(me, val1, val2) }
func (me literal) NotBetween(val1 interface{}, val2 interface{}) RangeExpression { return notBetween(me, val1, val2) }
func (me literal) Between(val RangeVal) RangeExpression { return between(me, val) }
func (me literal) NotBetween(val RangeVal) RangeExpression { return notBetween(me, val) }

type (
UpdateExpression interface {
Expand Down Expand Up @@ -1041,13 +1041,11 @@ type (
//The left hand side of the expression (e.g. I("a")
Lhs() Expression
//The right hand side of the expression could be a primitive value, dataset, or expression
Rhs1() interface{}
Rhs2() interface{}
Rhs() RangeVal
}
ranged struct {
lhs Expression
rhs1 interface{}
rhs2 interface{}
rhs RangeVal
op RangeOperation
}
RangeVal struct {
Expand All @@ -1064,19 +1062,15 @@ const (
)

func (me ranged) Clone() Expression {
return ranged{op: me.op, lhs: me.lhs.Clone(), rhs1: me.rhs1, rhs2: me.rhs2}
return ranged{op: me.op, lhs: me.lhs.Clone(), rhs: me.rhs}
}

func (me ranged) Expression() Expression {
return me
}

func (me ranged) Rhs1() interface{} {
return me.rhs1
}

func (me ranged) Rhs2() interface{} {
return me.rhs2
func (me ranged) Rhs() RangeVal {
return me.rhs
}

func (me ranged) Lhs() Expression {
Expand All @@ -1088,13 +1082,13 @@ func (me ranged) Op() RangeOperation {
}

//used internally to create an BETWEEN comparison RangeExpression
func between(lhs Expression, rhs1 interface{}, rhs2 interface{}) RangeExpression {
return ranged{op: BETWEEN_OP, lhs: lhs, rhs1: rhs1, rhs2: rhs2}
func between(lhs Expression, rhs RangeVal) RangeExpression {
return ranged{op: BETWEEN_OP, lhs: lhs, rhs: rhs}
}

//used internally to create an NOT BETWEEN comparison RangeExpression
func notBetween(lhs Expression, rhs1 interface{}, rhs2 interface{}) RangeExpression {
return ranged{op: NBETWEEN_OP, lhs: lhs, rhs1: rhs1, rhs2: rhs2}
func notBetween(lhs Expression, rhs RangeVal) RangeExpression {
return ranged{op: NBETWEEN_OP, lhs: lhs, rhs: rhs}
}

type (
Expand Down Expand Up @@ -1310,8 +1304,8 @@ func (me sqlFunctionExpression) Gt(val interface{}) BooleanExpression { return
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(val1 interface{}, val2 interface{}) RangeExpression { return between(me, val1, val2) }
func (me sqlFunctionExpression) NotBetween(val1 interface{}, val2 interface{}) RangeExpression { return notBetween(me, val1, val2) }
func (me sqlFunctionExpression) Between(val RangeVal) RangeExpression { return between(me, val) }
func (me sqlFunctionExpression) NotBetween(val RangeVal) RangeExpression { return notBetween(me, val) }

type (
//An Expression that represents another Expression casted to a SQL type
Expand Down Expand Up @@ -1378,8 +1372,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(val1 interface{}, val2 interface{}) RangeExpression { return between(me, val1, val2) }
func (me cast) NotBetween(val1 interface{}, val2 interface{}) RangeExpression{ return notBetween(me, val1, val2) }
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 bd7d9bf

Please sign in to comment.