Skip to content

Commit b3b1939

Browse files
authored
Merge pull request #486 from Azure/luczhan/multiplebindingsjava
Add Java samples with multiple bindings
2 parents 08f2348 + e9ae0be commit b3b1939

File tree

5 files changed

+118
-4
lines changed

5 files changed

+118
-4
lines changed

.gitignore

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -360,5 +360,9 @@ __azurite_db*__.json
360360
# Python virtual environment
361361
.venv
362362

363-
# Mac OS X
364-
.DS_Store
363+
# Mac OS X
364+
.DS_Store
365+
366+
# Java build files
367+
repo/
368+
target/

samples/samples-java/.gitignore

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
# Build output
2-
target/
3-
repo/
42
*.class
53

64
# Log file

samples/samples-java/src/main/java/com/function/AddProductToTwoTables.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
import java.io.IOException;
2424
import java.util.Optional;
2525

26+
/**
27+
* This function uses two SQL output bindings to upsert the product in the request body to the
28+
* Products and ProductsWithIdentity tables.
29+
*/
2630
public class AddProductToTwoTables {
2731
@FunctionName("AddProductToTwoTables")
2832
public HttpResponseMessage run(
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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.function;
8+
9+
import com.function.Common.Product;
10+
import com.microsoft.azure.functions.HttpMethod;
11+
import com.microsoft.azure.functions.HttpRequestMessage;
12+
import com.microsoft.azure.functions.HttpResponseMessage;
13+
import com.microsoft.azure.functions.HttpStatus;
14+
import com.microsoft.azure.functions.OutputBinding;
15+
import com.microsoft.azure.functions.annotation.AuthorizationLevel;
16+
import com.microsoft.azure.functions.annotation.FunctionName;
17+
import com.microsoft.azure.functions.annotation.HttpTrigger;
18+
import com.microsoft.azure.functions.sql.annotation.SQLInput;
19+
import com.microsoft.azure.functions.sql.annotation.SQLOutput;
20+
21+
import java.util.Optional;
22+
23+
/**
24+
* This function uses a SQL input binding to get products from the Products table
25+
* and upsert those products to the ProductsWithIdentity table.
26+
*/
27+
public class GetAndAddProducts {
28+
@FunctionName("GetAndAddProducts")
29+
public HttpResponseMessage run(
30+
@HttpTrigger(
31+
name = "req",
32+
methods = {HttpMethod.GET},
33+
authLevel = AuthorizationLevel.ANONYMOUS,
34+
route = "getandaddproducts/{cost}")
35+
HttpRequestMessage<Optional<String>> request,
36+
@SQLInput(
37+
name = "products",
38+
commandText = "SELECT * FROM Products WHERE Cost = @Cost",
39+
commandType = "Text",
40+
parameters = "@Cost={cost}",
41+
connectionStringSetting = "SqlConnectionString")
42+
Product[] products,
43+
@SQLOutput(
44+
name = "productsWithIdentity",
45+
commandText = "ProductsWithIdentity",
46+
connectionStringSetting = "SqlConnectionString")
47+
OutputBinding<Product[]> productsWithIdentity) {
48+
49+
productsWithIdentity.setValue(products);
50+
51+
return request.createResponseBuilder(HttpStatus.OK).header("Content-Type", "application/json").body(products).build();
52+
}
53+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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.function;
8+
9+
import com.function.Common.Product;
10+
import com.microsoft.azure.functions.HttpMethod;
11+
import com.microsoft.azure.functions.HttpRequestMessage;
12+
import com.microsoft.azure.functions.HttpResponseMessage;
13+
import com.microsoft.azure.functions.HttpStatus;
14+
import com.microsoft.azure.functions.annotation.AuthorizationLevel;
15+
import com.microsoft.azure.functions.annotation.FunctionName;
16+
import com.microsoft.azure.functions.annotation.HttpTrigger;
17+
import com.microsoft.azure.functions.sql.annotation.SQLInput;
18+
19+
import java.util.Optional;
20+
21+
/**
22+
* This function uses two SQL input bindings to get products from the Products and ProductsWithIdentity tables
23+
* and returns the combined results as a JSON array in the body of the HttpResponseMessage.
24+
*/
25+
public class GetProductsFromTwoTables {
26+
@FunctionName("GetProductsFromTwoTables")
27+
public HttpResponseMessage run(
28+
@HttpTrigger(
29+
name = "req",
30+
methods = {HttpMethod.GET},
31+
authLevel = AuthorizationLevel.ANONYMOUS,
32+
route = "getproductsfromtwotables/{cost}")
33+
HttpRequestMessage<Optional<String>> request,
34+
@SQLInput(
35+
name = "products",
36+
commandText = "SELECT * FROM Products WHERE Cost = @Cost",
37+
commandType = "Text",
38+
parameters = "@Cost={cost}",
39+
connectionStringSetting = "SqlConnectionString")
40+
Product[] products,
41+
@SQLInput(
42+
name = "productsWithIdentity",
43+
commandText = "SELECT * FROM ProductsWithIdentity WHERE Cost = @Cost",
44+
commandType = "Text",
45+
parameters = "@Cost={cost}",
46+
connectionStringSetting = "SqlConnectionString")
47+
Product[] productsWithIdentity) {
48+
49+
Product[] allProducts = new Product[products.length + productsWithIdentity.length];
50+
System.arraycopy(products, 0, allProducts, 0, products.length);
51+
System.arraycopy(productsWithIdentity, 0, allProducts, products.length, productsWithIdentity.length);
52+
53+
return request.createResponseBuilder(HttpStatus.OK).header("Content-Type", "application/json").body(allProducts).build();
54+
}
55+
}

0 commit comments

Comments
 (0)