Skip to content

Commit edc56a9

Browse files
author
James Craig
committed
Updated dependencies.
Added the ability to specify retries when opening the connection. Minor change in SQLHelper's constructor order.
1 parent 1d0c8d4 commit edc56a9

File tree

8 files changed

+58
-26
lines changed

8 files changed

+58
-26
lines changed

src/SQLHelper.DB/ExtensionMethods/DbCommandExtensions.cs

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,13 @@ public static DbCommand AddParameter(this DbCommand command, string id, DbType t
136136
/// Begins a transaction
137137
/// </summary>
138138
/// <param name="command">Command object</param>
139+
/// <param name="retries">The retries.</param>
139140
/// <returns>A transaction object</returns>
140-
public static DbTransaction BeginTransaction(this DbCommand command)
141+
public static DbTransaction BeginTransaction(this DbCommand command, int retries = 0)
141142
{
142143
if (command == null || command.Connection == null)
143144
return null;
144-
command.Open();
145+
command.Open(retries);
145146
command.Transaction = command.Connection.BeginTransaction();
146147
return command.Transaction;
147148
}
@@ -187,28 +188,32 @@ public static DbCommand Commit(this DbCommand command)
187188
/// <summary>
188189
/// Executes the stored procedure as a scalar query
189190
/// </summary>
191+
/// <typeparam name="DataType">The type of the ata type.</typeparam>
190192
/// <param name="command">Command object</param>
191193
/// <param name="defaultValue">Default value if there is an issue</param>
194+
/// <param name="retries">The retries.</param>
192195
/// <returns>The object of the first row and first column</returns>
193-
public static DataType ExecuteScalar<DataType>(this DbCommand command, DataType defaultValue = default(DataType))
196+
public static DataType ExecuteScalar<DataType>(this DbCommand command, DataType defaultValue = default(DataType), int retries = 0)
194197
{
195198
if (command == null)
196199
return defaultValue;
197-
command.Open();
200+
command.Open(retries);
198201
return command.ExecuteScalar().To(defaultValue);
199202
}
200203

201204
/// <summary>
202205
/// Executes the stored procedure as a scalar query async
203206
/// </summary>
207+
/// <typeparam name="DataType">The type of the ata type.</typeparam>
204208
/// <param name="command">Command object</param>
205209
/// <param name="defaultValue">Default value if there is an issue</param>
210+
/// <param name="retries">The retries.</param>
206211
/// <returns>The object of the first row and first column</returns>
207-
public static async Task<DataType> ExecuteScalarAsync<DataType>(this DbCommand command, DataType defaultValue = default(DataType))
212+
public static async Task<DataType> ExecuteScalarAsync<DataType>(this DbCommand command, DataType defaultValue = default(DataType), int retries = 0)
208213
{
209214
if (command == null)
210215
return defaultValue;
211-
command.Open();
216+
command.Open(retries);
212217
var ReturnValue = await command.ExecuteScalarAsync();
213218
return ReturnValue.To(defaultValue);
214219
}
@@ -254,14 +259,28 @@ public static DbParameter GetOrCreateParameter(this DbCommand command, string id
254259
/// Opens the connection
255260
/// </summary>
256261
/// <param name="command">Command object</param>
262+
/// <param name="retries">The retries.</param>
257263
/// <returns>The DBCommand object</returns>
258-
public static DbCommand Open(this DbCommand command)
264+
public static DbCommand Open(this DbCommand command, int retries = 0)
259265
{
260-
if (command != null
261-
&& command.Connection != null
262-
&& command.Connection.State != ConnectionState.Open)
263-
command.Connection.Open();
264-
return command;
266+
Exception holder = null;
267+
while (retries >= 0)
268+
{
269+
try
270+
{
271+
if (command != null
272+
&& command.Connection != null
273+
&& command.Connection.State != ConnectionState.Open)
274+
command.Connection.Open();
275+
return command;
276+
}
277+
catch (Exception e)
278+
{
279+
holder = e;
280+
}
281+
--retries;
282+
}
283+
throw holder;
265284
}
266285

267286
/// <summary>

src/SQLHelper.DB/HelperClasses/Batch.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,6 @@ private List<List<dynamic>> ExecuteCommands()
314314
using (DbCommand ExecutableCommand = Factory.CreateCommand())
315315
{
316316
SetupCommand(Connection, ExecutableCommand);
317-
318317
try
319318
{
320319
int Count = 0;
@@ -417,8 +416,8 @@ private void SetupCommand(DbConnection DatabaseConnection, DbCommand ExecutableC
417416
ExecutableCommand.Connection = DatabaseConnection;
418417
ExecutableCommand.CommandType = CommandType.Text;
419418
if (CheckTransaction())
420-
ExecutableCommand.BeginTransaction();
421-
ExecutableCommand.Open();
419+
ExecutableCommand.BeginTransaction(Source.Retries);
420+
ExecutableCommand.Open(Source.Retries);
422421
}
423422

424423
/// <summary>

src/SQLHelper.DB/HelperClasses/Connection.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,11 @@ public Connection(IConfiguration configuration, DbProviderFactory factory, strin
4646
/// <param name="connection">The connection.</param>
4747
/// <param name="name">The name.</param>
4848
/// <param name="parameterPrefix">The parameter prefix.</param>
49+
/// <param name="retries">The retries.</param>
4950
/// <exception cref="System.ArgumentNullException">configuration</exception>
50-
public Connection(IConfiguration configuration, DbProviderFactory factory, string connection, string name, string parameterPrefix = "@")
51+
public Connection(IConfiguration configuration, DbProviderFactory factory, string connection, string name, string parameterPrefix = "@", int retries = 0)
5152
{
53+
Retries = retries;
5254
Configuration = configuration ?? throw new System.ArgumentNullException(nameof(configuration));
5355
Name = string.IsNullOrEmpty(name) ? "Default" : name;
5456
SourceType = factory.GetType().FullName;
@@ -119,6 +121,12 @@ public Connection(IConfiguration configuration, DbProviderFactory factory, strin
119121
/// <value>The parameter prefix.</value>
120122
public string ParameterPrefix { get; protected set; }
121123

124+
/// <summary>
125+
/// Gets the number of retries if unable to connect.
126+
/// </summary>
127+
/// <value>The number of retries if unable to connect.</value>
128+
public int Retries { get; protected set; }
129+
122130
/// <summary>
123131
/// Source type, based on ADO.Net provider name or identifier used by CUL
124132
/// </summary>

src/SQLHelper.DB/HelperClasses/Interfaces/IConnection.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ public interface IConnection
6060
/// <value>The parameter prefix.</value>
6161
string ParameterPrefix { get; }
6262

63+
/// <summary>
64+
/// Gets the number of retries if unable to connect.
65+
/// </summary>
66+
/// <value>The number of retries if unable to connect.</value>
67+
int Retries { get; }
68+
6369
/// <summary>
6470
/// Source type, based on ADO.Net provider name
6571
/// </summary>

src/SQLHelper.DB/SQLHelper.DB.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@
2727
<ItemGroup>
2828
<PackageReference Include="SQLParser" Version="2.0.0" />
2929
<PackageReference Include="System.Data.Common" Version="4.3.0" />
30-
<PackageReference Include="System.Data.SqlClient" Version="4.4.2" />
30+
<PackageReference Include="System.Data.SqlClient" Version="4.4.3" />
3131
<PackageReference Include="System.Dynamic.Runtime" Version="4.3.0" />
3232
<PackageReference Include="BigBook" Version="2.0.4" />
33-
<PackageReference Include="Microsoft.Extensions.Configuration" Version="2.0.0" />
33+
<PackageReference Include="Microsoft.Extensions.Configuration" Version="2.0.2" />
3434
<PackageReference Include="Microsoft.CSharp" Version="4.4.1" />
3535
</ItemGroup>
3636

src/SQLHelper.DB/SQLHelper.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,18 @@ public class SQLHelper
3939
/// <param name="factory">The factory.</param>
4040
/// <param name="database">The database.</param>
4141
public SQLHelper(IConfiguration configuration, DbProviderFactory factory, string database = "Default")
42+
: this(new Connection(configuration, factory, database))
4243
{
43-
DatabaseConnection = new Connection(configuration, factory, database);
44-
Batch = new Batch(DatabaseConnection);
4544
}
4645

4746
/// <summary>
4847
/// Initializes a new instance of the <see cref="SQLHelper"/> class.
4948
/// </summary>
5049
/// <param name="connection">The connection to use.</param>
5150
public SQLHelper(IConnection connection)
52-
: this(connection.Configuration, connection.Factory, connection.Name)
5351
{
52+
DatabaseConnection = connection;
53+
Batch = new Batch(DatabaseConnection);
5454
}
5555

5656
/// <summary>

test/SQLHelper.Tests/BaseClasses/TestingDirectoryFixture.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public TestingDirectoryFixture()
3030
try
3131
{
3232
TempCommand.CommandText = "Create Database TestDatabase";
33-
TempCommand.Open();
33+
TempCommand.Open(3);
3434
TempCommand.ExecuteNonQuery();
3535
}
3636
catch { }
@@ -45,7 +45,7 @@ public TestingDirectoryFixture()
4545
try
4646
{
4747
TempCommand.CommandText = "Create Table TestTable(ID INT PRIMARY KEY IDENTITY,StringValue1 NVARCHAR(100),StringValue2 NVARCHAR(MAX),BigIntValue BIGINT,BitValue BIT,DecimalValue DECIMAL(12,6),FloatValue FLOAT,DateTimeValue DATETIME,GUIDValue UNIQUEIDENTIFIER,TimeSpanValue TIME(7))";
48-
TempCommand.Open();
48+
TempCommand.Open(3);
4949
TempCommand.ExecuteNonQuery();
5050
TempCommand.CommandText = "Create Table TestTableNotNull(ID INT PRIMARY KEY IDENTITY,UShortValue_ SMALLINT NOT NULL)";
5151
TempCommand.ExecuteNonQuery();
@@ -66,7 +66,7 @@ public void Dispose()
6666
try
6767
{
6868
TempCommand.CommandText = "ALTER DATABASE TestDatabase SET OFFLINE WITH ROLLBACK IMMEDIATE\r\nALTER DATABASE TestDatabase SET ONLINE\r\nDROP DATABASE TestDatabase";
69-
TempCommand.Open();
69+
TempCommand.Open(3);
7070
TempCommand.ExecuteNonQuery();
7171
}
7272
finally { TempCommand.Close(); }

test/SQLHelper.Tests/SQLHelper.Tests.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@
2323
</ItemGroup>
2424

2525
<ItemGroup>
26-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0" />
26+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.7.0" />
2727
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
2828
<PackageReference Include="xunit" Version="2.3.1" />
29-
<PackageReference Include="FileCurator" Version="2.0.2" />
29+
<PackageReference Include="FileCurator" Version="2.0.3" />
3030
</ItemGroup>
3131

3232
<ItemGroup>

0 commit comments

Comments
 (0)