Skip to content

Commit

Permalink
Merge pull request #32 from dchess/driver_patch
Browse files Browse the repository at this point in the history
Add patch for ms sql drivers
Fixes #31
When a system has more than one installation of the pydobc drivers, the previous method of setting the drivers automatically fails. This patch adds an option to pass in the driver string as a param or as an env var instead of automatically picking the drivers from the system using pyodbc.drivers().
  • Loading branch information
dchess authored Aug 22, 2020
2 parents 00a14ae + c752536 commit 701288b
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 6 deletions.
4 changes: 2 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ Insert into a table
df = pd.DataFrame(sample_data)
sql = MSSQL()
sql.insert_into("table_name", df)
sql.insert_into("table_name", df)
Execute a stored procedure
Expand All @@ -133,4 +133,4 @@ Execute a stored procedure
Documentation
---------------

Documentation and tutorials available at `sqlsorcery.readthedocs.io <https://sqlsorcery.readthedocs.io/en/latest/>`_
Documentation and tutorials available at `sqlsorcery.readthedocs.io <https://sqlsorcery.readthedocs.io/en/latest/>`_
3 changes: 2 additions & 1 deletion docs/source/cookbook/environment.rst
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ you can combine the params in your `.env` file for easy management.
MS_SCHEMA=
MS_USER=
MS_PWD=
MS_DRIVER=(optional if not using a system with a single driver install)
**MySQL**
Expand Down Expand Up @@ -101,4 +102,4 @@ you can combine the params in your `.env` file for easy management.
**SQLite**
SQLite only requires a filepath to connect. It is generally unnecessary
to specify via an env var.
to specify via an env var.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

setup(
name="sqlsorcery",
version="0.1.4",
version="0.1.5",
description="Dead simple wrapper for pandas and sqlalchemy",
long_description=long_description,
long_description_content_type="text/x-rst",
Expand Down
9 changes: 7 additions & 2 deletions sqlsorcery/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ class MSSQL(Connection):
for connecting to MS SQL."""

def __init__(
self, schema=None, port=None, server=None, db=None, user=None, pwd=None
self, schema=None, port=None, server=None, db=None, user=None, pwd=None, driver=None
):
"""Initializes an MS SQL database connection
Expand All @@ -238,17 +238,22 @@ def __init__(
**Security Warning**: always pass this in with environment
variables when used in production.
:type pwd: string
:param driver: Name of MS SQL driver installed in system
:type driver: string
"""
self.server = server or getenv("MS_SERVER") or getenv("DB_SERVER")
self.port = port or getenv("MS_PORT") or getenv("DB_PORT") or "1433"
self.db = db or getenv("MS_DB") or getenv("DB")
self.user = user or getenv("MS_USER") or getenv("DB_USER")
self.pwd = pwd or getenv("MS_PWD") or getenv("DB_PWD")
self.schema = schema or getenv("MS_SCHEMA") or getenv("DB_SCHEMA") or "dbo"
self.driver = pyodbc.drivers()[-1].replace(" ", "+")
self.driver = driver or getenv("MS_DRIVER") or self._get_driver()
cstr = f"mssql+pyodbc://{self.user}:{self.pwd}@{self.server}:{self.port}/{self.db}?driver={self.driver}"
self.engine = create_engine(cstr, fast_executemany=True)

def _get_driver(self):
return pyodbc.drivers()[-1].replace(" ", "+")


class MySQL(Connection):
"""Child class that inherits from Connection with specific configuration
Expand Down

0 comments on commit 701288b

Please sign in to comment.