Skip to content
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 6 commits into from
Sep 14, 2016

Conversation

denisvm
Copy link
Contributor

@denisvm denisvm commented Aug 2, 2016

Implementation of range clauses for BETWEEN and NOT BETWEEN.

sql, _, _ = db.From("test").Where(goqu.L("(a + b)").Between(10,100)).ToSql()
fmt.Println(sql)
// prints: SELECT * FROM "test" WHERE ((a + b) BETWEEN 10 AND 100)

PS: I've not found a clean way yet to implement the expression mapping in mapToExpressionList function.

if err := me.Literal(buf, rhs1); err != nil {
return err
}
buf.WriteString(" AND ")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can use the default_and_fragment here

//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{}
Copy link
Owner

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

  1. I think Rhs1 and 2 could be joined into a single slice of what would always be two values here and you could keep with the pattern on Lhs and Rhs and change the between methods to take variadic args but that could get messy.
  2. Create a new Range type 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 the goqu.Ex code which was commented out above.

What do you think? I lean towards option 2.

Copy link
Contributor Author

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

@denisvm
Copy link
Contributor Author

denisvm commented Aug 31, 2016

Hello @doug-martin, is there anything missing to merge this? Thanks!

}
ranged struct {
lhs Expression
rhs1 interface{}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can replace the rhs1, rhs2 with a single RangeVal

@doug-martin
Copy link
Owner

@denisvm sorry I didnt notice that you had pushed up your changes to address that comment. I think the only thing is merging your rhs1/2 to a single RHS since you have added the RangeVal. Comment once that is done and Ill merge this. Great work!

@@ -748,6 +770,10 @@ 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}}}))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

golint gripes about using RangeVal{1,10} this way. I would specify the Start and End like you do in https://github.com/doug-martin/goqu/pull/25/files#diff-7101a3ea121adf8d5b0acb5a2c520ca3R1524

@denisvm
Copy link
Contributor Author

denisvm commented Sep 14, 2016

@doug-martin I've merged both rhs1 and rhs2 values into a single RangeVal struct as you've suggested

@doug-martin doug-martin merged commit e051d5c into doug-martin:master Sep 14, 2016
@denisvm denisvm deleted the expression-between branch September 14, 2016 19:42
doug-martin added a commit that referenced this pull request Sep 14, 2016
* Add range clauses ([NOT] BETWEEN) support - #25 - @denisvm
* Readmefix #26- @tiagopotencia
@doug-martin
Copy link
Owner

Thanks again for the PR. I have published under v3.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants