33import os
44import platform
55import urllib.parse
6- from datetime import timedelta
6+ from datetime import datetime, date, time, timedelta
77from decimal import Decimal
88from typing import Optional
99
1010import oracledb
1111import psycopg2
1212import pymssql
13- import re
1413
1514from apps.db.db_sql import get_table_sql, get_field_sql, get_version_sql
1615from common.error import ParseSQLResultError
@@ -324,11 +323,14 @@ def get_schema(ds: CoreDatasource):
324323 with get_session(ds) as session:
325324 sql: str = ''
326325 if equals_ignore_case(ds.type, "sqlServer"):
327- sql = """select name from sys.schemas"""
326+ sql = """select name
327+ from sys.schemas"""
328328 elif equals_ignore_case(ds.type, "pg", "excel"):
329- sql = """SELECT nspname FROM pg_namespace"""
329+ sql = """SELECT nspname
330+ FROM pg_namespace"""
330331 elif equals_ignore_case(ds.type, "oracle"):
331- sql = """select * from all_users"""
332+ sql = """select *
333+ from all_users"""
332334 with session.execute(text(sql)) as result:
333335 res = result.fetchall()
334336 res_list = [item[0] for item in res]
@@ -338,15 +340,18 @@ def get_schema(ds: CoreDatasource):
338340 if equals_ignore_case(ds.type, 'dm'):
339341 with dmPython.connect(user=conf.username, password=conf.password, server=conf.host,
340342 port=conf.port, **extra_config_dict) as conn, conn.cursor() as cursor:
341- cursor.execute("""select OBJECT_NAME from dba_objects where object_type='SCH'""", timeout=conf.timeout)
343+ cursor.execute("""select OBJECT_NAME
344+ from dba_objects
345+ where object_type = 'SCH'""", timeout=conf.timeout)
342346 res = cursor.fetchall()
343347 res_list = [item[0] for item in res]
344348 return res_list
345349 elif equals_ignore_case(ds.type, 'redshift'):
346350 with redshift_connector.connect(host=conf.host, port=conf.port, database=conf.database, user=conf.username,
347351 password=conf.password,
348352 timeout=conf.timeout, **extra_config_dict) as conn, conn.cursor() as cursor:
349- cursor.execute("""SELECT nspname FROM pg_namespace""")
353+ cursor.execute("""SELECT nspname
354+ FROM pg_namespace""")
350355 res = cursor.fetchall()
351356 res_list = [item[0] for item in res]
352357 return res_list
@@ -355,7 +360,8 @@ def get_schema(ds: CoreDatasource):
355360 password=conf.password,
356361 options=f"-c statement_timeout={conf.timeout * 1000}",
357362 **extra_config_dict) as conn, conn.cursor() as cursor:
358- cursor.execute("""SELECT nspname FROM pg_namespace""")
363+ cursor.execute("""SELECT nspname
364+ FROM pg_namespace""")
359365 res = cursor.fetchall()
360366 res_list = [item[0] for item in res]
361367 return res_list
@@ -463,8 +469,16 @@ def get_fields(ds: CoreDatasource, table_name: str = None):
463469 return res_list
464470
465471
466- def convert_value(value):
467- """转换值为JSON可序列化的类型"""
472+ def convert_value(value, datetime_format='space'):
473+ """
474+ 将Python值转换为JSON可序列化的类型
475+
476+ :param value: 要转换的值
477+ :param datetime_format: 日期时间格式
478+ 'iso' - 2024-01-15T14:30:45 (ISO标准,带T)
479+ 'space' - 2024-01-15 14:30:45 (空格分隔,更常见)
480+ 'auto' - 自动选择
481+ """
468482 if value is None:
469483 return None
470484 # 处理 bytes 类型(包括 BIT 字段)
@@ -504,8 +518,26 @@ def convert_value(value):
504518 return str(value) # 或 value.total_seconds()
505519 elif isinstance(value, Decimal):
506520 return float(value)
507- elif hasattr(value, 'isoformat'): # 处理 datetime/date/time
508- return value.isoformat()
521+ # 4. 处理 datetime
522+ elif isinstance(value, datetime):
523+ if datetime_format == 'iso':
524+ return value.isoformat()
525+ elif datetime_format == 'space':
526+ return value.strftime('%Y-%m-%d %H:%M:%S')
527+ else: # 'auto' 或其他
528+ # 自动判断:没有时间部分只显示日期
529+ if value.hour == 0 and value.minute == 0 and value.second == 0 and value.microsecond == 0:
530+ return value.strftime('%Y-%m-%d')
531+ else:
532+ return value.strftime('%Y-%m-%d %H:%M:%S')
533+
534+ # 5. 处理 date
535+ elif isinstance(value, date):
536+ return value.isoformat() # 总是 YYYY-MM-DD
537+
538+ # 6. 处理 time
539+ elif isinstance(value, time):
540+ return str(value)
509541 else:
510542 return value
511543
0 commit comments