rc for skip of literal binds

This commit is contained in:
collerek
2022-01-06 18:22:07 +01:00
parent 067b61460d
commit c8586e5b8e
20 changed files with 188 additions and 155 deletions

View File

@ -3,15 +3,18 @@ repos:
rev: 21.9b0
hooks:
- id: black
exclude: docs_src
- repo: https://github.com/pycqa/flake8
rev: 3.9.2
hooks:
- id: flake8
exclude: docs_src
args: [ '--max-line-length=88' ]
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.910
hooks:
- id: mypy
exclude: docs_src
args: [--no-strict-optional, --ignore-missing-imports]
additional_dependencies: [
types-ujson>=0.1.1,

View File

@ -26,7 +26,9 @@ class Course(ormar.Model):
id: int = ormar.Integer(primary_key=True)
name: str = ormar.String(max_length=100)
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")

View File

@ -8,7 +8,9 @@ metadata = sqlalchemy.MetaData()
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
metadata = metadata

View File

@ -16,4 +16,5 @@ class Course(ormar.Model):
name: ormar.String(max_length=100)
completed: ormar.Boolean(default=False)
c1 = Course()
c1 = Course()

View File

@ -14,4 +14,4 @@ class Course(ormar.Model):
id = ormar.Integer(primary_key=True)
name = ormar.String(max_length=100)
completed = ormar.Boolean(default=False)
completed = ormar.Boolean(default=False)

View File

@ -19,4 +19,4 @@ class Course(ormar.Model):
@property_field
def prefixed_name(self):
return 'custom_prefix__' + self.name
return "custom_prefix__" + self.name

View File

@ -15,14 +15,17 @@ class Book(ormar.Model):
id: int = ormar.Integer(primary_key=True)
title: str = ormar.String(max_length=200)
author: str = ormar.String(max_length=100)
genre: str = ormar.String(max_length=100, default='Fiction',
choices=['Fiction', 'Adventure', 'Historic', 'Fantasy'])
genre: str = ormar.String(
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='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="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="Anna Karenina", author="Tolstoy, Leo", genre="Fiction")
await Book.objects.update(each=True, genre='Fiction')
all_books = await Book.objects.filter(genre='Fiction').all()
await Book.objects.update(each=True, genre="Fiction")
all_books = await Book.objects.filter(genre="Fiction").all()
assert len(all_books) == 3

View File

@ -15,18 +15,23 @@ class Book(ormar.Model):
id: int = ormar.Integer(primary_key=True)
title: str = ormar.String(max_length=200)
author: str = ormar.String(max_length=100)
genre: str = ormar.String(max_length=100, default='Fiction',
choices=['Fiction', 'Adventure', 'Historic', 'Fantasy'])
genre: str = ormar.String(
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='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="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="Anna Karenina", author="Tolstoy, Leo", genre="Fiction")
# 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
# 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

View File

@ -15,16 +15,21 @@ class Book(ormar.Model):
id: int = ormar.Integer(primary_key=True)
title: str = ormar.String(max_length=200)
author: str = ormar.String(max_length=100)
genre: str = ormar.String(max_length=100, default='Fiction',
choices=['Fiction', 'Adventure', 'Historic', 'Fantasy'])
genre: str = ormar.String(
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='War and Peace in Space', author="Tolstoy, Leo", genre='Fantasy')
await Book.objects.create(title='Anna Karenina', author="Tolstoy, Leo", genre='Fiction')
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(title="Anna Karenina", author="Tolstoy, Leo", genre="Fiction")
# delete accepts kwargs that will be used in filter
# 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()
assert len(all_books) == 2

View File

@ -36,10 +36,27 @@ class Car(ormar.Model):
# build some sample data
toyota = await Company.objects.create(name="Toyota", founded=1937)
await Car.objects.create(manufacturer=toyota, name="Corolla", year=2020, gearbox_type='Manual', 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')
await Car.objects.create(
manufacturer=toyota,
name="Corolla",
year=2020,
gearbox_type="Manual",
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",
)

View File

@ -36,33 +36,65 @@ class Car(ormar.Model):
# build some sample data
toyota = await Company.objects.create(name="Toyota", founded=1937)
await Car.objects.create(manufacturer=toyota, name="Corolla", year=2020, gearbox_type='Manual', 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')
await Car.objects.create(
manufacturer=toyota,
name="Corolla",
year=2020,
gearbox_type="Manual",
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}
all_cars = await Car.objects.select_related('manufacturer').exclude_fields(
['year', 'gearbox_type', 'gears', 'aircon_type', 'company__founded']).all()
all_cars = (
await Car.objects.select_related("manufacturer")
.exclude_fields(
["year", "gearbox_type", "gears", "aircon_type", "company__founded"]
)
.all()
)
for car in all_cars:
# 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
# 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
assert car.manufacturer.founded is None
# 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
all_cars = await Car.objects.select_related('manufacturer').exclude_fields('year').exclude_fields(
['gear', 'gearbox_type']).all()
all_cars = (
await Car.objects.select_related("manufacturer")
.exclude_fields("year")
.exclude_fields(["gear", "gearbox_type"])
.all()
)
# 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
# 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

View File

@ -1,33 +1,29 @@
# 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
await Car.objects.select_related('manufacturer').fields({'id': ...,
'name': ...,
'manufacturer': {
'name': ...}
}).all()
await Car.objects.select_related("manufacturer").fields(
{"id": ..., "name": ..., "manufacturer": {"name": ...}}
).all()
# 3. to include whole nested model use ellipsis
await Car.objects.select_related('manufacturer').fields({'id': ...,
'name': ...,
'manufacturer': ...
}).all()
await Car.objects.select_related("manufacturer").fields(
{"id": ..., "name": ..., "manufacturer": ...}
).all()
# 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': ...,
'name': ...,
'manufacturer': {'name'}
}).all()
await Car.objects.select_related("manufacturer").fields(
{"id": ..., "name": ..., "manufacturer": {"name"}}
).all()
# 5. of course set can have multiple fields
await Car.objects.select_related('manufacturer').fields({'id': ...,
'name': ...,
'manufacturer': {'name', 'founded'}
}).all()
await Car.objects.select_related("manufacturer").fields(
{"id": ..., "name": ..., "manufacturer": {"name", "founded"}}
).all()
# 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': ...,
'name': ...,
'manufacturer': {'id', 'name', 'founded'}
}).all()
await Car.objects.select_related("manufacturer").fields(
{"id": ..., "name": ..., "manufacturer": {"id", "name", "founded"}}
).all()

View File

@ -14,4 +14,4 @@ class Course(ormar.Model):
id: int = ormar.Integer(primary_key=True)
name: str = ormar.String(max_length=100)
department: Optional[Union[Department, Dict]] = ormar.ForeignKey(Department)
department: Optional[Union[Department, Dict]] = ormar.ForeignKey(Department)

View File

@ -1,5 +1,5 @@
import datetime
from typing import Any, Dict, TYPE_CHECKING, Type
from typing import Any, TYPE_CHECKING, Type
import sqlalchemy
@ -153,9 +153,8 @@ class FilterAction(QueryAction):
else:
aliased_column = self.column
clause = getattr(aliased_column, op_attr)(filter_value)
clause = self._compile_clause(
clause, modifiers={"escape": "\\" if self.has_escaped_character else None}
)
if self.has_escaped_character:
clause.modifiers["escape"] = "\\"
return clause
def _convert_dates_if_required(self) -> None:
@ -174,42 +173,3 @@ class FilterAction(QueryAction):
else x
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

View File

@ -121,21 +121,10 @@ class FilterGroup:
:return: complied and escaped clause
:rtype: sqlalchemy.sql.elements.TextClause
"""
# prefix = " NOT " if self.exclude else ""
if self.filter_type == FilterType.AND:
# clause = sqlalchemy.text(
# f"{prefix}( "
# + str(sqlalchemy.sql.and_(*self._get_text_clauses()))
# + " )"
# )
clause = sqlalchemy.sql.and_(*self._get_text_clauses())
clause = sqlalchemy.sql.and_(*self._get_text_clauses()).self_group()
else:
# clause = sqlalchemy.text(
# f"{prefix}( "
# + str(sqlalchemy.sql.or_(*self._get_text_clauses()))
# + " )"
# )
clause = sqlalchemy.sql.or_(*self._get_text_clauses())
clause = sqlalchemy.sql.or_(*self._get_text_clauses()).self_group()
if self.exclude:
clause = sqlalchemy.sql.not_(clause)
return clause

View File

@ -78,7 +78,7 @@ class AliasManager:
:rtype: List[text]
"""
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
all_columns = (
table.columns

View File

@ -152,6 +152,10 @@ disallow_untyped_calls = false
disallow_untyped_defs = false
disallow_incomplete_defs = false
[[tool.mypy.overrides]]
module = "docs_src.*"
ignore_errors = true
[[tool.mypy.overrides]]
module = ["sqlalchemy.*", "asyncpg"]
ignore_missing_imports = true

View File

@ -142,16 +142,16 @@ def test_combining_groups_together():
group = (Product.name == "Test") & (Product.rating >= 3.0)
group.resolve(model_cls=Product)
assert len(group._nested_groups) == 2
assert str(group.get_text_clause()) == (
"( ( product.name = 'Test' ) AND" " ( product.rating >= 3.0 ) )"
)
assert str(
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.resolve(model_cls=Product)
assert len(group._nested_groups) == 2
assert str(group.get_text_clause()) == (
" NOT ( ( product.name = 'Test' ) AND" " ( product.rating >= 3.0 ) )"
)
assert str(
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)) | (
Product.category.name << (["Toys", "Books"])
@ -159,11 +159,13 @@ def test_combining_groups_together():
group.resolve(model_cls=Product)
assert len(group._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
assert group_str == (
"( ( ( product.name = 'Test' ) AND ( product.rating >= 3.0 ) ) "
f"OR ( {category_prefix}_categories.name IN ('Toys', 'Books') ) )"
"(((product.name = 'Test') AND (product.rating >= 3.0)) "
f"OR ({category_prefix}_categories.name IN ('Toys', 'Books')))"
)
group = (Product.name % "Test") | (
@ -173,15 +175,17 @@ def test_combining_groups_together():
group.resolve(model_cls=Product)
assert len(group._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 = (
group._nested_groups[1]._nested_groups[0].actions[0].table_prefix
)
category_prefix = group._nested_groups[1]._nested_groups[1].actions[0].table_prefix
assert group_str == (
f"( ( product.name LIKE '%Test%' ) "
f"OR ( ( {price_list_prefix}_price_lists.name LIKE 'Aa%' ) "
f"OR ( {category_prefix}_categories.name IN ('Toys', 'Books') ) ) )"
f"((product.name LIKE '%Test%') "
f"OR (({price_list_prefix}_price_lists.name LIKE 'Aa%') "
f"OR ({category_prefix}_categories.name IN ('Toys', 'Books'))))"
)

View File

@ -40,9 +40,10 @@ def test_or_group():
assert result.actions[0].target_model == Author
assert result.actions[1].target_model == Book
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"_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[1].target_model == Book
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"_books.title = 'bb' )"
f"_books.title = 'bb')"
)
@ -68,12 +70,13 @@ def test_nested_and():
assert len(result._nested_groups) == 2
book_prefix = result._nested_groups[0].actions[1].table_prefix
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"_books.title = 'bb' ) AND "
f"( authors.name = 'cc' OR "
f"_books.title = 'bb') AND "
f"(authors.name = 'cc' OR "
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
book_prefix = result._nested_groups[0].actions[1].table_prefix
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"_books.title = 'bb' ) AND "
f"_books.title = 'bb') AND "
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[0]._nested_groups) == 2
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 = (
f"( ( ( authors.name = 'aa' OR {book_prefix}_books.title = 'bb' ) AND "
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"( {book_prefix}_books.year > 'xx' OR {book_prefix}_books.title = '22' ) ) )"
f"(((authors.name = 'aa' OR {book_prefix}_books.title = 'bb') AND "
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" ({book_prefix}_books.year > 'xx' OR {book_prefix}_books.title = '22')))"
)
assert result_qry.replace("\n", "") == expected_qry.replace("\n", "")

View File

@ -56,6 +56,10 @@ def create_test_database():
metadata.drop_all(engine)
@pytest.mark.skipif(
database._backend._dialect.name == "sqlite",
reason="wait for fix for sqlite in encode/databases",
)
@pytest.mark.asyncio
async def test_double_nested_reverse_relation():
async with database: