Skip to content

Commit

Permalink
optional use of pymssql for SQL Server
Browse files Browse the repository at this point in the history
  • Loading branch information
leo-schick committed Sep 29, 2022
1 parent ca83c09 commit ddc6be3
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions mara_db/dbs.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,18 @@ def __init__(self, host: str = None, port: int = None, database: str = None,
@property
def sqlalchemy_url(self):
import urllib.parse
import importlib

port = self.port if self.port else 1433
driver = self.odbc_driver.replace(' ','+')
return f'mssql+pyodbc://{self.user}:{urllib.parse.quote(self.password)}@{self.host}:{port}/{self.database}?driver={driver}'

if importlib.util.find_spec("pyodbc"):
driver = self.odbc_driver.replace(' ','+')
return f'mssql+pyodbc://{self.user}:{urllib.parse.quote(self.password)}@{self.host}:{port}/{self.database}?driver={driver}'

if importlib.util.find_spec("pymssql"):
return f'mssql+pymssql://{self.db.user}:{urllib.parse.quote(self.db.password)}@{self.db.host}:{port}/{self.db.database}'

raise ValueError('You need either python module pyodbc or pymssql to use sqlalchemy with SQLServerDB.')


class SqshSQLServerDB(SQLServerDB):
Expand Down

0 comments on commit ddc6be3

Please sign in to comment.