Skip to content

Commit 7d28bd3

Browse files
Make attribute properties readonly + cleanup (#682)
* Attribute cleanup and clarifications * more * Cleanup * Revert attribute rename * Undo doc changes
1 parent e871604 commit 7d28bd3

32 files changed

+97
-117
lines changed

Worker.Extensions.Sql/src/SqlInputAttribute.cs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT License. See License.txt in the project root for license information.
33

44
using System;
5+
using System.Data;
56
using Microsoft.Azure.Functions.Worker.Extensions.Abstractions;
67

78
namespace Microsoft.Azure.Functions.Worker.Extensions.Sql
@@ -13,12 +14,23 @@ public sealed class SqlInputAttribute : InputBindingAttribute
1314
/// </summary>
1415
/// <param name="commandText">Either a SQL query or stored procedure that will be run in the target database.</param>
1516
/// <param name="connectionStringSetting">The name of the app setting where the SQL connection string is stored</param>
16-
public SqlInputAttribute(string commandText, string connectionStringSetting)
17+
/// <param name="commandType">Specifies whether <see cref="CommandText"/> refers to a stored procedure or SQL query string. Defaults to <see cref="CommandType.Text"/></param>
18+
/// <param name="parameters">Optional - Specifies the parameters that will be used to execute the SQL query or stored procedure. See <see cref="Parameters"/> for more details.</param>
19+
public SqlInputAttribute(string commandText, string connectionStringSetting, CommandType commandType = CommandType.Text, string parameters = null)
1720
{
1821
this.CommandText = commandText ?? throw new ArgumentNullException(nameof(commandText));
1922
this.ConnectionStringSetting = connectionStringSetting ?? throw new ArgumentNullException(nameof(connectionStringSetting));
23+
this.CommandType = commandType;
24+
this.Parameters = parameters;
2025
}
2126

27+
/// <summary>
28+
/// Creates an instance of the <see cref="SqlInputAttribute"/>, which takes a SQL query or stored procedure to run and returns the output to the function.
29+
/// </summary>
30+
/// <param name="commandText">Either a SQL query or stored procedure that will be run in the target database.</param>
31+
/// <param name="connectionStringSetting">The name of the app setting where the SQL connection string is stored</param>
32+
public SqlInputAttribute(string commandText, string connectionStringSetting) : this(commandText, connectionStringSetting, CommandType.Text, null) { }
33+
2234
/// <summary>
2335
/// The name of the app setting where the SQL connection string is stored
2436
/// (see https://docs.microsoft.com/dotnet/api/microsoft.data.sqlclient.sqlconnection).
@@ -28,18 +40,19 @@ public SqlInputAttribute(string commandText, string connectionStringSetting)
2840
/// create a ConnectionStringSetting with a name like SqlServerAuthentication. The value of the SqlServerAuthentication app setting
2941
/// would look like "Data Source=test.database.windows.net;Database=TestDB;User ID={userid};Password={password}".
3042
/// </summary>
31-
public string ConnectionStringSetting { get; set; }
43+
public string ConnectionStringSetting { get; }
3244

3345
/// <summary>
3446
/// Either a SQL query or stored procedure that will be run in the target database.
3547
/// </summary>
36-
public string CommandText { get; set; }
48+
public string CommandText { get; }
3749

3850
/// <summary>
3951
/// Specifies whether <see cref="CommandText"/> refers to a stored procedure or SQL query string.
40-
/// Use <see cref="CommandType.StoredProcedure"/> for the former, <see cref="CommandType.Text"/> for the latter
52+
/// Use <see cref="CommandType.StoredProcedure"/> for the former, <see cref="CommandType.Text"/> for the latter.
53+
/// Defaults to <see cref="CommandType.Text"/>.
4154
/// </summary>
42-
public System.Data.CommandType CommandType { get; set; } = System.Data.CommandType.Text;
55+
public CommandType CommandType { get; }
4356

4457
/// <summary>
4558
/// Specifies the parameters that will be used to execute the SQL query or stored procedure specified in <see cref="CommandText"/>.
@@ -50,6 +63,6 @@ public SqlInputAttribute(string commandText, string connectionStringSetting)
5063
/// as in "@param1=,@param2=param2"
5164
/// Note that neither the parameter name nor the parameter value can have ',' or '='
5265
/// </summary>
53-
public string Parameters { get; set; }
66+
public string Parameters { get; }
5467
}
5568
}

