diff --git a/adapters/mysql/mysql_test.go b/adapters/mysql/mysql_test.go index a4fd3688..fb7f18df 100644 --- a/adapters/mysql/mysql_test.go +++ b/adapters/mysql/mysql_test.go @@ -169,7 +169,7 @@ func (me *mysqlTest) TestQuery() { } entries = entries[0:0] - assert.NoError(t, ds.Where(goqu.I("int").Between(3,6)).Order(goqu.I("id").Asc()).ScanStructs(&entries)) + assert.NoError(t, ds.Where(goqu.I("int").Between(goqu.RangeVal{Start:3,End:6})).Order(goqu.I("id").Asc()).ScanStructs(&entries)) assert.Len(t, entries, 4) assert.NoError(t, err) for _, entry := range entries { diff --git a/adapters/postgres/postgres_test.go b/adapters/postgres/postgres_test.go index 98e1979d..9b5828e7 100644 --- a/adapters/postgres/postgres_test.go +++ b/adapters/postgres/postgres_test.go @@ -158,7 +158,7 @@ func (me *postgresTest) TestQuery() { } entries = entries[0:0] - assert.NoError(t, ds.Where(goqu.I("int").Between(3,6)).Order(goqu.I("id").Asc()).ScanStructs(&entries)) + assert.NoError(t, ds.Where(goqu.I("int").Between(goqu.RangeVal{Start:3,End:6})).Order(goqu.I("id").Asc()).ScanStructs(&entries)) assert.Len(t, entries, 4) assert.NoError(t, err) for _, entry := range entries { diff --git a/adapters/sqlite3/sqlite3_test.go b/adapters/sqlite3/sqlite3_test.go index 92711c28..bd33fff1 100644 --- a/adapters/sqlite3/sqlite3_test.go +++ b/adapters/sqlite3/sqlite3_test.go @@ -158,7 +158,7 @@ func (me *sqlite3Test) TestQuery() { } entries = entries[0:0] - assert.NoError(t, ds.Where(goqu.I("int").Between(3,6)).Order(goqu.I("id").Asc()).ScanStructs(&entries)) + assert.NoError(t, ds.Where(goqu.I("int").Between(goqu.RangeVal{Start:3,End:6})).Order(goqu.I("id").Asc()).ScanStructs(&entries)) assert.Len(t, entries, 4) assert.NoError(t, err) for _, entry := range entries { diff --git a/dataset_test.go b/dataset_test.go index 87eb7e43..9c2aada0 100644 --- a/dataset_test.go +++ b/dataset_test.go @@ -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 ?)`) } @@ -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) @@ -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 ?)`) } diff --git a/default_adapter.go b/default_adapter.go index bb52517a..2ca2b16b 100644 --- a/default_adapter.go +++ b/default_adapter.go @@ -733,7 +733,7 @@ func (me *DefaultAdapter) BooleanExpressionSql(buf *SqlBuilder, operator Boolean return nil } -//Generates SQL for a RangeExpresion (e.g. I("a").Between(2,5) -> "a" BETWEEN 2 AND 5) +//Generates SQL for a RangeExpresion (e.g. I("a").Between(RangeVal{Start:2,End:5}) -> "a" BETWEEN 2 AND 5) func (me *DefaultAdapter) RangeExpressionSql(buf *SqlBuilder, operator RangeExpression) error { buf.WriteRune(left_paren_rune) if err := me.Literal(buf, operator.Lhs()); err != nil { @@ -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) diff --git a/example_test.go b/example_test.go index 06526ac3..72f8c8e1 100644 --- a/example_test.go +++ b/example_test.go @@ -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 diff --git a/expressions.go b/expressions.go index 968f1f67..d7db12b5 100644 --- a/expressions.go +++ b/expressions.go @@ -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) @@ -455,11 +455,11 @@ type ( } RangeMethods interface { //Creates a Range expression for between comparisons - // I("col").Between(1, 10) //("col" BETWEEN 1 AND 10) - Between(interface{}, interface{}) RangeExpression + // I("col").Between(RangeVal{Start:1, End:10}) //("col" BETWEEN 1 AND 10) + 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 + // I("col").NotBetween(RangeVal{Start:1, End:10}) //("col" NOT BETWEEN 1 AND 10) + NotBetween(RangeVal) RangeExpression } //Interface that an expression should implement if it can be used in an IN expression InMethods interface { @@ -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. @@ -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 { @@ -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 { @@ -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 { @@ -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 ( @@ -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 @@ -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