19
19
using MongoDB . Bson ;
20
20
using MongoDB . Bson . Serialization . Serializers ;
21
21
using MongoDB . Driver . Core . Bindings ;
22
- using MongoDB . Driver . Core . Connections ;
23
22
using MongoDB . Driver . Core . Misc ;
24
23
using MongoDB . Driver . Core . WireProtocol . Messages . Encoders ;
24
+ using MongoDB . Driver . Encryption ;
25
+ using static MongoDB . Driver . Encryption . EncryptedCollectionHelper ;
25
26
26
27
namespace MongoDB . Driver . Core . Operations
27
28
{
@@ -30,12 +31,48 @@ namespace MongoDB.Driver.Core.Operations
30
31
/// </summary>
31
32
public class CreateCollectionOperation : IWriteOperation < BsonDocument >
32
33
{
34
+ #region static
35
+ internal static IWriteOperation < BsonDocument > CreateEncryptedCreateCollectionOperationIfConfigured (
36
+ CollectionNamespace collectionNamespace ,
37
+ BsonDocument encryptedFields ,
38
+ MessageEncoderSettings messageEncoderSettings ,
39
+ Action < CreateCollectionOperation > createCollectionOperationConfigurator )
40
+ {
41
+ var mainOperation = new CreateCollectionOperation (
42
+ collectionNamespace ,
43
+ messageEncoderSettings )
44
+ {
45
+ EncryptedFields = encryptedFields
46
+ } ;
47
+
48
+ createCollectionOperationConfigurator ? . Invoke ( mainOperation ) ;
49
+
50
+ if ( encryptedFields != null )
51
+ {
52
+ return new CompositeWriteOperation < BsonDocument > (
53
+ ( CreateInnerCollectionOperation ( EncryptedCollectionHelper . GetAdditionalCollectionName ( encryptedFields , collectionNamespace , HelperCollectionForEncryption . Esc ) ) , IsMainOperation : false ) ,
54
+ ( CreateInnerCollectionOperation ( EncryptedCollectionHelper . GetAdditionalCollectionName ( encryptedFields , collectionNamespace , HelperCollectionForEncryption . Ecc ) ) , IsMainOperation : false ) ,
55
+ ( CreateInnerCollectionOperation ( EncryptedCollectionHelper . GetAdditionalCollectionName ( encryptedFields , collectionNamespace , HelperCollectionForEncryption . Ecos ) ) , IsMainOperation : false ) ,
56
+ ( mainOperation , IsMainOperation : true ) ,
57
+ ( new CreateIndexesOperation ( collectionNamespace , new [ ] { new CreateIndexRequest ( EncryptedCollectionHelper . AdditionalCreateIndexDocument ) } , messageEncoderSettings ) , IsMainOperation : false ) ) ;
58
+ }
59
+ else
60
+ {
61
+ return mainOperation ;
62
+ }
63
+
64
+ CreateCollectionOperation CreateInnerCollectionOperation ( string collectionName )
65
+ => new CreateCollectionOperation ( new CollectionNamespace ( collectionNamespace . DatabaseNamespace . DatabaseName , collectionName ) , messageEncoderSettings ) ;
66
+ }
67
+ #endregion
68
+
33
69
// fields
34
70
private bool ? _autoIndexId ;
35
71
private bool ? _capped ;
36
72
private Collation _collation ;
37
73
private readonly CollectionNamespace _collectionNamespace ;
38
74
private BsonValue _comment ;
75
+ private BsonDocument _encryptedFields ;
39
76
private TimeSpan ? _expireAfter ;
40
77
private BsonDocument _indexOptionDefaults ;
41
78
private long ? _maxDocuments ;
@@ -125,6 +162,12 @@ public CollectionNamespace CollectionNamespace
125
162
get { return _collectionNamespace ; }
126
163
}
127
164
165
+ internal BsonDocument EncryptedFields
166
+ {
167
+ get { return _encryptedFields ; }
168
+ private set { _encryptedFields = value ; }
169
+ }
170
+
128
171
/// <summary>
129
172
/// Gets or sets the expiration timespan for time series collections. Used to automatically delete documents in time series collections.
130
173
/// See https://www.mongodb.com/docs/manual/reference/command/create/ for supported options and https://www.mongodb.com/docs/manual/core/timeseries-collections/
@@ -282,7 +325,7 @@ public WriteConcern WriteConcern
282
325
}
283
326
284
327
// methods
285
- internal BsonDocument CreateCommand ( ICoreSessionHandle session , ConnectionDescription connectionDescription )
328
+ internal BsonDocument CreateCommand ( ICoreSessionHandle session )
286
329
{
287
330
var flags = GetFlags ( ) ;
288
331
var writeConcern = WriteConcernHelper . GetEffectiveWriteConcern ( session , _writeConcern ) ;
@@ -303,7 +346,8 @@ internal BsonDocument CreateCommand(ICoreSessionHandle session, ConnectionDescri
303
346
{ "comment" , _comment , _comment != null } ,
304
347
{ "writeConcern" , writeConcern , writeConcern != null } ,
305
348
{ "expireAfterSeconds" , ( ) => _expireAfter . Value . TotalSeconds , _expireAfter . HasValue } ,
306
- { "timeseries" , ( ) => _timeSeriesOptions . ToBsonDocument ( ) , _timeSeriesOptions != null }
349
+ { "timeseries" , ( ) => _timeSeriesOptions . ToBsonDocument ( ) , _timeSeriesOptions != null } ,
350
+ { "encryptedFields" , _encryptedFields , _encryptedFields != null }
307
351
} ;
308
352
}
309
353
@@ -337,7 +381,7 @@ public BsonDocument Execute(IWriteBinding binding, CancellationToken cancellatio
337
381
using ( var channel = channelSource . GetChannel ( cancellationToken ) )
338
382
using ( var channelBinding = new ChannelReadWriteBinding ( channelSource . Server , channel , binding . Session . Fork ( ) ) )
339
383
{
340
- var operation = CreateOperation ( channelBinding . Session , channel . ConnectionDescription ) ;
384
+ var operation = CreateOperation ( channelBinding . Session ) ;
341
385
return operation . Execute ( channelBinding , cancellationToken ) ;
342
386
}
343
387
}
@@ -351,14 +395,14 @@ public async Task<BsonDocument> ExecuteAsync(IWriteBinding binding, Cancellation
351
395
using ( var channel = await channelSource . GetChannelAsync ( cancellationToken ) . ConfigureAwait ( false ) )
352
396
using ( var channelBinding = new ChannelReadWriteBinding ( channelSource . Server , channel , binding . Session . Fork ( ) ) )
353
397
{
354
- var operation = CreateOperation ( channelBinding . Session , channel . ConnectionDescription ) ;
398
+ var operation = CreateOperation ( channelBinding . Session ) ;
355
399
return await operation . ExecuteAsync ( channelBinding , cancellationToken ) . ConfigureAwait ( false ) ;
356
400
}
357
401
}
358
402
359
- private WriteCommandOperation < BsonDocument > CreateOperation ( ICoreSessionHandle session , ConnectionDescription connectionDescription )
403
+ private WriteCommandOperation < BsonDocument > CreateOperation ( ICoreSessionHandle session )
360
404
{
361
- var command = CreateCommand ( session , connectionDescription ) ;
405
+ var command = CreateCommand ( session ) ;
362
406
return new WriteCommandOperation < BsonDocument > ( _collectionNamespace . DatabaseNamespace , command , BsonDocumentSerializer . Instance , _messageEncoderSettings ) ;
363
407
}
364
408
0 commit comments