Skip to content

Commit

Permalink
Add setencoding setdecoding to options
Browse files Browse the repository at this point in the history
  • Loading branch information
absci committed Dec 8, 2021
1 parent d1cc2d8 commit 0baa379
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,20 @@ Dictionary. Current available keys are:
Integer. Sets the timeout in seconds for the database query.
Default value is ``0`` which disables the timeout.

- [setencoding](https://github.com/mkleehammer/pyodbc/wiki/Connection#setencoding) and [setdecoding](https://github.com/mkleehammer/pyodbc/wiki/Connection#setdecoding)

```python
# Example
"OPTIONS": {
"setdecoding": [
{"sqltype": pyodbc.SQL_CHAR, "encoding": 'utf-8'},
{"sqltype": pyodbc.SQL_WCHAR, "encoding": 'utf-8'}],
"setencoding": [
{"encoding": "utf-8"}],
...
},
```

### Backend-specific settings

The following project-level settings also control the behavior of the backend:
Expand Down
8 changes: 8 additions & 0 deletions mssql/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,8 @@ def get_new_connection(self, conn_params):
retries = options.get('connection_retries', 5)
backoff_time = options.get('connection_retry_backoff_time', 5)
query_timeout = options.get('query_timeout', 0)
setencoding = options.get('setencoding', None)
setdecoding = options.get('setdecoding', None)

conn = None
retry_count = 0
Expand All @@ -341,6 +343,12 @@ def get_new_connection(self, conn_params):
raise

conn.timeout = query_timeout
if setencoding:
for entry in setencoding:
conn.setencoding(**entry)
if setdecoding:
for entry in setdecoding:
conn.setdecoding(**entry)
return conn

def init_connection_state(self):
Expand Down

0 comments on commit 0baa379

Please sign in to comment.