Coverage for app/venv/lib/python3.14/site-packages/weblate/trans/functions.py: 41%
23 statements
« prev ^ index » next coverage.py v7.15.2, created at 2026-10-07 07:15 +0000
« prev ^ index » next coverage.py v7.15.2, created at 2026-10-07 07:15 +0000
1# Copyright © Michal Čihař <michal@weblate.org>
2#
3# SPDX-License-Identifier: GPL-3.0-or-later
5from __future__ import annotations
7from typing import TYPE_CHECKING, Any
9from django.db.models import DateTimeField, Func
11if TYPE_CHECKING: 11 ↛ 12line 11 didn't jump to line 12 because the condition on line 11 was never true
12 from django.db.backends.base.base import BaseDatabaseWrapper
13 from django.db.models import Expression
14 from django.db.models.sql.compiler import SQLCompiler, _AsSqlType
16VALID_UNIT_VALUES = {
17 "MICROSECOND",
18 "SECOND",
19 "MINUTE",
20 "HOUR",
21 "DAY",
22 "WEEK",
23 "MONTH",
24 "QUARTER",
25 "YEAR",
26}
29class MySQLTimestampAdd(Func):
30 function = "TIMESTAMPADD"
31 output_field = DateTimeField()
33 def __init__(self, unit: str, interval: Expression, timestamp: Expression):
34 # unit is a string (not a Value/Expression) as mysql/mariadb throws an
35 # error if the unit argument to TIMESTAMPADD is quoted.
36 if unit not in VALID_UNIT_VALUES:
37 msg = f"Invalid unit: {unit}"
38 raise ValueError(msg)
39 self.unit = unit
40 super().__init__(interval, timestamp)
42 def as_sql(
43 self,
44 compiler: SQLCompiler,
45 connection: BaseDatabaseWrapper,
46 function: str | None = None,
47 template: str | None = None,
48 arg_joiner: str | None = None,
49 **extra_context: Any, # noqa: ANN401
50 ) -> _AsSqlType:
51 interval_sql, interval_params = self.source_expressions[0].as_sql(
52 compiler, connection
53 )
54 timestamp_sql, timestamp_params = self.source_expressions[1].as_sql(
55 compiler, connection
56 )
58 # override default template to avoid addition of unnecessary parentheses
59 # around function arguments
60 sql = f"{self.function}({self.unit}, {interval_sql}, {timestamp_sql})"
61 params = interval_params + timestamp_params # type: ignore[operator]
62 return sql, params