Skip to content

Commit

Permalink
Get rid of column_map
Browse files Browse the repository at this point in the history
  • Loading branch information
henadzit committed Jan 5, 2025
1 parent bafb6f7 commit 0535db7
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 29 deletions.
2 changes: 1 addition & 1 deletion tortoise/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
from tortoise.filters import get_m2m_filters
from tortoise.log import logger
from tortoise.models import Model, ModelMeta
from tortoise.utils import generate_schema_for_client
from tortoise.timezone import _reset_timezone_cache
from tortoise.utils import generate_schema_for_client


class Tortoise:
Expand Down
27 changes: 14 additions & 13 deletions tortoise/backends/base/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@

EXECUTOR_CACHE: Dict[
Tuple[str, Optional[str], str],
Tuple[list, str, list, str, Dict[str, Callable], str, Dict[str, str]],
Tuple[list, str, list, str, str, Dict[str, str]],
] = {}


Expand Down Expand Up @@ -78,11 +78,6 @@ def __init__(
self._prepare_insert_statement(columns_all, has_generated=False)
)

self.column_map: Dict[str, Callable[[Any, Any], Any]] = {}
for column in self.regular_columns_all:
field_object = self.model._meta.fields_map[column]
self.column_map[column] = field_object.to_db_value

table = self.model._meta.basetable
basequery = cast(QueryBuilder, self.model._meta.basequery)
self.delete_query = str(
Expand All @@ -95,7 +90,6 @@ def __init__(
self.insert_query,
self.regular_columns_all,
self.insert_query_all,
self.column_map,
self.delete_query,
self.update_cache,
)
Expand All @@ -106,7 +100,6 @@ def __init__(
self.insert_query,
self.regular_columns_all,
self.insert_query_all,
self.column_map,
self.delete_query,
self.update_cache,
) = EXECUTOR_CACHE[key]
Expand Down Expand Up @@ -186,15 +179,19 @@ def parameter(self, pos: int) -> Parameter:
async def execute_insert(self, instance: "Model") -> None:
if not instance._custom_generated_pk:
values = [
self.column_map[field_name](getattr(instance, field_name), instance)
self.model._meta.fields_map[field_name].to_db_value(
getattr(instance, field_name), instance
)
for field_name in self.regular_columns
]
insert_result = await self.db.execute_insert(self.insert_query, values)
await self._process_insert_result(instance, insert_result)

else:
values = [
self.column_map[field_name](getattr(instance, field_name), instance)
self.model._meta.fields_map[field_name].to_db_value(
getattr(instance, field_name), instance
)
for field_name in self.regular_columns_all
]
await self.db.execute_insert(self.insert_query_all, values)
Expand All @@ -211,14 +208,18 @@ async def execute_bulk_insert(
if instance._custom_generated_pk:
values_lists_all.append(
[
self.column_map[field_name](getattr(instance, field_name), instance)
self.model._meta.fields_map[field_name].to_db_value(
getattr(instance, field_name), instance
)
for field_name in self.regular_columns_all
]
)
else:
values_lists.append(
[
self.column_map[field_name](getattr(instance, field_name), instance)
self.model._meta.fields_map[field_name].to_db_value(
getattr(instance, field_name), instance
)
for field_name in self.regular_columns
]
)
Expand Down Expand Up @@ -284,7 +285,7 @@ async def execute_update(
if isinstance(instance_field, Expression):
expressions[field] = instance_field
else:
value = self.column_map[field](instance_field, instance)
value = self.model._meta.fields_map[field].to_db_value(instance_field, instance)
values.append(value)
values.append(self.model._meta.pk.to_db_value(instance.pk, instance))
return (
Expand Down
7 changes: 1 addition & 6 deletions tortoise/backends/sqlite/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,9 @@
insensitive_posix_sqlite_regexp,
posix_sqlite_regexp,
)
from tortoise.fields import (
BigIntField,
IntField,
SmallIntField,
)
from tortoise.fields import BigIntField, IntField, SmallIntField
from tortoise.filters import insensitive_posix_regex, posix_regex


# Conversion for the cases where it's hard to know the
# related field, e.g. in raw queries, math or annotations.
sqlite3.register_adapter(Decimal, str)
Expand Down
13 changes: 5 additions & 8 deletions tortoise/queryset.py
Original file line number Diff line number Diff line change
Expand Up @@ -1195,8 +1195,6 @@ def _make_query(self) -> None:
self.resolve_ordering(self.model, table, self._orderings, self._annotations)

self.resolve_filters()
# Need to get executor to get correct column_map
executor = self._db.executor_class(model=self.model, db=self._db)
for key, value in self.update_kwargs.items():
field_object = self.model._meta.fields_map.get(key)
if not field_object:
Expand All @@ -1207,7 +1205,7 @@ def _make_query(self) -> None:
self.model._validate_relation_type(key, value)
fk_field: str = field_object.source_field # type: ignore
db_field = self.model._meta.fields_map[fk_field].source_field
value = executor.column_map[fk_field](
value = self.model._meta.fields_map[fk_field].to_db_value(
getattr(value, field_object.to_field_instance.model_field_name),
None,
)
Expand All @@ -1227,7 +1225,7 @@ def _make_query(self) -> None:
)
).term
else:
value = executor.column_map[key](value, None)
value = self.model._meta.fields_map[key].to_db_value(value, None)

self.query = self.query.set(db_field, value)

Expand Down Expand Up @@ -1838,7 +1836,6 @@ def _make_queries(self) -> List[Tuple[str, List[Any]]]:
)

self.resolve_filters()
executor = self._db.executor_class(model=self.model, db=self._db)
pk_attr = self.model._meta.pk_attr
source_pk_attr = self.model._meta.fields_map[pk_attr].source_field or pk_attr
pk = Field(source_pk_attr)
Expand All @@ -1848,7 +1845,7 @@ def _make_queries(self) -> List[Tuple[str, List[Any]]]:
case = Case()
pk_list = []
for obj in objects_item:
pk_value = executor.column_map[pk_attr](obj.pk, None)
pk_value = self.model._meta.fields_map[pk_attr].to_db_value(obj.pk, None)
field_obj = obj._meta.fields_map[field]
field_value = field_obj.to_db_value(getattr(obj, field), obj)
case.when(
Expand Down Expand Up @@ -1952,7 +1949,7 @@ async def _execute_many(self, insert_sql: str, insert_sql_all: str) -> None:
if instance._custom_generated_pk:
values_lists_all.append(
[
self._executor.column_map[field_name](
self.model._meta.fields_map[field_name].to_db_value(
getattr(instance, field_name), instance
)
for field_name in self._executor.regular_columns_all
Expand All @@ -1961,7 +1958,7 @@ async def _execute_many(self, insert_sql: str, insert_sql_all: str) -> None:
else:
values_lists.append(
[
self._executor.column_map[field_name](
self.model._meta.fields_map[field_name].to_db_value(
getattr(instance, field_name), instance
)
for field_name in self._executor.regular_columns
Expand Down
2 changes: 1 addition & 1 deletion tortoise/timezone.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from functools import lru_cache
import os
from datetime import datetime, time, tzinfo
from functools import lru_cache
from typing import Optional, Union

import pytz
Expand Down

0 comments on commit 0535db7

Please sign in to comment.