@@ -66,18 +66,15 @@ public CouchClient(string connectionString, Action<CouchSettings> couchSettingsF
66
66
#region CRUD
67
67
68
68
/// <summary>
69
- /// Returns an instance of the CouchDB database with the given name.
69
+ /// Returns an instance of the CouchDB database with the given name.
70
70
/// If EnsureDatabaseExists is configured, it creates the database if it doesn't exists.
71
71
/// </summary>
72
72
/// <typeparam name="TSource">The type of database documents.</typeparam>
73
73
/// <param name="database">The database name.</param>
74
74
/// <returns>An instance of the CouchDB database with given name.</returns>
75
75
public CouchDatabase < TSource > GetDatabase < TSource > ( string database ) where TSource : CouchDocument
76
76
{
77
- if ( database == null )
78
- {
79
- throw new ArgumentNullException ( nameof ( database ) ) ;
80
- }
77
+ database = EscapeDatabaseName ( database ) ;
81
78
82
79
if ( _settings . CheckDatabaseExists )
83
80
{
@@ -102,15 +99,7 @@ public CouchDatabase<TSource> GetDatabase<TSource>(string database) where TSourc
102
99
/// <returns>A task that represents the asynchronous operation. The task result contains the newly created CouchDB database.</returns>
103
100
public async Task < CouchDatabase < TSource > > CreateDatabaseAsync < TSource > ( string database , int ? shards = null , int ? replicas = null ) where TSource : CouchDocument
104
101
{
105
- if ( database == null )
106
- {
107
- throw new ArgumentNullException ( nameof ( database ) ) ;
108
- }
109
-
110
- if ( ! _systemDatabases . Contains ( database ) && ! new Regex ( @"^[a-z][a-z0-9_$()+/-]*$" ) . IsMatch ( database ) )
111
- {
112
- throw new ArgumentException ( $ "Name { database } contains invalid characters. Please visit: https://docs.couchdb.org/en/stable/api/database/common.html#put--db", nameof ( database ) ) ;
113
- }
102
+ database = EscapeDatabaseName ( database ) ;
114
103
115
104
IFlurlRequest request = NewRequest ( )
116
105
. AppendPathSegment ( database ) ;
@@ -119,6 +108,7 @@ public async Task<CouchDatabase<TSource>> CreateDatabaseAsync<TSource>(string da
119
108
{
120
109
request = request . SetQueryParam ( "q" , shards . Value ) ;
121
110
}
111
+
122
112
if ( replicas . HasValue )
123
113
{
124
114
request = request . SetQueryParam ( "n" , replicas . Value ) ;
@@ -146,10 +136,7 @@ public async Task<CouchDatabase<TSource>> CreateDatabaseAsync<TSource>(string da
146
136
/// <returns>A task that represents the asynchronous operation.</returns>
147
137
public async Task DeleteDatabaseAsync < TSource > ( string database ) where TSource : CouchDocument
148
138
{
149
- if ( database == null )
150
- {
151
- throw new ArgumentNullException ( nameof ( database ) ) ;
152
- }
139
+ database = EscapeDatabaseName ( database ) ;
153
140
154
141
OperationResult result = await NewRequest ( )
155
142
. AppendPathSegment ( database )
@@ -158,7 +145,8 @@ public async Task DeleteDatabaseAsync<TSource>(string database) where TSource :
158
145
. SendRequestAsync ( )
159
146
. ConfigureAwait ( false ) ;
160
147
161
- if ( ! result . Ok ) {
148
+ if ( ! result . Ok )
149
+ {
162
150
throw new CouchException ( "Something went wrong during the delete." , "S" ) ;
163
151
}
164
152
}
@@ -168,7 +156,7 @@ public async Task DeleteDatabaseAsync<TSource>(string database) where TSource :
168
156
#region CRUD reflection
169
157
170
158
/// <summary>
171
- /// Returns an instance of the CouchDB database of the given type.
159
+ /// Returns an instance of the CouchDB database of the given type.
172
160
/// If EnsureDatabaseExists is configured, it creates the database if it doesn't exists.
173
161
/// </summary>
174
162
/// <typeparam name="TSource">The type of database documents.</typeparam>
@@ -179,7 +167,7 @@ public CouchDatabase<TSource> GetDatabase<TSource>() where TSource : CouchDocume
179
167
}
180
168
181
169
/// <summary>
182
- /// Creates a new database of the given type in the server.
170
+ /// Creates a new database of the given type in the server.
183
171
/// The name must begin with a lowercase letter and can contains only lowercase characters, digits or _, $, (, ), +, - and /.s
184
172
/// </summary>
185
173
/// <typeparam name="TSource">The type of database documents.</typeparam>
@@ -235,7 +223,7 @@ public CouchDatabase<TUser> GetUsersDatabase<TUser>() where TUser : CouchUser
235
223
#region Utils
236
224
237
225
/// <summary>
238
- /// Determines whether the server is up, running, and ready to respond to requests.
226
+ /// Determines whether the server is up, running, and ready to respond to requests.
239
227
/// </summary>
240
228
/// <returns>true is the server is not in maintenance_mode; otherwise, false.</returns>
241
229
public async Task < bool > IsUpAsync ( )
@@ -249,7 +237,7 @@ public async Task<bool> IsUpAsync()
249
237
. ConfigureAwait ( false ) ;
250
238
return result . Status == "ok" ;
251
239
}
252
- catch ( CouchNotFoundException )
240
+ catch ( CouchNotFoundException )
253
241
{
254
242
return false ;
255
243
}
@@ -292,6 +280,21 @@ private IFlurlRequest NewRequest()
292
280
return _flurlClient . Request ( ConnectionString ) ;
293
281
}
294
282
283
+ private string EscapeDatabaseName ( string database )
284
+ {
285
+ if ( database == null )
286
+ {
287
+ throw new ArgumentNullException ( nameof ( database ) ) ;
288
+ }
289
+
290
+ if ( ! _systemDatabases . Contains ( database ) && ! new Regex ( @"^[a-z][a-z0-9_$()+/-]*$" ) . IsMatch ( database ) )
291
+ {
292
+ throw new ArgumentException ( $ "Name { database } contains invalid characters. Please visit: https://docs.couchdb.org/en/stable/api/database/common.html#put--db", nameof ( database ) ) ;
293
+ }
294
+
295
+ return Uri . EscapeDataString ( database ) ;
296
+ }
297
+
295
298
/// <summary>
296
299
/// Performs the logout and disposes the HTTP client.
297
300
/// </summary>
@@ -305,7 +308,7 @@ protected virtual void Dispose(bool disposing)
305
308
{
306
309
if ( _settings . AuthenticationType == AuthenticationType . Cookie && _settings . LogOutOnDispose )
307
310
{
308
- AsyncContext . Run ( ( ) => LogoutAsync ( ) . ConfigureAwait ( false ) ) ;
311
+ _ = AsyncContext . Run ( ( ) => LogoutAsync ( ) . ConfigureAwait ( false ) ) ;
309
312
}
310
313
_flurlClient . Dispose ( ) ;
311
314
}
0 commit comments