Skip to content

Commit 373344c

Browse files
Merge pull request #398 from Azure/chgagnon/mergeFromMain
Merge latest from main into triggerbindings
2 parents a9012b8 + f2bec01 commit 373344c

File tree

19 files changed

+395
-35
lines changed

19 files changed

+395
-35
lines changed

Directory.Build.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@
1010
<DelaySign>True</DelaySign>
1111
<SignAssembly>True</SignAssembly>
1212
<AssemblyOriginatorKeyFile>$(MSBuildThisFileDirectory)/SQL2003.snk</AssemblyOriginatorKeyFile>
13+
<IsPackable>false</IsPackable>
1314
</PropertyGroup>
1415
</Project>

README.md

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ Azure SQL bindings for Azure Functions are supported for:
5555
- [ICollector&lt;T&gt;/IAsyncCollector&lt;T&gt;](#icollectortiasynccollectort)
5656
- [Array](#array)
5757
- [Single Row](#single-row)
58-
- [Primary Keys and Identity Columns](#primary-keys-and-identity-columns)
58+
- [Primary Key Special Cases](#primary-key-special-cases)
59+
- [Identity Columns](#identity-columns)
60+
- [Columns with Default Values](#columns-with-default-values)
5961
- [Trigger Binding](#trigger-binding)
6062
- [Change Tracking](#change-tracking)
6163
- [Internal State Tables](#internal-state-tables)
@@ -829,20 +831,26 @@ public static IActionResult Run(
829831
}
830832
```
831833
832-
#### Primary Keys and Identity Columns
834+
#### Primary Key Special Cases
833835
834836
Normally Output Bindings require two things :
835837
836838
1. The table being upserted to contains a Primary Key constraint (composed of one or more columns)
837839
2. Each of those columns must be present in the POCO object used in the attribute
838840
839-
If either of these are false then an error will be thrown.
841+
Normally if either of these are false then an error will be thrown. Below are the situations in which this is not the case :
840842
841-
This changes if one of the primary key columns is an identity column though. In that case there are two options based on how the function defines the output object:
843+
##### Identity Columns
844+
In the case where one of the primary key columns is an identity column, there are two options based on how the function defines the output object:
842845
843846
1. If the identity column isn't included in the output object then a straight insert is always performed with the other column values. See [AddProductWithIdentityColumn](./samples/samples-csharp/OutputBindingSamples/AddProductWithIdentityColumn.cs) for an example.
844847
2. If the identity column is included (even if it's an optional nullable value) then a merge is performed similar to what happens when no identity column is present. This merge will either insert a new row or update an existing row based on the existence of a row that matches the primary keys (including the identity column). See [AddProductWithIdentityColumnIncluded](./samples/samples-csharp/OutputBindingSamples/AddProductWithIdentityColumnIncluded.cs) for an example.
845848
849+
##### Columns with Default Values
850+
In the case where one of the primary key columns has a default value, there are also two options based on how the function defines the output object:
851+
1. If the column with a default value is not included in the output object, then a straight insert is always performed with the other values. See [AddProductWithDefaultPK](./samples/samples-csharp/OutputBindingSamples/AddProductWithDefaultPK.cs) for an example.
852+
2. If the column with a default value is included then a merge is performed similar to what happens when no default column is present. If there is a nullable column with a default value, then the provided column value in the output object will be upserted even if it is null.
853+
846854
### Trigger Binding
847855
848856
> **NOTE:** Trigger binding support is only available for C# functions at present.

builds/azure-pipelines/template-steps-build-test.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ steps:
3232
displayName: 'Set npm installation path for Windows'
3333
condition: and(succeeded(), eq(variables['Agent.OS'], 'Windows_NT'))
3434

35+
- bash: echo "##vso[task.setvariable variable=azureFunctionsExtensionBundlePath]$(func GetExtensionBundlePath)"
36+
displayName: 'Set Azure Functions extension bundle path'
37+
workingDirectory: $(Build.SourcesDirectory)/samples/samples-js
38+
3539
- task: DockerInstaller@0
3640
displayName: Docker Installer
3741
inputs:
@@ -67,6 +71,14 @@ steps:
6771
projects: '${{ parameters.solution }}'
6872
arguments: '--configuration ${{ parameters.configuration }} -p:GeneratePackageOnBuild=false -p:Version=${{ parameters.binariesVersion }}'
6973

74+
- task: CopyFiles@2
75+
displayName: 'Copy Sql extension dll to Azure Functions extension bundle'
76+
inputs:
77+
sourceFolder: $(Build.SourcesDirectory)/src/bin/${{ parameters.configuration }}/netstandard2.0
78+
contents: Microsoft.Azure.WebJobs.Extensions.Sql.dll
79+
targetFolder: $(azureFunctionsExtensionBundlePath)/bin
80+
overWrite: true
81+
7082
- script: |
7183
npm install
7284
npm run lint

java-library/pom.xml

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>com.microsoft.azure.functions</groupId>
6+
<artifactId>azure-functions-java-library-sql</artifactId>
7+
<version>0.0.0</version>
8+
<packaging>jar</packaging>
9+
10+
<parent>
11+
<groupId>com.microsoft.maven</groupId>
12+
<artifactId>java-8-parent</artifactId>
13+
<version>8.0.1</version>
14+
</parent>
15+
16+
<name>Microsoft Azure Functions Java SQL Types</name>
17+
<description>This package contains all Java annotations to interact with Microsoft Azure Functions runtime for SQL Bindings.</description>
18+
<url>https://aka.ms/sqlbindings</url>
19+
<organization>
20+
<name>Microsoft Azure</name>
21+
<url>https://azure.microsoft.com</url>
22+
</organization>
23+
24+
<properties>
25+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
26+
</properties>
27+
28+
<licenses>
29+
<license>
30+
<name>MIT License</name>
31+
<url>https://opensource.org/licenses/MIT</url>
32+
<distribution>repo</distribution>
33+
</license>
34+
</licenses>
35+
36+
<scm>
37+
<connection>scm:git:https://github.com/Azure/azure-functions-sql-extension</connection>
38+
<developerConnection>scm:git:git@github.com:Azure/azure-functions-sql-extension</developerConnection>
39+
<url>https://github.com/Azure/azure-functions-sql-extension</url>
40+
<tag>HEAD</tag>
41+
</scm>
42+
43+
<developers>
44+
<developer>
45+
<id>LucyZhang</id>
46+
<name>Lucy Zhang</name>
47+
<email>luczhan@microsoft.com</email>
48+
</developer>
49+
</developers>
50+
51+
<distributionManagement>
52+
<snapshotRepository>
53+
<id>ossrh</id>
54+
<name>Sonatype Snapshots</name>
55+
<url>https://oss.sonatype.org/content/repositories/snapshots/</url>
56+
<uniqueVersion>true</uniqueVersion>
57+
<layout>default</layout>
58+
</snapshotRepository>
59+
</distributionManagement>
60+
61+
<repositories>
62+
<repository>
63+
<id>maven.snapshots</id>
64+
<name>Maven Central Snapshot Repository</name>
65+
<url>https://oss.sonatype.org/content/repositories/snapshots/</url>
66+
<releases>
67+
<enabled>false</enabled>
68+
</releases>
69+
<snapshots>
70+
<enabled>true</enabled>
71+
</snapshots>
72+
</repository>
73+
</repositories>
74+
75+
<dependencies>
76+
<dependency>
77+
<groupId>com.microsoft.azure.functions</groupId>
78+
<artifactId>azure-functions-java-library</artifactId>
79+
<version>1.4.2</version>
80+
</dependency>
81+
</dependencies>
82+
83+
<build>
84+
<plugins>
85+
<plugin>
86+
<artifactId>maven-compiler-plugin</artifactId>
87+
<version>${maven-compiler.version}</version>
88+
</plugin>
89+
<plugin>
90+
<groupId>org.apache.maven.plugins</groupId>
91+
<artifactId>maven-source-plugin</artifactId>
92+
<version>${maven-source.version}</version>
93+
<executions>
94+
<execution>
95+
<id>attach-sources</id>
96+
<goals>
97+
<goal>jar</goal>
98+
</goals>
99+
</execution>
100+
</executions>
101+
</plugin>
102+
<plugin>
103+
<groupId>org.apache.maven.plugins</groupId>
104+
<artifactId>maven-javadoc-plugin</artifactId>
105+
<version>${maven-javadoc.version}</version>
106+
<configuration>
107+
<doclint>none</doclint>
108+
</configuration>
109+
<executions>
110+
<execution>
111+
<id>attach-javadocs</id>
112+
<goals>
113+
<goal>jar</goal>
114+
</goals>
115+
</execution>
116+
</executions>
117+
</plugin>
118+
</plugins>
119+
</build>
120+
</project>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for
4+
* license information.
5+
*/
6+
7+
package com.microsoft.azure.functions.sql.annotation;
8+
9+
import java.lang.annotation.Retention;
10+
import java.lang.annotation.Target;
11+
import java.lang.annotation.RetentionPolicy;
12+
import java.lang.annotation.ElementType;
13+
14+
import com.microsoft.azure.functions.annotation.CustomBinding;
15+
16+
@Retention(RetentionPolicy.RUNTIME)
17+
@Target(ElementType.PARAMETER)
18+
@CustomBinding(direction = "in", name = "sql", type = "sql")
19+
public @interface SQLInput {
20+
/**
21+
* Query string or name of stored procedure to be run.
22+
*/
23+
String commandText() default "";
24+
25+
/**
26+
* Text or Stored Procedure.
27+
*/
28+
String commandType() default "";
29+
30+
/**
31+
* Parameters to the query or stored procedure. This string must follow the format
32+
* "@param1=param1,@param2=param2" where @param1 is the name of the parameter and
33+
* param1 is the parameter value.
34+
*/
35+
String parameters() default "";
36+
37+
/**
38+
* Setting name for SQL connection string.
39+
*/
40+
String connectionStringSetting() default "";
41+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for
4+
* license information.
5+
*/
6+
7+
package com.microsoft.azure.functions.sql.annotation;
8+
9+
import java.lang.annotation.Retention;
10+
import java.lang.annotation.Target;
11+
import java.lang.annotation.RetentionPolicy;
12+
import java.lang.annotation.ElementType;
13+
14+
import com.microsoft.azure.functions.annotation.CustomBinding;
15+
16+
17+
@Retention(RetentionPolicy.RUNTIME)
18+
@Target({ ElementType.PARAMETER, ElementType.METHOD })
19+
@CustomBinding(direction = "out", name = "sql", type = "sql")
20+
public @interface SQLOutput {
21+
/**
22+
* Name of the table to upsert data to.
23+
*/
24+
String commandText() default "";
25+
26+
/**
27+
* Setting name for SQL connection string.
28+
*/
29+
String connectionStringSetting() default "";
30+
}

samples/samples-csharp/Common/Product.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,11 @@ public class ProductName
2525
{
2626
public string Name { get; set; }
2727
}
28+
29+
public class ProductWithDefaultPK
30+
{
31+
public string Name { get; set; }
32+
33+
public int Cost { get; set; }
34+
}
2835
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
CREATE TABLE [ProductsWithDefaultPK] (
2+
[ProductGuid] [uniqueidentifier] PRIMARY KEY NOT NULL DEFAULT(newsequentialid()),
3+
[Name] [nvarchar](100) NULL,
4+
[Cost] [int] NULL
5+
)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License. See License.txt in the project root for license information.
3+
4+
using Microsoft.AspNetCore.Mvc;
5+
using Microsoft.Azure.WebJobs.Extensions.Http;
6+
using Microsoft.Azure.WebJobs.Extensions.Sql.Samples.Common;
7+
8+
namespace Microsoft.Azure.WebJobs.Extensions.Sql.Samples.OutputBindingSamples
9+
{
10+
11+
public static class AddProductWithDefaultPK
12+
{
13+
/// <summary>
14+
/// This shows an example of a SQL Output binding where the target table has a default primary key
15+
/// of type uniqueidentifier and the column is not included in the output object. A new row will
16+
/// be inserted and the uniqueidentifier will be generated by the engine.
17+
/// </summary>
18+
/// <param name="req">The original request that triggered the function</param>
19+
/// <param name="product">The created ProductWithDefaultPK object</param>
20+
/// <returns>The CreatedResult containing the new object that was inserted</returns>
21+
[FunctionName(nameof(AddProductWithDefaultPK))]
22+
public static IActionResult Run(
23+
[HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "addproductwithdefaultpk")]
24+
[FromBody] ProductWithDefaultPK product,
25+
[Sql("dbo.ProductsWithDefaultPK", ConnectionStringSetting = "SqlConnectionString")] out ProductWithDefaultPK output)
26+
{
27+
output = product;
28+
return new CreatedResult($"/api/addproductwithdefaultpk", output);
29+
}
30+
}
31+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"bindings": [
3+
{
4+
"authLevel": "function",
5+
"name": "req",
6+
"direction": "in",
7+
"type": "httpTrigger",
8+
"methods": [
9+
"post"
10+
],
11+
"route": "addproductwithdefaultpk"
12+
},
13+
{
14+
"name": "$return",
15+
"type": "http",
16+
"direction": "out"
17+
},
18+
{
19+
"name": "products",
20+
"type": "sql",
21+
"direction": "out",
22+
"commandText": "[dbo].[ProductsWithDefaultPK]",
23+
"connectionStringSetting": "SqlConnectionString"
24+
}
25+
],
26+
"disabled": false
27+
}

0 commit comments

Comments
 (0)