|
6 | 6 | from pydantic import BaseModel |
7 | 7 | import uvicorn |
8 | 8 | from colorama import Fore, Style, init |
9 | | -import sys |
| 9 | +import sys, os |
10 | 10 |
|
11 | 11 | # Initialize colorama |
12 | 12 | init(autoreset=True) |
@@ -73,20 +73,35 @@ def log_query(index, query): |
73 | 73 | endpoint_text = f"{Fore.GREEN}execute-query{Style.RESET_ALL}" |
74 | 74 | print(f"{index_text} {endpoint_text}: Executed query: {query}") |
75 | 75 |
|
| 76 | +def find_file_path(file_name): |
| 77 | + base_path = sys._MEIPASS |
| 78 | + for root, _, files in os.walk(f"{base_path}/drivers"): |
| 79 | + if file_name in files: |
| 80 | + return os.path.join(root, file_name) |
| 81 | + raise f"{file_name} NotFound." |
| 82 | + |
| 83 | +def find_odbc_driver_path(): |
| 84 | + try: |
| 85 | + with open(find_file_path("odbcinst.ini"), 'r') as file: |
| 86 | + for line in file: |
| 87 | + line = line.strip() |
| 88 | + if line.startswith("Driver="): |
| 89 | + driver_name = line.split("/")[-1].strip() |
| 90 | + break |
| 91 | + |
| 92 | + return find_file_path(driver_name) |
| 93 | + except: |
| 94 | + print("Could not fild odbc driver internally...") |
| 95 | + return "{SQL Server}" |
| 96 | + |
76 | 97 | # Establish a database connection and return a unique connection ID |
77 | 98 | @app.post("/connect") |
78 | 99 | async def connect(request: ConnectRequest): |
79 | 100 | global connection_index_counter |
80 | 101 | try: |
81 | 102 | # Attempt to connect to the database |
82 | | - try: |
83 | | - connection = pyodbc.connect(f"Driver={{SQL Server}};Server={request.server};UID={request.username};PWD={request.password};", timeout=5) |
84 | | - except: |
85 | | - try: |
86 | | - base_path = sys._MEIPASS |
87 | | - connection = pyodbc.connect(f"Driver={base_path}/drivers/msodbcsql18/lib64/libmsodbcsql-18.4.so.1.1;Server={request.server};UID={request.username};PWD={request.password};", timeout=5) |
88 | | - except: |
89 | | - raise |
| 103 | + driver_path = find_odbc_driver_path() |
| 104 | + connection = pyodbc.connect(f"Driver={driver_path};Server={request.server};UID={request.username};PWD={request.password};", timeout=5) |
90 | 105 |
|
91 | 106 | # Generate a unique ID and index for the connection |
92 | 107 | connection_id = str(uuid.uuid4()) |
|
0 commit comments