Skip to content

Commit a7034a6

Browse files
Merge pull request #218127 from dzsquared/sqlbindings-java
updates for java library 0.1.1
2 parents 99c48f7 + b752970 commit a7034a6

File tree

3 files changed

+51
-29
lines changed

3 files changed

+51
-29
lines changed

articles/azure-functions/functions-bindings-azure-sql-input.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ public class GetToDoItems {
196196
authLevel = AuthorizationLevel.ANONYMOUS)
197197
HttpRequestMessage<Optional<String>> request,
198198
@SQLInput(
199+
name = "toDoItems",
199200
commandText = "SELECT * FROM dbo.ToDo",
200201
commandType = "Text",
201202
connectionStringSetting = "SqlConnectionString")
@@ -220,6 +221,7 @@ public class GetToDoItem {
220221
authLevel = AuthorizationLevel.ANONYMOUS)
221222
HttpRequestMessage<Optional<String>> request,
222223
@SQLInput(
224+
name = "toDoItems",
223225
commandText = "SELECT * FROM dbo.ToDo",
224226
commandType = "Text",
225227
parameters = "@Id={Query.id}",
@@ -251,6 +253,7 @@ public class DeleteToDo {
251253
authLevel = AuthorizationLevel.ANONYMOUS)
252254
HttpRequestMessage<Optional<String>> request,
253255
@SQLInput(
256+
name = "toDoItems",
254257
commandText = "dbo.DeleteToDo",
255258
commandType = "StoredProcedure",
256259
parameters = "@Id={Query.id}",
@@ -803,6 +806,7 @@ In the [Java functions runtime library](/java/api/overview/azure/functions/runti
803806
| **commandText** | Required. The Transact-SQL query command or name of the stored procedure executed by the binding. |
804807
| **connectionStringSetting** | Required. The name of an app setting that contains the connection string for the database against which the query or stored procedure is being executed. This value isn't the actual connection string and must instead resolve to an environment variable name. |
805808
| **commandType** | Required. A [CommandType](/dotnet/api/system.data.commandtype) value, which is ["Text"](/dotnet/api/system.data.commandtype#fields) for a query and ["StoredProcedure"](/dotnet/api/system.data.commandtype#fields) for a stored procedure. |
809+
|**name** | Required. The unique name of the function binding. |
806810
| **parameters** | Optional. Zero or more parameter values passed to the command during execution as a single string. Must follow the format `@param1=param1,@param2=param2`. Neither the parameter name nor the parameter value can contain a comma (`,`) or an equals sign (`=`). |
807811

808812
::: zone-end

articles/azure-functions/functions-bindings-azure-sql-output.md

Lines changed: 46 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ More samples for the Azure SQL output binding are available in the [GitHub repos
171171
This section contains the following examples:
172172

173173
* [HTTP trigger, write a record to a table](#http-trigger-write-record-to-table-java)
174-
<!-- * [HTTP trigger, write to two tables](#http-trigger-write-to-two-tables-java) -->
174+
* [HTTP trigger, write to two tables](#http-trigger-write-to-two-tables-java)
175175

176176
The examples refer to a `ToDoItem` class (in a separate file `ToDoItem.java`) and a corresponding database table:
177177

@@ -233,6 +233,7 @@ public class PostToDo {
233233
public HttpResponseMessage run(
234234
@HttpTrigger(name = "req", methods = {HttpMethod.POST}, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<String>> request,
235235
@SQLOutput(
236+
name = "toDoItem",
236237
commandText = "dbo.ToDo",
237238
connectionStringSetting = "SqlConnectionString")
238239
OutputBinding<ToDoItem> output) throws JsonParseException, JsonMappingException, JsonProcessingException {
@@ -248,8 +249,6 @@ public class PostToDo {
248249
}
249250
```
250251

251-
<!-- commented out until issue with java library resolved
252-
253252
<a id="http-trigger-write-to-two-tables-java"></a>
254253
### HTTP trigger, write to two tables
255254

@@ -267,9 +266,9 @@ The second table, `dbo.RequestLog`, corresponds to the following definition:
267266

268267
```sql
269268
CREATE TABLE dbo.RequestLog (
270-
Id int identity(1,1) primary key,
271-
RequestTimeStamp datetime2 not null,
272-
ItemCount int not null
269+
Id INT IDENTITY(1,1) PRIMARY KEY,
270+
RequestTimeStamp DATETIME2 NOT NULL DEFAULT(GETDATE()),
271+
ItemCount INT NOT NULL
273272
)
274273
```
275274

@@ -298,32 +297,50 @@ public class RequestLog {
298297

299298

300299
```java
301-
module.exports = async function (context, req) {
302-
context.log('JavaScript HTTP trigger and SQL output binding function processed a request.');
303-
context.log(req.body);
300+
package com.function;
304301

305-
const newLog = {
306-
RequestTimeStamp = Date.now(),
307-
ItemCount = 1
308-
}
302+
import java.util.*;
303+
import com.microsoft.azure.functions.annotation.*;
304+
import com.microsoft.azure.functions.*;
305+
import com.microsoft.azure.functions.sql.annotation.SQLOutput;
306+
import com.fasterxml.jackson.core.JsonParseException;
307+
import com.fasterxml.jackson.core.JsonProcessingException;
308+
import com.fasterxml.jackson.databind.JsonMappingException;
309+
import com.fasterxml.jackson.databind.ObjectMapper;
309310

310-
if (req.body) {
311-
context.bindings.todoItems = req.body;
312-
context.bindings.requestLog = newLog;
313-
context.res = {
314-
body: req.body,
315-
mimetype: "application/json",
316-
status: 201
317-
}
318-
} else {
319-
context.res = {
320-
status: 400,
321-
body: "Error reading request body"
322-
}
311+
import java.util.Optional;
312+
313+
public class PostToDoWithLog {
314+
@FunctionName("PostToDoWithLog")
315+
public HttpResponseMessage run(
316+
@HttpTrigger(name = "req", methods = {HttpMethod.POST}, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<String>> request,
317+
@SQLOutput(
318+
name = "toDoItem",
319+
commandText = "dbo.ToDo",
320+
connectionStringSetting = "SqlConnectionString")
321+
OutputBinding<ToDoItem> output,
322+
@SQLOutput(
323+
name = "requestLog",
324+
commandText = "dbo.RequestLog",
325+
connectionStringSetting = "SqlConnectionString")
326+
OutputBinding<RequestLog> outputLog,
327+
final ExecutionContext context) throws JsonParseException, JsonMappingException, JsonProcessingException {
328+
context.getLogger().info("Java HTTP trigger processed a request.");
329+
330+
String json = request.getBody().get();
331+
ObjectMapper mapper = new ObjectMapper();
332+
ToDoItem newToDo = mapper.readValue(json, ToDoItem.class);
333+
newToDo.Id = UUID.randomUUID();
334+
output.setValue(newToDo);
335+
336+
RequestLog newLog = new RequestLog();
337+
newLog.ItemCount = 1;
338+
outputLog.setValue(newLog);
339+
340+
return request.createResponseBuilder(HttpStatus.CREATED).header("Content-Type", "application/json").body(output).build();
323341
}
324342
}
325-
``` -->
326-
343+
```
327344

328345
::: zone-end
329346

@@ -819,6 +836,7 @@ In the [Java functions runtime library](/java/api/overview/azure/functions/runti
819836
|---------|---------|
820837
| **commandText** | Required. The name of the table being written to by the binding. |
821838
| **connectionStringSetting** | Required. The name of an app setting that contains the connection string for the database to which data is being written. This isn't the actual connection string and must instead resolve to an environment variable.|
839+
|**name** | Required. The unique name of the function binding. |
822840

823841
::: zone-end
824842

articles/azure-functions/functions-bindings-azure-sql.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ Add the Java library for SQL bindings to your functions project with an update t
177177
<dependency>
178178
<groupId>com.microsoft.azure.functions</groupId>
179179
<artifactId>azure-functions-java-library-sql</artifactId>
180-
<version>0.1.0</version>
180+
<version>0.1.1</version>
181181
</dependency>
182182
```
183183

0 commit comments

Comments
 (0)