disable broken tests on net_4_0
[mcs.git] / class / Npgsql / Npgsql / NpgsqlConnectionStringBuilder.cs
blobce51262c78160505d5a4723752ac47694f669d1a
1 // created on 29/11/2007
4 // Npgsql.NpgsqlConnectionStringBuilder.cs
6 //
8 // Author:
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.
38 //
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.
50 //
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.
62 #if NET_2_0
63 using System;
64 using System.Collections.Generic;
65 using System.Data.Common;
66 using System.Resources;
68 namespace Npgsql
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()
134 this.Clear();
137 public NpgsqlConnectionStringBuilder(string connectionString)
140 this.Clear();
142 this.originalConnectionString = connectionString;
144 base.ConnectionString = connectionString;
148 /// <summary>
149 /// Return an exact copy of this NpgsqlConnectionString.
150 /// </summary>
151 public NpgsqlConnectionStringBuilder Clone()
154 NpgsqlConnectionStringBuilder builder = new NpgsqlConnectionStringBuilder();
156 foreach (string key in this.Keys)
159 builder[key] = this[key];
162 return builder;
165 #region Parsing Functions
167 private static SslMode ToSslMode(object value)
170 if (value is SslMode)
173 return (SslMode) value;
176 else
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;
193 else
196 int ver = Convert.ToInt32(value);
199 switch (ver)
202 case 2:
204 return ProtocolVersion.Version2;
206 case 3:
208 return ProtocolVersion.Version3;
210 default:
212 throw new InvalidCastException(value.ToString());
218 private static string ToString(ProtocolVersion protocolVersion)
221 switch (protocolVersion)
224 case ProtocolVersion.Version2:
226 return "2";
228 case ProtocolVersion.Version3:
230 return "3";
232 default:
234 return string.Empty;
239 private static int ToInt32(object value, int min, int max, string key)
242 int v = Convert.ToInt32(value);
244 if (v < min)
247 throw new ArgumentOutOfRangeException(String.Format(resman.GetString("Exception_IntegerKeyValMin"), key, min), key);
250 else if (v > max)
253 throw new ArgumentOutOfRangeException(String.Format(resman.GetString("Exception_IntegerKeyValMax"), key, max), key);
256 return v;
260 private static Boolean ToBoolean(object value)
263 string text = value as string;
265 if (text != null)
268 switch (text.ToLowerInvariant())
271 case "t":
273 case "true":
275 case "y":
277 case "yes":
279 return true;
282 case "f":
284 case "false":
286 case "n":
288 case "no":
290 return false;
293 default:
295 throw new InvalidCastException(value.ToString());
299 else
302 return Convert.ToBoolean(value);
306 private Boolean ToIntegratedSecurity(object value)
308 string text = value as string;
309 if (text != null)
311 switch (text.ToLowerInvariant())
313 case "t":
314 case "true":
315 case "y":
316 case "yes":
317 case "sspi":
318 return true;
320 case "f":
321 case "false":
322 case "n":
323 case "no":
324 return false;
326 default:
327 throw new InvalidCastException(value.ToString());
330 else
332 return Convert.ToBoolean(value);
336 #endregion
338 #region Properties
340 private string _host;
343 public string Host
346 get { return _host; }
348 set { SetValue(GetKeyName(Keywords.Host), value); }
352 private int _port;
355 public int Port
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];
399 return _username;
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); }
416 private bool _ssl;
419 public bool SSL
422 get { return _ssl; }
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"; }
445 //set { }
449 private int _timeout;
452 public 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;
474 public 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;
540 public 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); }
572 #endregion
574 private static Keywords GetKey(string key)
577 switch (key.ToUpperInvariant())
580 case "HOST":
582 case "SERVER":
584 return Keywords.Host;
586 case "PORT":
588 return Keywords.Port;
590 case "PROTOCOL":
592 return Keywords.Protocol;
594 case "DATABASE":
596 case "DB":
598 return Keywords.Database;
600 case "USERNAME":
602 case "USER NAME":
604 case "USER":
606 case "USERID":
608 case "USER ID":
610 case "UID":
612 return Keywords.UserName;
614 case "PASSWORD":
616 case "PSW":
618 case "PWD":
620 return Keywords.Password;
622 case "SSL":
624 return Keywords.SSL;
626 case "SSLMODE":
628 return Keywords.SslMode;
630 case "ENCODING":
631 #pragma warning disable 618
632 return Keywords.Encoding;
633 #pragma warning restore 618
634 case "TIMEOUT":
636 return Keywords.Timeout;
638 case "SEARCHPATH":
640 return Keywords.SearchPath;
642 case "POOLING":
644 return Keywords.Pooling;
646 case "CONNECTIONLIFETIME":
648 return Keywords.ConnectionLifeTime;
650 case "MINPOOLSIZE":
652 return Keywords.MinPoolSize;
654 case "MAXPOOLSIZE":
656 return Keywords.MaxPoolSize;
658 case "SYNCNOTIFICATION":
660 return Keywords.SyncNotification;
662 case "COMMANDTIMEOUT":
664 return Keywords.CommandTimeout;
666 case "ENLIST":
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;
680 default:
682 throw new ArgumentException(resman.GetString("Exception_WrongKeyVal"), key);
687 internal static string GetKeyName(Keywords keyword)
690 if (keyword == Keywords.UserName)
693 return "USER ID";
695 else if (keyword == Keywords.IntegratedSecurity)
697 return "INTEGRATED SECURITY";
700 else
703 return keyword.ToString().ToUpperInvariant();
708 internal static object GetDefaultValue(Keywords keyword)
711 return defaults[keyword];
715 /// <summary>
716 /// Case insensative accessor for indivual connection string values.
717 /// </summary>
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));
753 /// <summary>
754 /// This function will set value for known key, both private member and base[key].
755 /// </summary>
756 /// <param name="keyword"></param>
757 /// <param name="value"></param>
758 private void SetValue(string keyword, object value)
761 if (value == null)
764 Remove(keyword);
767 else
770 Keywords key = GetKey(keyword);
772 SetValue(key, value);
774 if (key == Keywords.Protocol)
777 base[GetKeyName(key)] = ToString(this.Protocol);
780 else
783 base[GetKeyName(key)] = value;
789 /// <summary>
790 /// The function will modify private member only, not base[key].
791 /// </summary>
792 /// <param name="keyword"></param>
793 /// <param name="value"></param>
794 private void SetValue(Keywords keyword, object value)
797 string key_name = GetKeyName(keyword);
802 switch (keyword)
805 case Keywords.Host:
807 this._host = Convert.ToString(value);
809 break;
811 case Keywords.Port:
813 this._port = Convert.ToInt32(value);
815 break;
817 case Keywords.Protocol:
819 this._protocol = ToProtocolVersion(value);
821 break;
823 case Keywords.Database:
825 this._database = Convert.ToString(value);
827 break;
829 case Keywords.UserName:
831 this._username = Convert.ToString(value);
833 break;
835 case Keywords.Password:
837 this._password = Convert.ToString(value);
839 break;
841 case Keywords.SSL:
843 this._ssl = ToBoolean(value);
845 break;
847 case Keywords.SslMode:
849 this._sslmode = ToSslMode(value);
851 break;
853 #pragma warning disable 618
854 case Keywords.Encoding:
855 break;
856 #pragma warning restore 618
858 case Keywords.Timeout:
860 this._timeout = ToInt32(value, 0, TIMEOUT_LIMIT, key_name);
862 break;
864 case Keywords.SearchPath:
866 this._searchpath = Convert.ToString(value);
868 break;
870 case Keywords.Pooling:
872 this._pooling = ToBoolean(value);
874 break;
876 case Keywords.ConnectionLifeTime:
878 this._connection_life_time = Convert.ToInt32(value);
880 break;
882 case Keywords.MinPoolSize:
884 this._min_pool_size = (MaxPoolSize > 0) ? ToInt32(value, 0, MaxPoolSize, key_name) : Convert.ToInt32(value);
886 break;
888 case Keywords.MaxPoolSize:
890 this._max_pool_size = ToInt32(value, 0, POOL_SIZE_LIMIT, key_name);
892 break;
894 case Keywords.SyncNotification:
896 this._sync_notification = ToBoolean(value);
898 break;
900 case Keywords.CommandTimeout:
902 this._command_timeout = Convert.ToInt32(value);
904 break;
906 case Keywords.Enlist:
908 this._enlist = ToBoolean(value);
910 break;
912 case Keywords.PreloadReader:
914 this._preloadReader = ToBoolean(value);
916 break;
918 case Keywords.UseExtendedTypes:
920 this._useExtendedTypes = ToBoolean(value);
922 break;
923 case Keywords.IntegratedSecurity:
924 this._integrated_security = ToIntegratedSecurity(value);
925 break;
929 catch (InvalidCastException exception)
932 string exception_template = string.Empty;
934 switch (keyword)
937 case Keywords.Port:
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");
951 break;
953 case Keywords.SSL:
955 case Keywords.Pooling:
957 case Keywords.SyncNotification:
959 exception_template = resman.GetString("Exception_InvalidBooleanKeyVal");
961 break;
963 case Keywords.Protocol:
965 exception_template = resman.GetString("Exception_InvalidProtocolVersionKeyVal");
967 break;
970 if (!string.IsNullOrEmpty(exception_template))
973 throw new ArgumentException(string.Format(exception_template, key_name), key_name, exception);
976 throw;
980 /// <summary>
981 /// Clear the member and assign them to the default value.
982 /// </summary>
983 public override void Clear()
986 base.Clear();
988 foreach (Keywords keyword in defaults.Keys)
991 SetValue(keyword, defaults[keyword]);
997 public enum Keywords
1000 Host,
1002 Port,
1004 Protocol,
1006 Database,
1008 UserName,
1010 Password,
1012 SSL,
1014 SslMode,
1016 [Obsolete("UTF-8 is always used regardless of this setting.")] Encoding,
1018 Timeout,
1020 SearchPath,
1022 // These are for the connection pool
1024 Pooling,
1026 ConnectionLifeTime,
1028 MinPoolSize,
1030 MaxPoolSize,
1032 SyncNotification,
1034 // These are for the command
1036 CommandTimeout,
1038 // These are for the resource manager
1040 Enlist,
1042 PreloadReader,
1044 UseExtendedTypes,
1045 IntegratedSecurity
1049 public enum SslMode
1052 Disable = 1 << 0,
1054 Allow = 1 << 1,
1056 Prefer = 1 << 2,
1058 Require = 1 << 3
1061 #endif