Skip to content

Commit e29a8b2

Browse files
committed
updates for java library 0.1.1
1 parent eaa2d19 commit e29a8b2

File tree

3 files changed

+49
-29
lines changed

3 files changed

+49
-29
lines changed

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

Lines changed: 3 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 = "sqlInput1",
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 = "sqlInput2",
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 = "sqlInput3",
254257
commandText = "dbo.DeleteToDo",
255258
commandType = "StoredProcedure",
256259
parameters = "@Id={Query.id}",

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

Lines changed: 45 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 = "sqlOutput",
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 = "sqlOutput",
319+
commandText = "dbo.ToDo",
320+
connectionStringSetting = "SqlConnectionString")
321+
OutputBinding<ToDoItem> output,
322+
@SQLOutput(
323+
name = "sqlOutput2",
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

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

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

0 commit comments

Comments
 (0)