Skip to content

Commit a9f4b5c

Browse files
committed
feat: Ask to add database version information
1 parent 030392c commit a9f4b5c

File tree

3 files changed

+62
-6
lines changed

3 files changed

+62
-6
lines changed

backend/apps/chat/task/llm.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
from apps.datasource.crud.datasource import get_table_schema
2929
from apps.datasource.crud.permission import get_row_permission_filters, is_normal_user
3030
from apps.datasource.models.datasource import CoreDatasource
31-
from apps.db.db import exec_sql
31+
from apps.db.db import exec_sql, get_version
3232
from apps.system.crud.assistant import AssistantOutDs, AssistantOutDsFactory, get_assistant_ds
3333
from apps.system.schemas.system_schema import AssistantOutDsSchema
3434
from common.core.config import settings
@@ -91,13 +91,13 @@ def __init__(self, current_user: CurrentUser, chat_question: ChatQuestion,
9191
ds = self.out_ds_instance.get_ds(chat.datasource)
9292
if not ds:
9393
raise SingleMessageError("No available datasource configuration found")
94-
chat_question.engine = ds.type
94+
chat_question.engine = ds.type + get_version(ds)
9595
chat_question.db_schema = self.out_ds_instance.get_db_schema(ds.id)
9696
else:
9797
ds = self.session.get(CoreDatasource, chat.datasource)
9898
if not ds:
9999
raise SingleMessageError("No available datasource configuration found")
100-
chat_question.engine = ds.type_name if ds.type != 'excel' else 'PostgreSQL'
100+
chat_question.engine = (ds.type_name if ds.type != 'excel' else 'PostgreSQL') + get_version(ds)
101101
chat_question.db_schema = get_table_schema(session=self.session, current_user=current_user, ds=ds)
102102

103103
self.generate_sql_logs = list_generate_sql_logs(session=self.session, chart_id=chat_id)
@@ -451,7 +451,7 @@ def select_datasource(self):
451451
if self.current_assistant and self.current_assistant.type in dynamic_ds_types:
452452
_ds = self.out_ds_instance.get_ds(data['id'])
453453
self.ds = _ds
454-
self.chat_question.engine = _ds.type
454+
self.chat_question.engine = _ds.type + get_version(self.ds)
455455
self.chat_question.db_schema = self.out_ds_instance.get_db_schema(self.ds.id)
456456
_engine_type = self.chat_question.engine
457457
_chat.engine_type = _ds.type
@@ -461,7 +461,8 @@ def select_datasource(self):
461461
_datasource = None
462462
raise SingleMessageError(f"Datasource configuration with id {_datasource} not found")
463463
self.ds = CoreDatasource(**_ds.model_dump())
464-
self.chat_question.engine = _ds.type_name if _ds.type != 'excel' else 'PostgreSQL'
464+
self.chat_question.engine = (_ds.type_name if _ds.type != 'excel' else 'PostgreSQL') + get_version(
465+
self.ds)
465466
self.chat_question.db_schema = get_table_schema(session=self.session,
466467
current_user=self.current_user, ds=self.ds)
467468
_engine_type = self.chat_question.engine

backend/apps/db/db.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import urllib.parse
55
from decimal import Decimal
66

7-
from apps.db.db_sql import get_table_sql, get_field_sql
7+
from apps.db.db_sql import get_table_sql, get_field_sql, get_version_sql
88

99
if platform.system() != "Darwin":
1010
import dmPython
@@ -141,6 +141,35 @@ def check_connection(trans: Trans, ds: CoreDatasource, is_raise: bool = False):
141141
return False
142142

143143

144+
def get_version(ds: CoreDatasource):
145+
conf = DatasourceConf(**json.loads(aes_decrypt(ds.configuration))) if ds.type != "excel" else get_engine_config()
146+
db = DB.get_db(ds.type)
147+
sql = get_version_sql(ds, conf)
148+
try:
149+
if db.connect_type == ConnectType.sqlalchemy:
150+
with get_session(ds) as session:
151+
with session.execute(text(sql)) as result:
152+
res = result.fetchall()
153+
return res[0][0]
154+
else:
155+
if ds.type == 'dm':
156+
with dmPython.connect(user=conf.username, password=conf.password, server=conf.host,
157+
port=conf.port) as conn, conn.cursor() as cursor:
158+
cursor.execute(sql, timeout=10)
159+
res = cursor.fetchall()
160+
return res[0][0]
161+
elif ds.type == 'doris':
162+
with pymysql.connect(user=conf.username, passwd=conf.password, host=conf.host,
163+
port=conf.port, db=conf.database, connect_timeout=10,
164+
read_timeout=10) as conn, conn.cursor() as cursor:
165+
cursor.execute(sql)
166+
res = cursor.fetchall()
167+
return res[0][0]
168+
except Exception as e:
169+
print(e)
170+
return ''
171+
172+
144173
def get_schema(ds: CoreDatasource):
145174
conf = DatasourceConf(**json.loads(aes_decrypt(ds.configuration))) if ds.type != "excel" else get_engine_config()
146175
db = DB.get_db(ds.type)

backend/apps/db/db_sql.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,32 @@
33
from apps.datasource.models.datasource import CoreDatasource, DatasourceConf
44

55

6+
def get_version_sql(ds: CoreDatasource, conf: DatasourceConf):
7+
if ds.type == "mysql" or ds.type == "doris":
8+
return f"""
9+
SELECT VERSION()
10+
"""
11+
elif ds.type == "sqlServer":
12+
return f"""
13+
select SERVERPROPERTY('ProductVersion')
14+
"""
15+
elif ds.type == "pg" or ds.type == "excel":
16+
return f"""
17+
select version()
18+
"""
19+
elif ds.type == "oracle":
20+
return f"""
21+
SELECT version FROM v$instance
22+
"""
23+
elif ds.type == "ck":
24+
return f"""
25+
select version()
26+
"""
27+
elif ds.type == 'dm':
28+
return f"""
29+
SELECT * FROM v$version
30+
"""
31+
632
def get_table_sql(ds: CoreDatasource, conf: DatasourceConf):
733
if ds.type == "mysql" or ds.type == "doris":
834
return f"""

0 commit comments

Comments
 (0)