avoid bad indexing
[mono.git] / mcs / class / System.Data / System.Data.SqlClient.jvm / SqlCommand.cs
1 //\r
2 // System.Data.SqlClient.SqlCommand\r
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;
33 using System.Collections;
34 using System.Text;
35 using System.Text.RegularExpressions;
36 using System.Data;
37 using System.Data.Common;
38 using System.Data.ProviderBase;
39 using System.Xml;
40
41 using java.sql;
42
43 namespace System.Data.SqlClient
44 {
45         public class SqlCommand : AbstractDbCommand
46         {
47                 #region Fields
48
49                 #endregion // Fields
50
51                 #region Constructors
52
53                 // Initializes a new instance of the SqlCommand class.
54                 // The base constructor initializes all fields to their default values.
55                 // The following table shows initial property values for an instance of SqlCommand.
56                 public SqlCommand() : this(null, null, null)
57                 {
58                 }
59
60                 public SqlCommand(SqlConnection connection) : this(null, connection, null)
61                 {
62                 }
63
64                 // Initializes a new instance of the SqlCommand class with the text of the query.
65                 public SqlCommand(String cmdText) : this(cmdText, null, null)
66                 {
67                 }
68
69                 // Initializes a new instance of the SqlCommand class with the text of the query and a SqlConnection.
70                 public SqlCommand(String cmdText, SqlConnection connection) : this(cmdText, connection, null)
71                 {
72                 }
73
74                 // Initializes a new instance of the SqlCommand class with the text of the query, a SqlConnection, and the Transaction.
75                 public SqlCommand(
76                         String cmdText,
77                         SqlConnection connection,
78                         SqlTransaction transaction)
79                         : base(cmdText, connection, transaction)
80                 {
81                 }
82
83                 #endregion // Constructors
84
85                 #region Properties
86
87                 public override string CommandText {
88                         get {
89                                 string commandText = base.CommandText;
90                                 if (CommandType != CommandType.StoredProcedure || string.IsNullOrEmpty (commandText))
91                                         return commandText;
92
93                                 string trimmedCommandText = commandText.TrimEnd ();
94                                 if (commandText [0] != '[' && trimmedCommandText.Length > 0 && trimmedCommandText [trimmedCommandText.Length - 1] != ')')
95                                         commandText = String.Concat ("[", commandText, "]");
96
97                                 return commandText;
98                         }
99                         set {
100                                 base.CommandText = value;
101                         }
102                 }
103
104                 public new SqlConnection Connection\r
105                 {\r
106                         get { return (SqlConnection)base.Connection; }\r
107                         set { base.Connection = value; }\r
108                 }
109         
110                 public new SqlParameterCollection Parameters\r
111                 {\r
112                         get { \r
113                                 return (SqlParameterCollection)base.Parameters; \r
114                         }\r
115                 }
116
117                 public new SqlTransaction Transaction\r
118                 {\r
119                         get { return (SqlTransaction)base.Transaction; }\r
120                         set { base.Transaction = value; }\r
121                 }
122
123 #if USE_DOTNET_REGEX\r
124                 protected override Regex StoredProcedureRegExp\r
125 #else\r
126                 protected override java.util.regex.Pattern StoredProcedureRegExp {\r
127 #endif\r
128                         get { return SqlStatementsHelper.NamedParameterStoredProcedureRegExp; }\r
129                 }\r
130 \r
131                 protected override SimpleRegex ParameterRegExp\r
132                 {\r
133                         get { return SqlStatementsHelper.NamedParameterRegExp; }\r
134                 }
135
136                 #endregion // Properties
137
138                 #region Methods
139
140                 public XmlReader ExecuteXmlReader() {
141                         return SqlXmlTextReader.Create(ExecuteReader(CommandBehavior.SequentialAccess));
142                 }
143
144                 public new SqlDataReader ExecuteReader()\r
145                 {\r
146                         return (SqlDataReader)ExecuteReader(CommandBehavior.Default);\r
147                 }
148
149                 public new SqlDataReader ExecuteReader(CommandBehavior behavior)\r
150                 {\r
151                         return (SqlDataReader)base.ExecuteReader(behavior);\r
152                 }
153 \r
154                 public new SqlParameter CreateParameter()\r
155                 {\r
156                         return (SqlParameter)CreateParameterInternal();\r
157                 }\r
158 \r
159                 protected sealed override void CheckParameters()\r
160                 {\r
161                         // do nothing\r
162                 }\r
163 \r
164                 protected override AbstractDbParameter GetUserParameter(string parameterName, IList userParametersList, int userParametersListPosition/*,int userParametersListStart,int userParameterListCount*/)\r
165                 {\r
166 //                      Match match = SqlStatementsHelper.NamedParameterRegExp.Match(parameterName);
167 //                      parameterName = match.Result("${USERPARAM}");\r
168 //                      if (parameterName.Length == 0)\r
169 //                              return null;\r
170 \r
171                         for(int i=0; i < userParametersList.Count; i++) {\r
172                                 AbstractDbParameter userParameter = (AbstractDbParameter)userParametersList[i];\r
173                                 if (String.Compare(parameterName, userParameter.Placeholder.Trim(), true, System.Globalization.CultureInfo.InvariantCulture) == 0) {\r
174                                         return userParameter;\r
175                                 }\r
176                         }\r
177 \r
178                         return null;\r
179                 }\r
180 \r
181                 protected override AbstractDbParameter GetReturnParameter (IList userParametersList)\r
182                 {\r
183                         for(int i=0; i < userParametersList.Count; i++) {\r
184                                 AbstractDbParameter userParameter = (AbstractDbParameter)userParametersList[i];\r
185                                 if (userParameter.Direction == ParameterDirection.ReturnValue) {\r
186                                         return userParameter;\r
187                                 }\r
188                         }\r
189 \r
190                         return null; \r
191                 }\r
192 \r
193                 protected sealed override DbParameter CreateParameterInternal()\r
194                 {\r
195                         return new SqlParameter();\r
196                 }\r
197 \r
198                 protected sealed override DbDataReader CreateReader()\r
199                 {\r
200                         return new SqlDataReader(this);\r
201                 }\r
202 \r
203                 protected sealed override DbParameterCollection CreateParameterCollection(AbstractDbCommand parent)\r
204                 {\r
205                         return new SqlParameterCollection((SqlCommand)parent);\r
206                 }\r
207 \r
208                 protected internal sealed override SystemException CreateException(SQLException e)
209                 {\r
210                         return new SqlException(e, Connection);         \r
211                 }
212
213                 #endregion // Methods\r
214         }
215 }