Skip to content

Commit

Permalink
Add placeholder_factory to Parameterizer
Browse files Browse the repository at this point in the history
  • Loading branch information
henadzit committed Nov 18, 2024
1 parent 36a49ec commit d9c7474
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 4 deletions.
3 changes: 2 additions & 1 deletion pypika/queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -1228,7 +1228,8 @@ def minus(self, other: "QueryBuilder") -> _SetOperation:
@builder
def set(self, field: Union[Field, str], value: Any) -> "QueryBuilder":
field = Field(field) if not isinstance(field, Field) else field
self._updates.append((field, self._wrapper_cls(value)))
value = self.wrap_constant(value, wrapper_cls=self._wrapper_cls)
self._updates.append((field, value))

def __add__(self, other: "QueryBuilder") -> _SetOperation:
return self.union(other)
Expand Down
9 changes: 7 additions & 2 deletions pypika/terms.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from typing import (
TYPE_CHECKING,
Any,
Callable,
Iterable,
Iterator,
List,
Expand Down Expand Up @@ -333,7 +334,8 @@ class Parameterizer:
be accessed via the `values` attribute.
"""

def __init__(self) -> None:
def __init__(self, placeholder_factory: Optional[Callable[[int], str]] = None) -> None:
self.placeholder_factory = placeholder_factory
self.values = []

def should_parameterize(self, value: Any) -> bool:
Expand All @@ -347,7 +349,10 @@ def should_parameterize(self, value: Any) -> bool:

def create_param(self, value: Any) -> Parameter:
self.values.append(value)
return Parameter(idx=len(self.values))
if self.placeholder_factory:
return Parameter(self.placeholder_factory(len(self.values)))
else:
return Parameter(idx=len(self.values))


class Negative(Term):
Expand Down
5 changes: 5 additions & 0 deletions tests/test_parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,3 +235,8 @@ def test_limit_and_offest_in_mssql(self):
'SELECT * FROM "abc" ORDER BY (SELECT 0) OFFSET ? ROWS FETCH NEXT ? ROWS ONLY', sql
)
self.assertEqual([5, 10], parameterizer.values)

def test_placeholder_factory(self):
parameterizer = Parameterizer(placeholder_factory=lambda _: "%s")
param = parameterizer.create_param(1)
self.assertEqual("%s", param.get_sql())
2 changes: 1 addition & 1 deletion tests/test_updates.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def test_update__table_schema(self):

def test_update_with_none(self):
q = Query.update("abc").set("foo", None)
self.assertEqual('UPDATE "abc" SET "foo"=null', str(q))
self.assertEqual('UPDATE "abc" SET "foo"=NULL', str(q))

def test_update_from(self):
from_table = Table("from_table")
Expand Down

0 comments on commit d9c7474

Please sign in to comment.