rc for skip of literal binds
This commit is contained in:
@ -3,15 +3,18 @@ repos:
|
|||||||
rev: 21.9b0
|
rev: 21.9b0
|
||||||
hooks:
|
hooks:
|
||||||
- id: black
|
- id: black
|
||||||
|
exclude: docs_src
|
||||||
- repo: https://github.com/pycqa/flake8
|
- repo: https://github.com/pycqa/flake8
|
||||||
rev: 3.9.2
|
rev: 3.9.2
|
||||||
hooks:
|
hooks:
|
||||||
- id: flake8
|
- id: flake8
|
||||||
|
exclude: docs_src
|
||||||
args: [ '--max-line-length=88' ]
|
args: [ '--max-line-length=88' ]
|
||||||
- repo: https://github.com/pre-commit/mirrors-mypy
|
- repo: https://github.com/pre-commit/mirrors-mypy
|
||||||
rev: v0.910
|
rev: v0.910
|
||||||
hooks:
|
hooks:
|
||||||
- id: mypy
|
- id: mypy
|
||||||
|
exclude: docs_src
|
||||||
args: [--no-strict-optional, --ignore-missing-imports]
|
args: [--no-strict-optional, --ignore-missing-imports]
|
||||||
additional_dependencies: [
|
additional_dependencies: [
|
||||||
types-ujson>=0.1.1,
|
types-ujson>=0.1.1,
|
||||||
|
|||||||
@ -26,7 +26,9 @@ class Course(ormar.Model):
|
|||||||
id: int = ormar.Integer(primary_key=True)
|
id: int = ormar.Integer(primary_key=True)
|
||||||
name: str = ormar.String(max_length=100)
|
name: str = ormar.String(max_length=100)
|
||||||
completed: bool = ormar.Boolean(default=False)
|
completed: bool = ormar.Boolean(default=False)
|
||||||
department: Optional[Department] = ormar.ForeignKey(Department, related_name="my_courses")
|
department: Optional[Department] = ormar.ForeignKey(
|
||||||
|
Department, related_name="my_courses"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
department = Department(name="Science")
|
department = Department(name="Science")
|
||||||
|
|||||||
@ -8,7 +8,9 @@ metadata = sqlalchemy.MetaData()
|
|||||||
|
|
||||||
|
|
||||||
class Course(ormar.Model):
|
class Course(ormar.Model):
|
||||||
class Meta(ormar.ModelMeta): # note you don't have to subclass - but it's recommended for ide completion and mypy
|
class Meta(
|
||||||
|
ormar.ModelMeta
|
||||||
|
): # note you don't have to subclass - but it's recommended for ide completion and mypy
|
||||||
database = database
|
database = database
|
||||||
metadata = metadata
|
metadata = metadata
|
||||||
|
|
||||||
|
|||||||
@ -16,4 +16,5 @@ class Course(ormar.Model):
|
|||||||
name: ormar.String(max_length=100)
|
name: ormar.String(max_length=100)
|
||||||
completed: ormar.Boolean(default=False)
|
completed: ormar.Boolean(default=False)
|
||||||
|
|
||||||
c1 = Course()
|
|
||||||
|
c1 = Course()
|
||||||
|
|||||||
@ -14,4 +14,4 @@ class Course(ormar.Model):
|
|||||||
|
|
||||||
id = ormar.Integer(primary_key=True)
|
id = ormar.Integer(primary_key=True)
|
||||||
name = ormar.String(max_length=100)
|
name = ormar.String(max_length=100)
|
||||||
completed = ormar.Boolean(default=False)
|
completed = ormar.Boolean(default=False)
|
||||||
|
|||||||
@ -19,4 +19,4 @@ class Course(ormar.Model):
|
|||||||
|
|
||||||
@property_field
|
@property_field
|
||||||
def prefixed_name(self):
|
def prefixed_name(self):
|
||||||
return 'custom_prefix__' + self.name
|
return "custom_prefix__" + self.name
|
||||||
|
|||||||
@ -15,14 +15,17 @@ class Book(ormar.Model):
|
|||||||
id: int = ormar.Integer(primary_key=True)
|
id: int = ormar.Integer(primary_key=True)
|
||||||
title: str = ormar.String(max_length=200)
|
title: str = ormar.String(max_length=200)
|
||||||
author: str = ormar.String(max_length=100)
|
author: str = ormar.String(max_length=100)
|
||||||
genre: str = ormar.String(max_length=100, default='Fiction',
|
genre: str = ormar.String(
|
||||||
choices=['Fiction', 'Adventure', 'Historic', 'Fantasy'])
|
max_length=100,
|
||||||
|
default="Fiction",
|
||||||
|
choices=["Fiction", "Adventure", "Historic", "Fantasy"],
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
await Book.objects.create(title='Tom Sawyer', author="Twain, Mark", genre='Adventure')
|
await Book.objects.create(title="Tom Sawyer", author="Twain, Mark", genre="Adventure")
|
||||||
await Book.objects.create(title='War and Peace', author="Tolstoy, Leo", genre='Fiction')
|
await Book.objects.create(title="War and Peace", author="Tolstoy, Leo", genre="Fiction")
|
||||||
await Book.objects.create(title='Anna Karenina', author="Tolstoy, Leo", genre='Fiction')
|
await Book.objects.create(title="Anna Karenina", author="Tolstoy, Leo", genre="Fiction")
|
||||||
|
|
||||||
await Book.objects.update(each=True, genre='Fiction')
|
await Book.objects.update(each=True, genre="Fiction")
|
||||||
all_books = await Book.objects.filter(genre='Fiction').all()
|
all_books = await Book.objects.filter(genre="Fiction").all()
|
||||||
assert len(all_books) == 3
|
assert len(all_books) == 3
|
||||||
|
|||||||
@ -15,18 +15,23 @@ class Book(ormar.Model):
|
|||||||
id: int = ormar.Integer(primary_key=True)
|
id: int = ormar.Integer(primary_key=True)
|
||||||
title: str = ormar.String(max_length=200)
|
title: str = ormar.String(max_length=200)
|
||||||
author: str = ormar.String(max_length=100)
|
author: str = ormar.String(max_length=100)
|
||||||
genre: str = ormar.String(max_length=100, default='Fiction',
|
genre: str = ormar.String(
|
||||||
choices=['Fiction', 'Adventure', 'Historic', 'Fantasy'])
|
max_length=100,
|
||||||
|
default="Fiction",
|
||||||
|
choices=["Fiction", "Adventure", "Historic", "Fantasy"],
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
await Book.objects.create(title='Tom Sawyer', author="Twain, Mark", genre='Adventure')
|
await Book.objects.create(title="Tom Sawyer", author="Twain, Mark", genre="Adventure")
|
||||||
await Book.objects.create(title='War and Peace', author="Tolstoy, Leo", genre='Fiction')
|
await Book.objects.create(title="War and Peace", author="Tolstoy, Leo", genre="Fiction")
|
||||||
await Book.objects.create(title='Anna Karenina', author="Tolstoy, Leo", genre='Fiction')
|
await Book.objects.create(title="Anna Karenina", author="Tolstoy, Leo", genre="Fiction")
|
||||||
|
|
||||||
# if not exist the instance will be persisted in db
|
# if not exist the instance will be persisted in db
|
||||||
vol2 = await Book.objects.update_or_create(title="Volume II", author='Anonymous', genre='Fiction')
|
vol2 = await Book.objects.update_or_create(
|
||||||
|
title="Volume II", author="Anonymous", genre="Fiction"
|
||||||
|
)
|
||||||
assert await Book.objects.count() == 1
|
assert await Book.objects.count() == 1
|
||||||
|
|
||||||
# if pk or pkname passed in kwargs (like id here) the object will be updated
|
# if pk or pkname passed in kwargs (like id here) the object will be updated
|
||||||
assert await Book.objects.update_or_create(id=vol2.id, genre='Historic')
|
assert await Book.objects.update_or_create(id=vol2.id, genre="Historic")
|
||||||
assert await Book.objects.count() == 1
|
assert await Book.objects.count() == 1
|
||||||
|
|||||||
@ -15,16 +15,21 @@ class Book(ormar.Model):
|
|||||||
id: int = ormar.Integer(primary_key=True)
|
id: int = ormar.Integer(primary_key=True)
|
||||||
title: str = ormar.String(max_length=200)
|
title: str = ormar.String(max_length=200)
|
||||||
author: str = ormar.String(max_length=100)
|
author: str = ormar.String(max_length=100)
|
||||||
genre: str = ormar.String(max_length=100, default='Fiction',
|
genre: str = ormar.String(
|
||||||
choices=['Fiction', 'Adventure', 'Historic', 'Fantasy'])
|
max_length=100,
|
||||||
|
default="Fiction",
|
||||||
|
choices=["Fiction", "Adventure", "Historic", "Fantasy"],
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
await Book.objects.create(title='Tom Sawyer', author="Twain, Mark", genre='Adventure')
|
await Book.objects.create(title="Tom Sawyer", author="Twain, Mark", genre="Adventure")
|
||||||
await Book.objects.create(title='War and Peace in Space', author="Tolstoy, Leo", genre='Fantasy')
|
await Book.objects.create(
|
||||||
await Book.objects.create(title='Anna Karenina', author="Tolstoy, Leo", genre='Fiction')
|
title="War and Peace in Space", author="Tolstoy, Leo", genre="Fantasy"
|
||||||
|
)
|
||||||
|
await Book.objects.create(title="Anna Karenina", author="Tolstoy, Leo", genre="Fiction")
|
||||||
|
|
||||||
# delete accepts kwargs that will be used in filter
|
# delete accepts kwargs that will be used in filter
|
||||||
# acting in same way as queryset.filter(**kwargs).delete()
|
# acting in same way as queryset.filter(**kwargs).delete()
|
||||||
await Book.objects.delete(genre='Fantasy') # delete all fantasy books
|
await Book.objects.delete(genre="Fantasy") # delete all fantasy books
|
||||||
all_books = await Book.objects.all()
|
all_books = await Book.objects.all()
|
||||||
assert len(all_books) == 2
|
assert len(all_books) == 2
|
||||||
|
|||||||
@ -36,10 +36,27 @@ class Car(ormar.Model):
|
|||||||
|
|
||||||
# build some sample data
|
# build some sample data
|
||||||
toyota = await Company.objects.create(name="Toyota", founded=1937)
|
toyota = await Company.objects.create(name="Toyota", founded=1937)
|
||||||
await Car.objects.create(manufacturer=toyota, name="Corolla", year=2020, gearbox_type='Manual', gears=5,
|
await Car.objects.create(
|
||||||
aircon_type='Manual')
|
manufacturer=toyota,
|
||||||
await Car.objects.create(manufacturer=toyota, name="Yaris", year=2019, gearbox_type='Manual', gears=5,
|
name="Corolla",
|
||||||
aircon_type='Manual')
|
year=2020,
|
||||||
await Car.objects.create(manufacturer=toyota, name="Supreme", year=2020, gearbox_type='Auto', gears=6,
|
gearbox_type="Manual",
|
||||||
aircon_type='Auto')
|
gears=5,
|
||||||
|
aircon_type="Manual",
|
||||||
|
)
|
||||||
|
await Car.objects.create(
|
||||||
|
manufacturer=toyota,
|
||||||
|
name="Yaris",
|
||||||
|
year=2019,
|
||||||
|
gearbox_type="Manual",
|
||||||
|
gears=5,
|
||||||
|
aircon_type="Manual",
|
||||||
|
)
|
||||||
|
await Car.objects.create(
|
||||||
|
manufacturer=toyota,
|
||||||
|
name="Supreme",
|
||||||
|
year=2020,
|
||||||
|
gearbox_type="Auto",
|
||||||
|
gears=6,
|
||||||
|
aircon_type="Auto",
|
||||||
|
)
|
||||||
|
|||||||
@ -36,33 +36,65 @@ class Car(ormar.Model):
|
|||||||
|
|
||||||
# build some sample data
|
# build some sample data
|
||||||
toyota = await Company.objects.create(name="Toyota", founded=1937)
|
toyota = await Company.objects.create(name="Toyota", founded=1937)
|
||||||
await Car.objects.create(manufacturer=toyota, name="Corolla", year=2020, gearbox_type='Manual', gears=5,
|
await Car.objects.create(
|
||||||
aircon_type='Manual')
|
manufacturer=toyota,
|
||||||
await Car.objects.create(manufacturer=toyota, name="Yaris", year=2019, gearbox_type='Manual', gears=5,
|
name="Corolla",
|
||||||
aircon_type='Manual')
|
year=2020,
|
||||||
await Car.objects.create(manufacturer=toyota, name="Supreme", year=2020, gearbox_type='Auto', gears=6,
|
gearbox_type="Manual",
|
||||||
aircon_type='Auto')
|
gears=5,
|
||||||
|
aircon_type="Manual",
|
||||||
|
)
|
||||||
|
await Car.objects.create(
|
||||||
|
manufacturer=toyota,
|
||||||
|
name="Yaris",
|
||||||
|
year=2019,
|
||||||
|
gearbox_type="Manual",
|
||||||
|
gears=5,
|
||||||
|
aircon_type="Manual",
|
||||||
|
)
|
||||||
|
await Car.objects.create(
|
||||||
|
manufacturer=toyota,
|
||||||
|
name="Supreme",
|
||||||
|
year=2020,
|
||||||
|
gearbox_type="Auto",
|
||||||
|
gears=6,
|
||||||
|
aircon_type="Auto",
|
||||||
|
)
|
||||||
|
|
||||||
# select manufacturer but only name - to include related models use notation {model_name}__{column}
|
# select manufacturer but only name - to include related models use notation {model_name}__{column}
|
||||||
all_cars = await Car.objects.select_related('manufacturer').exclude_fields(
|
all_cars = (
|
||||||
['year', 'gearbox_type', 'gears', 'aircon_type', 'company__founded']).all()
|
await Car.objects.select_related("manufacturer")
|
||||||
|
.exclude_fields(
|
||||||
|
["year", "gearbox_type", "gears", "aircon_type", "company__founded"]
|
||||||
|
)
|
||||||
|
.all()
|
||||||
|
)
|
||||||
for car in all_cars:
|
for car in all_cars:
|
||||||
# excluded columns will yield None
|
# excluded columns will yield None
|
||||||
assert all(getattr(car, x) is None for x in ['year', 'gearbox_type', 'gears', 'aircon_type'])
|
assert all(
|
||||||
|
getattr(car, x) is None
|
||||||
|
for x in ["year", "gearbox_type", "gears", "aircon_type"]
|
||||||
|
)
|
||||||
# included column on related models will be available, pk column is always included
|
# included column on related models will be available, pk column is always included
|
||||||
# even if you do not include it in fields list
|
# even if you do not include it in fields list
|
||||||
assert car.manufacturer.name == 'Toyota'
|
assert car.manufacturer.name == "Toyota"
|
||||||
# also in the nested related models - you cannot exclude pk - it's always auto added
|
# also in the nested related models - you cannot exclude pk - it's always auto added
|
||||||
assert car.manufacturer.founded is None
|
assert car.manufacturer.founded is None
|
||||||
|
|
||||||
# fields() can be called several times, building up the columns to select
|
# fields() can be called several times, building up the columns to select
|
||||||
# models selected in select_related but with no columns in fields list implies all fields
|
# models selected in select_related but with no columns in fields list implies all fields
|
||||||
all_cars = await Car.objects.select_related('manufacturer').exclude_fields('year').exclude_fields(
|
all_cars = (
|
||||||
['gear', 'gearbox_type']).all()
|
await Car.objects.select_related("manufacturer")
|
||||||
|
.exclude_fields("year")
|
||||||
|
.exclude_fields(["gear", "gearbox_type"])
|
||||||
|
.all()
|
||||||
|
)
|
||||||
# all fiels from company model are selected
|
# all fiels from company model are selected
|
||||||
assert all_cars[0].manufacturer.name == 'Toyota'
|
assert all_cars[0].manufacturer.name == "Toyota"
|
||||||
assert all_cars[0].manufacturer.founded == 1937
|
assert all_cars[0].manufacturer.founded == 1937
|
||||||
|
|
||||||
# cannot exclude mandatory model columns - company__name in this example - note usage of dict/set this time
|
# cannot exclude mandatory model columns - company__name in this example - note usage of dict/set this time
|
||||||
await Car.objects.select_related('manufacturer').exclude_fields([{'company': {'name'}}]).all()
|
await Car.objects.select_related("manufacturer").exclude_fields(
|
||||||
|
[{"company": {"name"}}]
|
||||||
|
).all()
|
||||||
# will raise pydantic ValidationError as company.name is required
|
# will raise pydantic ValidationError as company.name is required
|
||||||
|
|||||||
@ -1,33 +1,29 @@
|
|||||||
# 1. like in example above
|
# 1. like in example above
|
||||||
await Car.objects.select_related('manufacturer').fields(['id', 'name', 'manufacturer__name']).all()
|
await Car.objects.select_related("manufacturer").fields(
|
||||||
|
["id", "name", "manufacturer__name"]
|
||||||
|
).all()
|
||||||
|
|
||||||
# 2. to mark a field as required use ellipsis
|
# 2. to mark a field as required use ellipsis
|
||||||
await Car.objects.select_related('manufacturer').fields({'id': ...,
|
await Car.objects.select_related("manufacturer").fields(
|
||||||
'name': ...,
|
{"id": ..., "name": ..., "manufacturer": {"name": ...}}
|
||||||
'manufacturer': {
|
).all()
|
||||||
'name': ...}
|
|
||||||
}).all()
|
|
||||||
|
|
||||||
# 3. to include whole nested model use ellipsis
|
# 3. to include whole nested model use ellipsis
|
||||||
await Car.objects.select_related('manufacturer').fields({'id': ...,
|
await Car.objects.select_related("manufacturer").fields(
|
||||||
'name': ...,
|
{"id": ..., "name": ..., "manufacturer": ...}
|
||||||
'manufacturer': ...
|
).all()
|
||||||
}).all()
|
|
||||||
|
|
||||||
# 4. to specify fields at last nesting level you can also use set - equivalent to 2. above
|
# 4. to specify fields at last nesting level you can also use set - equivalent to 2. above
|
||||||
await Car.objects.select_related('manufacturer').fields({'id': ...,
|
await Car.objects.select_related("manufacturer").fields(
|
||||||
'name': ...,
|
{"id": ..., "name": ..., "manufacturer": {"name"}}
|
||||||
'manufacturer': {'name'}
|
).all()
|
||||||
}).all()
|
|
||||||
|
|
||||||
# 5. of course set can have multiple fields
|
# 5. of course set can have multiple fields
|
||||||
await Car.objects.select_related('manufacturer').fields({'id': ...,
|
await Car.objects.select_related("manufacturer").fields(
|
||||||
'name': ...,
|
{"id": ..., "name": ..., "manufacturer": {"name", "founded"}}
|
||||||
'manufacturer': {'name', 'founded'}
|
).all()
|
||||||
}).all()
|
|
||||||
|
|
||||||
# 6. you can include all nested fields but it will be equivalent of 3. above which is shorter
|
# 6. you can include all nested fields but it will be equivalent of 3. above which is shorter
|
||||||
await Car.objects.select_related('manufacturer').fields({'id': ...,
|
await Car.objects.select_related("manufacturer").fields(
|
||||||
'name': ...,
|
{"id": ..., "name": ..., "manufacturer": {"id", "name", "founded"}}
|
||||||
'manufacturer': {'id', 'name', 'founded'}
|
).all()
|
||||||
}).all()
|
|
||||||
|
|||||||
@ -14,4 +14,4 @@ class Course(ormar.Model):
|
|||||||
|
|
||||||
id: int = ormar.Integer(primary_key=True)
|
id: int = ormar.Integer(primary_key=True)
|
||||||
name: str = ormar.String(max_length=100)
|
name: str = ormar.String(max_length=100)
|
||||||
department: Optional[Union[Department, Dict]] = ormar.ForeignKey(Department)
|
department: Optional[Union[Department, Dict]] = ormar.ForeignKey(Department)
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
import datetime
|
import datetime
|
||||||
from typing import Any, Dict, TYPE_CHECKING, Type
|
from typing import Any, TYPE_CHECKING, Type
|
||||||
|
|
||||||
import sqlalchemy
|
import sqlalchemy
|
||||||
|
|
||||||
@ -153,9 +153,8 @@ class FilterAction(QueryAction):
|
|||||||
else:
|
else:
|
||||||
aliased_column = self.column
|
aliased_column = self.column
|
||||||
clause = getattr(aliased_column, op_attr)(filter_value)
|
clause = getattr(aliased_column, op_attr)(filter_value)
|
||||||
clause = self._compile_clause(
|
if self.has_escaped_character:
|
||||||
clause, modifiers={"escape": "\\" if self.has_escaped_character else None}
|
clause.modifiers["escape"] = "\\"
|
||||||
)
|
|
||||||
return clause
|
return clause
|
||||||
|
|
||||||
def _convert_dates_if_required(self) -> None:
|
def _convert_dates_if_required(self) -> None:
|
||||||
@ -174,42 +173,3 @@ class FilterAction(QueryAction):
|
|||||||
else x
|
else x
|
||||||
for x in self.filter_value
|
for x in self.filter_value
|
||||||
]
|
]
|
||||||
|
|
||||||
def _compile_clause(
|
|
||||||
self, clause: sqlalchemy.sql.expression.BinaryExpression, modifiers: Dict
|
|
||||||
) -> sqlalchemy.sql.expression.TextClause:
|
|
||||||
"""
|
|
||||||
Compiles the clause to str using appropriate database dialect, replace columns
|
|
||||||
names with aliased names and converts it back to TextClause.
|
|
||||||
|
|
||||||
:param clause: original not compiled clause
|
|
||||||
:type clause: sqlalchemy.sql.elements.BinaryExpression
|
|
||||||
:param modifiers: sqlalchemy modifiers - used only to escape chars here
|
|
||||||
:type modifiers: Dict[str, NoneType]
|
|
||||||
:return: compiled and escaped clause
|
|
||||||
:rtype: sqlalchemy.sql.elements.TextClause
|
|
||||||
"""
|
|
||||||
for modifier, modifier_value in modifiers.items():
|
|
||||||
clause.modifiers[modifier] = modifier_value
|
|
||||||
|
|
||||||
# compiled_clause = clause.compile(
|
|
||||||
# dialect=self.target_model.Meta.database._backend._dialect,
|
|
||||||
# # compile_kwargs={"literal_binds": True},
|
|
||||||
# )
|
|
||||||
#
|
|
||||||
# compiled_clause2 = clause.compile(
|
|
||||||
# dialect=self.target_model.Meta.database._backend._dialect,
|
|
||||||
# compile_kwargs={"literal_binds": True},
|
|
||||||
# )
|
|
||||||
#
|
|
||||||
# alias = f"{self.table_prefix}_" if self.table_prefix else ""
|
|
||||||
# aliased_name = f"{alias}{self.table.name}.{self.column.name}"
|
|
||||||
# clause_text = self.compile_query(compiled_query=compiled_clause)
|
|
||||||
# clause_text = clause_text.replace(
|
|
||||||
# f"{self.table.name}.{self.column.name}", aliased_name
|
|
||||||
# )
|
|
||||||
# # dialect_name = self.target_model.Meta.database._backend._dialect.name
|
|
||||||
# # if dialect_name != "sqlite": # pragma: no cover
|
|
||||||
# # clause_text = clause_text.replace("%%", "%")
|
|
||||||
# clause = text(clause_text)
|
|
||||||
return clause
|
|
||||||
|
|||||||
@ -121,21 +121,10 @@ class FilterGroup:
|
|||||||
:return: complied and escaped clause
|
:return: complied and escaped clause
|
||||||
:rtype: sqlalchemy.sql.elements.TextClause
|
:rtype: sqlalchemy.sql.elements.TextClause
|
||||||
"""
|
"""
|
||||||
# prefix = " NOT " if self.exclude else ""
|
|
||||||
if self.filter_type == FilterType.AND:
|
if self.filter_type == FilterType.AND:
|
||||||
# clause = sqlalchemy.text(
|
clause = sqlalchemy.sql.and_(*self._get_text_clauses()).self_group()
|
||||||
# f"{prefix}( "
|
|
||||||
# + str(sqlalchemy.sql.and_(*self._get_text_clauses()))
|
|
||||||
# + " )"
|
|
||||||
# )
|
|
||||||
clause = sqlalchemy.sql.and_(*self._get_text_clauses())
|
|
||||||
else:
|
else:
|
||||||
# clause = sqlalchemy.text(
|
clause = sqlalchemy.sql.or_(*self._get_text_clauses()).self_group()
|
||||||
# f"{prefix}( "
|
|
||||||
# + str(sqlalchemy.sql.or_(*self._get_text_clauses()))
|
|
||||||
# + " )"
|
|
||||||
# )
|
|
||||||
clause = sqlalchemy.sql.or_(*self._get_text_clauses())
|
|
||||||
if self.exclude:
|
if self.exclude:
|
||||||
clause = sqlalchemy.sql.not_(clause)
|
clause = sqlalchemy.sql.not_(clause)
|
||||||
return clause
|
return clause
|
||||||
|
|||||||
@ -78,7 +78,7 @@ class AliasManager:
|
|||||||
:rtype: List[text]
|
:rtype: List[text]
|
||||||
"""
|
"""
|
||||||
alias = f"{alias}_" if alias else ""
|
alias = f"{alias}_" if alias else ""
|
||||||
aliased_fields = [f"{alias}{x}" for x in fields]
|
aliased_fields = [f"{alias}{x}" for x in fields] if fields else []
|
||||||
# TODO: check if normal fields still needed or only aliased one
|
# TODO: check if normal fields still needed or only aliased one
|
||||||
all_columns = (
|
all_columns = (
|
||||||
table.columns
|
table.columns
|
||||||
|
|||||||
@ -152,6 +152,10 @@ disallow_untyped_calls = false
|
|||||||
disallow_untyped_defs = false
|
disallow_untyped_defs = false
|
||||||
disallow_incomplete_defs = false
|
disallow_incomplete_defs = false
|
||||||
|
|
||||||
|
[[tool.mypy.overrides]]
|
||||||
|
module = "docs_src.*"
|
||||||
|
ignore_errors = true
|
||||||
|
|
||||||
[[tool.mypy.overrides]]
|
[[tool.mypy.overrides]]
|
||||||
module = ["sqlalchemy.*", "asyncpg"]
|
module = ["sqlalchemy.*", "asyncpg"]
|
||||||
ignore_missing_imports = true
|
ignore_missing_imports = true
|
||||||
|
|||||||
@ -142,16 +142,16 @@ def test_combining_groups_together():
|
|||||||
group = (Product.name == "Test") & (Product.rating >= 3.0)
|
group = (Product.name == "Test") & (Product.rating >= 3.0)
|
||||||
group.resolve(model_cls=Product)
|
group.resolve(model_cls=Product)
|
||||||
assert len(group._nested_groups) == 2
|
assert len(group._nested_groups) == 2
|
||||||
assert str(group.get_text_clause()) == (
|
assert str(
|
||||||
"( ( product.name = 'Test' ) AND" " ( product.rating >= 3.0 ) )"
|
group.get_text_clause().compile(compile_kwargs={"literal_binds": True})
|
||||||
)
|
) == ("((product.name = 'Test') AND (product.rating >= 3.0))")
|
||||||
|
|
||||||
group = ~((Product.name == "Test") & (Product.rating >= 3.0))
|
group = ~((Product.name == "Test") & (Product.rating >= 3.0))
|
||||||
group.resolve(model_cls=Product)
|
group.resolve(model_cls=Product)
|
||||||
assert len(group._nested_groups) == 2
|
assert len(group._nested_groups) == 2
|
||||||
assert str(group.get_text_clause()) == (
|
assert str(
|
||||||
" NOT ( ( product.name = 'Test' ) AND" " ( product.rating >= 3.0 ) )"
|
group.get_text_clause().compile(compile_kwargs={"literal_binds": True})
|
||||||
)
|
) == ("NOT ((product.name = 'Test') AND" " (product.rating >= 3.0))")
|
||||||
|
|
||||||
group = ((Product.name == "Test") & (Product.rating >= 3.0)) | (
|
group = ((Product.name == "Test") & (Product.rating >= 3.0)) | (
|
||||||
Product.category.name << (["Toys", "Books"])
|
Product.category.name << (["Toys", "Books"])
|
||||||
@ -159,11 +159,13 @@ def test_combining_groups_together():
|
|||||||
group.resolve(model_cls=Product)
|
group.resolve(model_cls=Product)
|
||||||
assert len(group._nested_groups) == 2
|
assert len(group._nested_groups) == 2
|
||||||
assert len(group._nested_groups[0]._nested_groups) == 2
|
assert len(group._nested_groups[0]._nested_groups) == 2
|
||||||
group_str = str(group.get_text_clause())
|
group_str = str(
|
||||||
|
group.get_text_clause().compile(compile_kwargs={"literal_binds": True})
|
||||||
|
)
|
||||||
category_prefix = group._nested_groups[1].actions[0].table_prefix
|
category_prefix = group._nested_groups[1].actions[0].table_prefix
|
||||||
assert group_str == (
|
assert group_str == (
|
||||||
"( ( ( product.name = 'Test' ) AND ( product.rating >= 3.0 ) ) "
|
"(((product.name = 'Test') AND (product.rating >= 3.0)) "
|
||||||
f"OR ( {category_prefix}_categories.name IN ('Toys', 'Books') ) )"
|
f"OR ({category_prefix}_categories.name IN ('Toys', 'Books')))"
|
||||||
)
|
)
|
||||||
|
|
||||||
group = (Product.name % "Test") | (
|
group = (Product.name % "Test") | (
|
||||||
@ -173,15 +175,17 @@ def test_combining_groups_together():
|
|||||||
group.resolve(model_cls=Product)
|
group.resolve(model_cls=Product)
|
||||||
assert len(group._nested_groups) == 2
|
assert len(group._nested_groups) == 2
|
||||||
assert len(group._nested_groups[1]._nested_groups) == 2
|
assert len(group._nested_groups[1]._nested_groups) == 2
|
||||||
group_str = str(group.get_text_clause())
|
group_str = str(
|
||||||
|
group.get_text_clause().compile(compile_kwargs={"literal_binds": True})
|
||||||
|
)
|
||||||
price_list_prefix = (
|
price_list_prefix = (
|
||||||
group._nested_groups[1]._nested_groups[0].actions[0].table_prefix
|
group._nested_groups[1]._nested_groups[0].actions[0].table_prefix
|
||||||
)
|
)
|
||||||
category_prefix = group._nested_groups[1]._nested_groups[1].actions[0].table_prefix
|
category_prefix = group._nested_groups[1]._nested_groups[1].actions[0].table_prefix
|
||||||
assert group_str == (
|
assert group_str == (
|
||||||
f"( ( product.name LIKE '%Test%' ) "
|
f"((product.name LIKE '%Test%') "
|
||||||
f"OR ( ( {price_list_prefix}_price_lists.name LIKE 'Aa%' ) "
|
f"OR (({price_list_prefix}_price_lists.name LIKE 'Aa%') "
|
||||||
f"OR ( {category_prefix}_categories.name IN ('Toys', 'Books') ) ) )"
|
f"OR ({category_prefix}_categories.name IN ('Toys', 'Books'))))"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -40,9 +40,10 @@ def test_or_group():
|
|||||||
assert result.actions[0].target_model == Author
|
assert result.actions[0].target_model == Author
|
||||||
assert result.actions[1].target_model == Book
|
assert result.actions[1].target_model == Book
|
||||||
assert (
|
assert (
|
||||||
str(result.get_text_clause()) == f"( authors.name = 'aa' OR "
|
str(result.get_text_clause().compile(compile_kwargs={"literal_binds": True}))
|
||||||
|
== f"(authors.name = 'aa' OR "
|
||||||
f"{result.actions[1].table_prefix}"
|
f"{result.actions[1].table_prefix}"
|
||||||
f"_books.title = 'bb' )"
|
f"_books.title = 'bb')"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@ -53,9 +54,10 @@ def test_and_group():
|
|||||||
assert result.actions[0].target_model == Author
|
assert result.actions[0].target_model == Author
|
||||||
assert result.actions[1].target_model == Book
|
assert result.actions[1].target_model == Book
|
||||||
assert (
|
assert (
|
||||||
str(result.get_text_clause()) == f"( authors.name = 'aa' AND "
|
str(result.get_text_clause().compile(compile_kwargs={"literal_binds": True}))
|
||||||
|
== f"(authors.name = 'aa' AND "
|
||||||
f"{result.actions[1].table_prefix}"
|
f"{result.actions[1].table_prefix}"
|
||||||
f"_books.title = 'bb' )"
|
f"_books.title = 'bb')"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@ -68,12 +70,13 @@ def test_nested_and():
|
|||||||
assert len(result._nested_groups) == 2
|
assert len(result._nested_groups) == 2
|
||||||
book_prefix = result._nested_groups[0].actions[1].table_prefix
|
book_prefix = result._nested_groups[0].actions[1].table_prefix
|
||||||
assert (
|
assert (
|
||||||
str(result.get_text_clause()) == f"( ( authors.name = 'aa' OR "
|
str(result.get_text_clause().compile(compile_kwargs={"literal_binds": True}))
|
||||||
|
== f"((authors.name = 'aa' OR "
|
||||||
f"{book_prefix}"
|
f"{book_prefix}"
|
||||||
f"_books.title = 'bb' ) AND "
|
f"_books.title = 'bb') AND "
|
||||||
f"( authors.name = 'cc' OR "
|
f"(authors.name = 'cc' OR "
|
||||||
f"{book_prefix}"
|
f"{book_prefix}"
|
||||||
f"_books.title = 'dd' ) )"
|
f"_books.title = 'dd'))"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@ -84,11 +87,12 @@ def test_nested_group_and_action():
|
|||||||
assert len(result._nested_groups) == 1
|
assert len(result._nested_groups) == 1
|
||||||
book_prefix = result._nested_groups[0].actions[1].table_prefix
|
book_prefix = result._nested_groups[0].actions[1].table_prefix
|
||||||
assert (
|
assert (
|
||||||
str(result.get_text_clause()) == f"( ( authors.name = 'aa' OR "
|
str(result.get_text_clause().compile(compile_kwargs={"literal_binds": True}))
|
||||||
|
== f"((authors.name = 'aa' OR "
|
||||||
f"{book_prefix}"
|
f"{book_prefix}"
|
||||||
f"_books.title = 'bb' ) AND "
|
f"_books.title = 'bb') AND "
|
||||||
f"{book_prefix}"
|
f"{book_prefix}"
|
||||||
f"_books.title = 'dd' )"
|
f"_books.title = 'dd')"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@ -108,12 +112,14 @@ def test_deeply_nested_or():
|
|||||||
assert len(result._nested_groups) == 2
|
assert len(result._nested_groups) == 2
|
||||||
assert len(result._nested_groups[0]._nested_groups) == 2
|
assert len(result._nested_groups[0]._nested_groups) == 2
|
||||||
book_prefix = result._nested_groups[0]._nested_groups[0].actions[1].table_prefix
|
book_prefix = result._nested_groups[0]._nested_groups[0].actions[1].table_prefix
|
||||||
result_qry = str(result.get_text_clause())
|
result_qry = str(
|
||||||
|
result.get_text_clause().compile(compile_kwargs={"literal_binds": True})
|
||||||
|
)
|
||||||
expected_qry = (
|
expected_qry = (
|
||||||
f"( ( ( authors.name = 'aa' OR {book_prefix}_books.title = 'bb' ) AND "
|
f"(((authors.name = 'aa' OR {book_prefix}_books.title = 'bb') AND "
|
||||||
f"( authors.name = 'cc' OR {book_prefix}_books.title = 'dd' ) ) "
|
f"(authors.name = 'cc' OR {book_prefix}_books.title = 'dd')) "
|
||||||
f"OR ( ( {book_prefix}_books.year < 1900 OR {book_prefix}_books.title = '11' ) AND "
|
f"OR (({book_prefix}_books.year < 1900 OR {book_prefix}_books.title = '11') AND"
|
||||||
f"( {book_prefix}_books.year > 'xx' OR {book_prefix}_books.title = '22' ) ) )"
|
f" ({book_prefix}_books.year > 'xx' OR {book_prefix}_books.title = '22')))"
|
||||||
)
|
)
|
||||||
assert result_qry.replace("\n", "") == expected_qry.replace("\n", "")
|
assert result_qry.replace("\n", "") == expected_qry.replace("\n", "")
|
||||||
|
|
||||||
|
|||||||
@ -56,6 +56,10 @@ def create_test_database():
|
|||||||
metadata.drop_all(engine)
|
metadata.drop_all(engine)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.skipif(
|
||||||
|
database._backend._dialect.name == "sqlite",
|
||||||
|
reason="wait for fix for sqlite in encode/databases",
|
||||||
|
)
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_double_nested_reverse_relation():
|
async def test_double_nested_reverse_relation():
|
||||||
async with database:
|
async with database:
|
||||||
|
|||||||
Reference in New Issue
Block a user