Skip to content

Commit 4a06bc8

Browse files
authored
Merge pull request #279189 from JetterMcTedder/main
Update docs for python V2 examples and optional config samples
2 parents 6717d9d + f683e5b commit 4a06bc8

File tree

3 files changed

+273
-20
lines changed

3 files changed

+273
-20
lines changed

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

Lines changed: 97 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
---
22
title: Azure SQL input binding for Functions
33
description: Learn to use the Azure SQL input binding in Azure Functions.
4-
author: dzsquared
4+
author: JetterMcTedder
55
ms.topic: reference
66
ms.custom: build-2023, devx-track-extended-java, devx-track-js, devx-track-python, devx-track-ts
7-
ms.date: 4/17/2023
8-
ms.author: drskwier
7+
ms.date: 6/20/2024
8+
ms.author: bspendolini
99
ms.reviewer: glenga
1010
zone_pivot_groups: programming-languages-set-functions
1111
---
@@ -413,7 +413,7 @@ The following example shows a SQL input binding that is [triggered by an HTTP](.
413413

414414
# [Model v3](#tab/nodejs-v3)
415415

416-
TypeScript samples are not documented for model v3.
416+
TypeScript samples aren't documented for model v3.
417417

418418
---
419419

@@ -488,7 +488,7 @@ The following example shows a SQL input binding that is [triggered by an HTTP](.
488488

489489
# [Model v3](#tab/nodejs-v3)
490490

491-
TypeScript samples are not documented for model v3.
491+
TypeScript samples aren't documented for model v3.
492492

493493
---
494494

@@ -568,7 +568,7 @@ The stored procedure `dbo.DeleteToDo` must be created on the database. In this
568568

569569
# [Model v3](#tab/nodejs-v3)
570570

571-
TypeScript samples are not documented for model v3.
571+
TypeScript samples aren't documented for model v3.
572572

573573
---
574574

@@ -816,6 +816,34 @@ The examples refer to a database table:
816816

817817
The following example shows a SQL input binding in a function.json file and a Python function that is [triggered by an HTTP](./functions-bindings-http-webhook-trigger.md) request and reads from a query and returns the results in the HTTP response.
818818

819+
# [v2](#tab/python-v2)
820+
821+
```python
822+
import json
823+
import logging
824+
import azure.functions as func
825+
from azure.functions.decorators.core import DataType
826+
827+
app = func.FunctionApp()
828+
829+
@app.function_name(name="GetToDo")
830+
@app.route(route="gettodo")
831+
@app.sql_input(arg_name="todo",
832+
command_text="select [Id], [order], [title], [url], [completed] from dbo.ToDo",
833+
command_type="Text",
834+
connection_string_setting="SqlConnectionString")
835+
def get_todo(req: func.HttpRequest, todo: func.SqlRowList) -> func.HttpResponse:
836+
rows = list(map(lambda r: json.loads(r.to_json()), todo))
837+
838+
return func.HttpResponse(
839+
json.dumps(rows),
840+
status_code=200,
841+
mimetype="application/json"
842+
)
843+
```
844+
845+
# [v1](#tab/python-v1)
846+
819847
The following is binding data in the function.json file:
820848

821849
```json
@@ -862,11 +890,42 @@ def main(req: func.HttpRequest, todoItems: func.SqlRowList) -> func.HttpResponse
862890
)
863891
```
864892

893+
---
894+
865895
<a id="http-trigger-look-up-id-from-query-string-python"></a>
866896
### HTTP trigger, get row by ID from query string
867897

868898
The following example shows a SQL input binding in a Python function that is [triggered by an HTTP](./functions-bindings-http-webhook-trigger.md) request and reads from a query filtered by a parameter from the query string and returns the row in the HTTP response.
869899

900+
# [v2](#tab/python-v2)
901+
902+
```python
903+
import json
904+
import logging
905+
import azure.functions as func
906+
from azure.functions.decorators.core import DataType
907+
908+
app = func.FunctionApp()
909+
910+
@app.function_name(name="GetToDo")
911+
@app.route(route="gettodo/{id}")
912+
@app.sql_input(arg_name="todo",
913+
command_text="select [Id], [order], [title], [url], [completed] from dbo.ToDo where Id = @Id",
914+
command_type="Text",
915+
parameters="@Id={id}",
916+
connection_string_setting="SqlConnectionString")
917+
def get_todo(req: func.HttpRequest, todo: func.SqlRowList) -> func.HttpResponse:
918+
rows = list(map(lambda r: json.loads(r.to_json()), todo))
919+
920+
return func.HttpResponse(
921+
json.dumps(rows),
922+
status_code=200,
923+
mimetype="application/json"
924+
)
925+
```
926+
927+
# [v1](#tab/python-v1)
928+
870929
The following is binding data in the function.json file:
871930

872931
```json
@@ -914,6 +973,7 @@ def main(req: func.HttpRequest, todoItem: func.SqlRowList) -> func.HttpResponse:
914973
)
915974
```
916975

976+
---
917977

918978
<a id="http-trigger-delete-one-or-multiple-rows-python"></a>
919979
### HTTP trigger, delete rows
@@ -924,6 +984,34 @@ The stored procedure `dbo.DeleteToDo` must be created on the database. In this
924984

925985
:::code language="sql" source="~/functions-sql-todo-sample/sql/create.sql" range="11-25":::
926986

987+
# [v2](#tab/python-v2)
988+
989+
```python
990+
import json
991+
import logging
992+
import azure.functions as func
993+
from azure.functions.decorators.core import DataType
994+
995+
app = func.FunctionApp()
996+
997+
@app.function_name(name="DeleteToDo")
998+
@app.route(route="deletetodo/{id}")
999+
@app.sql_input(arg_name="todo",
1000+
command_text="DeleteToDo",
1001+
command_type="StoredProcedure",
1002+
parameters="@Id={id}",
1003+
connection_string_setting="SqlConnectionString")
1004+
def get_todo(req: func.HttpRequest, todo: func.SqlRowList) -> func.HttpResponse:
1005+
rows = list(map(lambda r: json.loads(r.to_json()), todo))
1006+
1007+
return func.HttpResponse(
1008+
json.dumps(rows),
1009+
status_code=200,
1010+
mimetype="application/json"
1011+
)
1012+
```
1013+
1014+
# [v1](#tab/python-v1)
9271015

9281016
```json
9291017
{
@@ -970,6 +1058,8 @@ def main(req: func.HttpRequest, todoItems: func.SqlRowList) -> func.HttpResponse
9701058
)
9711059
```
9721060

1061+
---
1062+
9731063
::: zone-end
9741064

9751065

@@ -1058,7 +1148,7 @@ The attribute's constructor takes the SQL command text, the command type, parame
10581148

10591149
Queries executed by the input binding are [parameterized](/dotnet/api/microsoft.data.sqlclient.sqlparameter) in Microsoft.Data.SqlClient to reduce the risk of [SQL injection](/sql/relational-databases/security/sql-injection) from the parameter values passed into the binding.
10601150

1061-
If an exception occurs when a SQL input binding is executed then the function code will not execute. This may result in an error code being returned, such as an HTTP trigger returning a 500 error code.
1151+
If an exception occurs when a SQL input binding is executed then the function code won't execute. This may result in an error code being returned, such as an HTTP trigger returning a 500 error code.
10621152

10631153
## Next steps
10641154

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

Lines changed: 85 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
---
22
title: Azure SQL output binding for Functions
33
description: Learn to use the Azure SQL output binding in Azure Functions.
4-
author: dzsquared
4+
author: JetterMcTedder
55
ms.topic: reference
66
ms.custom: build-2023, devx-track-extended-java, devx-track-js, devx-track-python, devx-track-ts
7-
ms.date: 4/17/2023
8-
ms.author: drskwier
7+
ms.date: 6/20/2024
8+
ms.author: bspendolini
99
ms.reviewer: glenga
1010
zone_pivot_groups: programming-languages-set-functions
1111
---
@@ -44,7 +44,7 @@ The examples refer to a `ToDoItem` class and a corresponding database table:
4444

4545
:::code language="sql" source="~/functions-sql-todo-sample/sql/create.sql" range="1-7":::
4646

47-
To return [multiple output bindings](./dotnet-isolated-process-guide.md#multiple-output-bindings) in our samples, we will create a custom return type:
47+
To return [multiple output bindings](./dotnet-isolated-process-guide.md#multiple-output-bindings) in our samples, we'll create a custom return type:
4848

4949
```cs
5050
public static class OutputType
@@ -539,7 +539,7 @@ The following example shows a SQL output binding that adds records to a table, u
539539

540540
# [Model v3](#tab/nodejs-v3)
541541

542-
TypeScript samples are not documented for model v3.
542+
TypeScript samples aren't documented for model v3.
543543

544544
---
545545

@@ -621,7 +621,7 @@ CREATE TABLE dbo.RequestLog (
621621

622622
# [Model v3](#tab/nodejs-v3)
623623

624-
TypeScript samples are not documented for model v3.
624+
TypeScript samples aren't documented for model v3.
625625

626626
---
627627

@@ -860,6 +860,35 @@ The examples refer to a database table:
860860

861861
The following example shows a SQL output binding in a function.json file and a Python function that adds records to a table, using data provided in an HTTP POST request as a JSON body.
862862

863+
# [v2](#tab/python-v2)
864+
865+
```python
866+
import json
867+
import logging
868+
import azure.functions as func
869+
from azure.functions.decorators.core import DataType
870+
871+
app = func.FunctionApp()
872+
873+
@app.function_name(name="AddToDo")
874+
@app.route(route="addtodo")
875+
@app.sql_output(arg_name="todo",
876+
command_text="[dbo].[ToDo]",
877+
connection_string_setting="SqlConnectionString")
878+
def add_todo(req: func.HttpRequest, todo: func.Out[func.SqlRow]) -> func.HttpResponse:
879+
body = json.loads(req.get_body())
880+
row = func.SqlRow.from_dict(body)
881+
todo.set(row)
882+
883+
return func.HttpResponse(
884+
body=req.get_body(),
885+
status_code=201,
886+
mimetype="application/json"
887+
)
888+
```
889+
890+
# [v1](#tab/python-v1)
891+
863892
The following is binding data in the function.json file:
864893

865894
```json
@@ -918,6 +947,8 @@ def main(req: func.HttpRequest, todoItems: func.Out[func.SqlRow]) -> func.HttpRe
918947
)
919948
```
920949

950+
---
951+
921952
<a id="http-trigger-write-to-two-tables-python"></a>
922953
### HTTP trigger, write to two tables
923954

@@ -933,6 +964,53 @@ CREATE TABLE dbo.RequestLog (
933964
)
934965
```
935966

967+
# [v2](#tab/python-v2)
968+
969+
```python
970+
from datetime import datetime
971+
import json
972+
import logging
973+
import azure.functions as func
974+
975+
app = func.FunctionApp()
976+
977+
@app.function_name(name="PostToDo")
978+
@app.route(route="posttodo")
979+
@app.sql_output(arg_name="todoItems",
980+
command_text="[dbo].[ToDo]",
981+
connection_string_setting="SqlConnectionString")
982+
@app.sql_output(arg_name="requestLog",
983+
command_text="[dbo].[RequestLog]",
984+
connection_string_setting="SqlConnectionString")
985+
def add_todo(req: func.HttpRequest, todoItems: func.Out[func.SqlRow], requestLog: func.Out[func.SqlRow]) -> func.HttpResponse:
986+
logging.info('Python HTTP trigger and SQL output binding function processed a request.')
987+
try:
988+
req_body = req.get_json()
989+
rows = func.SqlRowList(map(lambda r: func.SqlRow.from_dict(r), req_body))
990+
except ValueError:
991+
pass
992+
993+
requestLog.set(func.SqlRow({
994+
"RequestTimeStamp": datetime.now().isoformat(),
995+
"ItemCount": 1
996+
}))
997+
998+
if req_body:
999+
todoItems.set(rows)
1000+
return func.HttpResponse(
1001+
"OK",
1002+
status_code=201,
1003+
mimetype="application/json"
1004+
)
1005+
else:
1006+
return func.HttpResponse(
1007+
"Error accessing request body",
1008+
status_code=400
1009+
)
1010+
```
1011+
1012+
# [v1](#tab/python-v1)
1013+
9361014
The following is binding data in the function.json file:
9371015

9381016
```json
@@ -1004,6 +1082,7 @@ def main(req: func.HttpRequest, todoItems: func.Out[func.SqlRow], requestLog: fu
10041082
)
10051083
```
10061084

1085+
---
10071086

10081087
::: zone-end
10091088

0 commit comments

Comments
 (0)