bf556bb0cd9bf48df58ec4ad376f9c01ee502bd6
[mono.git] / mcs / class / System.Data / System.Data.SqlClient.jvm / SqlConnection.cs
1 //
2 // System.Data.SqlClient.SqlConnection
3 //
4 // Authors:
5 //      Konstantin Triger <kostat@mainsoft.com>
6 //      Boris Kirzner <borisk@mainsoft.com>
7 //      
8 // (C) 2005 Mainsoft Corporation (http://www.mainsoft.com)
9 //
10
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31
32 using System.Data;
33 using System.Data.Common;
34 using System.Collections;
35 using System.Data.ProviderBase;
36
37 using java.sql;
38
39 using System.Configuration;
40 using Mainsoft.Data.Configuration;
41 using Mainsoft.Data.Jdbc.Providers;
42
43 namespace System.Data.SqlClient
44 {
45         public class SqlConnection : AbstractDBConnection
46         {
47                 #region Fields
48
49                 private const int DEFAULT_PACKET_SIZE = 8192;
50
51                 #endregion // Fields
52
53                 #region Constructors
54
55                 public SqlConnection() : this(null)
56                 {
57                 }
58
59                 public SqlConnection(String connectionString) : base(connectionString)
60                 {
61                 }
62
63                 #endregion // Constructors
64
65                 #region Events
66
67                 [DataCategory ("InfoMessage")]
68                 [DataSysDescription ("Event triggered when messages arrive from the DataSource.")]
69                 public event SqlInfoMessageEventHandler InfoMessage;
70
71                 #endregion // Events
72
73                 #region Properties
74
75                 public string WorkstationId
76                 {
77                         get { return (string)ConnectionStringBuilder["workstation id"]; }
78                 }
79
80                 public int PacketSize
81                 {
82                         get { 
83                                 string packetSize = (string)ConnectionStringBuilder["Packet Size"];
84                                 if (packetSize == null || packetSize.Length == 0) {
85                                         return DEFAULT_PACKET_SIZE;
86                                 }                               
87                                 try {
88                                         return Convert.ToInt32(packetSize);
89                                 }
90                                 catch(FormatException e) {
91                                         throw ExceptionHelper.InvalidValueForKey("packet size");
92                                 }
93                                 catch (OverflowException e) {
94                                         throw ExceptionHelper.InvalidValueForKey("packet size");
95                                 }
96                         }
97                 }
98
99                 protected override IConnectionProvider GetConnectionProvider() {
100                         IDictionary conProviderDict = ConnectionStringDictionary.Parse(ConnectionString);
101                         string provider = (string)conProviderDict["Provider"];
102                         if (provider == null)
103                                 provider = "SQLCLIENT";
104
105                         return GetConnectionProvider("Mainsoft.Data.Configuration/SqlClientProviders", provider);
106                 }
107
108                 #endregion // Properties
109
110                 #region Methods
111
112                 protected override DbTransaction BeginDbTransaction(IsolationLevel isolationLevel) {
113                         return BeginTransaction(isolationLevel);
114                 }
115
116                 public SqlTransaction BeginTransaction(String transactionName)
117                 {
118                         return BeginTransaction(IsolationLevel.ReadCommitted,transactionName);
119                 }
120
121                 public new SqlTransaction BeginTransaction(IsolationLevel isolationLevel)
122                 {
123                         return BeginTransaction(isolationLevel,"Transaction");
124                 }
125
126                 public new SqlTransaction BeginTransaction()
127                 {
128                         return BeginTransaction(IsolationLevel.ReadCommitted);
129                 }
130         
131                 public SqlTransaction BeginTransaction(IsolationLevel isolationLevel, string transactionName)
132                 {
133                         return new SqlTransaction(isolationLevel, this, transactionName);
134                 }
135
136                 public new SqlCommand CreateCommand()
137                 {
138                         return new SqlCommand(this);
139                 }
140
141                 protected override DbCommand CreateDbCommand() {
142                         return CreateCommand();
143                 }
144
145                 protected internal sealed override void OnSqlWarning(SQLWarning warning)
146                 {
147                         SqlErrorCollection col = new SqlErrorCollection(warning, this);
148                         OnSqlInfoMessage(new SqlInfoMessageEventArgs(col));
149                 }
150
151                 protected sealed override SystemException CreateException(SQLException e)
152                 {
153                         return new SqlException(e, this);               
154                 }
155
156                 protected sealed override SystemException CreateException(string message)
157                 {
158                         return new SqlException(message, null, this);           
159                 }
160
161                 private void OnSqlInfoMessage (SqlInfoMessageEventArgs value)
162                 {
163                         if (InfoMessage != null) {
164                                 InfoMessage (this, value);
165                         }
166                 }
167
168 #if NET_2_0
169
170                 [MonoNotSupported("")]
171                 public static void ChangePassword (string connectionString, string newPassword) 
172                 {
173                         throw new NotImplementedException ();
174
175                         // FIXME: refactored from Mono implementation.  Not finished!!!
176                         if (connectionString == null || newPassword == null || newPassword == String.Empty)
177                                 throw new ArgumentNullException ();
178                         if (newPassword.Length > 128)
179                                 throw new ArgumentException ("The value of newPassword exceeds its permittable length which is 128");
180
181                         SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder (connectionString);
182                         if (builder.IntegratedSecurity) {
183                                 throw new ArgumentException ("Can't use integrated security when changing password");
184                         }
185                         
186                         using (SqlConnection conn = new SqlConnection (connectionString)) {
187                                 conn.Open ();
188                                 SqlCommand cmd = conn.CreateCommand ();
189                                 cmd.CommandText = "sp_password";
190                                 cmd.CommandType = CommandType.StoredProcedure;
191                                 // FIXME: Need to extract old password and user from our structures
192                                 // of the connectionString.
193                                 cmd.Parameters.Add (builder.Password); // Is this good???
194                                 cmd.Parameters.Add (newPassword);
195                                 cmd.Parameters.Add (builder.UserID); // Is this good???
196                                 cmd.ExecuteNonQuery();
197                         }
198                 }
199
200                 #region Pooling
201
202                 [MonoNotSupported("Pooling not supported")]
203                 public static void ClearPool (SqlConnection connection) 
204                 {
205                         throw new NotImplementedException ();
206                 }
207
208                 [MonoNotSupported ("Pooling not supported")]
209                 public static void ClearAllPools () 
210                 {
211                         throw new NotImplementedException ();
212                 }
213
214                 #endregion
215                 #region Statistics
216
217                 [MonoNotSupported ("Statistics not supported")]
218                 public IDictionary RetrieveStatistics ()
219                 {
220                         throw new NotImplementedException ();
221                 }
222
223                 [MonoNotSupported ("Statistics not supported")]
224                 public void ResetStatistics ()
225                 {
226                         throw new NotImplementedException ();
227                 }
228
229                 #endregion
230 #endif
231                 #endregion // Methods
232
233         }
234 }