Skip to content

Commit b389e86

Browse files
committed
feat: add oracle instant client to support oracle version 11
1 parent f3f152c commit b389e86

File tree

3 files changed

+33
-9
lines changed

3 files changed

+33
-9
lines changed

backend/apps/datasource/crud/datasource.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -303,10 +303,16 @@ def preview(session: SessionDep, current_user: CurrentUser, id: int, data: Table
303303
{where}
304304
LIMIT 100"""
305305
elif ds.type == "oracle":
306-
sql = f"""SELECT "{'", "'.join(fields)}" FROM "{conf.dbSchema}"."{data.table.table_name}"
307-
{where}
308-
ORDER BY "{fields[0]}"
309-
OFFSET 0 ROWS FETCH NEXT 100 ROWS ONLY"""
306+
# sql = f"""SELECT "{'", "'.join(fields)}" FROM "{conf.dbSchema}"."{data.table.table_name}"
307+
# {where}
308+
# ORDER BY "{fields[0]}"
309+
# OFFSET 0 ROWS FETCH NEXT 100 ROWS ONLY"""
310+
sql = f"""SELECT * FROM
311+
(SELECT "{'", "'.join(fields)}" FROM "{conf.dbSchema}"."{data.table.table_name}"
312+
{where}
313+
ORDER BY "{fields[0]}")
314+
WHERE ROWNUM <= 100
315+
"""
310316
elif ds.type == "ck":
311317
sql = f"""SELECT "{'", "'.join(fields)}" FROM "{data.table.table_name}"
312318
{where}

backend/apps/db/db.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
from decimal import Decimal
66
from typing import Optional
77

8+
import oracledb
89
import psycopg2
910
import pymssql
11+
1012
from apps.db.db_sql import get_table_sql, get_field_sql, get_version_sql
1113
from common.error import ParseSQLResultError
1214

@@ -27,10 +29,20 @@
2729
from common.utils.utils import SQLBotLogUtil, equals_ignore_case
2830
from fastapi import HTTPException
2931
from apps.db.es_engine import get_es_connect, get_es_index, get_es_fields, get_es_data_by_http
32+
from common.core.config import settings
33+
34+
try:
35+
oracledb.init_oracle_client(
36+
lib_dir=settings.ORACLE_CLIENT_PATH
37+
)
38+
SQLBotLogUtil.info("init oracle client success, use thick mode")
39+
except Exception:
40+
SQLBotLogUtil.error("init oracle client failed, use thin mode")
3041

3142

3243
def get_uri(ds: CoreDatasource) -> str:
33-
conf = DatasourceConf(**json.loads(aes_decrypt(ds.configuration))) if not equals_ignore_case(ds.type, "excel") else get_engine_config()
44+
conf = DatasourceConf(**json.loads(aes_decrypt(ds.configuration))) if not equals_ignore_case(ds.type,
45+
"excel") else get_engine_config()
3446
return get_uri_from_config(ds.type, conf)
3547

3648

@@ -102,7 +114,8 @@ def get_origin_connect(type: str, conf: DatasourceConf):
102114

103115
# use sqlalchemy
104116
def get_engine(ds: CoreDatasource, timeout: int = 0) -> Engine:
105-
conf = DatasourceConf(**json.loads(aes_decrypt(ds.configuration))) if not equals_ignore_case(ds.type, "excel") else get_engine_config()
117+
conf = DatasourceConf(**json.loads(aes_decrypt(ds.configuration))) if not equals_ignore_case(ds.type,
118+
"excel") else get_engine_config()
106119
if conf.timeout is None:
107120
conf.timeout = timeout
108121
if timeout > 0:
@@ -233,7 +246,8 @@ def get_version(ds: CoreDatasource | AssistantOutDsSchema):
233246
conf = None
234247
if isinstance(ds, CoreDatasource):
235248
conf = DatasourceConf(
236-
**json.loads(aes_decrypt(ds.configuration))) if not equals_ignore_case(ds.type, "excel") else get_engine_config()
249+
**json.loads(aes_decrypt(ds.configuration))) if not equals_ignore_case(ds.type,
250+
"excel") else get_engine_config()
237251
if isinstance(ds, AssistantOutDsSchema):
238252
conf = DatasourceConf()
239253
conf.host = ds.host
@@ -319,7 +333,8 @@ def get_schema(ds: CoreDatasource):
319333

320334

321335
def get_tables(ds: CoreDatasource):
322-
conf = DatasourceConf(**json.loads(aes_decrypt(ds.configuration))) if not equals_ignore_case(ds.type, "excel") else get_engine_config()
336+
conf = DatasourceConf(**json.loads(aes_decrypt(ds.configuration))) if not equals_ignore_case(ds.type,
337+
"excel") else get_engine_config()
323338
db = DB.get_db(ds.type)
324339
sql, sql_param = get_table_sql(ds, conf, get_version(ds))
325340
if db.connect_type == ConnectType.sqlalchemy:
@@ -369,7 +384,8 @@ def get_tables(ds: CoreDatasource):
369384

370385

371386
def get_fields(ds: CoreDatasource, table_name: str = None):
372-
conf = DatasourceConf(**json.loads(aes_decrypt(ds.configuration))) if not equals_ignore_case(ds.type, "excel") else get_engine_config()
387+
conf = DatasourceConf(**json.loads(aes_decrypt(ds.configuration))) if not equals_ignore_case(ds.type,
388+
"excel") else get_engine_config()
373389
db = DB.get_db(ds.type)
374390
sql, p1, p2 = get_field_sql(ds, conf, table_name)
375391
if db.connect_type == ConnectType.sqlalchemy:

backend/common/core/config.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,5 +111,7 @@ def SQLALCHEMY_DATABASE_URI(self) -> PostgresDsn | str:
111111
TABLE_EMBEDDING_COUNT: int = 10
112112
DS_EMBEDDING_COUNT: int = 10
113113

114+
ORACLE_CLIENT_PATH: str = '/opt/sqlbot/db_client/oracle_instant_client'
115+
114116

115117
settings = Settings() # type: ignore

0 commit comments

Comments
 (0)