Showing
1 changed file
with
11 additions
and
4 deletions
| @@ -31,6 +31,7 @@ from typing import List, Dict, Any, Optional, Literal | @@ -31,6 +31,7 @@ from typing import List, Dict, Any, Optional, Literal | ||
| 31 | from dataclasses import dataclass, field | 31 | from dataclasses import dataclass, field |
| 32 | from ..utils.db import fetch_all | 32 | from ..utils.db import fetch_all |
| 33 | from datetime import datetime, timedelta, date | 33 | from datetime import datetime, timedelta, date |
| 34 | +from InsightEngine.utils.config import settings | ||
| 34 | 35 | ||
| 35 | # --- 1. 数据结构定义 --- | 36 | # --- 1. 数据结构定义 --- |
| 36 | 37 | ||
| @@ -183,6 +184,12 @@ class MediaCrawlerDB: | @@ -183,6 +184,12 @@ class MediaCrawlerDB: | ||
| 183 | formatted_results = [QueryResult(platform=r['p'], content_type=r['t'], title_or_content=r['title'], author_nickname=r.get('author'), url=r['url'], publish_time=self._to_datetime(r['ts']), engagement=self._extract_engagement(r), hotness_score=r.get('hotness_score', 0.0), source_keyword=r.get('source_keyword'), source_table=r['tbl']) for r in raw_results] | 184 | formatted_results = [QueryResult(platform=r['p'], content_type=r['t'], title_or_content=r['title'], author_nickname=r.get('author'), url=r['url'], publish_time=self._to_datetime(r['ts']), engagement=self._extract_engagement(r), hotness_score=r.get('hotness_score', 0.0), source_keyword=r.get('source_keyword'), source_table=r['tbl']) for r in raw_results] |
| 184 | return DBResponse("search_hot_content", params_for_log, results=formatted_results, results_count=len(formatted_results)) | 185 | return DBResponse("search_hot_content", params_for_log, results=formatted_results, results_count=len(formatted_results)) |
| 185 | 186 | ||
| 187 | + def _wrap_query_field_with_dialect(self, field: str) -> str: | ||
| 188 | + """根据数据库方言包装SQL查询""" | ||
| 189 | + if settings.DB_DIALECT == 'postgresql': | ||
| 190 | + return f'"{field}"' | ||
| 191 | + return f'`{field}`' | ||
| 192 | + | ||
| 186 | def search_topic_globally(self, topic: str, limit_per_table: int = 100) -> DBResponse: | 193 | def search_topic_globally(self, topic: str, limit_per_table: int = 100) -> DBResponse: |
| 187 | """ | 194 | """ |
| 188 | 【工具】全局话题搜索: 在数据库中(内容、评论、标签、来源关键字)全面搜索指定话题。 | 195 | 【工具】全局话题搜索: 在数据库中(内容、评论、标签、来源关键字)全面搜索指定话题。 |
| @@ -205,11 +212,11 @@ class MediaCrawlerDB: | @@ -205,11 +212,11 @@ class MediaCrawlerDB: | ||
| 205 | where_clauses = [] | 212 | where_clauses = [] |
| 206 | for idx, field in enumerate(config['fields']): | 213 | for idx, field in enumerate(config['fields']): |
| 207 | pname = f"term_{idx}" | 214 | pname = f"term_{idx}" |
| 208 | - where_clauses.append(f'"{field}" LIKE :{pname}') | 215 | + where_clauses.append(f'{self._wrap_query_field_with_dialect(field)} LIKE :{pname}') |
| 209 | param_dict[pname] = search_term | 216 | param_dict[pname] = search_term |
| 210 | param_dict['limit'] = limit_per_table | 217 | param_dict['limit'] = limit_per_table |
| 211 | where_clause = " OR ".join(where_clauses) | 218 | where_clause = " OR ".join(where_clauses) |
| 212 | - query = f'SELECT * FROM "{table}" WHERE {where_clause} ORDER BY id DESC LIMIT :limit' | 219 | + query = f'SELECT * FROM {self._wrap_query_field_with_dialect(table)} WHERE {where_clause} ORDER BY id DESC LIMIT :limit' |
| 213 | raw_results = self._execute_query(query, param_dict) | 220 | raw_results = self._execute_query(query, param_dict) |
| 214 | for row in raw_results: | 221 | for row in raw_results: |
| 215 | content = (row.get('title') or row.get('content') or row.get('desc') or row.get('content_text', '')) | 222 | content = (row.get('title') or row.get('content') or row.get('desc') or row.get('content_text', '')) |
| @@ -260,11 +267,11 @@ class MediaCrawlerDB: | @@ -260,11 +267,11 @@ class MediaCrawlerDB: | ||
| 260 | where_clauses = [] | 267 | where_clauses = [] |
| 261 | for idx, field in enumerate(config['fields']): | 268 | for idx, field in enumerate(config['fields']): |
| 262 | pname = f"term_{idx}" | 269 | pname = f"term_{idx}" |
| 263 | - where_clauses.append(f'"{field}" LIKE :{pname}') | 270 | + where_clauses.append(f'{self._wrap_query_field_with_dialect(field)} LIKE :{pname}') |
| 264 | param_dict[pname] = search_term | 271 | param_dict[pname] = search_term |
| 265 | param_dict['limit'] = limit_per_table | 272 | param_dict['limit'] = limit_per_table |
| 266 | where_clause = ' OR '.join(where_clauses) | 273 | where_clause = ' OR '.join(where_clauses) |
| 267 | - query = f'SELECT * FROM "{table}" WHERE {where_clause} ORDER BY id DESC LIMIT :limit' | 274 | + query = f'SELECT * FROM {self._wrap_query_field_with_dialect(table)} WHERE {where_clause} ORDER BY id DESC LIMIT :limit' |
| 268 | raw_results = self._execute_query(query, param_dict) | 275 | raw_results = self._execute_query(query, param_dict) |
| 269 | for row in raw_results: | 276 | for row in raw_results: |
| 270 | content = (row.get('title') or row.get('content') or row.get('desc') or row.get('content_text', '')) | 277 | content = (row.get('title') or row.get('content') or row.get('desc') or row.get('content_text', '')) |
-
Please register or login to post a comment