You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This repository contains extension code for the SQL trigger and bindings as well as a quick start, tutorial, and samples of how to use them. A high level explanation of the trigger and bindings is provided below. Additional information for each is in their respective sample sections.
8
8
9
9
-**input binding**: takes a SQL query to run on a provided table and returns the output of the query.
10
10
-**output binding**: takes a list of rows and upserts them into the user table (i.e. If a row doesn't already exist, it is added. If it does, it is updated).
11
11
-**trigger**: requires the user to specify the name of a table, and in the event a change occurs (i.e. the row is updated, deleted, or inserted), the trigger will return the updated rows and values along with any associated metadata.
12
12
13
-
## Table of Contents ##
13
+
## Table of Contents
14
14
15
15
-[SQL Extension for Azure Functions - Preview](#sql-extension-for-azure-functions---preview)
16
16
-[Introduction](#introduction)
@@ -39,9 +39,9 @@ This repository contains extension code for the SQL trigger and bindings as well
39
39
-[Trigger Samples](#trigger-samples)
40
40
-[Contributing](#contributing)
41
41
42
-
## Quick Start ##
42
+
## Quick Start
43
43
44
-
### SQL Setup ###
44
+
### SQL Setup
45
45
46
46
This requires already having a SQL database. If you need to create a SQL database, please refer to [Create Azure SQL Database](#Create-Azure-SQL-Database) in the tutorials section.
47
47
@@ -59,25 +59,25 @@ A primary key must be set in your SQL table before using the bindings. To do thi
59
59
ALTER TABLE ['your table name'] ADD CONSTRAINT PKey PRIMARY KEY CLUSTERED (['column to be primary key']);
60
60
```
61
61
62
-
2. SQL's [change tracking functionality](https://docs.microsoft.com/sql/relational-databases/track-changes/about-change-tracking-sql-server?view=sql-server-ver15) must be enabled on the database to use the trigger. Please note that change tracking has additional costs. If you do not plan on using the trigger, you can skip this step. To enable change tracking on the database, run:
62
+
1. SQL's [change tracking functionality](https://docs.microsoft.com/sql/relational-databases/track-changes/about-change-tracking-sql-server?view=sql-server-ver15) must be enabled on the database to use the trigger. Please note that change tracking has additional costs. If you do not plan on using the trigger, you can skip this step. To enable change tracking on the database, run:
63
63
64
64
```sql
65
65
ALTER DATABASE ['your database name']
66
66
SET CHANGE_TRACKING = ON
67
67
(CHANGE_RETENTION = 2 DAYS, AUTO_CLEANUP = ON)
68
68
```
69
69
70
-
3. Change tracking must be enabled on the table to use the trigger. If you do not plan on using the trigger, you can skip this step. To enable change tracking on the table, run:
70
+
1. Change tracking must be enabled on the table to use the trigger. If you do not plan on using the trigger, you can skip this step. To enable change tracking on the table, run:
71
71
72
72
```sql
73
73
ALTER TABLE ['your table name']
74
74
ENABLE CHANGE_TRACKING
75
75
WITH (TRACK_COLUMNS_UPDATED = ON)
76
76
```
77
77
78
-
4. Congrats on setting up your database! Now continue to set up your local environment and complete the quick start. For more information on what change tracking does for the bindings, go to the [Trigger](#Trigger) section.
78
+
1. Congrats on setting up your database! Now continue to set up your local environment and complete the quick start. For more information on what change tracking does for the bindings, go to the [Trigger](#Trigger) section.
79
79
80
-
### Set Up Local .NET Function App ###
80
+
### Set Up Local .NET Function App
81
81
82
82
These steps can be done in the CLI, Powershell. Completing this section will allow you to begin using the bindings.
83
83
@@ -101,21 +101,21 @@ These steps can be done in the CLI, Powershell. Completing this section will all
2. Ensure you have Azure Storage Emulator running. For information on the Azure Storage Emulator, refer [here](https://docs.microsoft.com/azure/storage/common/storage-use-emulator#get-the-storage-emulator)
104
+
1. Ensure you have Azure Storage Emulator running. For information on the Azure Storage Emulator, refer [here](https://docs.microsoft.com/azure/storage/common/storage-use-emulator#get-the-storage-emulator)
105
105
106
-
3. Get your SqlConnectionString. Your connection string can be found in your SQL database resource by going to the left blade and clicking 'Connection strings'. Copy the Connection String.
106
+
1. Get your SqlConnectionString. Your connection string can be found in your SQL database resource by going to the left blade and clicking 'Connection strings'. Copy the Connection String.
107
107
108
108
(*Note: when pasting in the connection string, you will need to replace part of the connection string where it says '{your_password}' with your Azure SQL Server password*)
109
109
110
-
4. In 'local.settings.json' in 'Values', verify you have the below. If not, add the below and replace "Your Connection String" with the your connection string from the previous step:
110
+
1. In 'local.settings.json' in 'Values', verify you have the below. If not, add the below and replace "Your Connection String" with the your connection string from the previous step:
@@ -131,11 +131,11 @@ These steps can be done in the CLI, Powershell. Completing this section will all
131
131
}
132
132
```
133
133
134
-
6. You have setup your local environment and are now ready to create your first SQL bindings! Continue to the [input](#Input-Binding-Tutorial), [output](#Output-Binding-Tutorial), and [trigger](#Trigger-Tutorial) binding tutorials, or refer to [More Samples](#More-Samples) for information on how to use the bindings and explore on your own.
134
+
1. You have setup your local environment and are now ready to create your first SQL bindings! Continue to the [input](#Input-Binding-Tutorial), [output](#Output-Binding-Tutorial), and [trigger](#Trigger-Tutorial) binding tutorials, or refer to [More Samples](#More-Samples) for information on how to use the bindings and explore on your own.
135
135
136
-
## Tutorials ##
136
+
## Tutorials
137
137
138
-
### Create Azure SQL Database ###
138
+
### Create Azure SQL Database
139
139
140
140
We will create a simple Azure SQL Database. For additional reference on Azure SQL Databases, go [here](https://docs.microsoft.com/azure/azure-sql/database/single-database-create-quickstart?tabs=azure-portal).
141
141
@@ -173,7 +173,7 @@ We will create a simple Azure SQL Database. For additional reference on Azure SQ
173
173
174
174
- Congratulations! You have successfully created an Azure SQL Database! Make sure you complete [Quick Start](#Quick-Start) before continuing to the rest of the tutorial.
175
175
176
-
### Input Binding Tutorial ###
176
+
### Input Binding Tutorial
177
177
178
178
Note: This tutorial requires that the Azure SQL database is setup as shown in [Create Azure SQL Database](#Create-Azure-SQL-Database).
179
179
@@ -183,19 +183,15 @@ Note: This tutorial requires that the Azure SQL database is setup as shown in [C
183
183
- In the file that opens, replace the 'public static async Task< IActionResult > Run' block with the below code.
@@ -225,7 +221,7 @@ Note: This tutorial requires that the Azure SQL database is setup as shown in [C
225
221
- You should see your database output in the browser window.
226
222
- Congratulations! You have successfully created your first SQL input binding! Checkout [Input Binding](#Input-Binding) for more information on how to use it and explore on your own!
227
223
228
-
### Output Binding Tutorial ###
224
+
### Output Binding Tutorial
229
225
230
226
Note: This tutorial requires that the Azure SQL database is setup as shown in [Create Azure SQL Database](#Create-Azure-SQL-Database), and that you have the 'Employee.cs' class from the [Input Binding Tutorial](#Input-Binding-Tutorial).
231
227
@@ -271,7 +267,7 @@ Note: This tutorial requires that the Azure SQL database is setup as shown in [C
271
267
- Hit 'F5' to run your code. Click the link to upsert the output array valuesin your SQL table. Your upserted values should launch in the browser.
272
268
- Congratulations! You have successfully created your first SQL output binding! Checkout [Output Binding](#Output-Binding) for more information on how to use it and explore on your own!
273
269
274
-
### Trigger Tutorial ###
270
+
### Trigger Tutorial
275
271
276
272
This tutorial requires that the Azure SQL database is setup as shown in [Create Azure SQL Database](#Create-Azure-SQL-Database), and that you have the 'Employee.cs' class from the [Input Binding Tutorial](#Input-Binding-Tutorial).
277
273
@@ -314,9 +310,9 @@ This tutorial requires that the Azure SQL database is setup as shown in [Create
314
310
-Update, insert, ordelete additional rows in your SQL table using the SQL query editor while the function app is running and observe the log updates.
315
311
- Congratulations! You have successfully created your first SQL trigger! Checkout [Trigger Samples](#Trigger-Samples) for more information on how to use the trigger and explore on your own!
316
312
317
-
## More Samples ##
313
+
## More Samples
318
314
319
-
### Input Binding ###
315
+
### Input Binding
320
316
321
317
The input binding takes four arguments
322
318
@@ -334,7 +330,7 @@ The following are valid binding types for the result of the query/stored procedu
334
330
335
331
The repo contains examples of each of these binding types [here](https://github.com/Azure/azure-functions-sql-extension/tree/dev/samples/SqlExtensionSamples/InputBindingSamples). A few examples are also included below.
336
332
337
-
#### Query String ###
333
+
#### Query String
338
334
339
335
The input binding executes the "select * from Products where Cost = @Cost" query, returning the result as an `IEnumerable<Product>`, where Product is a user-defined POCO. 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 `System.Data.CommandType.Text`, since the constructor argument of the binding is a raw query.
340
336
@@ -373,7 +369,7 @@ public class Product
373
369
}
374
370
```
375
371
376
-
#### Empty Parameter Value ####
372
+
#### Empty Parameter Value
377
373
378
374
In this case, the parameter value of the `@Name` parameter is an empty string.
379
375
@@ -392,7 +388,7 @@ In this case, the parameter value of the `@Name` parameter is an empty string.
392
388
}
393
389
```
394
390
395
-
#### Null Parameter Value ####
391
+
#### Null Parameter Value
396
392
397
393
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}`
398
394
@@ -411,7 +407,7 @@ If the `{name}` specified in the `getproducts-namenull/{name}` URL is "null", th
411
407
}
412
408
```
413
409
414
-
#### Stored Procedure ####
410
+
#### Stored Procedure
415
411
416
412
`SelectsProductCost` is the name of a procedure stored in the user's database. In this case, *CommandType* is `System.Data.CommandType.StoredProcedure`. The parameter value of the `@Cost` parameter in the procedure is once again the `{cost}` specified in the `getproducts-storedprocedure/{cost}` URL.
417
413
@@ -430,7 +426,7 @@ If the `{name}` specified in the `getproducts-namenull/{name}` URL is "null", th
430
426
}
431
427
```
432
428
433
-
#### IAsyncEnumerable ####
429
+
#### IAsyncEnumerable
434
430
435
431
Using the `IAsyncEnumerable` binding generally requires that the `Run` function be `async`. It is also important to call `DisposeAsync` at the end of function execution to make sure all resources used by the enumerator are freed.
436
432
@@ -455,7 +451,7 @@ public static async Task<IActionResult> Run(
455
451
}
456
452
```
457
453
458
-
### Output Binding ###
454
+
### Output Binding
459
455
460
456
The output binding takes a list of rows to be upserted into a user table. If the primary key value of the row already exists in the table, the row is interpreted as an update, meaning that the values of the other columns in the table for that primary key are updated. If the primary key value does not exist in the table, the row is interpreted as an insert. The upserting of the rows is batched by the output binding code.
461
457
@@ -472,7 +468,7 @@ The following are valid binding types for the rows to be upserted into the table
472
468
473
469
The repo contains examples of each of these binding types [here](https://github.com/Azure/azure-functions-sql-extension/tree/dev/samples/SqlExtensionSamples/OutputBindingSamples). A few examples are also included below.
474
470
475
-
#### ICollector<T>/IAsyncCollector<T> ####
471
+
#### ICollector<T>/IAsyncCollector<T>
476
472
477
473
When using an `ICollector`, it is not necessary to instantiate it. The function can add rows to the `ICollector` directly, and its contents are automatically upserted once the function exits.
478
474
@@ -516,7 +512,7 @@ public static async Task<IActionResult> Run(
516
512
}
517
513
```
518
514
519
-
#### Array ####
515
+
#### Array
520
516
521
517
This output binding type requires explicit instantiation within the function body. Note also that the `Product[]` array must be prefixed by `out` when attached to the output binding
522
518
@@ -545,7 +541,7 @@ public static IActionResult Run(
545
541
}
546
542
```
547
543
548
-
#### Single Row ####
544
+
#### Single Row
549
545
550
546
When binding to a single row, it is also necessary to prefix the row with `out`
551
547
@@ -566,9 +562,9 @@ public static IActionResult Run(
566
562
}
567
563
```
568
564
569
-
### Trigger ###
565
+
### Trigger
570
566
571
-
#### Change Tracking ####
567
+
#### Change Tracking
572
568
573
569
The trigger uses SQL's [change tracking functionality](https://docs.microsoft.com/sql/relational-databases/track-changes/about-change-tracking-sql-server?view=sql-server-ver15) to monitor a user table for changes. As such, it is necessary to enable change tracking on the database and table before using the trigger. This can be done in the query editor in the portal. If you need help navigating to it, visit the [Create Azure SQL Database](#Create-Azure-SQL-Database) section in the README.
The trigger needs to have read access to the table being monitored for changes as well as to the change tracking system tables. It also needs write access to an `az_func` schema within the database, where it will create additional worker tables to process the changes. Each user table will thus have an associated change tracking table and worker table. The worker table will contain roughly as many rows as the change tracking table, and will be cleaned up approximately as often as the change table.
596
592
597
-
#### Trigger Samples ####
593
+
#### Trigger Samples
598
594
The trigger takes two arguments
599
595
600
596
- **TableName**: Passed as a constructor argument to the binding. Represents the name of the table to be monitored for changes.
@@ -622,7 +618,7 @@ public static void Run(
622
618
}
623
619
```
624
620
625
-
## Contributing ##
621
+
## Contributing
626
622
627
623
This project welcomes contributions and suggestions. Most contributions require you to agree to a
628
624
Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us
0 commit comments