From 165ad823f5eec26da925b4496d063b76d88751e9 Mon Sep 17 00:00:00 2001 From: Zixiao Wang Date: Wed, 4 Jun 2025 15:09:17 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E8=BF=87=E6=BB=A4sql?= =?UTF-8?q?=E7=9A=84=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/expertproducts/views.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/apps/expertproducts/views.py b/apps/expertproducts/views.py index 93ac9c1..189078d 100644 --- a/apps/expertproducts/views.py +++ b/apps/expertproducts/views.py @@ -849,11 +849,15 @@ class CreatorSQLSearchAPI(APIView): return Response(response_data) def _extract_sql(self, text): - # 先去除 ... 内容 import re - text = re.sub(r".*?\s*", "", text, flags=re.DOTALL) - # 再提取SQL语句 - match = re.search(r"select[\s\S]+?;", text, re.IGNORECASE) + # 优先提取```sql ... ```代码块 + codeblock = re.search(r"```sql\s*([\s\S]+?)\s*```", text, re.IGNORECASE) + if codeblock: + sql = codeblock.group(1).strip() + return sql + # 回退:匹配以SELECT开头,后面跟任意内容(多行) + match = re.search(r"(?i)(SELECT[\s\S]*)", text) if match: - return match.group() + sql = match.group(1).strip() + return sql return text.strip()