Skip to content
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

MySql Decorators for Python #269

Draft
wants to merge 1 commit into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions azure/functions/decorators/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,5 @@
ASSISTANT_CREATE = "assistantCreate"
ASSISTANT_POST = "assistantPost"
SEMANTIC_SEARCH = "semanticSearch"
MYSQL = "mysql"
MYSQL_TRIGGER = "mysqlTrigger"
158 changes: 158 additions & 0 deletions azure/functions/decorators/function_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
from .warmup import WarmUpTrigger
from .._http_asgi import AsgiMiddleware
from .._http_wsgi import WsgiMiddleware, Context
from azure.functions.decorators.mysql import MySqlInput, MySqlOutput, MySqlTrigger


class Function(object):
Expand Down Expand Up @@ -1442,6 +1443,59 @@ def decorator():
return decorator()

return wrap

def mysql_trigger(self,
arg_name: str,
table_name: str,
connection_string_setting: str,
leases_table_name: Optional[str] = None,
data_type: Optional[DataType] = None,
**kwargs) -> Callable[..., Any]:
"""The mysql_trigger decorator adds :class:`MySqlTrigger`
to the :class:`FunctionBuilder` object
for building :class:`Function` object used in worker function
indexing model. This decorator will work only with extension bundle 4.x
and above.
This is equivalent to defining MySqlTrigger in the function.json which
enables function to be triggered when there are changes in the MySql
table.
All optional fields will be given default value by function host when
they are parsed by function host.
Ref: https://aka.ms/mysqlbindings
:param arg_name: The name of the variable that represents a
:class:`MySqlRowList` object in the function code
:param table_name: The name of the table monitored by the trigger
:param connection_string_setting: The name of an app setting that
contains the connection string for the database against which the
query or stored procedure is being executed
:param leases_table_name: The name of the table used to store
leases. If not specified, the leases table name will be
Leases_{FunctionId}_{TableId}.
:param data_type: Defines how Functions runtime should treat the
parameter value
:param kwargs: Keyword arguments for specifying additional binding
fields to include in the binding json
:return: Decorator function.
"""

@self._configure_function_builder
def wrap(fb):
def decorator():
fb.add_trigger(
trigger=MySqlTrigger(
name=arg_name,
table_name=table_name,
connection_string_setting=connection_string_setting,
leases_table_name=leases_table_name,
data_type=parse_singular_param_to_enum(data_type,
DataType),
**kwargs))
return fb

return decorator()

return wrap


