Skip to content

Commit 75508c4

Browse files
committed
feat: enable keyword-only arguments for the main methods
1 parent 3eb6ca0 commit 75508c4

File tree

5 files changed

+53
-40
lines changed

5 files changed

+53
-40
lines changed

examples/service/routers/account.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -112,15 +112,16 @@ async def query_last_login_view(
112112
aids: List[int] = Body(..., embed=True),
113113
):
114114
dicts = await AccountMgr.select_custom_fields(
115-
fields=[
115+
[
116116
"id", "extend ->> '$.last_login.ipv4' ipv4",
117117
"extend ->> '$.last_login.start_datetime' start_datetime",
118118
"CAST(extend ->> '$.last_login.online_sec' AS SIGNED) online_sec"
119119
],
120-
wheres=f"id IN ({','.join(map(str, aids))}) AND gender=1",
120+
f"id IN ({','.join(map(str, aids))}) AND gender=1",
121121
# wheres=Q(Q(id__in=aids), Q(gender=1), join_type="AND"),
122122
# wheres={"id__in": aids, "gender": 1},
123123
# wheres=[Q(id__in=aids), Q(gender=1)],
124+
limit=1
124125
)
125126
return {"dicts": dicts}
126127

@@ -181,14 +182,14 @@ async def bulk_clone_view(
181182
):
182183
ok = await AccountMgr.insert_into_select(
183184
wheres=Q(id__in=aids),
184-
remain_fields=["gender"],
185+
# remain_fields=["gender"],
185186
assign_field_dict={
186187
"locale": Cases("id", {3: LocaleEnum.zh_CN, 4: LocaleEnum.en_US, 5: LocaleEnum.fr_FR}, default=""),
187188
"active": False,
188189
"name": RawSQL("CONCAT(LEFT(name, 26), ' [NEW]')"),
189190
"extend": {},
190191
},
191-
to_table="account_bak",
192+
# to_table="account_bak",
192193
)
193194
return {"ok": ok}
194195

fastapi_esql/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
wrap_backticks,
2424
)
2525

26-
__version__ = "0.0.11"
26+
__version__ = "0.0.12"
2727

