New test.
[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 new SqlConnection Connection\r
88                 {\r
89                         get { return (SqlConnection)base.Connection; }\r
90                         set { base.Connection = value; }\r
91                 }
92         
93                 public new SqlParameterCollection Parameters\r
94                 {\r
95                         get { \r
96                                 return (SqlParameterCollection)base.Parameters; \r
97                         }\r
98                 }
99
100                 public new SqlTransaction Transaction\r
101                 {\r
102                         get { return (SqlTransaction)base.Transaction; }\r
103                         set { base.Transaction = value; }\r
104                 }
105
106 #if USE_DOTNET_REGEX\r
107                 protected override Regex StoredProcedureRegExp\r
108 #else\r
109                 protected override java.util.regex.Pattern StoredProcedureRegExp {\r
110 #endif\r
111                         get { return SqlStatementsHelper.NamedParameterStoredProcedureRegExp; }\r
112                 }\r
113 \r
114                 protected override SimpleRegex ParameterRegExp\r
115                 {\r
116                         get { return SqlStatementsHelper.NamedParameterRegExp; }\r
117                 }
118
119                 #endregion // Properties
120
121                 #region Methods
122
123                 public XmlReader ExecuteXmlReader() {
124                         return SqlXmlTextReader.Create(ExecuteReader(CommandBehavior.SequentialAccess));
125                 }
126
127                 public new SqlDataReader ExecuteReader()\r
128                 {\r
129                         return (SqlDataReader)ExecuteReader(CommandBehavior.Default);\r
130                 }
131
132                 public new SqlDataReader ExecuteReader(CommandBehavior behavior)\r
133                 {\r
134                         return (SqlDataReader)base.ExecuteReader(behavior);\r
135                 }
136 \r
137                 public new SqlParameter CreateParameter()\r
138                 {\r
139                         return (SqlParameter)CreateParameterInternal();\r
140                 }\r
141 \r
142                 protected sealed override void CheckParameters()\r
143                 {\r
144                         // do nothing\r
145                 }\r
146 \r
147                 protected override AbstractDbParameter GetUserParameter(string parameterName, IList userParametersList, int userParametersListPosition/*,int userParametersListStart,int userParameterListCount*/)\r
148                 {\r
149 //                      Match match = SqlStatementsHelper.NamedParameterRegExp.Match(parameterName);
150 //                      parameterName = match.Result("${USERPARAM}");\r
151 //                      if (parameterName.Length == 0)\r
152 //                              return null;\r
153 \r
154                         for(int i=0; i < userParametersList.Count; i++) {\r
155                                 AbstractDbParameter userParameter = (AbstractDbParameter)userParametersList[i];\r
156                                 if (String.Compare(parameterName, userParameter.Placeholder.Trim(), true, System.Globalization.CultureInfo.InvariantCulture) == 0) {\r
157                                         return userParameter;\r
158                                 }\r
159                         }\r
160 \r
161                         return null;\r
162                 }\r
163 \r
164                 protected override AbstractDbParameter GetReturnParameter (IList userParametersList)\r
165                 {\r
166                         for(int i=0; i < userParametersList.Count; i++) {\r
167                                 AbstractDbParameter userParameter = (AbstractDbParameter)userParametersList[i];\r
168                                 if (userParameter.Direction == ParameterDirection.ReturnValue) {\r
169                                         return userParameter;\r
170                                 }\r
171                         }\r
172 \r
173                         return null; \r
174                 }\r
175 \r
176                 protected sealed override DbParameter CreateParameterInternal()\r
177                 {\r
178                         return new SqlParameter();\r
179                 }\r
180 \r
181                 protected sealed override DbDataReader CreateReader()\r
182                 {\r
183                         return new SqlDataReader(this);\r
184                 }\r
185 \r
186                 protected sealed override DbParameterCollection CreateParameterCollection(AbstractDbCommand parent)\r
187                 {\r
188                         return new SqlParameterCollection((SqlCommand)parent);\r
189                 }\r
190 \r
191                 protected internal sealed override SystemException CreateException(SQLException e)
192                 {\r
193                         return new SqlException(e, Connection);         \r
194                 }
195
196                 #endregion // Methods\r
197         }
198 }