Skip to content

Commit

Permalink
Merge pull request #36 from jojoduquartier/develop
Browse files Browse the repository at this point in the history
changes to use allow user to not use database name in mysql/mssql connection string `use_dbname_to_connect`
  • Loading branch information
jojoduquartier committed May 4, 2021
2 parents 87cfa09 + 913d053 commit 9abb7b1
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 16 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,10 @@ the manifest file has been added and a wheel file created
## [Version 1.0.5]
- no breaking changes
- snowflake connection is rather different so changes were made so that user can enter all snowflake specific connection parameters
- If users want to use the snowflake connection object, they can use a `raw_connection` argument in `dsdbmanager.snowflake_.Snowflake.create_engine`
- If users want to use the snowflake connection object, they can use a `raw_connection` argument in `dsdbmanager.snowflake_.Snowflake.create_engine`

## [Version 1.0.6]
- no breaking changes
- mysql and mssql doesn't always use database names in connection string. I encountered cases like these recently so I added a boolean field
when adding new databases. The field `use_dbname_to_connect` should be `True` if user wants database name to be used in database connection and `False` otherwise.
This change should only impact mysql and mssql connections.
2 changes: 1 addition & 1 deletion dsdbmanager/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from .configuring import ConfigFilesManager
from .dbobject import DsDbManager, DbMiddleware

__version__ = '1.0.5'
__version__ = '1.0.6'
__configurer__ = ConfigFilesManager()

# first initialize empty files
Expand Down
8 changes: 7 additions & 1 deletion dsdbmanager/configuring.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,14 +261,20 @@ def add_new_database_info(self):
default=-1,
type=int
)
use_dbname_to_connect = click.prompt(
"Database Name is used in MySql/MSSql connection string (True or False)?",
default=True,
type=bool
)

host_dict = dict(
name=name,
host=host,
schema=schema,
port=port,
service_name=service_name,
sid=sid
sid=sid,
use_dbname_to_connect=use_dbname_to_connect
)

# we don't want to store schema, service name or port if
Expand Down
11 changes: 9 additions & 2 deletions dsdbmanager/dbobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,11 +276,18 @@ def __init__(self, engine: sa.engine.Engine, connect_only: bool, schema: str = N

if not connect_only:
inspection = reflection.Inspector.from_engine(self._sqlalchemy_engine)
views = inspection.get_view_names(schema=schema)
tables = inspection.get_table_names(schema=schema)
# without schema, the inspector throws AttributeError
# any other error should be raised
try:
views = inspection.get_view_names(schema=schema)
tables = inspection.get_table_names(schema=schema)

except AttributeError as _:
views, tables = [], []

if not (tables + views):
pass

self._metadata = TableMeta(self.sqlalchemy_engine, schema, tables + views)
self._insert = TableInsert(self.sqlalchemy_engine, schema, tables + views)
self._update = TableUpdate(self.sqlalchemy_engine, schema, tables + views)
Expand Down
27 changes: 22 additions & 5 deletions dsdbmanager/mssql_.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,17 @@ def __init__(self, db_name: str, host_dict: host_type = None):
:param host_dict: optional database info with host, ports etc
"""
self.db_name = db_name
self.host_dict: host_type = ConfigFilesManager().get_hosts() if not host_dict else host_dict
self.host_dict: host_type = ConfigFilesManager(
).get_hosts() if not host_dict else host_dict

if not self.host_dict or 'mssql' not in self.host_dict:
raise MissingFlavor("No databases available for mssql", None)

self.host_dict = self.host_dict.get('mssql').get(self.db_name, {})

if not self.host_dict:
raise MissingDatabase(f"{self.db_name} has not been added for mssql", None)
raise MissingDatabase(
f"{self.db_name} has not been added for mssql", None)

def create_engine(self, user: str = None, pwd: str = None, **kwargs):
"""
Expand All @@ -35,12 +37,27 @@ def create_engine(self, user: str = None, pwd: str = None, **kwargs):
try:
import pymssql
except ImportError as e:
raise MissingPackage("You need the pymssql package to initiate connection", e)
raise MissingPackage(
"You need the pymssql package to initiate connection", e
)

host = self.host_dict.get('host')

if 'port' not in self.host_dict:
return sa.create_engine(f'mssql+pymssql://{user}:{pwd}@{host}/{self.db_name}', **kwargs)
if (
'use_dbname_to_connect' in self.host_dict and
not self.host_dict['use_dbname_to_connect']
):
return sa.create_engine(f'mssql+pymssql://{user}:{pwd}@{host}/', **kwargs)

else:
return sa.create_engine(f'mssql+pymssql://{user}:{pwd}@{host}/{self.db_name}', **kwargs)

port = self.host_dict.get('port')
return sa.create_engine(f'mssql+pymssql://{user}:{pwd}@{host}{port}/{self.db_name}', **kwargs)
if (
'use_dbname_to_connect' in self.host_dict and
not self.host_dict['use_dbname_to_connect']
):
return sa.create_engine(f'mssql+pymssql://{user}:{pwd}@{host}{port}/', **kwargs)
else:
return sa.create_engine(f'mssql+pymssql://{user}:{pwd}@{host}{port}/{self.db_name}', **kwargs)
26 changes: 21 additions & 5 deletions dsdbmanager/mysql_.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,17 @@ def __init__(self, db_name: str, host_dict: host_type = None):
:param host_dict: optional database info with host, ports etc
"""
self.db_name = db_name
self.host_dict: host_type = ConfigFilesManager().get_hosts() if not host_dict else host_dict
self.host_dict: host_type = ConfigFilesManager(
).get_hosts() if not host_dict else host_dict

if not self.host_dict or 'mysql' not in self.host_dict:
raise MissingFlavor("No databases available for mysql", None)

self.host_dict = self.host_dict.get('mysql').get(self.db_name, {})

if not self.host_dict:
raise MissingDatabase(f"{self.db_name} has not been added for mysql", None)
raise MissingDatabase(
f"{self.db_name} has not been added for mysql", None)

def create_engine(self, user: str = None, pwd: str = None, **kwargs):
"""
Expand All @@ -35,12 +37,26 @@ def create_engine(self, user: str = None, pwd: str = None, **kwargs):
try:
import pymysql
except ImportError as e:
raise MissingPackage("You need the pymysql package to initiate connection", e)
raise MissingPackage(
"You need the PyMySQL package to initiate connection", e
)

host = self.host_dict.get('host')

if 'port' not in self.host_dict:
return sa.create_engine(f'mysql+pymysql://{user}:{pwd}@{host}:/{self.db_name}', **kwargs)
if (
'use_dbname_to_connect' in self.host_dict and
not self.host_dict['use_dbname_to_connect']
):
return sa.create_engine(f'mysql+pymysql://{user}:{pwd}@{host}:/', **kwargs)
else:
return sa.create_engine(f'mysql+pymysql://{user}:{pwd}@{host}:/{self.db_name}', **kwargs)

port = self.host_dict.get('port')
return sa.create_engine(f'mysql+pymysql://{user}:{pwd}@{host}:{port}/{self.db_name}', **kwargs)
if (
'use_dbname_to_connect' in self.host_dict and
not self.host_dict['use_dbname_to_connect']
):
return sa.create_engine(f'mysql+pymysql://{user}:{pwd}@{host}:{port}/', **kwargs)
else:
return sa.create_engine(f'mysql+pymysql://{user}:{pwd}@{host}:{port}/{self.db_name}', **kwargs)
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
sqlalchemy
numpy
numpy>=1.16.5
pandas
toolz
click
Expand Down

0 comments on commit 9abb7b1

Please sign in to comment.