feat: 采集可以按字段名称查找对应的数据表
This commit is contained in:
@@ -247,6 +247,42 @@ def _column_form(columns: list[dict]) -> dict:
|
||||
return form
|
||||
|
||||
|
||||
async def search_by_field(client: NqiClient, keyword: str) -> list[dict]:
|
||||
"""按字段中文名、英文名或字段分类,找出包含它的表。"""
|
||||
text = keyword.strip()
|
||||
if not text or len(text) > 80 or any(ord(char) < 32 for char in text):
|
||||
raise ValueError("请输入字段名称")
|
||||
await ensure_session(client)
|
||||
payload = await _post_json(
|
||||
client,
|
||||
f"{PAGE}/search",
|
||||
{"key": text, "field": ["columnname_cn", "columnname", "fieldtype"]},
|
||||
)
|
||||
rows = payload.get("CFG_ADHOC_CONF_SEARCH") if isinstance(payload, dict) else None
|
||||
if not isinstance(rows, list):
|
||||
raise NqiError("没有查到相关的表")
|
||||
grouped: dict[str, dict] = {}
|
||||
order: list[str] = []
|
||||
for row in rows:
|
||||
if not isinstance(row, dict):
|
||||
continue
|
||||
tablename = str(row.get("tablename") or "").strip()
|
||||
field_name = str(row.get("columnname_cn") or row.get("columnname") or "").strip()
|
||||
if not tablename or not field_name:
|
||||
continue
|
||||
item = grouped.get(tablename)
|
||||
if item is None:
|
||||
title = str(row.get("tablename_cn") or tablename).strip() or tablename
|
||||
item = {"tablename": tablename, "name": title, "fields": []}
|
||||
grouped[tablename] = item
|
||||
order.append(tablename)
|
||||
column = str(row.get("columnname") or "").strip()
|
||||
if any(existing["column"] == column and existing["name"] == field_name for existing in item["fields"]):
|
||||
continue
|
||||
item["fields"].append({"name": field_name, "column": column, "type": str(row.get("datatype") or "").strip()})
|
||||
return [grouped[name] for name in order]
|
||||
|
||||
|
||||
async def table_fields(client: NqiClient, tablename: str) -> list[dict]:
|
||||
await ensure_session(client)
|
||||
rows = await _columns(client, tablename)
|
||||
|
||||
Reference in New Issue
Block a user