1 // created on 29/11/2007
4 // Npgsql.NpgsqlConnectionStringBuilder.cs
10 // Glen Parker (glenebob@nwlink.com)
12 // Ben Sagal (bensagal@gmail.com)
14 // Tao Wang (dancefire@gmail.com)
16 // Veerapuram Varadhan (vvaradhan@novell.com)
20 // Copyright (C) 2007 The Npgsql Development Team
22 // npgsql-general@gborg.postgresql.org
24 // http://gborg.postgresql.org/project/npgsql/projdisplay.php
28 // Copyright (C) 2009 Novell Inc
30 // Permission to use, copy, modify, and distribute this software and its
32 // documentation for any purpose, without fee, and without a written
34 // agreement is hereby granted, provided that the above copyright notice
36 // and this paragraph and the following two paragraphs appear in all copies.
40 // IN NO EVENT SHALL THE NPGSQL DEVELOPMENT TEAM BE LIABLE TO ANY PARTY
42 // FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES,
44 // INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
46 // DOCUMENTATION, EVEN IF THE NPGSQL DEVELOPMENT TEAM HAS BEEN ADVISED OF
48 // THE POSSIBILITY OF SUCH DAMAGE.
52 // THE NPGSQL DEVELOPMENT TEAM SPECIFICALLY DISCLAIMS ANY WARRANTIES,
54 // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
56 // AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
58 // ON AN "AS IS" BASIS, AND THE NPGSQL DEVELOPMENT TEAM HAS NO OBLIGATIONS
60 // TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
64 using System
.Collections
.Generic
;
65 using System
.Data
.Common
;
66 using System
.Resources
;
71 public sealed class NpgsqlConnectionStringBuilder
: DbConnectionStringBuilder
74 private static readonly ResourceManager resman
= new ResourceManager(typeof (NpgsqlConnectionStringBuilder
));
76 private static readonly Dictionary
<Keywords
, object> defaults
= new Dictionary
<Keywords
, object>();
79 private string originalConnectionString
;
82 private const int POOL_SIZE_LIMIT
= 1024;
84 private const int TIMEOUT_LIMIT
= 1024;
87 static NpgsqlConnectionStringBuilder()
90 defaults
.Add(Keywords
.Host
, string.Empty
);
92 defaults
.Add(Keywords
.Port
, 5432);
94 defaults
.Add(Keywords
.Protocol
, ProtocolVersion
.Unknown
);
96 defaults
.Add(Keywords
.Database
, string.Empty
);
98 defaults
.Add(Keywords
.UserName
, string.Empty
);
100 defaults
.Add(Keywords
.Password
, string.Empty
);
102 defaults
.Add(Keywords
.SSL
, false);
104 defaults
.Add(Keywords
.SslMode
, SslMode
.Disable
);
106 defaults
.Add(Keywords
.Timeout
, 15);
108 defaults
.Add(Keywords
.SearchPath
, string.Empty
);
110 defaults
.Add(Keywords
.Pooling
, true);
112 defaults
.Add(Keywords
.ConnectionLifeTime
, 15);
114 defaults
.Add(Keywords
.MinPoolSize
, 1);
116 defaults
.Add(Keywords
.MaxPoolSize
, 20);
118 defaults
.Add(Keywords
.SyncNotification
, false);
120 defaults
.Add(Keywords
.CommandTimeout
, 20);
122 defaults
.Add(Keywords
.Enlist
, false);
124 defaults
.Add(Keywords
.PreloadReader
, false);
126 defaults
.Add(Keywords
.UseExtendedTypes
, false);
127 defaults
.Add(Keywords
.IntegratedSecurity
, false);
131 public NpgsqlConnectionStringBuilder()
137 public NpgsqlConnectionStringBuilder(string connectionString
)
142 this.originalConnectionString
= connectionString
;
144 base.ConnectionString
= connectionString
;
149 /// Return an exact copy of this NpgsqlConnectionString.
151 public NpgsqlConnectionStringBuilder
Clone()
154 NpgsqlConnectionStringBuilder builder
= new NpgsqlConnectionStringBuilder();
156 foreach (string key
in this.Keys
)
159 builder
[key
] = this[key
];
165 #region Parsing Functions
167 private static SslMode
ToSslMode(object value)
170 if (value is SslMode
)
173 return (SslMode
) value;
179 return (SslMode
) Enum
.Parse(typeof (SslMode
), value.ToString(), true);
184 private static ProtocolVersion
ToProtocolVersion(object value)
187 if (value is ProtocolVersion
)
190 return (ProtocolVersion
) value;
196 int ver
= Convert
.ToInt32(value);
204 return ProtocolVersion
.Version2
;
208 return ProtocolVersion
.Version3
;
212 throw new InvalidCastException(value.ToString());
218 private static string ToString(ProtocolVersion protocolVersion
)
221 switch (protocolVersion
)
224 case ProtocolVersion
.Version2
:
228 case ProtocolVersion
.Version3
:
239 private static int ToInt32(object value, int min
, int max
, string key
)
242 int v
= Convert
.ToInt32(value);
247 throw new ArgumentOutOfRangeException(String
.Format(resman
.GetString("Exception_IntegerKeyValMin"), key
, min
), key
);
253 throw new ArgumentOutOfRangeException(String
.Format(resman
.GetString("Exception_IntegerKeyValMax"), key
, max
), key
);
260 private static Boolean
ToBoolean(object value)
263 string text
= value as string;
268 switch (text
.ToLowerInvariant())
295 throw new InvalidCastException(value.ToString());
302 return Convert
.ToBoolean(value);
306 private Boolean
ToIntegratedSecurity(object value)
308 string text
= value as string;
311 switch (text
.ToLowerInvariant())
327 throw new InvalidCastException(value.ToString());
332 return Convert
.ToBoolean(value);
340 private string _host
;
346 get { return _host; }
348 set { SetValue(GetKeyName(Keywords.Host), value); }
358 get { return _port; }
360 set { SetValue(GetKeyName(Keywords.Port), value); }
364 private ProtocolVersion _protocol
;
367 public ProtocolVersion Protocol
370 get { return _protocol; }
372 set { SetValue(GetKeyName(Keywords.Protocol), value); }
375 private string _database
;
378 public string Database
381 get { return _database; }
383 set { SetValue(GetKeyName(Keywords.Database), value); }
386 private string _username
;
389 public string UserName
394 if (_integrated_security
)
396 System
.Security
.Principal
.WindowsIdentity identity
= System
.Security
.Principal
.WindowsIdentity
.GetCurrent();
397 _username
= identity
.Name
.Split('\\')[1];
402 set { SetValue(GetKeyName(Keywords.UserName), value); }
405 private string _password
;
408 public string Password
411 get { return _password; }
413 set { SetValue(GetKeyName(Keywords.Password), value); }
424 set { SetValue(GetKeyName(Keywords.SSL), value); }
428 private SslMode _sslmode
;
431 public SslMode SslMode
434 get { return _sslmode; }
436 set { SetValue(GetKeyName(Keywords.SslMode), value); }
440 [Obsolete("UTF-8 is always used regardless of this setting.")]
441 public string Encoding
443 get { return "UNICODE"; }
449 private int _timeout
;
455 get { return _timeout; }
457 set { SetValue(GetKeyName(Keywords.Timeout), value); }
460 private string _searchpath
;
463 public string SearchPath
466 get { return _searchpath; }
468 set { SetValue(GetKeyName(Keywords.SearchPath), value); }
471 private bool _pooling
;
477 get { return _pooling; }
479 set { SetValue(GetKeyName(Keywords.Pooling), value); }
482 private int _connection_life_time
;
485 public int ConnectionLifeTime
488 get { return _connection_life_time; }
490 set { SetValue(GetKeyName(Keywords.ConnectionLifeTime), value); }
493 private int _min_pool_size
;
496 public int MinPoolSize
499 get { return _min_pool_size; }
501 set { SetValue(GetKeyName(Keywords.MinPoolSize), value); }
504 private int _max_pool_size
;
507 public int MaxPoolSize
510 get { return _max_pool_size; }
512 set { SetValue(GetKeyName(Keywords.MaxPoolSize), value); }
515 private bool _sync_notification
;
518 public bool SyncNotification
521 get { return _sync_notification; }
523 set { SetValue(GetKeyName(Keywords.SyncNotification), value); }
526 private int _command_timeout
;
529 public int CommandTimeout
532 get { return _command_timeout; }
534 set { SetValue(GetKeyName(Keywords.CommandTimeout), value); }
537 private bool _enlist
;
543 get { return _enlist; }
545 set { SetValue(GetKeyName(Keywords.Enlist), value); }
548 private bool _preloadReader
;
550 public bool PreloadReader
552 get { return _preloadReader; }
553 set { SetValue(GetKeyName(Keywords.PreloadReader), value); }
556 private bool _useExtendedTypes
;
558 public bool UseExtendedTypes
560 get { return _useExtendedTypes; }
561 set { SetValue(GetKeyName(Keywords.UseExtendedTypes), value); }
564 private bool _integrated_security
;
566 public bool IntegratedSecurity
568 get { return _integrated_security; }
569 set { SetValue(GetKeyName(Keywords.IntegratedSecurity), value); }
574 private static Keywords
GetKey(string key
)
577 switch (key
.ToUpperInvariant())
584 return Keywords
.Host
;
588 return Keywords
.Port
;
592 return Keywords
.Protocol
;
598 return Keywords
.Database
;
612 return Keywords
.UserName
;
620 return Keywords
.Password
;
628 return Keywords
.SslMode
;
631 #pragma warning disable 618
632 return Keywords
.Encoding
;
633 #pragma warning restore 618
636 return Keywords
.Timeout
;
640 return Keywords
.SearchPath
;
644 return Keywords
.Pooling
;
646 case "CONNECTIONLIFETIME":
648 return Keywords
.ConnectionLifeTime
;
652 return Keywords
.MinPoolSize
;
656 return Keywords
.MaxPoolSize
;
658 case "SYNCNOTIFICATION":
660 return Keywords
.SyncNotification
;
662 case "COMMANDTIMEOUT":
664 return Keywords
.CommandTimeout
;
668 return Keywords
.Enlist
;
670 case "PRELOADREADER":
671 case "PRELOAD READER":
672 return Keywords
.PreloadReader
;
674 case "USEEXTENDEDTYPES":
675 case "USE EXTENDED TYPES":
676 return Keywords
.UseExtendedTypes
;
677 case "INTEGRATED SECURITY":
678 return Keywords
.IntegratedSecurity
;
682 throw new ArgumentException(resman
.GetString("Exception_WrongKeyVal"), key
);
687 internal static string GetKeyName(Keywords keyword
)
690 if (keyword
== Keywords
.UserName
)
695 else if (keyword
== Keywords
.IntegratedSecurity
)
697 return "INTEGRATED SECURITY";
703 return keyword
.ToString().ToUpperInvariant();
708 internal static object GetDefaultValue(Keywords keyword
)
711 return defaults
[keyword
];
716 /// Case insensative accessor for indivual connection string values.
718 public override object this[string keyword
]
721 get { return this[GetKey(keyword)]; }
723 set { this[GetKey(keyword)] = value; }
726 public object this[Keywords keyword
]
729 get { return base[GetKeyName(keyword)]; }
731 set { SetValue(GetKeyName(keyword), value); }
735 public override bool Remove(string keyword
)
738 Keywords key
= GetKey(keyword
);
740 SetValue(key
, defaults
[key
]);
742 return base.Remove(keyword
);
746 public bool ContainsKey(Keywords keyword
)
749 return base.ContainsKey(GetKeyName(keyword
));
754 /// This function will set value for known key, both private member and base[key].
756 /// <param name="keyword"></param>
757 /// <param name="value"></param>
758 private void SetValue(string keyword
, object value)
770 Keywords key
= GetKey(keyword
);
772 SetValue(key
, value);
774 if (key
== Keywords
.Protocol
)
777 base[GetKeyName(key
)] = ToString(this.Protocol
);
783 base[GetKeyName(key
)] = value;
790 /// The function will modify private member only, not base[key].
792 /// <param name="keyword"></param>
793 /// <param name="value"></param>
794 private void SetValue(Keywords keyword
, object value)
797 string key_name
= GetKeyName(keyword
);
807 this._host
= Convert
.ToString(value);
813 this._port
= Convert
.ToInt32(value);
817 case Keywords
.Protocol
:
819 this._protocol
= ToProtocolVersion(value);
823 case Keywords
.Database
:
825 this._database
= Convert
.ToString(value);
829 case Keywords
.UserName
:
831 this._username
= Convert
.ToString(value);
835 case Keywords
.Password
:
837 this._password
= Convert
.ToString(value);
843 this._ssl
= ToBoolean(value);
847 case Keywords
.SslMode
:
849 this._sslmode
= ToSslMode(value);
853 #pragma warning disable 618
854 case Keywords
.Encoding
:
856 #pragma warning restore 618
858 case Keywords
.Timeout
:
860 this._timeout
= ToInt32(value, 0, TIMEOUT_LIMIT
, key_name
);
864 case Keywords
.SearchPath
:
866 this._searchpath
= Convert
.ToString(value);
870 case Keywords
.Pooling
:
872 this._pooling
= ToBoolean(value);
876 case Keywords
.ConnectionLifeTime
:
878 this._connection_life_time
= Convert
.ToInt32(value);
882 case Keywords
.MinPoolSize
:
884 this._min_pool_size
= (MaxPoolSize
> 0) ? ToInt32(value, 0, MaxPoolSize
, key_name
) : Convert
.ToInt32(value);
888 case Keywords
.MaxPoolSize
:
890 this._max_pool_size
= ToInt32(value, 0, POOL_SIZE_LIMIT
, key_name
);
894 case Keywords
.SyncNotification
:
896 this._sync_notification
= ToBoolean(value);
900 case Keywords
.CommandTimeout
:
902 this._command_timeout
= Convert
.ToInt32(value);
906 case Keywords
.Enlist
:
908 this._enlist
= ToBoolean(value);
912 case Keywords
.PreloadReader
:
914 this._preloadReader
= ToBoolean(value);
918 case Keywords
.UseExtendedTypes
:
920 this._useExtendedTypes
= ToBoolean(value);
923 case Keywords
.IntegratedSecurity
:
924 this._integrated_security
= ToIntegratedSecurity(value);
929 catch (InvalidCastException exception
)
932 string exception_template
= string.Empty
;
939 case Keywords
.Timeout
:
941 case Keywords
.ConnectionLifeTime
:
943 case Keywords
.MinPoolSize
:
945 case Keywords
.MaxPoolSize
:
947 case Keywords
.CommandTimeout
:
949 exception_template
= resman
.GetString("Exception_InvalidIntegerKeyVal");
955 case Keywords
.Pooling
:
957 case Keywords
.SyncNotification
:
959 exception_template
= resman
.GetString("Exception_InvalidBooleanKeyVal");
963 case Keywords
.Protocol
:
965 exception_template
= resman
.GetString("Exception_InvalidProtocolVersionKeyVal");
970 if (!string.IsNullOrEmpty(exception_template
))
973 throw new ArgumentException(string.Format(exception_template
, key_name
), key_name
, exception
);
981 /// Clear the member and assign them to the default value.
983 public override void Clear()
988 foreach (Keywords keyword
in defaults
.Keys
)
991 SetValue(keyword
, defaults
[keyword
]);
1016 [Obsolete("UTF-8 is always used regardless of this setting.")] Encoding
,
1022 // These are for the connection pool
1034 // These are for the command
1038 // These are for the resource manager