Skip to content

Commit

Permalink
PG类型问题
Browse files Browse the repository at this point in the history
  • Loading branch information
feiazifeiazi committed Oct 11, 2024
1 parent cbd3a56 commit 7c8b3a7
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion sql/engines/pgsql.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
@file: pgsql.py
@time: 2019/03/29
"""
import json
import re
import psycopg2
import logging
Expand Down Expand Up @@ -204,8 +205,28 @@ def query(
rows = cursor.fetchall()
fields = cursor.description

column_type_codes = [i[1] for i in fields] if fields else []
# 定义 JSON 和 JSONB 的 type_code,# 114 是 json,3802 是 jsonb
JSON_TYPE_CODE = 114
JSONB_TYPE_CODE = 3802
# 对 rows 进行循环处理,判断是否是 jsonb 或 json 类型
converted_rows = []
for row in rows:
new_row = []
for idx, col_value in enumerate(row):
column_type_code = column_type_codes[idx]
# 只在列类型为 json 或 jsonb 时转换
if column_type_code in [JSON_TYPE_CODE, JSONB_TYPE_CODE]:
if isinstance(col_value, (dict, list)):
new_row.append(json.dumps(col_value)) # 转为 JSON 字符串
else:
new_row.append(col_value)
else:
new_row.append(col_value)
converted_rows.append(tuple(new_row))

result_set.column_list = [i[0] for i in fields] if fields else []
result_set.rows = rows
result_set.rows = converted_rows
result_set.affected_rows = effect_row
except Exception as e:
logger.warning(
Expand Down

0 comments on commit 7c8b3a7

Please sign in to comment.