diff --git a/docs/SetupGuide_Python.md b/docs/SetupGuide_Python.md index 3e2bf9fea..bfc2ad2f2 100644 --- a/docs/SetupGuide_Python.md +++ b/docs/SetupGuide_Python.md @@ -13,12 +13,10 @@ - [Empty Parameter Value](#empty-parameter-value) - [Null Parameter Value](#null-parameter-value) - [Stored Procedure](#stored-procedure) - - [IAsyncEnumerable](#iasyncenumerable) - [Output Binding](#output-binding) - [function.json Properties for Output Bindings](#functionjson-properties-for-output-bindings) - [Setup for Output Bindings](#setup-for-output-bindings) - [Samples for Output Bindings](#samples-for-output-bindings) - - [ICollector\/IAsyncCollector\](#icollectortiasynccollectort) - [Array](#array) - [Single Row](#single-row) - [Trigger Binding](#trigger-binding) @@ -115,23 +113,19 @@ Note: This tutorial requires that a SQL database is setup as shown in [Create a #### Query String -_TODO_ +See the [GetProducts](https://github.com/Azure/azure-functions-sql-extension/tree/main/samples/samples-python/GetProducts) sample #### Empty Parameter Value -_TODO_ +See the [GetProductsNameEmpty](https://github.com/Azure/azure-functions-sql-extension/tree/main/samples/samples-python/GetProductsNameEmpty) sample #### Null Parameter Value -_TODO_ +See the [GetProductsNameNull](https://github.com/Azure/azure-functions-sql-extension/tree/main/samples/samples-python/GetProductsNameNull) sample #### Stored Procedure -_TODO_ - -#### IAsyncEnumerable - -_TODO_ +See the [GetProductsStoredProcedure](https://github.com/Azure/azure-functions-sql-extension/tree/main/samples/samples-python/GetProductsStoredProcedure) sample ## Output Binding @@ -153,7 +147,7 @@ The following table explains the binding configuration properties that you set i Note: This tutorial requires that a SQL database is setup as shown in [Create a SQL Server](./GeneralSetup.md#create-a-sql-server). -- Open your app in VS Code +- Open your project in VS Code - Press 'F1' and search for 'Azure Functions: Create Function' - Choose HttpTrigger -> (Provide a function name) -> anonymous - In the file that opens (`__init__.py`), replace the `def main(req: func.HttpRequest) -> func.HttpResponse:` block with the below code. Note that the casing of the Object field names and the table column names must match. @@ -198,17 +192,13 @@ Note: This tutorial requires that a SQL database is setup as shown in [Create a ### Samples for Output Bindings -#### ICollector<T>/IAsyncCollector<T> - -_TODO_ - #### Array -_TODO_ +See the [AddProductsArray](https://github.com/Azure/azure-functions-sql-extension/tree/main/samples/samples-python/AddProductsArray) sample #### Single Row -_TODO_ +See the [AddProduct](https://github.com/Azure/azure-functions-sql-extension/tree/main/samples/samples-python/AddProduct) sample ## Trigger Binding diff --git a/samples/samples-python/GetProducts/__init__.py b/samples/samples-python/GetProducts/__init__.py index b87950e4f..f0b26e4ba 100644 --- a/samples/samples-python/GetProducts/__init__.py +++ b/samples/samples-python/GetProducts/__init__.py @@ -4,6 +4,10 @@ import json import azure.functions as func +# The input binding executes the `SELECT * FROM Products WHERE Cost = @Cost` query. +# The Parameters argument passes the `{cost}` specified in the URL that triggers the function, +# `getproducts/{cost}`, as the value of the `@Cost` parameter in the query. +# CommandType is set to `Text`, since the constructor argument of the binding is a raw query. def main(req: func.HttpRequest, products: func.SqlRowList) -> func.HttpResponse: rows = list(map(lambda r: json.loads(r.to_json()), products)) diff --git a/samples/samples-python/GetProductsNameEmpty/__init__.py b/samples/samples-python/GetProductsNameEmpty/__init__.py index b87950e4f..64422097b 100644 --- a/samples/samples-python/GetProductsNameEmpty/__init__.py +++ b/samples/samples-python/GetProductsNameEmpty/__init__.py @@ -4,6 +4,7 @@ import json import azure.functions as func +# The parameter value of the `@Name` parameter is an empty string. def main(req: func.HttpRequest, products: func.SqlRowList) -> func.HttpResponse: rows = list(map(lambda r: json.loads(r.to_json()), products)) diff --git a/samples/samples-python/GetProductsNameNull/__init__.py b/samples/samples-python/GetProductsNameNull/__init__.py new file mode 100644 index 000000000..9d06b5530 --- /dev/null +++ b/samples/samples-python/GetProductsNameNull/__init__.py @@ -0,0 +1,17 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +import json +import azure.functions as func + +# If the `{name}` specified in the `getproducts-namenull/{name}` URL is "null", the +# query returns all rows for which the Name column is `NULL`. Otherwise, it returns +# all rows for which the value of the Name column matches the string passed in `{name}` +def main(req: func.HttpRequest, products: func.SqlRowList) -> func.HttpResponse: + rows = list(map(lambda r: json.loads(r.to_json()), products)) + + return func.HttpResponse( + json.dumps(rows), + status_code=200, + mimetype="application/json" + ) diff --git a/samples/samples-python/GetProductsNameNull/function.json b/samples/samples-python/GetProductsNameNull/function.json new file mode 100644 index 000000000..41258c3e9 --- /dev/null +++ b/samples/samples-python/GetProductsNameNull/function.json @@ -0,0 +1,29 @@ +{ + "bindings": [ + { + "authLevel": "function", + "name": "req", + "type": "httpTrigger", + "direction": "in", + "methods": [ + "get" + ], + "route":"getproducts-namenull/{name}" + }, + { + "name": "$return", + "type": "http", + "direction": "out" + }, + { + "name": "products", + "type": "sql", + "direction": "in", + "commandText": "if @Name is null select * from Products where Name is null else select * from Products where @Name = name", + "commandType": "Text", + "parameters": "@Name={name}", + "connectionStringSetting": "SqlConnectionString" + } + ], + "disabled": false + } \ No newline at end of file diff --git a/samples/samples-python/GetProductsStoredProcedure/__init__.py b/samples/samples-python/GetProductsStoredProcedure/__init__.py index b87950e4f..8661646f0 100644 --- a/samples/samples-python/GetProductsStoredProcedure/__init__.py +++ b/samples/samples-python/GetProductsStoredProcedure/__init__.py @@ -4,6 +4,9 @@ import json import azure.functions as func +# `SelectProductsCost` is the name of a procedure stored in the user's database. +# The CommandType is `StoredProcedure`. The parameter value of the `@Cost` parameter in the +# procedure is the `{cost}` specified in the `getproducts-storedprocedure/{cost}` URL. def main(req: func.HttpRequest, products: func.SqlRowList) -> func.HttpResponse: rows = list(map(lambda r: json.loads(r.to_json()), products))