Skip to content

Commit

Permalink
sprintfix: 修复参数化查询失效的问题 (fixed #1556)
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhuoZhuoCrayon committed Jul 12, 2023
1 parent e5698e8 commit 07d9e90
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 29 deletions.
23 changes: 10 additions & 13 deletions apps/core/ipchooser/tools/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ def fuzzy_cond(

@staticmethod
def handle_plugin_conditions(
params: typing.Dict, plugin_names: typing.List[str], return_sql: bool = True
) -> typing.Union[typing.List[int], str]:
params: typing.Dict, plugin_names: typing.List[str]
) -> typing.Tuple[bool, typing.Optional[QuerySet]]:

if not params.get("conditions"):
return []
return False, None

select: typing.Dict[str, str] = {
"status": f"{node_man_models.ProcessStatus._meta.db_table}.status",
Expand All @@ -95,7 +95,7 @@ def handle_plugin_conditions(

if condition["key"] in ["source_id", "plugin_name"]:
key: str = {"source_id": "source_id", "plugin_name": "name"}[condition["key"]]
sql_params.extend([f'"{cond_val}"' for cond_val in values])
sql_params.extend(values)
wheres.append(
f'{node_man_models.ProcessStatus._meta.db_table}.{key} in ({",".join(["%s"] * len(values))})'
)
Expand All @@ -105,9 +105,9 @@ def handle_plugin_conditions(
for cond_val in values:
if cond_val == -1:
# 无版本插件筛选
sql_params.append('""')
sql_params.append("")
else:
sql_params.append(f'"{cond_val}"')
sql_params.append(cond_val)
wheres.append(
f'{node_man_models.ProcessStatus._meta.db_table}.version in ({",".join(["%s"] * len(values))})'
)
Expand All @@ -116,7 +116,7 @@ def handle_plugin_conditions(

elif condition["key"] in [f"{plugin}_status" for plugin in plugin_names]:
# 插件状态的精确搜索
sql_params.extend([f'"{cond_val}"' for cond_val in values])
sql_params.extend(values)
wheres.append(
f'{node_man_models.ProcessStatus._meta.db_table}.status in ({",".join(["%s"] * len(values))})'
)
Expand All @@ -126,7 +126,7 @@ def handle_plugin_conditions(

if wheres:
wheres = init_wheres + wheres
host_queryset: QuerySet = (
host_id_queryset: QuerySet = (
node_man_models.Host.objects.extra(
select=select,
tables=[node_man_models.ProcessStatus._meta.db_table],
Expand All @@ -136,11 +136,8 @@ def handle_plugin_conditions(
.order_by()
.values_list("bk_host_id", flat=True)
)
if return_sql:
return host_queryset.query
else:
return set(host_queryset) or [-1]
return []
return True, host_id_queryset
return False, None

@staticmethod
def fetch_match_node_types(is_proxy: bool, return_all_node_type: bool) -> typing.List[str]:
Expand Down
29 changes: 13 additions & 16 deletions apps/node_man/handlers/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,19 +193,19 @@ def list(params: Dict[str, Any]):
user_biz = plugin_operate_bizs

# 查询
sql: str = HostQuerySqlHelper.handle_plugin_conditions(
params=params, plugin_names=tools.PluginV2Tools.fetch_head_plugins(), return_sql=True
need_query, host_id_queryset = HostQuerySqlHelper.handle_plugin_conditions(
params=params, plugin_names=tools.PluginV2Tools.fetch_head_plugins()
)
extra_wheres = []
if sql:
extra_wheres = [f"{Host._meta.db_table}.bk_host_id in ({sql})"]

hosts_status_sql = (
HostQuerySqlHelper.multiple_cond_sql(
params=params, biz_scope=user_biz, return_all_node_type=True, extra_wheres=extra_wheres
).exclude(bk_host_id__in=params.get("exclude_hosts", []))
HostQuerySqlHelper.multiple_cond_sql(params=params, biz_scope=user_biz, return_all_node_type=True).exclude(
bk_host_id__in=params.get("exclude_hosts", [])
)
).order_by()

if need_query:
hosts_status_sql = hosts_status_sql.filter(bk_host_id__in=host_id_queryset)

# 计算总数

try:
Expand Down Expand Up @@ -312,19 +312,16 @@ def operate(params: dict, username: str):

if params.get("exclude_hosts") is not None:
# 跨页全选
sql: str = HostQuerySqlHelper.handle_plugin_conditions(
params=params, plugin_names=tools.PluginV2Tools.fetch_head_plugins(), return_sql=True
need_query, host_id_queryset = HostQuerySqlHelper.handle_plugin_conditions(
params=params, plugin_names=tools.PluginV2Tools.fetch_head_plugins()
)
extra_wheres = []
if sql:
extra_wheres = [f"{Host._meta.db_table}.bk_host_id in ({sql})"]
db_host_sql = (
HostQuerySqlHelper.multiple_cond_sql(
params, user_biz, return_all_node_type=True, extra_wheres=extra_wheres
)
HostQuerySqlHelper.multiple_cond_sql(params, user_biz, return_all_node_type=True)
.exclude(bk_host_id__in=params.get("exclude_hosts", []))
.values("bk_host_id", "bk_biz_id", "bk_cloud_id", "inner_ip", "node_type", "os_type")
)
if need_query:
db_host_sql = db_host_sql.filter(bk_host_id__in=host_id_queryset)

else:
# 不是跨页全选
Expand Down

0 comments on commit 07d9e90

Please sign in to comment.