Source code for neosqlite.collection.expr_evaluator.sql_converters.arithmetic

"""SQL converters for arithmetic operators."""

from __future__ import annotations

from typing import TYPE_CHECKING, Any

if TYPE_CHECKING:
    pass


from .base import BaseSqlMixin


[docs] class ArithmeticMixin(BaseSqlMixin): """SQL converter for operators in the ArithmeticMixin category."""
[docs] def _convert_arithmetic_operator( self, operator: str, operands: list[Any] ) -> tuple[str, list[Any]]: """Convert arithmetic operators to SQL.""" if len(operands) < 2: raise ValueError(f"{operator} requires at least 2 operands") sql_parts = [] all_params = [] for operand in operands: operand_sql, operand_params = self._convert_operand_to_sql(operand) sql_parts.append(operand_sql) all_params.extend(operand_params) sql_operator = self._map_arithmetic_operator(operator) if operator == "$divide": # SQLite '/' on two integers truncates; MongoDB always returns # a double. Force REAL division (#116). sql_parts = [ f"({part}) * 1.0" if idx == 0 else f"({part})" for idx, part in enumerate(sql_parts) ] sql = f"({f' {sql_operator} '.join(sql_parts)})" return sql, all_params