-
Notifications
You must be signed in to change notification settings - Fork 71
Add SQL Binding support #124
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
6b5a402
add sql bindings
lucyzhang929 471139e
Merge branch 'dev' into luczhan/sqlbindings
YunchuWang ee015ac
fix spacing
lucyzhang929 d13ff1a
Merge branch 'luczhan/sqlbindings' of https://github.com/lucyzhang929…
lucyzhang929 63b9679
fix spacing
lucyzhang929 9100764
fix imports
lucyzhang929 77513f1
Merge branch 'dev' into luczhan/sqlbindings
vrdmr b50eac8
Merge branch 'dev' into luczhan/sqlbindings
YunchuWang 14f04dc
Merge branch 'dev' into luczhan/sqlbindings
YunchuWang 09bfe80
Merge branch 'dev' into luczhan/sqlbindings
YunchuWang 21c0a40
Merge branch 'dev' into luczhan/sqlbindings
YunchuWang 8491864
Merge branch 'dev' into luczhan/sqlbindings
YunchuWang 4754fd5
add sql bindings
lucyzhang929 1fb6fce
fix spacing
lucyzhang929 32a9e16
fix spacing
lucyzhang929 628e7a2
Merge branch 'luczhan/sqlbindings' of https://github.com/Charles-Gagn…
Charles-Gagnon 88cb81f
Add tests + PR comments
Charles-Gagnon eabe1b7
Fix lint
Charles-Gagnon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| # Copyright (c) Microsoft Corporation. All rights reserved. | ||
| # Licensed under the MIT License. | ||
|
|
||
| import collections | ||
| import json | ||
|
|
||
| from . import _abc | ||
|
|
||
|
|
||
| class SqlRow(_abc.SqlRow, collections.UserDict): | ||
| """A SQL Row. | ||
|
|
||
| SqlRow objects are ''UserDict'' subclasses and behave like dicts. | ||
| """ | ||
|
|
||
| @classmethod | ||
| def from_json(cls, json_data: str) -> 'SqlRow': | ||
| """Create a SqlRow from a JSON string.""" | ||
| return cls.from_dict(json.loads(json_data)) | ||
|
|
||
| @classmethod | ||
| def from_dict(cls, dct: dict) -> 'SqlRow': | ||
| """Create a SqlRow from a dict object""" | ||
| return cls({k: v for k, v in dct.items()}) | ||
|
|
||
| def to_json(self) -> str: | ||
| """Return the JSON representation of the SqlRow""" | ||
| return json.dumps(dict(self)) | ||
|
|
||
| def __getitem__(self, key): | ||
| return collections.UserDict.__getitem__(self, key) | ||
|
|
||
| def __setitem__(self, key, value): | ||
| return collections.UserDict.__setitem__(self, key, value) | ||
|
|
||
| def __repr__(self) -> str: | ||
| return ( | ||
| f'<SqlRow at 0x{id(self):0x}>' | ||
| ) | ||
|
|
||
|
|
||
| class SqlRowList(_abc.SqlRowList, collections.UserList): | ||
| "A ''UserList'' subclass containing a list of :class:'~SqlRow' objects" | ||
| pass |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| # Copyright (c) Microsoft Corporation. All rights reserved. | ||
| # Licensed under the MIT License. | ||
|
|
||
| import collections.abc | ||
| import json | ||
| import typing | ||
|
|
||
| from azure.functions import _sql as sql | ||
|
|
||
| from . import meta | ||
|
|
||
|
|
||
| class SqlConverter(meta.InConverter, meta.OutConverter, | ||
| binding='sql'): | ||
|
|
||
| @classmethod | ||
| def check_input_type_annotation(cls, pytype: type) -> bool: | ||
| return issubclass(pytype, sql.SqlRowList) | ||
|
|
||
| @classmethod | ||
| def check_output_type_annotation(cls, pytype: type) -> bool: | ||
| return issubclass(pytype, (sql.SqlRowList, sql.SqlRow)) | ||
|
|
||
| @classmethod | ||
| def decode(cls, | ||
| data: meta.Datum, | ||
| *, | ||
| trigger_metadata) -> typing.Optional[sql.SqlRowList]: | ||
| if data is None or data.type is None: | ||
| return None | ||
|
|
||
| data_type = data.type | ||
|
|
||
| if data_type in ['string', 'json']: | ||
| body = data.value | ||
|
|
||
| elif data_type == 'bytes': | ||
| body = data.value.decode('utf-8') | ||
|
|
||
| else: | ||
| raise NotImplementedError( | ||
| f'Unsupported payload type: {data_type}') | ||
|
|
||
| rows = json.loads(body) | ||
| if not isinstance(rows, list): | ||
| rows = [rows] | ||
|
|
||
| return sql.SqlRowList( | ||
| (None if row is None else sql.SqlRow.from_dict(row)) | ||
| for row in rows) | ||
|
|
||
| @classmethod | ||
| def encode(cls, obj: typing.Any, *, | ||
| expected_type: typing.Optional[type]) -> meta.Datum: | ||
| if isinstance(obj, sql.SqlRow): | ||
| data = sql.SqlRowList([obj]) | ||
|
|
||
| elif isinstance(obj, sql.SqlRowList): | ||
| data = obj | ||
|
|
||
| elif isinstance(obj, collections.abc.Iterable): | ||
| data = sql.SqlRowList() | ||
|
|
||
| for row in obj: | ||
| if not isinstance(row, sql.SqlRow): | ||
| raise NotImplementedError( | ||
| f'Unsupported list type: {type(obj)}, \ | ||
| lists must contain SqlRow objects') | ||
| else: | ||
| data.append(row) | ||
|
|
||
| else: | ||
| raise NotImplementedError(f'Unsupported type: {type(obj)}') | ||
|
|
||
| return meta.Datum( | ||
| type='json', | ||
| value=json.dumps([dict(d) for d in data]) | ||
| ) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.