Skip to content

Commit e3ba23e

Browse files
committed
feat: add havings to select_custom_fields
1 parent a5345e0 commit e3ba23e

File tree

4 files changed

+17
-4
lines changed

4 files changed

+17
-4
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ await AccountMgr.select_custom_fields(
5555
],
5656
wheres=["id BETWEEN 1 AND 12"],
5757
groups=["locale", "gender"],
58+
havings=["cnt > 0"],
5859
orders=["locale", "-gender"],
5960
limit=10,
6061
)
@@ -66,6 +67,7 @@ Generate sql and execute
6667
FROM account
6768
WHERE id BETWEEN 1 AND 12
6869
GROUP BY locale, gender
70+
HAVING cnt > 0
6971
ORDER BY locale ASC, gender DESC
7072
LIMIT 10
7173
```

examples/service/routers/account.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ async def query_group_by_locale_view(
5757
],
5858
wheres=["id BETWEEN 1 AND 12"],
5959
groups=["locale", "gender"],
60+
havings=["cnt > 0"],
6061
orders=["locale", "-gender"],
6162
limit=10,
6263
)

fastapi_esql/orm/base_manager.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ async def select_custom_fields(
6565
fields: List[str],
6666
wheres: List[str],
6767
groups: Optional[List[str]] = None,
68+
havings: Optional[List[str]] = None,
6869
orders: Optional[List[str]] = None,
6970
limit: int = 0,
7071
conn: Optional[BaseDBAsyncClient] = None,
@@ -74,6 +75,7 @@ async def select_custom_fields(
7475
fields,
7576
wheres,
7677
groups,
78+
havings,
7779
orders,
7880
limit,
7981
)

fastapi_esql/utils/sqlizer.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ def __init__(self, sql: str):
1818

1919
class SQLizer:
2020

21+
@classmethod
22+
def resolve_condition(cls, conditions: List[str]) -> str:
23+
return " AND ".join(conditions)
24+
2125
@classmethod
2226
def resolve_ordering(cls, orderings: List[str]) -> str:
2327
orders = []
@@ -57,17 +61,21 @@ def select_custom_fields(
5761
fields: List[str],
5862
wheres: List[str],
5963
groups: Optional[List[str]] = None,
64+
havings: Optional[List[str]] = None,
6065
orders: Optional[List[str]] = None,
6166
limit: int = 0,
6267
) -> Optional[str]:
6368
if not all([table, fields, wheres]):
6469
raise WrongParamsError("Please check your params")
70+
if havings and not groups:
71+
raise WrongParamsError("Please check your params")
6572

6673
group_by = f" GROUP BY {', '.join(groups)}" if groups else ""
74+
having = f" HAVING {cls.resolve_condition(havings)}" if havings else ""
6775
order_by = f" ORDER BY {cls.resolve_ordering(orders)}" if orders else ""
6876
limit_ = f" LIMIT {limit}" if limit else ""
6977
# NOTE Doesn't support `offset` parameter due to it's bad performance
70-
extras = [group_by, order_by, limit_]
78+
extras = [group_by, having, order_by, limit_]
7179

7280
sql = """
7381
SELECT
@@ -77,7 +85,7 @@ def select_custom_fields(
7785
{}""".format(
7886
", ".join(fields),
7987
table,
80-
" AND ".join(wheres),
88+
cls.resolve_condition(wheres),
8189
"\n".join(extras) if extras else "",
8290
)
8391
logger.debug(sql)
@@ -102,7 +110,7 @@ def upsert_json_field(
102110
sql = f"""
103111
UPDATE {table}
104112
SET {json_field} = JSON_SET(COALESCE({json_field}, '{{}}'), {", ".join(params)})
105-
WHERE {" AND ".join(wheres)}"""
113+
WHERE {cls.resolve_condition(wheres)}"""
106114
logger.debug(sql)
107115
return sql
108116

@@ -158,7 +166,7 @@ def insert_into_select(
158166
({", ".join(fields)})
159167
SELECT {", ".join(remain_fields + assign_fields)}
160168
FROM {table}
161-
WHERE {" AND ".join(wheres)}"""
169+
WHERE {cls.resolve_condition(wheres)}"""
162170
logger.debug(sql)
163171
return sql
164172

0 commit comments

Comments
 (0)