|
23 | 23 | - [Array](#array) |
24 | 24 | - [Single Row](#single-row) |
25 | 25 | - [Trigger Binding](#trigger-binding) |
| 26 | + - [SqlTriggerAttribute](#sqltriggerattribute) |
| 27 | + - [Setup for Trigger Bindings](#setup-for-trigger-bindings) |
26 | 28 |
|
27 | 29 | ## Binding Model |
28 | 30 |
|
@@ -352,4 +354,60 @@ public static Task<Product> Run( |
352 | 354 |
|
353 | 355 | ## Trigger Binding |
354 | 356 |
|
355 | | -> Trigger binding support is only available for in-proc C# functions at present. |
| 357 | +See [Trigger Binding Overview](./BindingsOverview.md#trigger-binding) for general information about the Azure SQL Trigger binding. |
| 358 | +
|
| 359 | +### SqlTriggerAttribute |
| 360 | +
|
| 361 | +The SqlAttribute for Trigger bindings takes two [arguments](https://github.com/Azure/azure-functions-sql-extension/blob/release/trigger/Worker.Extensions.Sql/src/SqlTriggerAttribute.cs) |
| 362 | +
|
| 363 | +- **TableName**: Represents the name of the table to be monitored for changes. |
| 364 | +- **ConnectionStringSetting**: Specifies the name of the app setting that contains the SQL connection string used to connect to a database. The connection string must follow the format specified [here](https://docs.microsoft.com/dotnet/api/microsoft.data.sqlclient.sqlconnection.connectionstring?view=sqlclient-dotnet-core-2.0). |
| 365 | +
|
| 366 | +The trigger binding can bind to type `IReadOnlyList<SqlChange<T>>`: |
| 367 | +
|
| 368 | +- **IReadOnlyList<SqlChange\<T\>>**: If there are multiple rows updated in the SQL table, the user function will get invoked with a batch of changes, where each element is a `SqlChange` object. Here `T` is a generic type-argument that can be substituted with a user-defined POCO, or Plain Old C# Object, representing the user table row. The POCO should therefore follow the schema of the queried table. See the [Query String](#query-string) section for an example of what the POCO should look like. The two properties of class `SqlChange<T>` are `Item` of type `T` which represents the table row and `Operation` of type `SqlChangeOperation` which indicates the kind of row operation (insert, update, or delete) that triggered the user function. |
| 369 | +
|
| 370 | +Note that for insert and update operations, the user function receives POCO object containing the latest values of table columns. For delete operation, only the properties corresponding to the primary keys of the row are populated. |
| 371 | +
|
| 372 | +Any time when the changes happen to the "Products" table, the user function will be invoked with a batch of changes. The changes are processed sequentially, so if there are a large number of changes pending to be processed, the function will be passed a batch containing the earliest changes first. |
| 373 | +
|
| 374 | +### Setup for Trigger Bindings |
| 375 | +
|
| 376 | +Note: This tutorial requires that a SQL database is setup as shown in [Create a SQL Server](./GeneralSetup.md#create-a-sql-server), and that you have the 'Employee.cs' file from the [Setup for Input Bindings](#setup-for-input-bindings) section. |
| 377 | +
|
| 378 | +- Create a new file with the following content: |
| 379 | +
|
| 380 | + ```csharp |
| 381 | + using System.Collections.Generic; |
| 382 | + using Microsoft.Azure.WebJobs; |
| 383 | + using Microsoft.Extensions.Logging; |
| 384 | + using Microsoft.Azure.WebJobs.Extensions.Sql; |
| 385 | +
|
| 386 | + namespace Company.Function |
| 387 | + { |
| 388 | + public static class EmployeeTrigger |
| 389 | + { |
| 390 | + private static readonly Action<ILogger, string, Exception> _loggerMessage = LoggerMessage.Define<string>(LogLevel.Information, eventId: new EventId(0, "INFO"), formatString: "{Message}"); |
| 391 | + |
| 392 | + [Function("EmployeeTrigger")] |
| 393 | + public static void Run( |
| 394 | + [SqlTrigger("[dbo].[Employees]", "SqlConnectionString")] |
| 395 | + IReadOnlyList<SqlChange<Employee>> changes, FunctionContext context) |
| 396 | + { |
| 397 | + foreach (SqlChange<Employee> change in changes) |
| 398 | + { |
| 399 | + Employee employee = change.Item; |
| 400 | + _loggerMessage(context.GetLogger("EmployeeTrigger"), $"Change operation: {change.Operation}", null); |
| 401 | + _loggerMessage(context.GetLogger("EmployeeTrigger"), $"EmployeeID: {employee.EmployeeId}, FirstName: {employee.FirstName}, LastName: {employee.LastName}, Company: {employee.Company}, Team: {employee.Team}", null); |
| 402 | + } |
| 403 | + } |
| 404 | + } |
| 405 | + } |
| 406 | + ``` |
| 407 | +
|
| 408 | +- *Skip these steps if you have not completed the output binding tutorial.* |
| 409 | + - Open your output binding file and modify some of the values. For example, change the value of Team column from 'Functions' to 'Azure SQL'. |
| 410 | + - Hit 'F5' to run your code. Click the link of the HTTP trigger from the output binding tutorial. |
| 411 | +- Update, insert, or delete rows in your SQL table while the function app is running and observe the function logs. |
| 412 | +- You should see the new log messages in the Visual Studio Code terminal containing the values of row-columns after the update operation. |
| 413 | +- Congratulations! You have successfully created your first SQL trigger binding! |
0 commit comments