Merge pull request #1624 from esdrubal/getprocesstimes
[mono.git] / mcs / class / System.Data / System.Data.SqlClient / SqlConnection.cs
index 7e2cfde31b259364139503c0115196a56abd4285..38f7ec7bb6e0d98f2dba2283d9519727e02a4524 100644 (file)
@@ -53,18 +53,13 @@ using System.Net;
 using System.Net.Sockets;
 using System.Text;
 using System.Xml;
-#if NET_2_0
 using System.Collections.Generic;
-#endif
+using System.Security;
 
 namespace System.Data.SqlClient
 {
        [DefaultEvent ("InfoMessage")]
-#if NET_2_0
        public sealed class SqlConnection : DbConnection, IDbConnection, ICloneable
-#else
-       public sealed class SqlConnection : Component, IDbConnection, ICloneable
-#endif // NET_2_0
        {
                #region Fields
 
@@ -72,15 +67,11 @@ namespace System.Data.SqlClient
 
                // The set of SQL connection pools
                static TdsConnectionPoolManager sqlConnectionPools = new TdsConnectionPoolManager (TdsVersion.tds80);
-#if NET_2_0
-               const int DEFAULT_PACKETSIZE = 32768;
+               const int DEFAULT_PACKETSIZE = 8000;
                const int MAX_PACKETSIZE = 32768;
-#else
-               const int DEFAULT_PACKETSIZE = 32767;
-               const int MAX_PACKETSIZE = 32767;
-#endif
                const int MIN_PACKETSIZE = 512;
                const int DEFAULT_CONNECTIONTIMEOUT = 15;
+               const int DEFAULT_CONNECTIONLIFETIME = 0;
                const int DEFAULT_MAXPOOLSIZE = 100;
                const int MIN_MAXPOOLSIZE = 1;
                const int DEFAULT_MINPOOLSIZE = 0;
@@ -92,6 +83,9 @@ namespace System.Data.SqlClient
                // The connection string that identifies this connection
                string connectionString;
 
+               // The connection credentials
+               SqlCredential credentials;
+
                // The transaction object for the current transaction
                SqlTransaction transaction;
 
@@ -99,6 +93,7 @@ namespace System.Data.SqlClient
                
                TdsConnectionParameters parms;
                bool connectionReset;
+               int connectionLifeTime;
                bool pooling;
                string dataSource;
                int connectionTimeout;
@@ -131,6 +126,12 @@ namespace System.Data.SqlClient
                        ConnectionString = connectionString;
                }
 
+               public SqlConnection (string connectionString, SqlCredential cred)
+               {
+                       ConnectionString = connectionString;
+                       Credentials = cred;
+               }
+
                #endregion // Constructors
 
                #region Properties
@@ -153,26 +154,25 @@ namespace System.Data.SqlClient
                        }
                }
        
