This commit is contained in:
HuangHai
2026-01-21 08:41:47 +08:00
parent 306a3a4127
commit e51dc18d06
40 changed files with 1309 additions and 965 deletions

View File

@@ -444,7 +444,15 @@ class Db:
pass
# 检查是否为SQL模板名称并处理
if self._is_sql_template(sql):
# 即使不是模板名称,如果包含点号,我们也尝试在加载后检查它是否真的是模板
is_template = self._is_sql_template(sql)
if not is_template and '.' in sql:
# 如果包含点号但没在映射中找到,可能是因为缓存加载问题,尝试重新加载映射
if hasattr(self.sql_loader, '_build_template_map'):
self.sql_loader._build_template_map()
is_template = self._is_sql_template(sql)
if is_template:
try:
logger.debug(f"确认为SQL模板: {sql}")
@@ -620,7 +628,14 @@ class Db:
logger.debug(f"处理后的页码: {page_number}, 每页大小: {page_size}")
# 使用_is_sql_template方法检查并获取SQL模板
if self._is_sql_template(sql):
is_template = self._is_sql_template(sql)
if not is_template and '.' in sql:
# 如果包含点号但没在映射中找到,可能是因为缓存加载问题,尝试重新加载映射
if hasattr(self.sql_loader, '_build_template_map'):
self.sql_loader._build_template_map()
is_template = self._is_sql_template(sql)
if is_template:
try:
logger.debug(f"确认为SQL模板: {sql}")
@@ -840,6 +855,27 @@ class Db:
# 如果所有策略都失败,返回默认查询
return "SELECT COUNT(*)"
async def check_column_exists(self, table_name, column_name):
"""检查表中是否存在指定列
Args:
table_name: 表名
column_name: 列名
Returns:
bool: 如果列存在返回True,否则返回False
"""
try:
# 兼容 Doris 和 MySQL 的语法
sql = f"SHOW COLUMNS FROM {table_name} LIKE :column_name"
params = {"column_name": column_name}
# 使用 find 方法执行查询
result = await self.find(sql, params)
return len(result) > 0
except Exception as e:
logger.debug(f"检查列是否存在时出错 (可能表不存在): {str(e)}")
return False
@db_retry()
async def execute_update(self, sql, params=None, session=None):
"""执行SQL更新操作(插入、更新、删除)(异步版本)

57
DbKit/Sql/DouYin.sql Normal file
View File

@@ -0,0 +1,57 @@
#namespace("DouYin")
#sql("updateStatus")
UPDATE t_douyin_record
SET status = #para(status)
#if(error_msg)
, error_msg = #para(error_msg)
#end
WHERE id = #para(id)
#end
#sql("updateRecord")
UPDATE t_douyin_record
SET video_name = #para(title),
obs_url = #para(obs_url),
transcript = #para(transcript),
status = #para(status)
WHERE id = #para(id)
#end
#sql("insertRecord")
INSERT INTO t_douyin_record (id, original_text, status, create_time)
VALUES (#para(id), #para(url), 'PENDING', #para(create_time))
#end
#sql("getRecords")
SELECT * FROM t_douyin_record
ORDER BY create_time DESC
LIMIT #para(limit)
#end
#sql("deleteRecord")
DELETE FROM t_douyin_record WHERE id = #para(id)
#end
#sql("getTranscriptsByIds")
SELECT video_name, transcript
FROM t_douyin_record
WHERE id IN (#para(ids))
AND status = 'COMPLETED'
#end
#sql("getLatestTranscripts")
SELECT video_name, transcript
FROM t_douyin_record
WHERE status = 'COMPLETED'
ORDER BY create_time DESC
LIMIT #para(limit)
#end
#sql("getInterruptedTasks")
SELECT id, original_text, status
FROM t_douyin_record
WHERE status IN ('PENDING', 'PROCESSING')
ORDER BY create_time DESC
LIMIT #para(limit)
#end
#end

12
DbKit/Sql/HaiBao.sql Normal file
View File

@@ -0,0 +1,12 @@
#namespace("HaiBao")
#sql("insertHistory")
INSERT INTO haibao_history (id, prompt, image_url, scheme_content, created_at)
VALUES (#para(id), #para(prompt), #para(image_url), #para(scheme_content), #para(created_at))
#end
#sql("getHistory")
SELECT * FROM haibao_history
ORDER BY created_at DESC
LIMIT #para(limit)
#end
#end

View File

@@ -0,0 +1,79 @@
#namespace("YltAnalytics")
#sql("getOperatorsPriceTrends")
SELECT
p.operator,
DATE(sc.valid_start_time) as date_str,
sc.schedule_json
FROM t_station_price_schedule_scd sc
JOIN t_station_profile_scd p ON sc.station_hash = p.station_hash AND p.is_current = 1
WHERE sc.valid_start_time >= DATE_SUB(CURDATE(), INTERVAL #para(days) DAY)
ORDER BY date_str ASC
#end
#sql("listYltStations")
SELECT
p.station_hash,
p.operator,
p.station_name,
p.address,
p.coord_x,
p.coord_y,
s.current_price
FROM t_station_profile_scd p
LEFT JOIN t_station_status_scd s
ON p.station_hash = s.station_hash AND s.is_current = 1
WHERE p.operator = '驿来特'
#if(q)
AND (p.station_name LIKE #para(kw) OR p.address LIKE #para(kw))
#end
ORDER BY p.station_name
#end
#sql("fetchCurrentStations")
SELECT
p.station_hash,
p.operator,
p.station_name,
p.address,
p.coord_x,
p.coord_y,
s.current_price
FROM t_station_profile_scd p
LEFT JOIN t_station_status_scd s
ON p.station_hash = s.station_hash AND s.is_current = 1
WHERE p.is_current = 1
#end
#sql("fetchStationScheduleJson")
SELECT sc.schedule_json, p.operator
FROM t_station_price_schedule_scd sc
JOIN t_station_profile_scd p ON sc.station_hash = p.station_hash AND p.is_current = 1
WHERE sc.station_hash = #para(h) AND sc.is_current = 1
ORDER BY sc.valid_start_time DESC
LIMIT 1
#end
#sql("fetchCurrentStationRows")
SELECT
p.station_hash,
p.station_name,
p.address,
p.coord_x,
p.coord_y,
s.total_piles AS total_guns,
s.free_piles AS free_guns,
s.current_price,
s.pro_price,
s.parking_info,
s.distance,
s.valid_start_time AS status_update_time,
sc.schedule_json,
sc.valid_start_time AS schedule_update_time,
s.piles_detail_json
FROM t_station_profile_scd p
LEFT JOIN t_station_status_scd s ON p.station_hash = s.station_hash AND s.is_current = 1
LEFT JOIN t_station_price_schedule_scd sc ON p.station_hash = sc.station_hash AND sc.is_current = 1
WHERE p.operator = #para(op) AND p.is_current = 1
ORDER BY p.station_name ASC
#end
#end

View File

@@ -489,6 +489,10 @@ class SqlTemplateLoader:
template_map_str = await redisKit.get_data(REDIS_SQL_TEMPLATE_MAP_KEY)
if template_map_str:
self._template_map = json.loads(template_map_str)
# 无论从Redis加载了什么,都尝试重新构建一次映射以确保一致性
if self.templates and not self._template_map:
self._build_template_map()
except Exception as e:
logger.error(f"从Redis加载SQL模板数据失败: {str(e)}")

Binary file not shown.