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

Fixed a bug related to django4 foreign key lookups #647

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ tests/logs
Gemfile.lock

/docs/.jekyll-metadata
/docs/djongocs/assets/*
/docs/djongocs/assets/*

.vscode/
33 changes: 30 additions & 3 deletions djongo/sql2mongo/sql_tokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from ..exceptions import SQLDecodeError, NotSupportedError

all_token_types = U['SQLConstIdentifier',
'SQLConstIntIdentifier',
'SQLConstParameterizedIdentifier',
'djongo.sql2mongo.functions.CountFunc',
'djongo.sql2mongo.functions.SimpleFunc',
'SQLIdentifier',
Expand Down Expand Up @@ -40,9 +42,14 @@ def tokens2sql(token: Token,
except ValueError:
yield SQLIdentifier(token[0][1], query)
else:
yield SQLConstIdentifier(token, query)
# TMiles-2022.XI.18 - Specify int identifier to match original functionality.
yield SQLConstIntIdentifier(token, query)
elif isinstance(token[0], Function):
yield SQLFunc.token2sql(token, query)
elif token[0].ttype == tokens.Name.Placeholder:
# TMiles-2022.XI.18
# Use a parameterized const identifier to handle newer django foreign key lookup queries.
yield SQLConstParameterizedIdentifier(token, query)
else:
yield SQLIdentifier(token, query)
elif isinstance(token, Function):
Expand Down Expand Up @@ -152,17 +159,37 @@ def column(self) -> str:
return name


# TMiles-2022.XI.18
# Break ConstIdentifier into Int and Parameterized subclasses to allow for django 4
# foreign key lookups and for future expansion to other non-int constants.
class SQLConstIdentifier(AliasableToken):

def __init__(self, *args):
super().__init__(*args)

def to_mongo(self) -> dict:
return {'$literal': self.value}

class SQLConstIntIdentifier(SQLConstIdentifier):

def __init__(self, *args):
super().__init__(*args)

@property
def value(self) -> int:
return int(self._token[0][1].value)

def to_mongo(self) -> dict:
return {'$literal': self.value}
class SQLConstParameterizedIdentifier(SQLConstIdentifier):

def __init__(self, *args):
tok = args[0]
if (tok[0].ttype != tokens.Name.Placeholder):
raise Exception("Token is not a placeholder")
super().__init__(*args)

@property
def value(self):
return self.query.params[self.placeholder_index(self._token[0])]


class SQLComparison(SQLToken):
Expand Down
30 changes: 30 additions & 0 deletions tests/mock_tests/test_sqlparsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1124,6 +1124,36 @@ def test_const_simple(self):
ans = [(1,)]
self.eval_aggregate(pipeline, return_value, ans)

# TMiles - 2022.XI.18
# Test for the django 4.x foreign key lookups.
def test_parameterizedConst(self):
self.sql = 'SELECT %s AS "a" FROM "table1" WHERE "table1"."col2" = %s LIMIT 1'
self.params = [1,2]
pipeline = [
{
'$match': {
'col2': {
'$eq': 2
}
}
},
{
'$limit': 1
},
{
'$project': {
'a': {
'$literal': 1
}
}
},

]
return_value = [{'a': 1}]
ans = [(1,)]
self.eval_aggregate(pipeline, return_value, ans)


@skip
class TestQueryUpdate(ResultQuery):

Expand Down