Skip to content

Commit

Permalink
Introduce allow_parametrize to ValueWrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
henadzit committed Nov 18, 2024
1 parent d9c7474 commit e4db01e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
27 changes: 24 additions & 3 deletions pypika/terms.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,9 +301,12 @@ class Parameter(Term):
is_aggregate = None

def __init__(self, placeholder: Optional[str] = None, idx: Optional[int] = None) -> None:
if not placeholder and not idx:
if not placeholder and idx is None:
raise ValueError("Must provide either a placeholder or an idx")

if idx is not None and idx < 1:
raise ValueError("idx must start at 1")

if placeholder and idx:
raise ValueError("Cannot provide both a placeholder and an idx")

Expand Down Expand Up @@ -371,9 +374,23 @@ def get_sql(self, **kwargs: Any) -> str:
class ValueWrapper(Term):
is_aggregate = None

def __init__(self, value: Any, alias: Optional[str] = None) -> None:
def __init__(
self, value: Any, alias: Optional[str] = None, allow_parametrize: bool = True
) -> None:
"""
A wrapper for a constant value such as a string or number.
:param value:
The value to be wrapped.
:param alias:
An optional alias for the value.
:param allow_parametrize:
Whether the value should be replaced with a parameter in the query if parameterizer
is used.
"""
super().__init__(alias)
self.value = value
self.allow_parametrize = allow_parametrize

def get_value_sql(self, **kwargs: Any) -> str:
quote_char = kwargs.get("secondary_quote_char") or ""
Expand Down Expand Up @@ -409,7 +426,11 @@ def get_sql(
parameterizer: Optional[Parameterizer] = None,
**kwargs: Any,
) -> str:
if parameterizer is None or not parameterizer.should_parameterize(self.value):
if (
parameterizer is None
or not parameterizer.should_parameterize(self.value)
or not self.allow_parametrize
):
sql = self.get_value_sql(
quote_char=quote_char, secondary_quote_char=secondary_quote_char, **kwargs
)
Expand Down
14 changes: 13 additions & 1 deletion tests/test_terms.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from unittest import TestCase

from pypika import Field, Query, Table
from pypika.terms import AtTimezone
from pypika.terms import AtTimezone, Parameterizer, ValueWrapper


class FieldAliasTests(TestCase):
Expand Down Expand Up @@ -49,3 +49,15 @@ def test_passes_kwargs_to_field_get_sql(self):
'FROM "customers" JOIN "accounts" ON "customers"."account_id"="accounts"."account_id"',
query.get_sql(with_namespace=True),
)


class ValueWrapperTests(TestCase):
def test_allow_parametrize(self):
value = ValueWrapper("foo")
self.assertEqual("'foo'", value.get_sql())

value = ValueWrapper("foo")
self.assertEqual("?", value.get_sql(parameterizer=Parameterizer()))

value = ValueWrapper("foo", allow_parametrize=False)
self.assertEqual("'foo'", value.get_sql(parameterizer=Parameterizer()))

0 comments on commit e4db01e

Please sign in to comment.