-
Notifications
You must be signed in to change notification settings - Fork 213
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add range clauses ([NOT] BETWEEN) support #25
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9708aad
add range clauses support
denisvm d737ff0
range expression's comment typo
denisvm 7d421de
add range methods to LiteralExpressions
denisvm 4065028
replace magic string with cleaner variable
denisvm 3781086
expressions map support for range clauses
denisvm 482d3b5
Replace both rhs1 and rhs2 for a single RangeVal struct with explicit…
denisvm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -86,6 +86,10 @@ func mapToExpressionList(ex map[string]interface{}, eType ExpressionListType) (E | |
ored = lhs.Lt(op[opKey]) | ||
case "lte": | ||
ored = lhs.Lte(op[opKey]) | ||
// case "between": | ||
// ored = lhs.Between(op[opKey]) | ||
// case "notbetween": | ||
// ored = lhs.NotBetween(op[opKey]) | ||
case "in": | ||
ored = lhs.In(op[opKey]) | ||
case "notin": | ||
|
@@ -443,6 +447,14 @@ type ( | |
// I("col").Lte(1) //("col" <= 1) | ||
Lte(interface{}) BooleanExpression | ||
} | ||
RangeMethods interface { | ||
//Creates a Range expression for between comparisons | ||
// I("col").Between(1, 10) //("col" BETWEEN 1 AND 10) | ||
Between(interface{}, interface{}) RangeExpression | ||
//Creates a Range expression for between comparisons | ||
// I("col").NotBetween(1, 10) //("col" NOT BETWEEN 1 AND 10) | ||
NotBetween(interface{}, interface{}) RangeExpression | ||
} | ||
//Interface that an expression should implement if it can be used in an IN expression | ||
InMethods interface { | ||
//Creates a Boolean expression for IN clauses | ||
|
@@ -525,6 +537,7 @@ type ( | |
Expression | ||
AliasMethods | ||
ComparisonMethods | ||
RangeMethods | ||
InMethods | ||
StringMethods | ||
BooleanMethods | ||
|
@@ -682,6 +695,12 @@ func (me identifier) Desc() OrderedExpression { return desc( | |
func (me identifier) Distinct() SqlFunctionExpression { return DISTINCT(me) } | ||
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) } | ||
|
||
//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) } | ||
|
||
type ( | ||
//Expression for representing "literal" sql. | ||
// L("col = 1") -> col = 1) | ||
|
@@ -690,6 +709,7 @@ type ( | |
Expression | ||
AliasMethods | ||
ComparisonMethods | ||
RangeMethods | ||
OrderedMethods | ||
//Returns the literal sql | ||
Literal() string | ||
|
@@ -750,6 +770,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) } | ||
|
||
type ( | ||
UpdateExpression interface { | ||
|
@@ -1003,6 +1025,68 @@ func checkBoolExpType(op BooleanOperation, lhs Expression, rhs interface{}, inve | |
return boolean{op: op, lhs: lhs, rhs: rhs} | ||
} | ||
|
||
|
||
type ( | ||
RangeOperation int | ||
RangeExpression interface { | ||
Expression | ||
//Returns the operator for the expression | ||
Op() RangeOperation | ||
//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{} | ||
} | ||
ranged struct { | ||
lhs Expression | ||
rhs1 interface{} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can replace the rhs1, rhs2 with a single RangeVal |
||
rhs2 interface{} | ||
op RangeOperation | ||
} | ||
) | ||
|
||
const ( | ||
//BETWEEN | ||
BETWEEN_OP RangeOperation = iota | ||
//NOT BETWEEN | ||
NBETWEEN_OP | ||
) | ||
|
||
func (me ranged) Clone() Expression { | ||
return ranged{op: me.op, lhs: me.lhs.Clone(), rhs1: me.rhs1, rhs2: me.rhs2} | ||
} | ||
|
||
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) Lhs() Expression { | ||
return me.lhs | ||
} | ||
|
||
func (me ranged) Op() RangeOperation { | ||
return me.op | ||
} | ||
|
||
//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} | ||
} | ||
|
||
//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} | ||
} | ||
|
||
type ( | ||
//Expression for Aliased expressions | ||
// I("a").As("b") -> "a" AS "b" | ||
|
@@ -1216,6 +1300,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) } | ||
|
||
type ( | ||
//An Expression that represents another Expression casted to a SQL type | ||
|
@@ -1282,6 +1368,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) } | ||
|
||
type ( | ||
compoundType int | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So Im torn on this you could go a couple of different ways
I("a").Between(Range(1,2))
which could still allow you to get the single Rhs and allow you to include between and notbetween in thegoqu.Ex
code which was commented out above.What do you think? I lean towards option 2.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, option 2 looks cleaner and less error prone :D