-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathsql.py
78 lines (58 loc) · 2.2 KB
/
sql.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
import collections.abc
import typing
from azure.functions import _json as json
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])
)