def generic_trigger(self,
arg_name: str,
Expand Down Expand Up @@ -3551,6 +3605,110 @@ def decorator():
return decorator()

return wrap

def mysql_input(self,
arg_name: str,
command_text: str,
connection_string_setting: str,
command_type: Optional[str] = 'Text',
parameters: Optional[str] = None,
data_type: Optional[DataType] = None,
**kwargs) -> Callable[..., Any]:
"""The mysql_input decorator adds
:class:`MySqlInput` to the :class:`FunctionBuilder` object
for building :class:`Function` object used in worker function
indexing model. This decorator will work only with extension bundle 4.x
and above.
This is equivalent to defining MySqlInput in the function.json which
enables the function to read from a MySql database.
All optional fields will be given default value by function host when
they are parsed by function host.
Ref: https://aka.ms/mysqlbindings
:param arg_name: The name of the variable that represents a
:class:`MySqlRowList` input object in function code
:param command_text: The Transact-SQL query command or name of the
stored procedure executed by the binding
:param connection_string_setting: The name of an app setting that
contains the connection string for the database against which the
query or stored procedure is being executed
:param command_type: A CommandType value, which is Text for a query
and StoredProcedure for a stored procedure
:param parameters: Zero or more parameter values passed to the
command during execution as a single string. Must follow the format
@param1=param1,@param2=param2
:param data_type: Defines how Functions runtime should treat the
parameter value
:param kwargs: Keyword arguments for specifying additional binding
fields to include in the binding json
:return: Decorator function.
"""

@self._configure_function_builder
def wrap(fb):
def decorator():
fb.add_binding(
binding=MySqlInput(
name=arg_name,
command_text=command_text,
connection_string_setting=connection_string_setting,
command_type=command_type,
parameters=parameters,
data_type=parse_singular_param_to_enum(data_type,
DataType),
**kwargs))
return fb

return decorator()

return wrap

def mysql_output(self,
arg_name: str,
command_text: str,
connection_string_setting: str,
data_type: Optional[DataType] = None,
**kwargs) -> Callable[..., Any]:
"""The mysql_output decorator adds
:class:`MySqlOutput` to the :class:`FunctionBuilder` object
for building :class:`Function` object used in worker function
indexing model. This decorator will work only with extension bundle 4.x
and above.
This is equivalent to defining MySqlOutput in the function.json which
enables the function to write to a MySql database.
All optional fields will be given default value by function host when
they are parsed by function host.
Ref: https://aka.ms/mysqlbindings
:param arg_name: The name of the variable that represents
MySql output object in function code
:param command_text: The Transact-SQL query command or name of the
stored procedure executed by the binding
:param connection_string_setting: The name of an app setting that
contains the connection string for the database against which the
query or stored procedure is being executed
:param data_type: Defines how Functions runtime should treat the
parameter value
:param kwargs: Keyword arguments for specifying additional binding
fields to include in the binding json
:return: Decorator function.
"""

@self._configure_function_builder
def wrap(fb):
def decorator():
fb.add_binding(
binding=MySqlOutput(
name=arg_name,
command_text=command_text,
connection_string_setting=connection_string_setting,
data_type=parse_singular_param_to_enum(data_type,
DataType),
**kwargs))
return fb

return decorator()

return wrap



class SettingsApi(DecoratorApi, ABC):
Expand Down
61 changes: 61 additions & 0 deletions azure/functions/decorators/mysql.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
from typing import Optional

from azure.functions.decorators.constants import MYSQL, MYSQL_TRIGGER
from azure.functions.decorators.core import DataType, InputBinding, \
OutputBinding, Trigger


class MySqlInput(InputBinding):
@staticmethod
def get_binding_name() -> str:
return MYSQL

def __init__(self,
name: str,
command_text: str,
connection_string_setting: str,
command_type: Optional[str] = 'Text',
parameters: Optional[str] = None,
data_type: Optional[DataType] = None,
**kwargs):
self.command_text = command_text
self.connection_string_setting = connection_string_setting
self.command_type = command_type
self.parameters = parameters
super().__init__(name=name, data_type=data_type)


class MySqlOutput(OutputBinding):
@staticmethod
def get_binding_name() -> str:
return MYSQL

def __init__(self,
name: str,
command_text: str,
connection_string_setting: str,
data_type: Optional[DataType] = None,
**kwargs):
self.command_text = command_text
self.connection_string_setting = connection_string_setting
super().__init__(name=name, data_type=data_type)


class MySqlTrigger(Trigger):
@staticmethod
def get_binding_name() -> str:
return MYSQL_TRIGGER

def __init__(self,
name: str,
table_name: str,
connection_string_setting: str,
leases_table_name: Optional[str] = None,
data_type: Optional[DataType] = None,
**kwargs):
self.table_name = table_name
self.connection_string_setting = connection_string_setting
self.leases_table_name = leases_table_name
super().__init__(name=name, data_type=data_type)
110 changes: 110 additions & 0 deletions docs/ProgModelSpec.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,45 @@ class TriggerApi(DecoratorApi, ABC):

pass

def mysql_trigger(self,
arg_name: str,
table_name: str,
connection_string_setting: str,
leases_table_name: Optional[str] = None,
data_type: Optional[DataType] = None,
**kwargs) -> Callable[..., Any]:
"""The mysql_trigger decorator adds :class:`MySqlTrigger`
to the :class:`FunctionBuilder` object
for building :class:`Function` object used in worker function
indexing model. This decorator will work only with extension bundle 4.x
and above.
This is equivalent to defining MySqlTrigger in the function.json which
enables function to be triggered when there are changes in the MySql
table.
All optional fields will be given default value by function host when
they are parsed by function host.

Ref: https://aka.ms/mysqlbindings

:param arg_name: The name of the variable that represents a
:class:`MySqlRowList` object in the function code
:param table_name: The name of the table monitored by the trigger
:param connection_string_setting: The name of an app setting that
contains the connection string for the database against which the
query or stored procedure is being executed
:param leases_table_name: The name of the table used to store
leases. If not specified, the leases table name will be
Leases_{FunctionId}_{TableId}.
:param data_type: Defines how Functions runtime should treat the
parameter value
:param kwargs: Keyword arguments for specifying additional binding
fields to include in the binding json

:return: Decorator function.
"""

pass

def generic_trigger(self,
arg_name: str,
type: str,
Expand Down Expand Up @@ -1112,6 +1151,77 @@ class BindingApi(DecoratorApi, ABC):

pass

def mysql_input(self,
arg_name: str,
command_text: str,
connection_string_setting: str,
command_type: Optional[str] = 'Text',
parameters: Optional[str] = None,
data_type: Optional[DataType] = None,
**kwargs) -> Callable[..., Any]:
"""The mysql_input decorator adds
:class:`MySqlInput` to the :class:`FunctionBuilder` object
for building :class:`Function` object used in worker function
indexing model. This decorator will work only with extension bundle 4.x
and above.
This is equivalent to defining MySqlInput in the function.json which
enables the function to read from a MySql database.
All optional fields will be given default value by function host when
they are parsed by function host.
Ref: https://aka.ms/mysqlbindings
:param arg_name: The name of the variable that represents a
:class:`MySqlRowList` input object in function code
:param command_text: The Transact-SQL query command or name of the
stored procedure executed by the binding
:param connection_string_setting: The name of an app setting that
contains the connection string for the database against which the
query or stored procedure is being executed
:param command_type: A CommandType value, which is Text for a query
and StoredProcedure for a stored procedure
:param parameters: Zero or more parameter values passed to the
command during execution as a single string. Must follow the format
@param1=param1,@param2=param2
:param data_type: Defines how Functions runtime should treat the
parameter value
:param kwargs: Keyword arguments for specifying additional binding
fields to include in the binding json
:return: Decorator function.
"""

pass

def mysql_output(self,
arg_name: str,
command_text: str,
connection_string_setting: str,
data_type: Optional[DataType] = None,
**kwargs) -> Callable[..., Any]:
"""The mysql_output decorator adds
:class:`MySqlOutput` to the :class:`FunctionBuilder` object
for building :class:`Function` object used in worker function
indexing model. This decorator will work only with extension bundle 4.x
and above.
This is equivalent to defining MySqlOutput in the function.json which
enables the function to write to a MySql database.
All optional fields will be given default value by function host when
they are parsed by function host.
Ref: https://aka.ms/mysqlbindings
:param arg_name: The name of the variable that represents
MySql output object in function code
:param command_text: The Transact-SQL query command or name of the
stored procedure executed by the binding
:param connection_string_setting: The name of an app setting that
contains the connection string for the database against which the
query or stored procedure is being executed
:param data_type: Defines how Functions runtime should treat the
parameter value
:param kwargs: Keyword arguments for specifying additional binding
fields to include in the binding json
:return: Decorator function.
"""

pass

def generic_input_binding(self,
arg_name: str,
type: str,
Expand Down
Loading
Loading