Skip to content

Commit 43f852e

Browse files
feat: MySql Decorators for Python (#269)
* mysql decorators * bring the import statement in the character limit * Fix indentation and white spaces * Fix pipeline errors * Fix newline at end of file * Fix indentation in test_decorators.py * Fix newline at end of file * Fix indentation in MySQL test cases * Add newline at end of test_mysql.py file * Remove trailing whitespace and add newline * add new lines --------- Co-authored-by: Gavin Aguiar <[email protected]>
1 parent 773c401 commit 43f852e

File tree

6 files changed

+569
-1
lines changed

6 files changed

+569
-1
lines changed

azure/functions/decorators/constants.py

+2
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,5 @@
4343
ASSISTANT_CREATE = "assistantCreate"
4444
ASSISTANT_POST = "assistantPost"
4545
SEMANTIC_SEARCH = "semanticSearch"
46+
MYSQL = "mysql"
47+
MYSQL_TRIGGER = "mysqlTrigger"

azure/functions/decorators/function_app.py

+157
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@
4747
from .warmup import WarmUpTrigger
4848
from .._http_asgi import AsgiMiddleware
4949
from .._http_wsgi import WsgiMiddleware, Context
50+
from azure.functions.decorators.mysql import MySqlInput, MySqlOutput, \
51+
MySqlTrigger
5052

5153

5254
class Function(object):
@@ -1443,6 +1445,58 @@ def decorator():
14431445

14441446
return wrap
14451447

1448+
def mysql_trigger(self,
1449+
arg_name: str,
1450+
table_name: str,
1451+
connection_string_setting: str,
1452+
leases_table_name: Optional[str] = None,
1453+
data_type: Optional[DataType] = None,
1454+
**kwargs) -> Callable[..., Any]:
1455+
"""The mysql_trigger decorator adds :class:`MySqlTrigger`
1456+
to the :class:`FunctionBuilder` object
1457+
for building :class:`Function` object used in worker function
1458+
indexing model. This decorator will work only with extension bundle 4.x
1459+
and above.
1460+
This is equivalent to defining MySqlTrigger in the function.json which
1461+
enables function to be triggered when there are changes in the MySql
1462+
table.
1463+
All optional fields will be given default value by function host when
1464+
they are parsed by function host.
1465+
Ref: https://aka.ms/mysqlbindings
1466+
:param arg_name: The name of the variable that represents a
1467+
:class:`MySqlRowList` object in the function code
1468+
:param table_name: The name of the table monitored by the trigger
1469+
:param connection_string_setting: The name of an app setting that
1470+
contains the connection string for the database against which the
1471+
query or stored procedure is being executed
1472+
:param leases_table_name: The name of the table used to store
1473+
leases. If not specified, the leases table name will be
1474+
Leases_{FunctionId}_{TableId}.
1475+
:param data_type: Defines how Functions runtime should treat the
1476+
parameter value
1477+
:param kwargs: Keyword arguments for specifying additional binding
1478+
fields to include in the binding json
1479+
:return: Decorator function.
1480+
"""
1481+
1482+
@self._configure_function_builder
1483+
def wrap(fb):
1484+
def decorator():
1485+
fb.add_trigger(
1486+
trigger=MySqlTrigger(
1487+
name=arg_name,
1488+
table_name=table_name,
1489+
connection_string_setting=connection_string_setting,
1490+
leases_table_name=leases_table_name,
1491+
data_type=parse_singular_param_to_enum(data_type,
1492+
DataType),
1493+
**kwargs))
1494+
return fb
1495+
1496+
return decorator()
1497+
1498+
return wrap
1499+
14461500
def generic_trigger(self,
14471501
arg_name: str,
14481502
type: str,
@@ -3552,6 +3606,109 @@ def decorator():
35523606

35533607
return wrap
35543608

3609+
def mysql_input(self,
3610+
arg_name: str,
3611+
command_text: str,
3612+
connection_string_setting: str,
3613+
command_type: Optional[str] = 'Text',
3614+
parameters: Optional[str] = None,
3615+
data_type: Optional[DataType] = None,
3616+
**kwargs) -> Callable[..., Any]:
3617+
"""The mysql_input decorator adds
3618+
:class:`MySqlInput` to the :class:`FunctionBuilder` object
3619+
for building :class:`Function` object used in worker function
3620+
indexing model. This decorator will work only with extension bundle 4.x
3621+
and above.
3622+
This is equivalent to defining MySqlInput in the function.json which
3623+
enables the function to read from a MySql database.
3624+
All optional fields will be given default value by function host when
3625+
they are parsed by function host.
3626+
Ref: https://aka.ms/mysqlbindings
3627+
:param arg_name: The name of the variable that represents a
3628+
:class:`MySqlRowList` input object in function code
3629+
:param command_text: The Transact-SQL query command or name of the
3630+
stored procedure executed by the binding
3631+
:param connection_string_setting: The name of an app setting that
3632+
contains the connection string for the database against which the
3633+
query or stored procedure is being executed
3634+
:param command_type: A CommandType value, which is Text for a query
3635+
and StoredProcedure for a stored procedure
3636+
:param parameters: Zero or more parameter values passed to the
3637+
command during execution as a single string. Must follow the format
3638+
@param1=param1,@param2=param2
3639+
:param data_type: Defines how Functions runtime should treat the
3640+
parameter value
3641+
:param kwargs: Keyword arguments for specifying additional binding
3642+
fields to include in the binding json
3643+
:return: Decorator function.
3644+
"""
3645+
3646+
@self._configure_function_builder
3647+
def wrap(fb):
3648+
def decorator():
3649+
fb.add_binding(
3650+
binding=MySqlInput(
3651+
name=arg_name,
3652+
command_text=command_text,
3653+
connection_string_setting=connection_string_setting,
3654+
command_type=command_type,
3655+
parameters=parameters,
3656+
data_type=parse_singular_param_to_enum(data_type,
3657+
DataType),
3658+
**kwargs))
3659+
return fb
3660+
3661+
return decorator()
3662+
3663+
return wrap
3664+
3665+
def mysql_output(self,
3666+
arg_name: str,
3667+
command_text: str,
3668+
connection_string_setting: str,
3669+
data_type: Optional[DataType] = None,
3670+
**kwargs) -> Callable[..., Any]:
3671+
"""The mysql_output decorator adds
3672+
:class:`MySqlOutput` to the :class:`FunctionBuilder` object
3673+
for building :class:`Function` object used in worker function
3674+
indexing model. This decorator will work only with extension bundle 4.x
3675+
and above.
3676+
This is equivalent to defining MySqlOutput in the function.json which
3677+
enables the function to write to a MySql database.
3678+
All optional fields will be given default value by function host when
3679+
they are parsed by function host.
3680+
Ref: https://aka.ms/mysqlbindings
3681+
:param arg_name: The name of the variable that represents
3682+
MySql output object in function code
3683+
:param command_text: The Transact-SQL query command or name of the
3684+
stored procedure executed by the binding
3685+
:param connection_string_setting: The name of an app setting that
3686+
contains the connection string for the database against which the
3687+
query or stored procedure is being executed
3688+
:param data_type: Defines how Functions runtime should treat the
3689+
parameter value
3690+
:param kwargs: Keyword arguments for specifying additional binding
3691+
fields to include in the binding json
3692+
:return: Decorator function.
3693+
"""
3694+
3695+
@self._configure_function_builder
3696+
def wrap(fb):
3697+
def decorator():
3698+
fb.add_binding(
3699+
binding=MySqlOutput(
3700+
name=arg_name,
3701+
command_text=command_text,
3702+
connection_string_setting=connection_string_setting,
3703+
data_type=parse_singular_param_to_enum(data_type,
3704+
DataType),
3705+
**kwargs))
3706+
return fb
3707+
3708+
return decorator()
3709+
3710+
return wrap
3711+
35553712

35563713
class SettingsApi(DecoratorApi, ABC):
35573714
"""Interface to extend for using existing settings decorator in

azure/functions/decorators/mysql.py

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License.
3+
from typing import Optional
4+
5+
from azure.functions.decorators.constants import MYSQL, MYSQL_TRIGGER
6+
from azure.functions.decorators.core import DataType, InputBinding, \
7+
OutputBinding, Trigger
8+
9+
10+
class MySqlInput(InputBinding):
11+
@staticmethod
12+
def get_binding_name() -> str:
13+
return MYSQL
14+
15+
def __init__(self,
16+
name: str,
17+
command_text: str,
18+
connection_string_setting: str,
19+
command_type: Optional[str] = 'Text',
20+
parameters: Optional[str] = None,
21+
data_type: Optional[DataType] = None,
22+
**kwargs):
23+
self.command_text = command_text
24+
self.connection_string_setting = connection_string_setting
25+
self.command_type = command_type
26+
self.parameters = parameters
27+
super().__init__(name=name, data_type=data_type)
28+
29+
30+
class MySqlOutput(OutputBinding):
31+
@staticmethod
32+
def get_binding_name() -> str:
33+
return MYSQL
34+
35+
def __init__(self,
36+
name: str,
37+
command_text: str,
38+
connection_string_setting: str,
39+
data_type: Optional[DataType] = None,
40+
**kwargs):
41+
self.command_text = command_text
42+
self.connection_string_setting = connection_string_setting
43+
super().__init__(name=name, data_type=data_type)
44+
45+
46+
class MySqlTrigger(Trigger):
47+
@staticmethod
48+
def get_binding_name() -> str:
49+
return MYSQL_TRIGGER
50+
51+
def __init__(self,
52+
name: str,
53+
table_name: str,
54+
connection_string_setting: str,
55+
leases_table_name: Optional[str] = None,
56+
data_type: Optional[DataType] = None,
57+
**kwargs):
58+
self.table_name = table_name
59+
self.connection_string_setting = connection_string_setting
60+
self.leases_table_name = leases_table_name
61+
super().__init__(name=name, data_type=data_type)

docs/ProgModelSpec.pyi

+110
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,45 @@ class TriggerApi(DecoratorApi, ABC):
595595

596596
pass
597597

598+
def mysql_trigger(self,
599+
arg_name: str,
600+
table_name: str,
601+
connection_string_setting: str,
602+
leases_table_name: Optional[str] = None,
603+
data_type: Optional[DataType] = None,
604+
**kwargs) -> Callable[..., Any]:
605+
"""The mysql_trigger decorator adds :class:`MySqlTrigger`
606+
to the :class:`FunctionBuilder` object
607+
for building :class:`Function` object used in worker function
608+
indexing model. This decorator will work only with extension bundle 4.x
609+
and above.
610+
This is equivalent to defining MySqlTrigger in the function.json which
611+
enables function to be triggered when there are changes in the MySql
612+
table.
613+
All optional fields will be given default value by function host when
614+
they are parsed by function host.
615+
616+
Ref: https://aka.ms/mysqlbindings
617+
618+
:param arg_name: The name of the variable that represents a
619+
:class:`MySqlRowList` object in the function code
620+
:param table_name: The name of the table monitored by the trigger
621+
:param connection_string_setting: The name of an app setting that
622+
contains the connection string for the database against which the
623+
query or stored procedure is being executed
624+
:param leases_table_name: The name of the table used to store
625+
leases. If not specified, the leases table name will be
626+
Leases_{FunctionId}_{TableId}.
627+
:param data_type: Defines how Functions runtime should treat the
628+
parameter value
629+
:param kwargs: Keyword arguments for specifying additional binding
630+
fields to include in the binding json
631+
632+
:return: Decorator function.
633+
"""
634+
635+
pass
636+
598637
def generic_trigger(self,
599638
arg_name: str,
600639
type: str,
@@ -1112,6 +1151,77 @@ class BindingApi(DecoratorApi, ABC):
11121151

11131152
pass
11141153

1154+
def mysql_input(self,
1155+
arg_name: str,
1156+
command_text: str,
1157+
connection_string_setting: str,
1158+
command_type: Optional[str] = 'Text',
1159+
parameters: Optional[str] = None,
1160+
data_type: Optional[DataType] = None,
1161+
**kwargs) -> Callable[..., Any]:
1162+
"""The mysql_input decorator adds
1163+
:class:`MySqlInput` to the :class:`FunctionBuilder` object
1164+
for building :class:`Function` object used in worker function
1165+
indexing model. This decorator will work only with extension bundle 4.x
1166+
and above.
1167+
This is equivalent to defining MySqlInput in the function.json which
1168+
enables the function to read from a MySql database.
1169+
All optional fields will be given default value by function host when
1170+
they are parsed by function host.
1171+
Ref: https://aka.ms/mysqlbindings
1172+
:param arg_name: The name of the variable that represents a
1173+
:class:`MySqlRowList` input object in function code
1174+
:param command_text: The Transact-SQL query command or name of the
1175+
stored procedure executed by the binding
1176+
:param connection_string_setting: The name of an app setting that
1177+
contains the connection string for the database against which the
1178+
query or stored procedure is being executed
1179+
:param command_type: A CommandType value, which is Text for a query
1180+
and StoredProcedure for a stored procedure
1181+
:param parameters: Zero or more parameter values passed to the
1182+
command during execution as a single string. Must follow the format
1183+
@param1=param1,@param2=param2
1184+
:param data_type: Defines how Functions runtime should treat the
1185+
parameter value
1186+
:param kwargs: Keyword arguments for specifying additional binding
1187+
fields to include in the binding json
1188+
:return: Decorator function.
1189+
"""
1190+
1191+
pass
1192+
1193+
def mysql_output(self,
1194+
arg_name: str,
1195+
command_text: str,
1196+
connection_string_setting: str,
1197+
data_type: Optional[DataType] = None,
1198+
**kwargs) -> Callable[..., Any]:
1199+
"""The mysql_output decorator adds
1200+
:class:`MySqlOutput` to the :class:`FunctionBuilder` object
1201+
for building :class:`Function` object used in worker function
1202+
indexing model. This decorator will work only with extension bundle 4.x
1203+
and above.
1204+
This is equivalent to defining MySqlOutput in the function.json which
1205+
enables the function to write to a MySql database.
1206+
All optional fields will be given default value by function host when
1207+
they are parsed by function host.
1208+
Ref: https://aka.ms/mysqlbindings
1209+
:param arg_name: The name of the variable that represents
1210+
MySql output object in function code
1211+
:param command_text: The Transact-SQL query command or name of the
1212+
stored procedure executed by the binding
1213+
:param connection_string_setting: The name of an app setting that
1214+
contains the connection string for the database against which the
1215+
query or stored procedure is being executed
1216+
:param data_type: Defines how Functions runtime should treat the
1217+
parameter value
1218+
:param kwargs: Keyword arguments for specifying additional binding
1219+
fields to include in the binding json
1220+
:return: Decorator function.
1221+
"""
1222+
1223+
pass
1224+
11151225
def generic_input_binding(self,
11161226
arg_name: str,
11171227
type: str,

0 commit comments

Comments
 (0)