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 19, 2024
1 parent 485b854 commit 61b08e4
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 @@ -333,9 +333,12 @@ class Parameter(Term):
is_aggregate = None

def __init__(self, placeholder: str | None = None, idx: int | None = 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 @@ -403,9 +406,23 @@ def get_sql(self, **kwargs: Any) -> str:
class ValueWrapper(Term):
is_aggregate = None

def __init__(self, value: Any, alias: str | None = None) -> None:
def __init__(
self, value: Any, alias: str | None = 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:
return self.get_formatted_value(self.value, **kwargs)
Expand Down Expand Up @@ -443,7 +460,11 @@ def get_sql(
parameterizer: Parameterizer | None = 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 61b08e4

Please sign in to comment.