2828
__all__ = [
2929
"QsParsingError",

fastapi_esql/orm/base_manager.py

+37-32
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,8 @@ async def get_by_pk(
2525

2626
@classmethod
2727
async def create_from_dict(cls, params: Dict[str, Any]) -> Optional[Model]:
28-
params["using_db"] = cls.rw_conn
2928
try:
30-
return await cls.model.create(**params)
29+
return await cls.model.create(**params, using_db=cls.rw_conn)
3130
except Exception as e:
3231
logger.exception(e)
3332
return None
@@ -63,6 +62,7 @@ async def select_custom_fields(
6362
cls,
6463
fields: List[str],
6564
wheres: Union[str, Q, Dict[str, Any], List[Q]],
65+
*,
6666
index: Optional[str] = None,
6767
groups: Optional[List[str]] = None,
6868
having: Optional[str] = None,
@@ -76,13 +76,13 @@ async def select_custom_fields(
7676
cls.table,
7777
fields,
7878
wheres,
79-
index,
80-
groups,
81-
having,
82-
orders,
83-
offset,
84-
limit,
85-
cls.model,
79+
index=index,
80+
groups=groups,
81+
having=having,
82+
orders=orders,
83+
offset=offset,
84+
limit=limit,
85+
model=cls.model,
8686
)
8787
conn = conn or cls.ro_conn
8888
converters = {
@@ -96,6 +96,7 @@ async def select_one_record(
9696
cls,
9797
fields: List[str],
9898
wheres: Union[str, Q, Dict[str, Any], List[Q]],
99+
*,
99100
index: Optional[str] = None,
100101
groups: Optional[List[str]] = None,
101102
having: Optional[str] = None,
@@ -107,13 +108,13 @@ async def select_one_record(
107108
cls.table,
108109
fields,
109110
wheres,
110-
index,
111-
groups,
112-
having,
113-
orders,
114-
0,
115-
1,
116-
cls.model,
111+
index=index,
112+
groups=groups,
113+
having=having,
114+
orders=orders,
115+
offset=0,
116+
limit=1,
117+
model=cls.model,
117118
)
118119
conn = conn or cls.ro_conn
119120
converters = {
@@ -127,6 +128,7 @@ async def update_json_field(
127128
cls,
128129
json_field: str,
129130
wheres: Union[str, Q, Dict[str, Any], List[Q]],
131+
*,
130132
merge_dict: Optional[Dict[str, Any]] = None,
131133
path_value_dict: Optional[Dict[str, Any]] = None,
132134
remove_paths: Optional[List[str]] = None,
@@ -136,11 +138,11 @@ async def update_json_field(
136138
cls.table,
137139
json_field,
138140
wheres,
139-
merge_dict,
140-
path_value_dict,
141-
remove_paths,
142-
json_type,
143-
cls.model,
141+
merge_dict=merge_dict,
142+
path_value_dict=path_value_dict,
143+
remove_paths=remove_paths,
144+
json_type=json_type,
145+
model=cls.model,
144146
)
145147
return await CursorHandler.sum_row_cnt(sql, cls.rw_conn, logger)
146148

@@ -149,6 +151,7 @@ async def upsert_on_duplicate(
149151
cls,
150152
dicts: List[Dict[str, Any]],
151153
insert_fields: List[str],
154+
*,
152155
upsert_fields: Optional[List[str]] = None,
153156
merge_fields: Optional[List[str]] = None,
154157
using_values: bool = False,
@@ -157,27 +160,28 @@ async def upsert_on_duplicate(
157160
cls.table,
158161
dicts,
159162
insert_fields,
160-
upsert_fields,
161-
merge_fields,
162-
using_values,
163+
upsert_fields=upsert_fields,
164+
merge_fields=merge_fields,
165+
using_values=using_values,
163166
)
164167
return await CursorHandler.sum_row_cnt(sql, cls.rw_conn, logger)
165168

166169
@classmethod
167170
async def insert_into_select(
168171
cls,
169172
wheres: Union[str, Q, Dict[str, Any], List[Q]],
170-
remain_fields: List[str],
171-
assign_field_dict: Dict[str, Any],
173+
*,
174+
remain_fields: List[str] = None,
175+
assign_field_dict: Dict[str, Any] = None,
172176
to_table: Optional[str] = None,
173177
):
174178
sql = SQLizer.insert_into_select(
175179
cls.table,
176180
wheres,
177-
remain_fields,
178-
assign_field_dict,
179-
to_table,
180-
cls.model,
181+
remain_fields=remain_fields,
182+
assign_field_dict=assign_field_dict,
183+
to_table=to_table,
184+
model=cls.model,
181185
)
182186
return await CursorHandler.exec_if_ok(sql, cls.rw_conn, logger)
183187

@@ -187,6 +191,7 @@ async def bulk_update_from_dicts(
187191
dicts: List[Dict[str, Any]],
188192
join_fields: List[str],
189193
update_fields: List[str],
194+
*,
190195
merge_fields: Optional[List[str]] = None,
191196
using_values: bool = True,
192197
):
@@ -195,7 +200,7 @@ async def bulk_update_from_dicts(
195200
dicts,
196201
join_fields,
197202
update_fields,
198-
merge_fields,
199-
using_values,
203+
merge_fields=merge_fields,
204+
using_values=using_values,
200205
)
201206
return await CursorHandler.sum_row_cnt(sql, cls.rw_conn, logger)

fastapi_esql/utils/sqlizer.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ def select_custom_fields(
115115
table: str,
116116
fields: List[str],
117117
wheres: Union[str, Q, Dict[str, Any], List[Q]],
118+
*,
118119
index: Optional[str] = None,
119120
groups: Optional[List[str]] = None,
120121
having: Optional[str] = None,
@@ -156,6 +157,7 @@ def update_json_field(
156157
table: str,
157158
json_field: str,
158159
wheres: Union[str, Q, Dict[str, Any], List[Q]],
160+
*,
159161
merge_dict: Optional[Dict[str, Any]] = None,
160162
path_value_dict: Optional[Dict[str, Any]] = None,
161163
remove_paths: Optional[List[str]] = None,
@@ -196,6 +198,7 @@ def upsert_on_duplicate(
196198
table: str,
197199
dicts: List[Dict[str, Any]],
198200
insert_fields: List[str],
201+
*,
199202
upsert_fields: Optional[List[str]] = None,
200203
merge_fields: Optional[List[str]] = None,
201204
using_values: bool = False,
@@ -246,8 +249,9 @@ def insert_into_select(
246249
cls,
247250
table: str,
248251
wheres: Union[str, Q, Dict[str, Any], List[Q]],
249-
remain_fields: List[str],
250-
assign_field_dict: Dict[str, Any],
252+
*,
253+
remain_fields: List[str] = None,
254+
assign_field_dict: Dict[str, Any] = None,
251255
to_table: Optional[str] = None,
252256
model: Optional[Model] = None,
253257
) -> Optional[str]:
@@ -256,6 +260,8 @@ def insert_into_select(
256260
if not any([remain_fields, assign_field_dict]):
257261
raise WrongParamsError("At least one no empty parameter is required between `remain_fields` and `assign_field_dict`")
258262

263+
remain_fields = remain_fields or []
264+
assign_field_dict = assign_field_dict or {}
259265
fields = [*remain_fields]
260266
assign_fields = []
261267
for k, v in assign_field_dict.items():
@@ -313,6 +319,7 @@ def bulk_update_from_dicts(
313319
dicts: List[Dict[str, Any]],
314320
join_fields: List[str],
315321
update_fields: List[str],
322+
*,
316323
merge_fields: Optional[List[str]] = None,
317324
using_values: bool = True,
318325
) -> Optional[str]:

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "fastapi-efficient-sql"
3-
version = "0.0.11"
3+
version = "0.0.12"
44
description = "Generate bulk DML SQL and execute them based on Tortoise ORM and mysql8.0+, and integrated with FastAPI."
55
authors = ["BryanLee <[email protected]>"]
66
keywords = ["sql", "fastapi", "tortoise-orm", "mysql8", "bulk-operation"]

0 commit comments

Comments
 (0)