Skip to content
Merged
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
45 changes: 45 additions & 0 deletions Worker.Extensions.Sql/src/SqlChange.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

namespace Microsoft.Azure.Functions.Worker.Extensions.Sql
{
/// <summary>
/// Represents the changed row in the user table.
/// </summary>
/// <typeparam name="T">POCO class representing the row in the user table</typeparam>
public sealed class SqlChange<T>
{
/// <summary>
/// Initializes a new instance of the <see cref="SqlChange{T}"/> class.
/// </summary>
/// <param name="operation">Change operation</param>
/// <param name="item">POCO representing the row in the user table on which the change operation took place</param>
public SqlChange(SqlChangeOperation operation, T item)
{
this.Operation = operation;
this.Item = item;
}

/// <summary>
/// Change operation (insert, update, or delete).
/// </summary>
public SqlChangeOperation Operation { get; }

/// <summary>
/// POCO representing the row in the user table on which the change operation took place. If the change
/// operation is <see cref="SqlChangeOperation.Delete">, then only the properties corresponding to the primary
/// keys will be populated.
/// </summary>
public T Item { get; }
}

/// <summary>
/// Represents the type of change operation in the table row.
/// </summary>
public enum SqlChangeOperation
{
Insert,
Update,
Delete
}
}
32 changes: 32 additions & 0 deletions Worker.Extensions.Sql/src/SqlTriggerAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using System;
using Microsoft.Azure.Functions.Worker.Extensions.Abstractions;

namespace Microsoft.Azure.Functions.Worker.Extensions.Sql
{
public sealed class SqlTriggerAttribute : TriggerBindingAttribute
{
/// <summary>
/// Initializes a new instance of the <see cref="SqlTriggerAttribute"/> class, which triggers the function when any changes on the specified table are detected.
/// </summary>
/// <param name="tableName">Name of the table to watch for changes.</param>
/// <param name="connectionStringSetting">The name of the app setting where the SQL connection string is stored</param>
public SqlTriggerAttribute(string tableName, string connectionStringSetting)
{
this.TableName = tableName ?? throw new ArgumentNullException(nameof(tableName));
this.ConnectionStringSetting = connectionStringSetting ?? throw new ArgumentNullException(nameof(connectionStringSetting));
}

/// <summary>
/// Name of the app setting containing the SQL connection string.
/// </summary>
public string ConnectionStringSetting { get; }

/// <summary>
/// Name of the table to watch for changes.
/// </summary>
public string TableName { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Timer" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Storage.Queues" />
<PackageReference Include="Microsoft.AspNetCore.Http" />
<PackageReference Include="Newtonsoft.Json" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="../../Worker.Extensions.Sql/src/Microsoft.Azure.Functions.Worker.Extensions.Sql.csproj" />
Expand Down
30 changes: 30 additions & 0 deletions samples/samples-outofproc/TriggerBindingSamples/ProductsTrigger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using System.Collections.Generic;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Extensions.Sql;
using Microsoft.Azure.WebJobs.Extensions.Sql.SamplesOutOfProc.Common;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System;

namespace Microsoft.Azure.WebJobs.Extensions.Sql.SamplesOutOfProc.TriggerBindingSamples
{
public class ProductsTrigger
{
private static readonly Action<ILogger, string, Exception> _loggerMessage = LoggerMessage.Define<string>(LogLevel.Information, eventId: new EventId(0, "INFO"), formatString: "{Message}");

[Function("ProductsTrigger")]
public static void Run(
[SqlTrigger("[dbo].[Products]", "SqlConnectionString")]
IReadOnlyList<SqlChange<Product>> changes, FunctionContext context)
{
// The output is used to inspect the trigger binding parameter in test methods.
if (changes != null && changes.Count > 0)
{
_loggerMessage(context.GetLogger("ProductsTrigger"), "SQL Changes: " + JsonConvert.SerializeObject(changes), null);
}
}
}
}
6 changes: 6 additions & 0 deletions samples/samples-outofproc/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@
"Microsoft.Azure.Functions.Worker.Sdk.Generators": "1.0.0-preview1"
}
},
"Newtonsoft.Json": {
"type": "Direct",
"requested": "[13.0.2, )",
"resolved": "13.0.2",
"contentHash": "R2pZ3B0UjeyHShm9vG+Tu0EBb2lC8b0dFzV9gVn50ofHXh9Smjk6kTn7A/FdAsC8B5cKib1OnGYOXxRBz5XQDg=="
},
"Azure.Core": {
"type": "Transitive",
"resolved": "1.24.0",
Expand Down
2 changes: 1 addition & 1 deletion test/Integration/SqlTriggerBindingIntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public SqlTriggerBindingIntegrationTests(ITestOutputHelper output = null) : base
/// </summary>
[Theory]
[SqlInlineData()]
[UnsupportedLanguages(SupportedLanguages.Java, SupportedLanguages.OutOfProc)]
[UnsupportedLanguages(SupportedLanguages.Java)]
public async Task SingleOperationTriggerTest(SupportedLanguages lang)
{
this.SetChangeTrackingForTable("Products");
Expand Down