From 0535db75b31eb0c598d34c0fb90cbcdb6c54ace0 Mon Sep 17 00:00:00 2001 From: henadzit Date: Sun, 5 Jan 2025 23:45:59 +0100 Subject: [PATCH] Get rid of column_map --- tortoise/__init__.py | 2 +- tortoise/backends/base/executor.py | 27 ++++++++++++++------------- tortoise/backends/sqlite/executor.py | 7 +------ tortoise/queryset.py | 13 +++++-------- tortoise/timezone.py | 2 +- 5 files changed, 22 insertions(+), 29 deletions(-) diff --git a/tortoise/__init__.py b/tortoise/__init__.py index 467835ee7..a234ebe0b 100644 --- a/tortoise/__init__.py +++ b/tortoise/__init__.py @@ -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: diff --git a/tortoise/backends/base/executor.py b/tortoise/backends/base/executor.py index 8248724dc..08ecd9464 100644 --- a/tortoise/backends/base/executor.py +++ b/tortoise/backends/base/executor.py @@ -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]], ] = {} @@ -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( @@ -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, ) @@ -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] @@ -186,7 +179,9 @@ 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) @@ -194,7 +189,9 @@ async def execute_insert(self, instance: "Model") -> None: 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) @@ -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 ] ) @@ -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 ( diff --git a/tortoise/backends/sqlite/executor.py b/tortoise/backends/sqlite/executor.py index de4c5e09e..1efd6fef9 100644 --- a/tortoise/backends/sqlite/executor.py +++ b/tortoise/backends/sqlite/executor.py @@ -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) diff --git a/tortoise/queryset.py b/tortoise/queryset.py index dfeee7343..4f6b20fad 100644 --- a/tortoise/queryset.py +++ b/tortoise/queryset.py @@ -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: @@ -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, ) @@ -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) @@ -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) @@ -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( @@ -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 @@ -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 diff --git a/tortoise/timezone.py b/tortoise/timezone.py index 346ac3588..3081ad21b 100644 --- a/tortoise/timezone.py +++ b/tortoise/timezone.py @@ -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