Skip to content

Add SQL Decorator for V2 model. #185

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 11 commits into from
Aug 31, 2023
2 changes: 2 additions & 0 deletions azure/functions/decorators/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
EVENT_GRID_TRIGGER = "eventGridTrigger"
EVENT_GRID = "eventGrid"
TABLE = "table"
SQL = "sql"
SQL_TRIGGER = "sqlTrigger"
DAPR_SERVICE_INVOCATION_TRIGGER = "daprServiceInvocationTrigger"
DAPR_BINDING_TRIGGER = "daprBindingTrigger"
DAPR_TOPIC_TRIGGER = "daprTopicTrigger"
Expand Down
165 changes: 165 additions & 0 deletions azure/functions/decorators/function_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from azure.functions.decorators.servicebus import ServiceBusQueueTrigger, \
ServiceBusQueueOutput, ServiceBusTopicTrigger, \
ServiceBusTopicOutput
from azure.functions.decorators.sql import SqlTrigger, SqlInput, SqlOutput
from azure.functions.decorators.table import TableInput, TableOutput
from azure.functions.decorators.timer import TimerTrigger
from azure.functions.decorators.utils import parse_singular_param_to_enum, \
Expand Down Expand Up @@ -1069,6 +1070,61 @@ def decorator():

return wrap

def sql_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 sql_trigger decorator adds :class:`SqlTrigger`
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 SqlTrigger in the function.json which
enables function to be triggered when there are changes in the Sql
table.
All optional fields will be given default value by function host when
they are parsed by function host.

Ref: https://aka.ms/sqlbindings

:param arg_name: The name of the variable that represents a
:class:`SqlRowList` 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=SqlTrigger(
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,
type: str,
Expand Down Expand Up @@ -1859,6 +1915,115 @@ def decorator():

return wrap

def sql_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 sql_input decorator adds
:class:`SqlInput` 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 SqlInput in the function.json which
enables the function to read from a Sql database.
All optional fields will be given default value by function host when
they are parsed by function host.

Ref: https://aka.ms/sqlbindings

:param arg_name: The name of the variable that represents a
:class:`SqlRowList` 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=SqlInput(
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 sql_output(self,
arg_name: str,
command_text: str,
connection_string_setting: str,
data_type: Optional[DataType] = None,
**kwargs) -> Callable[..., Any]:
"""The sql_output decorator adds
:class:`SqlOutput` 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 SqlOutput in the function.json which
enables the function to write to a Sql database.
All optional fields will be given default value by function host when
they are parsed by function host.

Ref: https://aka.ms/sqlbindings

:param arg_name: The name of the variable that represents
Sql 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=SqlOutput(
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

def generic_input_binding(self,
arg_name: str,
type: str,
Expand Down
61 changes: 61 additions & 0 deletions azure/functions/decorators/sql.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 SQL, SQL_TRIGGER
from azure.functions.decorators.core import DataType, InputBinding, \
OutputBinding, Trigger


class SqlInput(InputBinding):
@staticmethod
def get_binding_name() -> str:
return SQL

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 SqlOutput(OutputBinding):
@staticmethod
def get_binding_name() -> str:
return SQL

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 SqlTrigger(Trigger):
@staticmethod
def get_binding_name() -> str:
return SQL_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)
118 changes: 117 additions & 1 deletion docs/ProgModelSpec.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,46 @@ class TriggerApi(DecoratorApi, ABC):
"""

pass


def sql_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 sql_trigger decorator adds :class:`SqlTrigger`
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 SqlTrigger in the function.json which
enables function to be triggered when there are changes in the Sql
table.
All optional fields will be given default value by function host when
they are parsed by function host.

Ref: https://aka.ms/sqlbindings

:param arg_name: The name of the variable that represents a
:class:`SqlRowList` 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 @@ -989,6 +1028,83 @@ class BindingApi(DecoratorApi, ABC):

pass

def sql_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 sql_input decorator adds
:class:`SqlInput` 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 SqlInput in the function.json which
enables the function to read from a Sql database.
All optional fields will be given default value by function host when
they are parsed by function host.

Ref: https://aka.ms/sqlbindings

:param arg_name: The name of the variable that represents a
:class:`SqlRowList` 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 sql_output(self,
arg_name: str,
command_text: str,
connection_string_setting: str,
data_type: Optional[DataType] = None,
**kwargs) -> Callable[..., Any]:
"""The sql_output decorator adds
:class:`SqlOutput` 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 SqlOutput in the function.json which
enables the function to write to a Sql database.
All optional fields will be given default value by function host when
they are parsed by function host.

Ref: https://aka.ms/sqlbindings

:param arg_name: The name of the variable that represents
Sql 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