-#if !NET_2_0
-               [DataSysDescription ("Current connection timeout value, 'Connect Timeout=X' in the ConnectionString.")] 
-#endif
+               public SqlCredential Credentials {
+                       get {
+                               return credentials;
+                       }
+                       set {
+                               credentials = value;
+                       }
+               }
+       
                [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
                public
-#if NET_2_0
                override
-#endif // NET_2_0
                int ConnectionTimeout {
                        get { return connectionTimeout; }
                }
 
-#if !NET_2_0
-               [DataSysDescription ("Current SQL Server database, 'Initial Catalog=X' in the connection string.")]
-#endif
                [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
                public
-#if NET_2_0
                override
-#endif // NET_2_0
                string Database {
                        get {
                                if (State == ConnectionState.Open)
@@ -186,23 +186,14 @@ namespace System.Data.SqlClient
                        set { dataReader = value; }
                }
 
-#if !NET_2_0
-               [DataSysDescription ("Current SqlServer that the connection is opened to, 'Data Source=X' in the connection string. ")]
-#else
                [Browsable(true)]
-#endif
                [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
                public
-#if NET_2_0
                override
-#endif // NET_2_0
                string DataSource {
                        get { return dataSource; }
                }
 
-#if !NET_2_0
-               [DataSysDescription ("Network packet size, 'Packet Size=x' in the connection string.")]
-#endif
                [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
                public int PacketSize {
                        get {
@@ -213,14 +204,9 @@ namespace System.Data.SqlClient
                }
 
                [Browsable (false)]
-#if !NET_2_0
-               [DataSysDescription ("Version of the SQL Server accessed by the SqlConnection.")]
-#endif
                [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
                public
-#if NET_2_0
                override
-#endif // NET_2_0
                string ServerVersion {
                        get {
                                if (state == ConnectionState.Closed)
@@ -231,14 +217,9 @@ namespace System.Data.SqlClient
                }
 
                [Browsable (false)]
-#if !NET_2_0
-               [DataSysDescription ("The ConnectionState indicating whether the connection is open or closed.")]
-#endif
                [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
                public
-#if NET_2_0
                override
-#endif // NET_2_0
                ConnectionState State {
                        get { return state; }
                }
@@ -252,9 +233,6 @@ namespace System.Data.SqlClient
                        set { transaction = value; }
                }
 
-#if !NET_2_0
-               [DataSysDescription ("Workstation Id, 'Workstation ID=x' in the connection string.")]
-#endif
                [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
                public string WorkstationId {
                        get { return parms.Hostname; }
@@ -265,7 +243,6 @@ namespace System.Data.SqlClient
                        set { xmlReader = value; }
                }
 
-#if NET_2_0
                public bool FireInfoMessageEventOnUserErrors { 
                        get { return fireInfoMessageEventOnUserErrors; } 
                        set { fireInfoMessageEventOnUserErrors = value; }
@@ -276,20 +253,19 @@ namespace System.Data.SqlClient
                        get { return statisticsEnabled; } 
                        set { statisticsEnabled = value; }
                }
-#endif
+
+               protected override DbProviderFactory DbProviderFactory {
+                       get {
+                               return SqlClientFactory.Instance;
+                       }
+               }
+
                #endregion // Properties
 
                #region Events
 
-#if !NET_2_0
-               [DataSysDescription ("Event triggered when messages arrive from the DataSource.")]
-#endif
                public event SqlInfoMessageEventHandler InfoMessage;
 
-#if !NET_2_0
-               [DataSysDescription ("Event triggered when the connection changes state.")]
-               public new event StateChangeEventHandler StateChange;
-#endif
 
                #endregion // Events
 
@@ -354,7 +330,6 @@ namespace System.Data.SqlClient
                        case IsolationLevel.ReadCommitted:
                                isolevel = "READ COMMITTED";
                                break;
-#if NET_2_0
                        case IsolationLevel.Snapshot:
                                isolevel = "SNAPSHOT";
                                break;
@@ -369,16 +344,11 @@ namespace System.Data.SqlClient
                                                "value, {0}, is not supported by " +
                                                "the .Net Framework SqlClient " +
                                                "Data Provider.", (int) iso));
-#endif
                        default:
-#if NET_2_0
                                throw new ArgumentOutOfRangeException ("IsolationLevel",
                                        string.Format (CultureInfo.CurrentCulture,
                                                "The IsolationLevel enumeration value, {0}, is invalid.",
                                                (int) iso));
-#else
-                               throw new ArgumentException ("Invalid IsolationLevel parameter: must be ReadCommitted, ReadUncommitted, RepeatableRead, or Serializable.");
-#endif
                        }
 
                        tds.Execute (String.Format ("SET TRANSACTION ISOLATION LEVEL {0};BEGIN TRANSACTION {1}", isolevel, transactionName));
@@ -388,9 +358,7 @@ namespace System.Data.SqlClient
                }
 
                public
-#if NET_2_0
                override
-#endif // NET_2_0
                void ChangeDatabase (string database)
                {
                        if (!IsValidDatabaseName (database))
@@ -411,9 +379,7 @@ namespace System.Data.SqlClient
                }
 
                public
-#if NET_2_0
                override
-#endif // NET_2_0
                void Close ()
                {
                        if (transaction != null && transaction.IsOpen)
@@ -488,7 +454,6 @@ namespace System.Data.SqlClient
                        return new SqlConnection (ConnectionString);
                }
 
-#if NET_2_0
                protected override DbTransaction BeginDbTransaction (IsolationLevel isolationLevel)
                {
                        return BeginTransaction (isolationLevel);
@@ -498,27 +463,9 @@ namespace System.Data.SqlClient
                {
                        return CreateCommand ();
                }
-#else
-               IDbTransaction IDbConnection.BeginTransaction ()
-               {
-                       return BeginTransaction ();
-               }
-
-               IDbTransaction IDbConnection.BeginTransaction (IsolationLevel iso)
-               {
-                       return BeginTransaction (iso);
-               }
-
-               IDbCommand IDbConnection.CreateCommand ()
-               {
-                       return CreateCommand ();
-               }
-#endif
 
                public
-#if NET_2_0
                override
-#endif // NET_2_0
                void Open ()
                {
                        string serverName = string.Empty;
@@ -532,14 +479,14 @@ namespace System.Data.SqlClient
                                if (!pooling) {
                                        if(!ParseDataSource (dataSource, out port, out serverName))
                                                throw new SqlException(20, 0, "SQL Server does not exist or access denied.",  17, "ConnectionOpen (Connect()).", dataSource, parms.ApplicationName, 0);
-                                       tds = new Tds80 (serverName, port, PacketSize, ConnectionTimeout);
+                                       tds = new Tds80 (serverName, port, PacketSize, ConnectionTimeout, 0);
                                        tds.Pooling = false;
                                }
                                else {
                                        if(!ParseDataSource (dataSource, out port, out serverName))
                                                throw new SqlException(20, 0, "SQL Server does not exist or access denied.",  17, "ConnectionOpen (Connect()).", dataSource, parms.ApplicationName, 0);
                                        
-                                       TdsConnectionInfo info = new TdsConnectionInfo (serverName, port, packetSize, ConnectionTimeout, minPoolSize, maxPoolSize);
+                                       TdsConnectionInfo info = new TdsConnectionInfo (serverName, port, packetSize, ConnectionTimeout, minPoolSize, maxPoolSize, connectionLifeTime);
                                        pool = sqlConnectionPools.GetConnectionPool (connectionString, info);
                                        tds = pool.GetConnection ();
                                }
@@ -554,6 +501,16 @@ namespace System.Data.SqlClient
 
                        if (!tds.IsConnected) {
                                try {
+                                       if (Credentials != null) {
+                                               if (parms.User != String.Empty)
+                                                       throw new ArgumentException("UserID already specified");
+                                               if (parms.PasswordSet)
+                                                       throw new ArgumentException("Password already specified");
+                                               if (parms.DomainLogin != false)
+                                                       throw new ArgumentException("Cannot use credentials with DomainLogin");
+                                               parms.User = Credentials.UserId;
+                                               parms.Password = Credentials.Password;
+                                       }
                                        tds.Connect (parms);
                                } catch {
                                        if (pooling)
@@ -756,15 +713,14 @@ namespace System.Data.SqlClient
                                parms.Reset ();
                        dataSource = string.Empty;
                        connectionTimeout = DEFAULT_CONNECTIONTIMEOUT;
+                       connectionLifeTime = DEFAULT_CONNECTIONLIFETIME;
                        connectionReset = true;
                        pooling = true;
                        maxPoolSize = DEFAULT_MAXPOOLSIZE;
                        minPoolSize = DEFAULT_MINPOOLSIZE;
                        packetSize = DEFAULT_PACKETSIZE;
                        port = DEFAULT_PORT;
- #if NET_2_0
                        async = false;
- #endif
                }
 
                private void SetProperties (string name , string value)
@@ -790,6 +746,7 @@ namespace System.Data.SqlClient
                                        connectionTimeout = tmpTimeout;
                                break;
                        case "connection lifetime" :
+                               connectionLifeTime = ConvertToInt32 ("connection lifetime", value, DEFAULT_CONNECTIONLIFETIME);
                                break;
                        case "connection reset" :
                                connectionReset = ConvertToBoolean ("connection reset", value, true);
@@ -841,7 +798,6 @@ namespace System.Data.SqlClient
                                else
                                        minPoolSize = tmpMinPoolSize;
                                break;
-#if NET_2_0
                        case "multipleactiveresultsets":
                                // FIXME: not implemented
                                ConvertToBoolean (name, value, false);
@@ -850,7 +806,6 @@ namespace System.Data.SqlClient
                        case "async" :
                                async = ConvertToBoolean (name, value, false);
                                break;
-#endif
                        case "net" :
                        case "network" :
                        case "network library" :
@@ -868,7 +823,10 @@ namespace System.Data.SqlClient
                                break;
                        case "password" :
                        case "pwd" :
-                               parms.Password = value;
+                               parms.Password = new SecureString();
+                               foreach (char c in value)
+                                       parms.Password.AppendChar(c);
+                               parms.PasswordSet = true;
                                break;
                        case "persistsecurityinfo" :
                        case "persist security info" :
@@ -887,11 +845,9 @@ namespace System.Data.SqlClient
                        case "workstation id" :
                                parms.Hostname = value;
                                break;
-#if NET_2_0
                        case "user instance":
                                userInstance = ConvertToBoolean (name, value, false);
                                break;
-#endif
                        default :
                                throw new ArgumentException("Keyword not supported : '" + name + "'.");
                        }
@@ -922,13 +878,6 @@ namespace System.Data.SqlClient
                                InfoMessage (this, value);
                }
 
-#if !NET_2_0
-               private new void OnStateChange (StateChangeEventArgs value)
-               {
-                       if (StateChange != null)
-                               StateChange (this, value);
-               }
-#endif
 
                private sealed class SqlMonitorSocket : UdpClient
                {
@@ -969,11 +918,7 @@ namespace System.Data.SqlClient
 
                                if (Client.Available <= 0)
                                        return -1; // Error
-#if NET_2_0
                                IPEndPoint endpoint = new IPEndPoint (Dns.GetHostEntry ("localhost").AddressList [0], 0);
-#else
-                               IPEndPoint endpoint = new IPEndPoint (Dns.GetHostByName ("localhost").AddressList [0], 0);
-#endif
                                Byte [] rawrs;
 
                                rawrs = Receive (ref endpoint);
@@ -1000,7 +945,6 @@ namespace System.Data.SqlClient
                        }
                }
 
-#if NET_2_0
                struct ColumnInfo
                {
                        public string name;
@@ -1716,11 +1660,9 @@ namespace System.Data.SqlClient
                        }
                }
 
-#endif // NET_2_0
 
                #endregion // Methods
 
-#if NET_2_0
                #region Fields Net 2
 
                bool async;
@@ -1737,7 +1679,6 @@ namespace System.Data.SqlClient
 
                #endregion // Properties Net 2
 
-#endif // NET_2_0
 
        }
 }