Merge pull request #39 from microsoft/trtran/port_param

add port param to ConnectionInfo for Python
This commit is contained in:
Tri Tran 2019-05-15 16:48:57 -07:00 коммит произвёл GitHub
Родитель 6093c19850 818f3ebd1f
Коммит 9443e7c4a9
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
3 изменённых файлов: 19 добавлений и 6 удалений

Двоичные данные
Python/dist/sqlmlutils-0.6.0.zip поставляемый

Двоичный файл не отображается.

10
Python/sqlmlutils/connectioninfo.py Normal file → Executable file
Просмотреть файл

@ -6,11 +6,12 @@ class ConnectionInfo:
"""
def __init__(self, driver: str = "SQL Server", server: str = "localhost", database: str = "master",
def __init__(self, driver: str = "SQL Server", server: str = "localhost", port: str = "", database: str = "master",
uid: str = "", pwd: str = ""):
"""
:param driver: Driver to use to connect to SQL Server.
:param server: SQL Server hostname or a specific instance to connect to.
:param port: SQL Server port number.
:param database: Database to connect to.
:param uid: uid to connect with. If not specified, utilizes trusted authentication.
:param pwd: pwd to connect with. If uid is not specified, pwd is ignored; uses trusted auth instead
@ -20,6 +21,7 @@ class ConnectionInfo:
"""
self._driver = driver
self._server = server
self._port = port
self._database = database
self._uid = uid
self._pwd = pwd
@ -32,6 +34,10 @@ class ConnectionInfo:
def server(self):
return self._server
@property
def port(self):
return self._port
@property
def database(self):
return self._database
@ -48,7 +54,7 @@ class ConnectionInfo:
def connection_string(self):
return "Driver={driver};Server={server};Database={database};{auth};".format(
driver=self._driver,
server=self._server,
server=self._server if self._port == "" else "{servername},{port}".format(servername=self._server, port=self._port),
database=self._database,
auth="Trusted_Connection=Yes" if self._uid == "" else
"uid={uid};pwd={pwd}".format(uid=self._uid, pwd=self._pwd)

15
Python/sqlmlutils/sqlqueryexecutor.py Normal file → Executable file
Просмотреть файл

@ -51,10 +51,17 @@ class SQLQueryExecutor:
return [row for row in self._mssqlconn]
def __enter__(self):
self._mssqlconn = _mssql.connect(server=self._connection.server,
user=self._connection.uid,
password=self._connection.pwd,
database=self._connection.database)
if self._connection.port == "":
self._mssqlconn = _mssql.connect(server=self._connection.server,
user=self._connection.uid,
password=self._connection.pwd,
database=self._connection.database)
else:
self._mssqlconn = _mssql.connect(server=self._connection.server,
port=self._connection.port,
user=self._connection.uid,
password=self._connection.pwd,
database=self._connection.database)
self._mssqlconn.set_msghandler(_sql_msg_handler)
return self