@@ -37,47 +37,42 @@ namespace Hangfire.PostgreSql
37
37
public class PostgreSqlStorage : JobStorage
38
38
{
39
39
private readonly Action < NpgsqlConnection > _connectionSetup ;
40
- private readonly string _connectionString ;
40
+ private readonly NpgsqlConnectionStringBuilder _connectionStringBuilder ;
41
41
private readonly NpgsqlConnection _existingConnection ;
42
42
43
- public PostgreSqlStorage ( string nameOrConnectionString )
44
- : this ( nameOrConnectionString , new PostgreSqlStorageOptions ( ) ) { }
43
+ public PostgreSqlStorage ( string connectionString )
44
+ : this ( connectionString , new PostgreSqlStorageOptions ( ) ) { }
45
45
46
- public PostgreSqlStorage ( string nameOrConnectionString , PostgreSqlStorageOptions options )
47
- : this ( nameOrConnectionString , null , options ) { }
46
+ public PostgreSqlStorage ( string connectionString , PostgreSqlStorageOptions options )
47
+ : this ( connectionString , null , options ) { }
48
48
49
49
/// <summary>
50
- /// Initializes PostgreSqlStorage from the provided PostgreSqlStorageOptions and either the provided connection
51
- /// string or the connection string with provided name pulled from the application config file.
50
+ /// Initializes PostgreSqlStorage from the provided PostgreSqlStorageOptions and either the provided connection string.
52
51
/// </summary>
53
- /// <param name="nameOrConnectionString">
54
- /// Either a SQL Server connection string or the name of
55
- /// a SQL Server connection string located in the connectionStrings node in the application config
56
- /// </param>
52
+ /// <param name="connectionString">PostgreSQL connection string</param>
57
53
/// <param name="connectionSetup">Optional setup action to apply to created connections</param>
58
- /// <param name="options"></param>
59
- /// <exception cref="ArgumentNullException"><paramref name="nameOrConnectionString " /> argument is null.</exception>
54
+ /// <param name="options">Storage options </param>
55
+ /// <exception cref="ArgumentNullException"><paramref name="connectionString " /> argument is null.</exception>
60
56
/// <exception cref="ArgumentNullException"><paramref name="options" /> argument is null.</exception>
61
- /// <exception cref="ArgumentException">
62
- /// <paramref name="nameOrConnectionString" /> argument is neither
63
- /// a valid SQL Server connection string nor the name of a connection string in the application
64
- /// config file.
65
- /// </exception>
66
- public PostgreSqlStorage ( string nameOrConnectionString ,
57
+ /// <exception cref="ArgumentException"><paramref name="connectionString" /> argument not a valid PostgreSQL connection string config file.</exception>
58
+ public PostgreSqlStorage (
59
+ string connectionString ,
67
60
Action < NpgsqlConnection > connectionSetup ,
68
61
PostgreSqlStorageOptions options )
69
62
{
70
- if ( nameOrConnectionString == null )
63
+ if ( connectionString == null )
71
64
{
72
- throw new ArgumentNullException ( nameof ( nameOrConnectionString ) ) ;
65
+ throw new ArgumentNullException ( nameof ( connectionString ) ) ;
73
66
}
74
67
75
68
Options = options ?? throw new ArgumentNullException ( nameof ( options ) ) ;
76
69
77
- _connectionString = IsConnectionString ( nameOrConnectionString )
78
- ? nameOrConnectionString
79
- : throw new ArgumentException ( $ "Could not find connection string with name '{ nameOrConnectionString } ' in application config file") ;
70
+ if ( ! TryCreateConnectionStringBuilder ( connectionString , out NpgsqlConnectionStringBuilder builder ) )
71
+ {
72
+ throw new ArgumentException ( $ "Connection string [{ connectionString } ] is not valid", nameof ( connectionString ) ) ;
73
+ }
80
74
75
+ _connectionStringBuilder = builder ;
81
76
_connectionSetup = connectionSetup ;
82
77
83
78
if ( options . PrepareSchemaIfNecessary )
@@ -176,13 +171,12 @@ public override string ToString()
176
171
177
172
try
178
173
{
179
- NpgsqlConnectionStringBuilder connectionStringBuilder = new ( _connectionString ) ;
180
174
StringBuilder builder = new ( ) ;
181
175
182
176
builder . Append ( "Host: " ) ;
183
- builder . Append ( connectionStringBuilder . Host ) ;
177
+ builder . Append ( _connectionStringBuilder . Host ) ;
184
178
builder . Append ( ", DB: " ) ;
185
- builder . Append ( connectionStringBuilder . Database ) ;
179
+ builder . Append ( _connectionStringBuilder . Database ) ;
186
180
builder . Append ( ", Schema: " ) ;
187
181
builder . Append ( Options . SchemaName ) ;
188
182
@@ -211,15 +205,11 @@ internal NpgsqlConnection CreateAndOpenConnection()
211
205
NpgsqlConnection connection = _existingConnection ;
212
206
if ( connection == null )
213
207
{
214
- NpgsqlConnectionStringBuilder connectionStringBuilder = new ( _connectionString ) ;
215
- if ( ! Options . EnableTransactionScopeEnlistment )
216
- {
217
- connectionStringBuilder . Enlist = false ;
218
- }
208
+ _connectionStringBuilder . Enlist = Options . EnableTransactionScopeEnlistment ;
219
209
220
- SetTimezoneToUtcForNpgsqlCompatibility ( connectionStringBuilder ) ;
210
+ SetTimezoneToUtcForNpgsqlCompatibility ( _connectionStringBuilder ) ;
221
211
222
- connection = new NpgsqlConnection ( connectionStringBuilder . ToString ( ) ) ;
212
+ connection = new NpgsqlConnection ( _connectionStringBuilder . ToString ( ) ) ;
223
213
_connectionSetup ? . Invoke ( connection ) ;
224
214
}
225
215
@@ -399,9 +389,18 @@ private void InitializeQueueProviders()
399
389
QueueProviders = new PersistentJobQueueProviderCollection ( defaultQueueProvider ) ;
400
390
}
401
391
402
- private bool IsConnectionString ( string nameOrConnectionString )
392
+ private bool TryCreateConnectionStringBuilder ( string connectionString , out NpgsqlConnectionStringBuilder builder )
403
393
{
404
- return nameOrConnectionString . Contains ( ";" ) ;
394
+ try
395
+ {
396
+ builder = new NpgsqlConnectionStringBuilder ( connectionString ) ;
397
+ return true ;
398
+ }
399
+ catch ( ArgumentException )
400
+ {
401
+ builder = null ;
402
+ return false ;
403
+ }
405
404
}
406
405
}
407
406
}
0 commit comments