Worker.Extensions.Sql/src/SqlOutputAttribute.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,11 @@ public SqlOutputAttribute(string commandText, string connectionStringSetting)
2828
/// create a ConnectionStringSetting with a name like SqlServerAuthentication. The value of the SqlServerAuthentication app setting
2929
/// would look like "Data Source=test.database.windows.net;Database=TestDB;User ID={userid};Password={password}".
3030
/// </summary>
31-
public string ConnectionStringSetting { get; set; }
31+
public string ConnectionStringSetting { get; }
3232

3333
/// <summary>
3434
/// The table name to upsert the values to.
3535
/// </summary>
36-
public string CommandText { get; set; }
37-
38-
/// <summary>
39-
/// Specifies <see cref="CommandText"/> as Text.
40-
/// </summary>
41-
public System.Data.CommandType CommandType { get; } = System.Data.CommandType.Text;
36+
public string CommandText { get; }
4237
}
4338
}

docs/SetupGuide_Dotnet.md

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ See [Input Binding Overview](./BindingsOverview.md#input-binding) for general in
5555

5656
The [SqlAttribute](https://github.com/Azure/azure-functions-sql-extension/blob/main/src/SqlAttribute.cs) for Input bindings takes four arguments:
5757

58-
- **CommandText**: Passed as a constructor argument to the binding. Represents either a query string or the name of a stored procedure.
59-
- **ConnectionStringSetting**: Passed as a constructor argument to the binding. Specifies the name of the app setting that contains the SQL connection string used to connect to a database. The connection string must follow the format specified [here](https://docs.microsoft.com/dotnet/api/microsoft.data.sqlclient.sqlconnection.connectionstring?view=sqlclient-dotnet-core-2.0).
60-
- **CommandType**: Specifies whether CommandText is a query (`System.Data.CommandType.Text`) or a stored procedure (`System.Data.CommandType.StoredProcedure`)
58+
- **CommandText**: Represents either a query string or the name of a stored procedure based on the value of the CommandType.
59+
- **ConnectionStringSetting**: Specifies the name of the app setting that contains the SQL connection string used to connect to a database. The connection string must follow the format specified [here](https://docs.microsoft.com/dotnet/api/microsoft.data.sqlclient.sqlconnection.connectionstring?view=sqlclient-dotnet-core-2.0).
60+
- **CommandType**: Specifies whether CommandText is a query (`System.Data.CommandType.Text`) or a stored procedure (`System.Data.CommandType.StoredProcedure`). Defaults to `CommandType.Text`.
6161
- **Parameters**: The parameters to the query/stored procedure. This string must follow the format "@param1=param1,@param2=param2" where @param1 is the name of the parameter and param1 is the parameter value. Each pair of parameter name, parameter value is separated by a comma. Within each pair, the parameter name and value is separated by an equals sign. This means that neither the parameter name nor value can contain "," or "=". To specify a `NULL` parameter value, do "@param1=null,@param2=param2". To specify an empty string as a value, do "@param1=,@param2=param2", i.e. do not put any text after the equals sign of the corresponding parameter name. This argument is auto-resolvable (see Query String examples).
6262

6363
The following are valid binding types for the result of the query/stored procedure execution:
@@ -83,8 +83,7 @@ Note: This tutorial requires that a SQL database is setup as shown in [Create a
8383
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = "employees")] HttpRequest req,
8484
ILogger log,
8585
[Sql("select * from Employees",
86-
"SqlConnectionString"
87-
CommandType = System.Data.CommandType.Text)]
86+
"SqlConnectionString")]
8887
IEnumerable<Employee> employee)
8988
{
9089
return new OkObjectResult(employee);
@@ -130,8 +129,7 @@ The input binding executes the `select * from Products where Cost = @Cost` query
130129
HttpRequest req,
131130
[Sql("select * from Products where Cost = @Cost",
132131
"SqlConnectionString",
133-
CommandType = System.Data.CommandType.Text,
134-
Parameters = "@Cost={cost}")]
132+
parameters: "@Cost={cost}")]
135133
IEnumerable<Product> products)
136134
{
137135
return (ActionResult)new OkObjectResult(products);
@@ -169,8 +167,7 @@ In this case, the parameter value of the `@Name` parameter is an empty string.
169167
HttpRequest req,
170168
[Sql("select * from Products where Cost = @Cost and Name = @Name",
171169
"SqlConnectionString",
172-
CommandType = System.Data.CommandType.Text,
173-
Parameters = "@Cost={cost},@Name=")]
170+
parameters: "@Cost={cost},@Name=")]
174171
IEnumerable<Product> products)
175172
{
176173
return (ActionResult)new OkObjectResult(products);
@@ -188,8 +185,7 @@ If the `{name}` specified in the `getproducts-namenull/{name}` URL is "null", th
188185
HttpRequest req,
189186
[Sql("if @Name is null select * from Products where Name is null else select * from Products where @Name = name",
190187
"SqlConnectionString",
191-
CommandType = System.Data.CommandType.Text,
192-
Parameters = "@Name={name}")]
188+
parameters: "@Name={name}")]
193189
IEnumerable<Product> products)
194190
{
195191
return (ActionResult)new OkObjectResult(products);
@@ -207,8 +203,8 @@ If the `{name}` specified in the `getproducts-namenull/{name}` URL is "null", th
207203
HttpRequest req,
208204
[Sql("SelectProductsCost",
209205
"SqlConnectionString",
210-
CommandType = System.Data.CommandType.StoredProcedure,
211-
Parameters = "@Cost={cost}")]
206+
System.Data.CommandType.StoredProcedure,
207+
"@Cost={cost}")]
212208
IEnumerable<Product> products)
213209
{
214210
return (ActionResult)new OkObjectResult(products);
@@ -225,8 +221,7 @@ public static async Task<IActionResult> Run(
225221
HttpRequest req,
226222
[Sql("select * from Products where cost = @Cost",
227223
"SqlConnectionString",
228-
CommandType = System.Data.CommandType.Text,
229-
Parameters = "@Cost={cost}")]
224+
parameters: "@Cost={cost}")]
230225
IAsyncEnumerable<Product> products)
231226
{
232227
var enumerator = products.GetAsyncEnumerator();
@@ -248,8 +243,8 @@ See [Output Binding Overview](./BindingsOverview.md#output-binding) for general
248243
249244
The [SqlAttribute](https://github.com/Azure/azure-functions-sql-extension/blob/main/src/SqlAttribute.cs) for Output bindings takes two arguments:
250245
251-
- **CommandText**: Passed as a constructor argument to the binding. Represents the name of the table into which rows will be upserted.
252-
- **ConnectionStringSetting**: Passed as a constructor argument to the binding. Specifies the name of the app setting that contains the SQL connection string used to connect to a database. The connection string must follow the format specified [here](https://docs.microsoft.com/dotnet/api/microsoft.data.sqlclient.sqlconnection.connectionstring?view=sqlclient-dotnet-core-2.0).
246+
- **CommandText**: Represents the name of the table into which rows will be upserted.
247+
- **ConnectionStringSetting**: Specifies the name of the app setting that contains the SQL connection string used to connect to a database. The connection string must follow the format specified [here](https://docs.microsoft.com/dotnet/api/microsoft.data.sqlclient.sqlconnection.connectionstring?view=sqlclient-dotnet-core-2.0).
253248
254249
The following are valid binding types for the rows to be upserted into the table:
255250
@@ -408,7 +403,7 @@ See [Trigger Binding Overview](./BindingsOverview.md#trigger-binding) for genera
408403
409404
The SqlAttribute for Trigger bindings takes two [arguments](https://github.com/Azure/azure-functions-sql-extension/blob/main/src/TriggerBinding/SqlTriggerAttribute.cs)
410405
411-
- **TableName**: Passed as a constructor argument to the binding. Represents the name of the table to be monitored for changes.
406+
- **TableName**: Represents the name of the table to be monitored for changes.
412407
- **ConnectionStringSetting**: Specifies the name of the app setting that contains the SQL connection string used to connect to a database. The connection string must follow the format specified [here](https://docs.microsoft.com/dotnet/api/microsoft.data.sqlclient.sqlconnection.connectionstring?view=sqlclient-dotnet-core-2.0).
413408
414409
The trigger binding can bind to type `IReadOnlyList<SqlChange<T>>`:

docs/SetupGuide_DotnetOutOfProc.md

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,10 @@ See [Input Binding Overview](./BindingsOverview.md#input-binding) for general in
6666

6767
The [SqlInputAttribute](https://github.com/Azure/azure-functions-sql-extension/blob/main/Worker.Extensions.Sql/src/SqlInputAttribute.cs) takes four arguments:
6868

69-
- **CommandText**: Passed as a constructor argument to the binding. Represents either a query string or the name of a stored procedure.
70-
- **CommandType**: Specifies whether CommandText is a query (`System.Data.CommandType.Text`) or a stored procedure (`System.Data.CommandType.StoredProcedure`)
71-
- **Parameters**: The parameters to the query/stored procedure. This string must follow the format "@param1=param1,@param2=param2" where @param1 is the name of the parameter and param1 is the parameter value. Each pair of parameter name, parameter value is separated by a comma. Within each pair, the parameter name and value is separated by an equals sign. This means that neither the parameter name nor value can contain "," or "=". To specify a `NULL` parameter value, do "@param1=null,@param2=param2". To specify an empty string as a value, do "@param1=,@param2=param2", i.e. do not put any text after the equals sign of the corresponding parameter name. This argument is auto-resolvable (see Query String examples).
69+
- **CommandText**: Represents either a query string or the name of a stored procedure.
7270
- **ConnectionStringSetting**: Specifies the name of the app setting that contains the SQL connection string used to connect to a database. The connection string must follow the format specified [here](https://docs.microsoft.com/dotnet/api/microsoft.data.sqlclient.sqlconnection.connectionstring?view=sqlclient-dotnet-core-2.0).
71+
- **CommandType**: Specifies whether CommandText is a query (`System.Data.CommandType.Text`) or a stored procedure (`System.Data.CommandType.StoredProcedure`). Default is `Text`
72+
- **Parameters**: The parameters to the query/stored procedure. This string must follow the format "@param1=param1,@param2=param2" where @param1 is the name of the parameter and param1 is the parameter value. Each pair of parameter name, parameter value is separated by a comma. Within each pair, the parameter name and value is separated by an equals sign. This means that neither the parameter name nor value can contain "," or "=". To specify a `NULL` parameter value, do "@param1=null,@param2=param2". To specify an empty string as a value, do "@param1=,@param2=param2", i.e. do not put any text after the equals sign of the corresponding parameter name. This argument is auto-resolvable (see Query String examples).
7373

7474
The following are valid binding types for the result of the query/stored procedure execution:
7575

@@ -96,15 +96,14 @@ Note: This tutorial requires that a SQL database is setup as shown in [Create a
9696
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = "employees")] HttpRequest req,
9797
ILogger log,
9898
[SqlInput("select * from Employees",
99-
"SqlConnectionString",
100-
CommandType = System.Data.CommandType.Text)]
99+
"SqlConnectionString")]
101100
IEnumerable<Employee> employees)
102101
{
103102
return employees;
104103
}
105104
```
106105
107-
*In the above, `select * from Employees` is the SQL script run by the input binding. The CommandType on the line below specifies whether the first line is a query or a stored procedure. On the next line, the ConnectionStringSetting specifies that the app setting that contains the SQL connection string used to connect to the database is "SqlConnectionString." For more information on this, see the [SqlInputAttribute for Input Bindings](#sqlinputattribute-for-input-bindings) section*
106+
*In the above sample, `select * from Employees` is the SQL script run by the input binding. The CommandType on the line below specifies whether the first line is a query or a stored procedure. On the next line, the ConnectionStringSetting parameter specifies that the app setting that contains the SQL connection string used to connect to the database is "SqlConnectionString." For more information on this, see the [SqlInputAttribute for Input Bindings](#sqlinputattribute-for-input-bindings) section*
108107
- Add 'using Microsoft.Azure.Functions.Worker.Extensions.Sql;' for using *SqlInput*, the out of proc sql input binding.
109108
- Add 'using System.Collections.Generic;' to the namespaces list at the top of the page.
110109
- Currently, there is an error for the IEnumerable. We'll fix this by creating an Employee class.
@@ -143,8 +142,7 @@ The input binding executes the `select * from Products where Cost = @Cost` query
143142
HttpRequestData req,
144143
[SqlInput("select * from Products where Cost = @Cost",
145144
"SqlConnectionString",
146-
CommandType = System.Data.CommandType.Text,
147-
Parameters = "@Cost={cost}")]
145+
parameters: "@Cost={cost}")]
148146
IEnumerable<Product> products)
149147
{
150148
return products;
@@ -182,8 +180,7 @@ In this case, the parameter value of the `@Name` parameter is an empty string.
182180
HttpRequestData req,
183181
[SqlInput("select * from Products where Cost = @Cost and Name = @Name",
184182
"SqlConnectionString",
185-
CommandType = System.Data.CommandType.Text,
186-
Parameters = "@Cost={cost},@Name=")]
183+
parameters: "@Cost={cost},@Name=")]
187184
IEnumerable<Product> products)
188185
{
189186
return products;
@@ -201,8 +198,7 @@ If the `{name}` specified in the `getproducts-namenull/{name}` URL is "null", th
201198
HttpRequestData req,
202199
[SqlInput("if @Name is null select * from Products where Name is null else select * from Products where @Name = name",
203200
"SqlConnectionString",
204-
CommandType = System.Data.CommandType.Text,
205-
Parameters = "@Name={name}")]
201+
parameters: "@Name={name}")]
206202
IEnumerable<Product> products)
207203
{
208204
return products;
@@ -220,8 +216,8 @@ If the `{name}` specified in the `getproducts-namenull/{name}` URL is "null", th
220216
HttpRequestData req,
221217
[SqlInput("SelectProductsCost",
222218
"SqlConnectionString",
223-
CommandType = System.Data.CommandType.StoredProcedure,
224-
Parameters = "@Cost={cost}")]
219+
System.Data.CommandType.StoredProcedure,
220+
"@Cost={cost}")]
225221
IEnumerable<Product> products)
226222
{
227223
return products;
@@ -239,8 +235,7 @@ public static async Task<List<Product>> Run(
239235
HttpRequestData req,
240236
[SqlInput("select * from Products where cost = @Cost",
241237
"SqlConnectionString",
242-
CommandType = System.Data.CommandType.Text,
243-
Parameters = "@Cost={cost}")]
238+
parameters: "@Cost={cost}")]
244239
IAsyncEnumerable<Product> products)
245240
{
246241
var enumerator = products.GetAsyncEnumerator();
@@ -262,7 +257,7 @@ See [Output Binding Overview](./BindingsOverview.md#output-binding) for general
262257
263258
The [SqlOutputAttribute](https://github.com/Azure/azure-functions-sql-extension/blob/main/Worker.Extensions.Sql/src/SqlOutputAttribute.cs) takes two arguments:
264259
265-
- **CommandText**: Passed as a constructor argument to the binding. Represents the name of the table into which rows will be upserted.
260+
- **CommandText**: Represents the name of the table into which rows will be upserted.
266261
- **ConnectionStringSetting**: Specifies the name of the app setting that contains the SQL connection string used to connect to a database. The connection string must follow the format specified [here](https://docs.microsoft.com/dotnet/api/microsoft.data.sqlclient.sqlconnection.connectionstring?view=sqlclient-dotnet-core-2.0).
267262
268263
The following are valid binding types for the rows to be upserted into the table:

0 commit comments

Comments
 (0)