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

Added reset_where method for specific columns. #325

Merged
merged 2 commits into from
Mar 16, 2020
Merged

Added reset_where method for specific columns. #325

merged 2 commits into from
Mar 16, 2020

Conversation

wout
Copy link
Contributor

@wout wout commented Mar 16, 2020

This will reset where for a specific column. Initially, you could also call query.reset_where without a block, which would reset all where clauses. But I removed that because it could lead to unexpected/unwanted results.

Closes #319

@@ -135,6 +135,11 @@ class Avram::Criteria(T, V)
rows.query.group_by(column)
end

# :nodoc:
def __reset_where : Avram::QueryBuilder
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a way to make this private?
It feels weird to have to use __ in a lang that has real method protection.

Copy link
Contributor Author

@wout wout Mar 16, 2020

Choose a reason for hiding this comment

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

I agree, it is something I would do in JS. The question is, should it be private? Things like Thread for example are considered private in Crystal, while they are accessible.

Copy link
Member

Choose a reason for hiding this comment

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

It may be possible but would require reworking how Criteria and Queries work. This needs to be public so the Queryable can call it, but it shouldn't be used by the public. It can be and it'll work but it just reads strangely so adding the __ hides it a bit more.

We also do this in distinct_on so that you do MyQuery.new.distinct_on(&.name) isntead of MyQuery.new.name.distinct_on which reads super weird

Copy link
Member

@paulcsmith paulcsmith Mar 16, 2020

Choose a reason for hiding this comment

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

Hmm I'm not sure what you mean. If marked as private I am fairly certain it will not work since we are calling it from another class

I don't think there is a way to make this method only accessible from some classes, but not others. But maybe I'm misunderstanding

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, you're right, there isn't another way. You can't make it private. But I think the __ can be omitted. Some methods and object in Crystal are considered private, while being publicly accessible. A mention in the docs would be sufficient, I think.

Copy link
Member

@paulcsmith paulcsmith left a comment

Choose a reason for hiding this comment

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

Looks great!!

@@ -135,6 +135,11 @@ class Avram::Criteria(T, V)
rows.query.group_by(column)
end

# :nodoc:
def __reset_where : Avram::QueryBuilder
Copy link
Member

Choose a reason for hiding this comment

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

It may be possible but would require reworking how Criteria and Queries work. This needs to be public so the Queryable can call it, but it shouldn't be used by the public. It can be and it'll work but it just reads strangely so adding the __ hides it a bit more.

We also do this in distinct_on so that you do MyQuery.new.distinct_on(&.name) isntead of MyQuery.new.name.distinct_on which reads super weird

Copy link
Member

@paulcsmith paulcsmith left a comment

Choose a reason for hiding this comment

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

Actually just one suggestion and then ready to go!


query.reset_where(:age)

query.args.should be_empty
Copy link
Member

Choose a reason for hiding this comment

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

Could you add a statement test here to match what was done on line 41?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's what I mentioned on Gitter 😂 . The result of where_sql is cached, so the test would fail. You can't call statement twice because it will always return the first result.

Copy link
Member

Choose a reason for hiding this comment

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

Ahhh! 👍

Copy link
Member

@paulcsmith paulcsmith left a comment

Choose a reason for hiding this comment

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

Looks good! Thanks @wout. If someone can figure out a better way to "hide" or make that method private and wants to do a PR I'll accept it. I can't thin of a way but if we can that'd be cool

@wout
Copy link
Contributor Author

wout commented Mar 16, 2020

Looks good! Thanks @wout. If someone can figure out a better way to "hide" or make that method private and wants to do a PR I'll accept it. I can't thin of a way but if we can that'd be cool.

You're welcome!

Just a thought, maybe something like this would make it less awkward:

def private_reset_where
  ...
end

Makes it instantly obvious that this is not something for an end user.

@paulcsmith
Copy link
Member

I like that! Once I merge this in could you create a PR changing __distinct_on and __reset_where to use private?

@paulcsmith paulcsmith merged commit 6dcbb6b into luckyframework:master Mar 16, 2020
@wout
Copy link
Contributor Author

wout commented Mar 16, 2020

Of course, coming up!

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.

Add “reset_where” to queryable
3 participants