forked from npgsql/npgsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNpgsqlConnectionStringBuilder.cs
More file actions
1112 lines (1009 loc) · 40.2 KB
/
NpgsqlConnectionStringBuilder.cs
File metadata and controls
1112 lines (1009 loc) · 40.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// created on 29/11/2007
// Npgsql.NpgsqlConnectionStringBuilder.cs
//
// Author:
// Glen Parker (glenebob@gmail.com)
// Ben Sagal (bensagal@gmail.com)
// Tao Wang (dancefire@gmail.com)
//
// Copyright (C) 2007 The Npgsql Development Team
// npgsql-general@gborg.postgresql.org
// http://gborg.postgresql.org/project/npgsql/projdisplay.php
//
// Permission to use, copy, modify, and distribute this software and its
// documentation for any purpose, without fee, and without a written
// agreement is hereby granted, provided that the above copyright notice
// and this paragraph and the following two paragraphs appear in all copies.
//
// IN NO EVENT SHALL THE NPGSQL DEVELOPMENT TEAM BE LIABLE TO ANY PARTY
// FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES,
// INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
// DOCUMENTATION, EVEN IF THE NPGSQL DEVELOPMENT TEAM HAS BEEN ADVISED OF
// THE POSSIBILITY OF SUCH DAMAGE.
//
// THE NPGSQL DEVELOPMENT TEAM SPECIFICALLY DISCLAIMS ANY WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
// ON AN "AS IS" BASIS, AND THE NPGSQL DEVELOPMENT TEAM HAS NO OBLIGATIONS
// TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Diagnostics;
using System.Reflection;
using System.Resources;
using System.Runtime.Versioning;
using System.Text;
// Keep the xml comment warning quiet for this file.
#pragma warning disable 1591
namespace Npgsql
{
public sealed class NpgsqlConnectionStringBuilder : DbConnectionStringBuilder
{
private delegate string ValueNativeToString(object value);
private class ValueDescription
{
internal readonly IComparable ImplicitDefault;
internal readonly IComparable ExplicitDefault;
internal readonly bool DefaultsDiffer;
internal readonly bool StoreInBase;
private readonly ValueNativeToString NativeToString;
/// <summary>
/// Set both ImplicitDefault and ExplicitDefault to the <paramref name="t"/>'s default value.
/// </summary>
/// <param name="t"></param>
/// <param name="storeInBase"></param>
/// <param name="nativeToString"></param>
internal ValueDescription(Type t, bool storeInBase = true, ValueNativeToString nativeToString = null)
{
ImplicitDefault = GetImplicitDefault(t);
ExplicitDefault = ImplicitDefault;
DefaultsDiffer = false;
StoreInBase = storeInBase;
NativeToString = nativeToString;
}
/// <summary>
/// Set ImplicitDefault to the default value of <paramref name="explicitDefault"/>'s type,
/// and ExplicitDefault to <paramref name="explicitDefault"/>.
/// </summary>
/// <param name="explicitDefault"></param>
/// <param name="storeInBase"></param>
/// <param name="nativeToString"></param>
internal ValueDescription(IComparable explicitDefault, bool storeInBase = true, ValueNativeToString nativeToString = null)
{
ImplicitDefault = GetImplicitDefault(explicitDefault.GetType());
ExplicitDefault = explicitDefault;
DefaultsDiffer = (ImplicitDefault.CompareTo(ExplicitDefault) != 0);
StoreInBase = storeInBase;
NativeToString = nativeToString;
}
private static IComparable GetImplicitDefault(Type t)
{
if (t == typeof(string))
{
return string.Empty;
}
else
{
return (IComparable)Activator.CreateInstance(t);
}
}
internal string ConvertNativeToString(object value)
{
string asString = value as string;
if (asString != null)
{
return asString.Trim();
}
else if (NativeToString != null)
{
return NativeToString(value);
}
else
{
return value.ToString();
}
}
}
private static readonly ResourceManager resman = new ResourceManager(MethodBase.GetCurrentMethod().DeclaringType);
private static readonly Dictionary<Keywords, ValueDescription> valueDescriptions = new Dictionary<Keywords, ValueDescription>();
private string originalConnectionString;
private const int POOL_SIZE_LIMIT = 1024;
private const int TIMEOUT_LIMIT = 1024;
static NpgsqlConnectionStringBuilder()
{
// Set up value descriptions.
// All connection string values have an implicit default (its type's default value),
// and an explicit default which can be different from its
// implicit default.
valueDescriptions.Add(Keywords.Host, new ValueDescription(typeof(string)));
valueDescriptions.Add(Keywords.Port, new ValueDescription((Int32)5432));
valueDescriptions.Add(Keywords.Protocol, new ValueDescription(typeof(ProtocolVersion), true, ProtocolVersionToString));
valueDescriptions.Add(Keywords.Database, new ValueDescription(typeof(string)));
valueDescriptions.Add(Keywords.UserName, new ValueDescription(typeof(string)));
valueDescriptions.Add(Keywords.Password, new ValueDescription(typeof(string)));
valueDescriptions.Add(Keywords.SSL, new ValueDescription(typeof(bool)));
valueDescriptions.Add(Keywords.SslMode, new ValueDescription(typeof(SslMode)));
#pragma warning disable 618
valueDescriptions.Add(Keywords.Encoding, new ValueDescription("UTF8", false));
#pragma warning restore 618
valueDescriptions.Add(Keywords.Timeout, new ValueDescription((Int32)15));
valueDescriptions.Add(Keywords.SearchPath, new ValueDescription(typeof(string)));
valueDescriptions.Add(Keywords.Pooling, new ValueDescription(true));
valueDescriptions.Add(Keywords.ConnectionLifeTime, new ValueDescription(typeof(Int32)));
valueDescriptions.Add(Keywords.MinPoolSize, new ValueDescription((Int32)1));
valueDescriptions.Add(Keywords.MaxPoolSize, new ValueDescription((Int32)20));
valueDescriptions.Add(Keywords.SyncNotification, new ValueDescription(typeof(bool)));
valueDescriptions.Add(Keywords.CommandTimeout, new ValueDescription((Int32)20));
valueDescriptions.Add(Keywords.Enlist, new ValueDescription(typeof(bool)));
valueDescriptions.Add(Keywords.PreloadReader, new ValueDescription(typeof(bool)));
valueDescriptions.Add(Keywords.UseExtendedTypes, new ValueDescription(typeof(bool)));
valueDescriptions.Add(Keywords.IntegratedSecurity, new ValueDescription(typeof(bool)));
valueDescriptions.Add(Keywords.Compatible, new ValueDescription(THIS_VERSION));
valueDescriptions.Add(Keywords.AlwaysPrepare, new ValueDescription(typeof(bool)));
}
public NpgsqlConnectionStringBuilder()
{
_password = new PasswordBytes();
this.Clear();
}
public NpgsqlConnectionStringBuilder(string connectionString)
{
_password = new PasswordBytes();
this.originalConnectionString = connectionString;
base.ConnectionString = connectionString;
CheckValues();
}
/// <summary>
/// Return an exact copy of this NpgsqlConnectionString.
/// </summary>
public NpgsqlConnectionStringBuilder Clone()
{
NpgsqlConnectionStringBuilder builder = new NpgsqlConnectionStringBuilder();
foreach (string key in this.Keys)
{
builder[key] = this[key];
}
return builder;
}
private void CheckValues()
{
if ((MaxPoolSize > 0) && (MinPoolSize > MaxPoolSize))
{
string key = GetKeyName(Keywords.MinPoolSize);
throw new ArgumentOutOfRangeException(
key, String.Format(resman.GetString("Exception_IntegerKeyValMax"), key, MaxPoolSize));
}
}
#region Parsing Functions
private static SslMode ToSslMode(object value)
{
if (value is SslMode)
{
return (SslMode) value;
}
else
{
return (SslMode) Enum.Parse(typeof (SslMode), value.ToString(), true);
}
}
private static ProtocolVersion ToProtocolVersion(object value)
{
if (value is ProtocolVersion)
{
return (ProtocolVersion) value;
}
else
{
int ver = Convert.ToInt32(value);
switch (ver)
{
case 2:
return ProtocolVersion.Version2;
case 3:
return ProtocolVersion.Version3;
default:
throw new InvalidCastException(value.ToString());
}
}
}
private static string ProtocolVersionToString(object protocolVersion)
{
switch ((ProtocolVersion)protocolVersion)
{
case ProtocolVersion.Version2:
return "2";
case ProtocolVersion.Version3:
return "3";
default:
return string.Empty;
}
}
private static int ToInt32(object value, int min, int max, Keywords keyword)
{
int v = Convert.ToInt32(value);
if (v < min)
{
string key = GetKeyName(keyword);
throw new ArgumentOutOfRangeException(
key, String.Format(resman.GetString("Exception_IntegerKeyValMin"), key, min));
}
else if (v > max)
{
string key = GetKeyName(keyword);
throw new ArgumentOutOfRangeException(
key, String.Format(resman.GetString("Exception_IntegerKeyValMax"), key, max));
}
return v;
}
private static Boolean ToBoolean(object value)
{
string text = value as string;
if (text != null)
{
switch (text.ToLowerInvariant())
{
case "t":
case "true":
case "y":
case "yes":
return true;
case "f":
case "false":
case "n":
case "no":
return false;
default:
throw new InvalidCastException(value.ToString());
}
}
else
{
return Convert.ToBoolean(value);
}
}
private Boolean ToIntegratedSecurity(object value)
{
string text = value as string;
if (text != null)
{
switch (text.ToLowerInvariant())
{
case "t":
case "true":
case "y":
case "yes":
case "sspi":
return true;
case "f":
case "false":
case "n":
case "no":
return false;
default:
throw new InvalidCastException(value.ToString());
}
}
else
{
return Convert.ToBoolean(value);
}
}
#endregion
#region Properties
private string _host;
/// <summary>
/// Gets or sets the backend server host name.
/// </summary>
public string Host
{
get { return _host; }
set { SetValue(GetKeyName(Keywords.Host), Keywords.Host, value); }
}
private int _port;
/// <summary>
/// Gets or sets the backend server port.
/// </summary>
public int Port
{
get { return _port; }
set { SetValue(GetKeyName(Keywords.Port), Keywords.Port, value); }
}
private ProtocolVersion _protocol;
/// <summary>
/// Gets or sets the specified backend communication protocol version.
/// </summary>
public ProtocolVersion Protocol
{
get { return _protocol; }
set { SetValue(GetKeyName(Keywords.Protocol), Keywords.Protocol, value); }
}
private string _database;
///<summary>
/// Gets or sets the name of the database to be used after a connection is opened.
/// </summary>
/// <value>The name of the database to be
/// used after a connection is opened.</value>
public string Database
{
get { return _database; }
set { SetValue(GetKeyName(Keywords.Database), Keywords.Database, value); }
}
private string _username;
/// <summary>
/// Gets or sets the login user name.
/// </summary>
public string UserName
{
get
{
if ((_integrated_security) && (String.IsNullOrEmpty(_username)))
_username = WindowsIdentityUserName;
return _username;
}
set { SetValue(GetKeyName(Keywords.UserName), Keywords.UserName, value); }
}
/// <summary>
/// This is a pretty horrible hack to fix https://github.com/npgsql/Npgsql/issues/133
/// In a nutshell, starting with .NET 4.5 WindowsIdentity inherits from ClaimsIdentity
/// which doesn't exist in mono, and calling UserName getter above bombs.
/// The workaround is that the function that actually deals with WindowsIdentity never
/// gets called on mono, so never gets JITted and the problem goes away.
/// </summary>
private string WindowsIdentityUserName
{
get
{
var identity = System.Security.Principal.WindowsIdentity.GetCurrent();
return identity.Name.Split('\\')[1];
}
}
private PasswordBytes _password;
/// <summary>
/// Gets or sets the login password as a UTF8 encoded byte array.
/// </summary>
public byte[] PasswordAsByteArray
{
get { return _password.PasswordAsByteArray; }
set
{
if (value == null)
{
throw new ArgumentNullException("PasswordAsByteArray");
}
_password.PasswordAsByteArray = value;
}
}
/// <summary>
/// Sets the login password as a string.
/// </summary>
public string Password
{
set { SetValue(GetKeyName(Keywords.Password), Keywords.Password, value); }
}
private bool _ssl;
/// <summary>
/// Gets or sets a value indicating whether to attempt to use SSL.
/// </summary>
public bool SSL
{
get { return _ssl; }
set { SetValue(GetKeyName(Keywords.SSL), Keywords.SSL, value); }
}
private SslMode _sslmode;
/// <summary>
/// Gets or sets a value indicating whether to attempt to use SSL.
/// </summary>
public SslMode SslMode
{
get { return _sslmode; }
set { SetValue(GetKeyName(Keywords.SslMode), Keywords.SslMode, value); }
}
/// <summary>
/// Gets the backend encoding. Always returns "UTF8".
/// </summary>
[Obsolete("UTF8 is always used regardless of this setting.")]
public string Encoding
{
#pragma warning disable 618
get { return (string)valueDescriptions[Keywords.Encoding].ExplicitDefault; }
#pragma warning restore 618
}
private int _timeout;
/// <summary>
/// Gets or sets the time to wait while trying to establish a connection
/// before terminating the attempt and generating an error.
/// </summary>
/// <value>The time (in seconds) to wait for a connection to open. The default value is 15 seconds.</value>
public int Timeout
{
get { return _timeout; }
set { SetValue(GetKeyName(Keywords.Timeout), Keywords.Timeout, value); }
}
private string _searchpath;
/// <summary>
/// Gets or sets the schema search path.
/// </summary>
public string SearchPath
{
get { return _searchpath; }
set { SetValue(GetKeyName(Keywords.SearchPath), Keywords.SearchPath, value); }
}
private bool _pooling;
/// <summary>
/// Gets or sets a value indicating whether connection pooling should be used.
/// </summary>
public bool Pooling
{
get { return _pooling; }
set { SetValue(GetKeyName(Keywords.Pooling), Keywords.Pooling, value); }
}
private int _connection_life_time;
/// <summary>
/// Gets or sets the time to wait before closing unused connections in the pool if the count
/// of all connections exeeds MinPoolSize.
/// </summary>
/// <remarks>
/// If connection pool contains unused connections for ConnectionLifeTime seconds,
/// the half of them will be closed. If there will be unused connections in a second
/// later then again the half of them will be closed and so on.
/// This strategy provide smooth change of connection count in the pool.
/// </remarks>
/// <value>The time (in seconds) to wait. The default value is 15 seconds.</value>
public int ConnectionLifeTime
{
get { return _connection_life_time; }
set { SetValue(GetKeyName(Keywords.ConnectionLifeTime), Keywords.ConnectionLifeTime, value); }
}
private int _min_pool_size;
/// <summary>
/// Gets or sets the minimum connection pool size.
/// </summary>
public int MinPoolSize
{
get { return _min_pool_size; }
set { SetValue(GetKeyName(Keywords.MinPoolSize), Keywords.MinPoolSize, value); }
}
private int _max_pool_size;
/// <summary>
/// Gets or sets the maximum connection pool size.
/// </summary>
public int MaxPoolSize
{
get { return _max_pool_size; }
set { SetValue(GetKeyName(Keywords.MaxPoolSize), Keywords.MaxPoolSize, value); }
}
private bool _sync_notification;
/// <summary>
/// Gets or sets a value indicating whether to listen for notifications and report them between command activity.
/// </summary>
public bool SyncNotification
{
get { return _sync_notification; }
set { SetValue(GetKeyName(Keywords.SyncNotification), Keywords.SyncNotification, value); }
}
private int _command_timeout;
/// <summary>
/// Gets the time to wait while trying to execute a command
/// before terminating the attempt and generating an error.
/// </summary>
/// <value>The time (in seconds) to wait for a command to complete. The default value is 20 seconds.</value>
public int CommandTimeout
{
get { return _command_timeout; }
set { SetValue(GetKeyName(Keywords.CommandTimeout), Keywords.CommandTimeout, value); }
}
private bool _enlist;
public bool Enlist
{
get { return _enlist; }
set { SetValue(GetKeyName(Keywords.Enlist), Keywords.Enlist, value); }
}
private bool _preloadReader;
/// <summary>
/// Gets or sets a value indicating whether datareaders are loaded in their entirety (for compatibility with earlier code).
/// </summary>
public bool PreloadReader
{
get { return _preloadReader; }
set { SetValue(GetKeyName(Keywords.PreloadReader), Keywords.PreloadReader, value); }
}
private bool _useExtendedTypes;
public bool UseExtendedTypes
{
get { return _useExtendedTypes; }
set { SetValue(GetKeyName(Keywords.UseExtendedTypes), Keywords.UseExtendedTypes, value); }
}
private bool _integrated_security;
public bool IntegratedSecurity
{
get { return _integrated_security; }
set
{
if (value == true)
CheckIntegratedSecuritySupport();
SetValue(GetKeyName(Keywords.IntegratedSecurity), Keywords.IntegratedSecurity, value);
}
}
/// <summary>
/// No integrated security if we're on mono and .NET 4.5 because of ClaimsIdentity,
/// see https://github.com/npgsql/Npgsql/issues/133
/// </summary>
[Conditional("NET45")]
private static void CheckIntegratedSecuritySupport()
{
if (Type.GetType("Mono.Runtime") != null)
throw new NotSupportedException("IntegratedSecurity is currently unsupported on mono and .NET 4.5 (see https://github.com/npgsql/Npgsql/issues/133)");
}
private Version _compatible;
private static readonly Version THIS_VERSION =
MethodBase.GetCurrentMethod().DeclaringType.Assembly.GetName().Version;
/// <summary>
/// Compatibilty version. When possible, behaviour caused by breaking changes will be preserved
/// if this version is less than that where the breaking change was introduced.
/// </summary>
public Version Compatible
{
get { return _compatible; }
set { SetValue(GetKeyName(Keywords.Compatible), Keywords.Compatible, value); }
}
private string _application_name;
/// <summary>
/// Gets or sets the ootional application name parameter to be sent to the backend during connection initiation.
/// </summary>
public string ApplicationName
{
get { return _application_name; }
set { SetValue(GetKeyName(Keywords.ApplicationName), Keywords.ApplicationName, value); }
}
private bool _always_prepare;
/// <summary>
/// Gets or sets a value indicating whether to silently Prepare() all commands before execution.
/// </summary>
public bool AlwaysPrepare
{
get { return _always_prepare; }
set { SetValue(GetKeyName(Keywords.AlwaysPrepare), Keywords.AlwaysPrepare, value); }
}
#endregion
private static Keywords GetKey(string key)
{
switch (key.ToUpperInvariant())
{
case "HOST":
case "SERVER":
return Keywords.Host;
case "PORT":
return Keywords.Port;
case "PROTOCOL":
return Keywords.Protocol;
case "DATABASE":
case "DB":
return Keywords.Database;
case "USERNAME":
case "USER NAME":
case "USER":
case "USERID":
case "USER ID":
case "UID":
return Keywords.UserName;
case "PASSWORD":
case "PSW":
case "PWD":
return Keywords.Password;
case "SSL":
return Keywords.SSL;
case "SSLMODE":
return Keywords.SslMode;
case "ENCODING":
#pragma warning disable 618
return Keywords.Encoding;
#pragma warning restore 618
case "TIMEOUT":
return Keywords.Timeout;
case "SEARCHPATH":
return Keywords.SearchPath;
case "POOLING":
return Keywords.Pooling;
case "CONNECTIONLIFETIME":
return Keywords.ConnectionLifeTime;
case "MINPOOLSIZE":
return Keywords.MinPoolSize;
case "MAXPOOLSIZE":
return Keywords.MaxPoolSize;
case "SYNCNOTIFICATION":
return Keywords.SyncNotification;
case "COMMANDTIMEOUT":
return Keywords.CommandTimeout;
case "ENLIST":
return Keywords.Enlist;
case "PRELOADREADER":
case "PRELOAD READER":
return Keywords.PreloadReader;
case "USEEXTENDEDTYPES":
case "USE EXTENDED TYPES":
return Keywords.UseExtendedTypes;
case "INTEGRATED SECURITY":
return Keywords.IntegratedSecurity;
case "COMPATIBLE":
return Keywords.Compatible;
case "APPLICATIONNAME":
return Keywords.ApplicationName;
case "ALWAYSPREPARE":
return Keywords.AlwaysPrepare;
default:
throw new ArgumentException(resman.GetString("Exception_WrongKeyVal"), key);
}
}
internal static string GetKeyName(Keywords keyword)
{
switch (keyword)
{
case Keywords.Host:
return "HOST";
case Keywords.Port:
return "PORT";
case Keywords.Protocol:
return "PROTOCOL";
case Keywords.Database:
return "DATABASE";
case Keywords.UserName:
return "USER ID";
case Keywords.Password:
return "PASSWORD";
case Keywords.SSL:
return "SSL";
case Keywords.SslMode:
return "SSLMODE";
#pragma warning disable 618
case Keywords.Encoding:
#pragma warning restore 618
return "ENCODING";
case Keywords.Timeout:
return "TIMEOUT";
case Keywords.SearchPath:
return "SEARCHPATH";
case Keywords.Pooling:
return "POOLING";
case Keywords.ConnectionLifeTime:
return "CONNECTIONLIFETIME";
case Keywords.MinPoolSize:
return "MINPOOLSIZE";
case Keywords.MaxPoolSize:
return "MAXPOOLSIZE";
case Keywords.SyncNotification:
return "SYNCNOTIFICATION";
case Keywords.CommandTimeout:
return "COMMANDTIMEOUT";
case Keywords.Enlist:
return "ENLIST";
case Keywords.PreloadReader:
return "PRELOADREADER";
case Keywords.UseExtendedTypes:
return "USEEXTENDEDTYPES";
case Keywords.IntegratedSecurity:
return "INTEGRATED SECURITY";
case Keywords.Compatible:
return "COMPATIBLE";
case Keywords.ApplicationName:
return "APPLICATIONNAME";
case Keywords.AlwaysPrepare:
return "ALWAYSPREPARE";
default:
return keyword.ToString().ToUpperInvariant();
}
}
internal static object GetDefaultValue(Keywords keyword)
{
return valueDescriptions[keyword].ExplicitDefault;
}
/// <summary>
/// Case insensative accessor for indivual connection string values.
/// </summary>
public override object this[string keyword]
{
get { return GetValue(GetKey(keyword)); }
set { this[GetKey(keyword)] = value; }
}
public object this[Keywords keyword]
{
get { return GetValue(keyword); }
set { SetValue(GetKeyName(keyword), keyword, value); }
}
public override bool Remove(string keyword)
{
Keywords key = GetKey(keyword);
SetValue(key, GetDefaultValue(key));
return base.Remove(keyword);
}
public bool ContainsKey(Keywords keyword)
{
return base.ContainsKey(GetKeyName(keyword));
}
/// <summary>
/// This function will set value for known key, both private member and base[key].
/// </summary>
/// <param name="keyword"></param>
/// <param name="key"></param>
/// <param name="value"></param>
/// <returns>value, coerced as needed to the stored type.</returns>
private object SetValue(string keyword, Keywords key, object value)
{
ValueDescription description;
description = valueDescriptions[key];
if (! description.StoreInBase)
{
return value;
}
if (value == null)
{
Remove(keyword);
return value;
}
value = SetValue(key, value);
// If the value matches both the parameter's default and the type's default, remove it from base,
// otherwise convert to string and set the base value.
if (
description.DefaultsDiffer ||
(description.ExplicitDefault.CompareTo((IComparable)value) != 0)
)
{
base[keyword] = description.ConvertNativeToString(value);
}
else
{
base.Remove(keyword);
}
return value;
}
/// <summary>
/// The function will modify private member only, not base[key].
/// </summary>
/// <param name="keyword"></param>
/// <param name="value"></param>
/// <returns>value, coerced as needed to the stored type.</returns>
private object SetValue(Keywords keyword, object value)
{
try
{
switch (keyword)
{
case Keywords.Host:
return this._host = Convert.ToString(value);
case Keywords.Port:
return this._port = Convert.ToInt32(value);
case Keywords.Protocol:
return this._protocol = ToProtocolVersion(value);
case Keywords.Database:
return this._database = Convert.ToString(value);
case Keywords.UserName:
return this._username = Convert.ToString(value);
case Keywords.Password:
this._password.Password = value as string;
return value as string;
case Keywords.SSL:
return this._ssl = ToBoolean(value);
case Keywords.SslMode:
return this._sslmode = ToSslMode(value);
#pragma warning disable 618
case Keywords.Encoding:
return Encoding;
#pragma warning restore 618
case Keywords.Timeout:
return this._timeout = ToInt32(value, 0, TIMEOUT_LIMIT, keyword);
case Keywords.SearchPath:
return this._searchpath = Convert.ToString(value);
case Keywords.Pooling:
return this._pooling = ToBoolean(value);
case Keywords.ConnectionLifeTime:
return this._connection_life_time = Convert.ToInt32(value);
case Keywords.MinPoolSize:
return this._min_pool_size = ToInt32(value, 0, POOL_SIZE_LIMIT, keyword);
case Keywords.MaxPoolSize:
return this._max_pool_size = ToInt32(value, 0, POOL_SIZE_LIMIT, keyword);
case Keywords.SyncNotification:
return this._sync_notification = ToBoolean(value);
case Keywords.CommandTimeout:
return this._command_timeout = Convert.ToInt32(value);
case Keywords.Enlist:
return this._enlist = ToBoolean(value);
case Keywords.PreloadReader:
return this._preloadReader = ToBoolean(value);
case Keywords.UseExtendedTypes:
return this._useExtendedTypes = ToBoolean(value);
case Keywords.IntegratedSecurity:
var v2 = ToIntegratedSecurity(value);
if (v2 == true)
CheckIntegratedSecuritySupport();
return this._integrated_security = ToIntegratedSecurity(v2);
case Keywords.Compatible:
Version ver = new Version(value.ToString());
if (ver > THIS_VERSION)
throw new ArgumentException("Attempt to set compatibility with version " + value +
" when using version " + THIS_VERSION);
return _compatible = ver;
case Keywords.ApplicationName:
return this._application_name = Convert.ToString(value);
case Keywords.AlwaysPrepare:
return this._always_prepare = Convert.ToBoolean(value);
}
}
catch (InvalidCastException exception)
{
string exception_template = string.Empty;
switch (keyword)
{
case Keywords.Port:
case Keywords.Timeout:
case Keywords.ConnectionLifeTime:
case Keywords.MinPoolSize:
case Keywords.MaxPoolSize:
case Keywords.CommandTimeout:
exception_template = resman.GetString("Exception_InvalidIntegerKeyVal");
break;
case Keywords.SSL:
case Keywords.Pooling:
case Keywords.SyncNotification:
exception_template = resman.GetString("Exception_InvalidBooleanKeyVal");
break;
case Keywords.Protocol:
exception_template = resman.GetString("Exception_InvalidProtocolVersionKeyVal");
break;
}
if (!string.IsNullOrEmpty(exception_template))
{
string key_name = GetKeyName(keyword);
throw new ArgumentException(string.Format(exception_template, key_name), key_name, exception);
}
throw;
}
return null;
}
/// <summary>
/// The function will access private member only, not base[key].
/// </summary>
/// <param name="keyword"></param>
/// <returns>value.</returns>
private object GetValue(Keywords keyword)
{
switch (keyword)
{
case Keywords.Host:
return this._host;
case Keywords.Port:
return this._port;
case Keywords.Protocol:
return this._protocol;
case Keywords.Database:
return this._database;
case Keywords.UserName:
return this._username;
case Keywords.Password:
return this._password.Password;
case Keywords.SSL:
return this._ssl;
case Keywords.SslMode:
return this._sslmode;
#pragma warning disable 618
case Keywords.Encoding:
return Encoding;
#pragma warning restore 618
case Keywords.Timeout:
return this._timeout;
case Keywords.SearchPath:
return this._searchpath;
case Keywords.Pooling:
return this._pooling;
case Keywords.ConnectionLifeTime:
return this._connection_life_time;
case Keywords.MinPoolSize:
return this._min_pool_size;
case Keywords.MaxPoolSize:
return this._max_pool_size;
case Keywords.SyncNotification:
return this._sync_notification;
case Keywords.CommandTimeout:
return this._command_timeout;
case Keywords.Enlist:
return this._enlist;
case Keywords.PreloadReader:
return this._preloadReader;