-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixes #13, #22 Co-authored-by: Tim Graham <[email protected]>
- Loading branch information
Showing
10 changed files
with
204 additions
and
260 deletions.
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
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 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 |
---|---|---|
@@ -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 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.