-
Notifications
You must be signed in to change notification settings - Fork 23
refactor query generation #33
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
Merged
Merged
Changes from all commits
Commits
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from django.db.models.expressions import Col | ||
|
||
|
||
def col(self, compiler, connection): # noqa: ARG001 | ||
return self.target.column | ||
|
||
|
||
def register_expressions(): | ||
Col.as_mql = col |
This file contains hidden or 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 hidden or 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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
from django.db import NotSupportedError | ||
from django.db.models.expressions import Col | ||
from django.db.models.functions.datetime import Extract | ||
|
||
from .query_utils import process_lhs | ||
|
||
|
||
def extract(self, compiler, connection): | ||
lhs_mql = process_lhs(self, compiler, connection) | ||
if self.lookup_name == "week": | ||
operator = "$week" | ||
elif self.lookup_name == "month": | ||
operator = "$month" | ||
elif self.lookup_name == "year": | ||
operator = "$year" | ||
else: | ||
raise NotSupportedError("%s is not supported." % self.__class__.__name__) | ||
if isinstance(self.lhs, Col): | ||
lhs_mql = f"${lhs_mql}" | ||
return {operator: lhs_mql} | ||
|
||
|
||
def register_functions(): | ||
Extract.as_mql = extract |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,47 @@ | ||
from django.db import NotSupportedError | ||
from django.db.models.expressions import Col | ||
from django.db.models.fields.related_lookups import In, MultiColSource, RelatedIn | ||
from django.db.models.lookups import BuiltinLookup, Exact, IsNull, UUIDTextMixin | ||
|
||
from .query_utils import process_lhs, process_rhs | ||
|
||
|
||
def builtin_lookup(self, compiler, connection): | ||
lhs_mql = process_lhs(self, compiler, connection) | ||
value = process_rhs(self, compiler, connection) | ||
rhs_mql = connection.operators[self.lookup_name](value) | ||
return {lhs_mql: rhs_mql} | ||
|
||
|
||
def exact(self, compiler, connection): | ||
lhs_mql = process_lhs(self, compiler, connection) | ||
value = process_rhs(self, compiler, connection) | ||
if isinstance(self.lhs, Col): | ||
lhs_mql = f"${lhs_mql}" | ||
return {"$expr": {"$eq": [lhs_mql, value]}} | ||
|
||
|
||
def in_(self, compiler, connection): | ||
if isinstance(self.lhs, MultiColSource): | ||
raise NotImplementedError("MultiColSource is not supported.") | ||
return builtin_lookup(self, compiler, connection) | ||
|
||
|
||
def is_null(self, compiler, connection): | ||
if not isinstance(self.rhs, bool): | ||
raise ValueError("The QuerySet value for an isnull lookup must be True or False.") | ||
lhs_mql = process_lhs(self, compiler, connection) | ||
rhs_mql = connection.operators["isnull"](self.rhs) | ||
return {lhs_mql: rhs_mql} | ||
|
||
|
||
def uuid_text_mixin(self, compiler, connection): # noqa: ARG001 | ||
raise NotSupportedError("Pattern lookups on UUIDField are not supported.") | ||
|
||
|
||
def register_lookups(): | ||
BuiltinLookup.as_mql = builtin_lookup | ||
Exact.as_mql = exact | ||
In.as_mql = RelatedIn.as_mql = in_ | ||
IsNull.as_mql = is_null | ||
UUIDTextMixin.as_mql = uuid_text_mixin |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.