Skip to content

Commit 9756418

Browse files
authored
Merge pull request #20 from henadzit/feat/query-supports-parametrization
Add QueryBuilder.get_parameterized_sql
2 parents 413d7ed + 764f994 commit 9756418

File tree

5 files changed

+29
-13
lines changed

5 files changed

+29
-13
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
## 0.3
44

5+
### 0.3.2
6+
- Added `QueryBuilder.get_parameterized_sql`
7+
58
### 0.3.1
69
- `Array` can be parametrized
710

README.md

+6-11
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,20 @@
55
[![image](https://github.com/tortoise/pypika-tortoise/workflows/pypi/badge.svg)](https://github.com/tortoise/pypika-tortoise/actions?query=workflow:pypi)
66
[![image](https://github.com/tortoise/pypika-tortoise/workflows/ci/badge.svg)](https://github.com/tortoise/pypika-tortoise/actions?query=workflow:ci)
77

8-
Forked from [pypika](https://github.com/kayak/pypika) and streamline just for tortoise-orm.
8+
Forked from [pypika](https://github.com/kayak/pypika) and adapted just for tortoise-orm.
99

1010
## Why forked?
1111

12-
The original repo include many databases that tortoise-orm don't need, and which aims to be a perfect sql builder and
13-
should consider more compatibilities, but tortoise-orm is not, and we need add new features and update it ourselves.
12+
The original repository includes many databases that Tortoise ORM doesn’t require. It aims to be a comprehensive SQL builder with broad compatibility, but that’s not the goal for Tortoise ORM. Having it forked makes it easier to add new features for Tortoise.
1413

15-
## What change?
14+
## What changed?
1615

17-
Delete many codes that tortoise-orm don't need, and add features just tortoise-orm considers to.
18-
19-
## What affect tortoise-orm?
20-
21-
Nothing, because this repo keeps the original struct and code.
16+
Deleted unnecessary code that Tortoise ORM doesn’t require, and added features tailored specifically for Tortoise ORM.
2217

2318
## ThanksTo
2419

25-
- [pypika](https://github.com/kayak/pypika), a python SQL query builder that exposes the full richness of the SQL
26-
language using a syntax that reflects the resulting query.
20+
- [pypika](https://github.com/kayak/pypika), a Python SQL query builder that exposes the full expressiveness of SQL,
21+
using a syntax that mirrors the resulting query structure.
2722

2823
## License
2924

pypika/queries.py

+11
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
Index,
1717
Node,
1818
Order,
19+
Parameterizer,
1920
PeriodCriterion,
2021
Rollup,
2122
Star,
@@ -1564,6 +1565,16 @@ def _with_sql(self, **kwargs: Any) -> str:
15641565
for clause in self._with
15651566
)
15661567

1568+
def get_parameterized_sql(self, **kwargs) -> tuple[str, list]:
1569+
"""
1570+
Returns a tuple containing the query string and a list of parameters
1571+
"""
1572+
parameterizer = kwargs.pop("parameterizer", Parameterizer())
1573+
return (
1574+
self.get_sql(parameterizer=parameterizer, **kwargs),
1575+
parameterizer.values,
1576+
)
1577+
15671578
def _distinct_sql(self, **kwargs: Any) -> str:
15681579
return "DISTINCT " if self._distinct else ""
15691580

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "pypika-tortoise"
3-
version = "0.3.1"
3+
version = "0.3.2"
44
description = "Forked from pypika and streamline just for tortoise-orm"
55
authors = ["long2ice <long2ice@gmail.com>"]
66
license = "Apache-2.0"

tests/test_query.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
SQLLiteQuery,
1212
SQLLiteQueryBuilder,
1313
)
14-
from pypika.queries import CreateQueryBuilder, DropQueryBuilder, QueryBuilder
14+
from pypika.queries import CreateQueryBuilder, DropQueryBuilder, QueryBuilder, Table
1515

1616

1717
class QueryTablesTests(unittest.TestCase):
@@ -209,3 +209,10 @@ def test_query_builder_copy(self):
209209
self.assertIsNot(qb._on_conflict_fields, qb2._on_conflict_fields)
210210
self.assertEqual(qb._on_conflict_do_updates, qb2._on_conflict_do_updates)
211211
self.assertIsNot(qb._on_conflict_do_updates, qb2._on_conflict_do_updates)
212+
213+
def test_get_parameterized_sql(self):
214+
table = Table("abc")
215+
q = QueryBuilder().from_(table).select("foo").where(table.bar == 1)
216+
sql, params = q.get_parameterized_sql()
217+
self.assertEqual('SELECT "foo" FROM "abc" WHERE "bar"=?', sql)
218+
self.assertEqual([1], params)

0 commit comments

Comments
 (0)