Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions backend/apps/datasource/crud/datasource.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,10 +303,16 @@ def preview(session: SessionDep, current_user: CurrentUser, id: int, data: Table
{where}
LIMIT 100"""
elif ds.type == "oracle":
sql = f"""SELECT "{'", "'.join(fields)}" FROM "{conf.dbSchema}"."{data.table.table_name}"
{where}
ORDER BY "{fields[0]}"
OFFSET 0 ROWS FETCH NEXT 100 ROWS ONLY"""
# sql = f"""SELECT "{'", "'.join(fields)}" FROM "{conf.dbSchema}"."{data.table.table_name}"
# {where}
# ORDER BY "{fields[0]}"
# OFFSET 0 ROWS FETCH NEXT 100 ROWS ONLY"""
sql = f"""SELECT * FROM
(SELECT "{'", "'.join(fields)}" FROM "{conf.dbSchema}"."{data.table.table_name}"
{where}
ORDER BY "{fields[0]}")
WHERE ROWNUM <= 100
"""
elif ds.type == "ck":
sql = f"""SELECT "{'", "'.join(fields)}" FROM "{data.table.table_name}"
{where}
Expand Down
26 changes: 21 additions & 5 deletions backend/apps/db/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
from decimal import Decimal
from typing import Optional

import oracledb
import psycopg2
import pymssql

from apps.db.db_sql import get_table_sql, get_field_sql, get_version_sql
from common.error import ParseSQLResultError

Expand All @@ -27,10 +29,20 @@
from common.utils.utils import SQLBotLogUtil, equals_ignore_case
from fastapi import HTTPException
from apps.db.es_engine import get_es_connect, get_es_index, get_es_fields, get_es_data_by_http
from common.core.config import settings

try:
oracledb.init_oracle_client(
lib_dir=settings.ORACLE_CLIENT_PATH
)
SQLBotLogUtil.info("init oracle client success, use thick mode")
except Exception:
SQLBotLogUtil.error("init oracle client failed, use thin mode")


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


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

# use sqlalchemy
def get_engine(ds: CoreDatasource, timeout: int = 0) -> Engine:
conf = DatasourceConf(**json.loads(aes_decrypt(ds.configuration))) if not equals_ignore_case(ds.type, "excel") else get_engine_config()
conf = DatasourceConf(**json.loads(aes_decrypt(ds.configuration))) if not equals_ignore_case(ds.type,
"excel") else get_engine_config()
if conf.timeout is None:
conf.timeout = timeout
if timeout > 0:
Expand Down Expand Up @@ -233,7 +246,8 @@ def get_version(ds: CoreDatasource | AssistantOutDsSchema):
conf = None
if isinstance(ds, CoreDatasource):
conf = DatasourceConf(
**json.loads(aes_decrypt(ds.configuration))) if not equals_ignore_case(ds.type, "excel") else get_engine_config()
**json.loads(aes_decrypt(ds.configuration))) if not equals_ignore_case(ds.type,
"excel") else get_engine_config()
if isinstance(ds, AssistantOutDsSchema):
conf = DatasourceConf()
conf.host = ds.host
Expand Down Expand Up @@ -319,7 +333,8 @@ def get_schema(ds: CoreDatasource):


def get_tables(ds: CoreDatasource):
conf = DatasourceConf(**json.loads(aes_decrypt(ds.configuration))) if not equals_ignore_case(ds.type, "excel") else get_engine_config()
conf = DatasourceConf(**json.loads(aes_decrypt(ds.configuration))) if not equals_ignore_case(ds.type,
"excel") else get_engine_config()
db = DB.get_db(ds.type)
sql, sql_param = get_table_sql(ds, conf, get_version(ds))
if db.connect_type == ConnectType.sqlalchemy:
Expand Down Expand Up @@ -369,7 +384,8 @@ def get_tables(ds: CoreDatasource):


def get_fields(ds: CoreDatasource, table_name: str = None):
conf = DatasourceConf(**json.loads(aes_decrypt(ds.configuration))) if not equals_ignore_case(ds.type, "excel") else get_engine_config()
conf = DatasourceConf(**json.loads(aes_decrypt(ds.configuration))) if not equals_ignore_case(ds.type,
"excel") else get_engine_config()
db = DB.get_db(ds.type)
sql, p1, p2 = get_field_sql(ds, conf, table_name)
if db.connect_type == ConnectType.sqlalchemy:
Expand Down
2 changes: 2 additions & 0 deletions backend/common/core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,5 +111,7 @@ def SQLALCHEMY_DATABASE_URI(self) -> PostgresDsn | str:
TABLE_EMBEDDING_COUNT: int = 10
DS_EMBEDDING_COUNT: int = 10

ORACLE_CLIENT_PATH: str = '/opt/sqlbot/db_client/oracle_instant_client'


settings = Settings() # type: ignore