Copy from 72246 to trunk
[mono.git] / mcs / class / System.Data / System.Data.SqlClient.jvm / SqlDataReader.cs
1 //\r
2 // System.Data.SqlClient.SqlDataReader\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.Data.SqlTypes;
33 using System.Data.ProviderBase;
34
35 using java.sql;
36
37 namespace System.Data.SqlClient
38 {
39     public class SqlDataReader : AbstractDataReader
40     {
41
42                 #region Constructors
43
44                 internal SqlDataReader(SqlCommand command) : base(command)
45         {
46         }
47
48                 #endregion // Constructors
49
50                 #region Properties
51
52                 #endregion // Properties
53
54                 #region Methods
55
56                 protected sealed override SystemException CreateException(string message, SQLException e)\r
57                 {\r
58                         return new SqlException(message, e, (SqlConnection)_command.Connection);                \r
59                 }\r
60 \r
61                 protected sealed override SystemException CreateException(java.io.IOException e)\r
62                 {\r
63                         return new SqlException(e, (SqlConnection)_command.Connection);\r
64                 }\r
65 \r
66                 public override String GetDataTypeName(int columnIndex)\r
67                 {\r
68                         try {\r
69                                 string jdbcTypeName = Results.getMetaData().getColumnTypeName(columnIndex + 1);\r
70                                 \r
71                                 return SqlConvert.JdbcTypeNameToDbTypeName(jdbcTypeName);\r
72                         }\r
73                         catch (SQLException e) {\r
74                                 throw CreateException(e);\r
75                         }\r
76                 }\r
77 \r
78                 protected override int GetProviderType(int jdbcType)
79                 {
80                         return (int)SqlConvert.JdbcTypeToSqlDbType(jdbcType);   
81                 }
82
83         // Gets the value of the specified column as a SqlBinary.
84         public SqlBinary GetSqlBinary(int columnIndex)
85         {
86                         byte[] bytes = GetBytes(columnIndex);
87             if(IsDBNull(columnIndex)) {
88                                 return SqlBinary.Null;
89                         }
90             else {
91                                 return new SqlBinary(bytes);
92             }
93         }
94
95         // Gets the value of the specified column as a SqlBoolean.
96         public SqlBoolean GetSqlBoolean(int columnIndex)
97         {
98                         bool boolean = GetBoolean(columnIndex);
99             if(IsDBNull(columnIndex)) {
100                 return SqlBoolean.Null;
101                         }
102                         else {
103                                 return new SqlBoolean(boolean);\r
104             }
105         }
106
107         // Gets the value of the specified column as a SqlByte.
108         public SqlByte GetSqlByte(int columnIndex)
109         {
110                         byte byt = GetByte(columnIndex);
111             if(IsDBNull(columnIndex)) {
112                 return SqlByte.Null;
113                         }
114             else {\r
115                 return new SqlByte(byt);\r
116             }
117         }
118
119
120         // Gets the value of the specified column as a SqlDecimal.
121         public SqlDecimal GetSqlDecimal(int columnIndex)
122         {
123                         decimal dec = GetDecimal(columnIndex);
124             if(IsDBNull(columnIndex)) {
125                 return SqlDecimal.Null;
126                         }
127             else {\r
128                 return new SqlDecimal(dec);\r
129             }
130         }
131
132         // Gets the value of the specified column as a SqlDateTime.
133         public SqlDateTime GetSqlDateTime(int columnIndex)
134         {
135                         DateTime dateTime = GetDateTime(columnIndex);
136             if(IsDBNull(columnIndex)) {
137                 return SqlDateTime.Null;
138                         }
139             else {\r
140                 return new SqlDateTime(dateTime);\r
141             }
142         }
143
144         // Gets the value of the specified column as a SqlDouble.
145         public SqlDouble GetSqlDouble(int columnIndex)
146         {
147                         double doubl = GetDouble(columnIndex);
148             if(IsDBNull(columnIndex)) {
149                 return SqlDouble.Null;
150                         }
151             else {\r
152                 return new SqlDouble(doubl);\r
153             }
154         }
155
156         // Gets the value of the specified column as a SqlInt16.
157         public SqlInt16 GetSqlInt16(int columnIndex)
158         {
159                         short s = GetInt16(columnIndex);
160             if(IsDBNull(columnIndex)) {
161                 return SqlInt16.Null;
162                         }
163             else {\r
164                 return new SqlInt16(s);\r
165             }
166         }
167
168         // Gets the value of the specified column as a SqlInt32.
169         public SqlInt32 GetSqlInt32(int columnIndex)
170         {
171                         int i = GetInt32(columnIndex);
172             if(IsDBNull(columnIndex)) {
173                 return SqlInt32.Null;
174                         }
175             else {\r
176                 return new SqlInt32(i);\r
177             }
178         }
179
180         // Gets the value of the specified column as a SqlInt64.
181         public SqlInt64 GetSqlInt64(int columnIndex)
182         {
183                         long l = GetInt64(columnIndex);
184             if(IsDBNull(columnIndex)) {
185                 return SqlInt64.Null;
186                         }
187             else {\r
188                 return new SqlInt64(l);\r
189             }
190         }
191
192         // Gets the value of the specified column as a SqlMoney.
193         public SqlMoney GetSqlMoney(int columnIndex)
194         {
195                         decimal dec = GetDecimal(columnIndex);
196             if(IsDBNull(columnIndex)) {
197                 return SqlMoney.Null;
198                         }
199                         else {
200                                 return new SqlMoney(dec);
201                         }
202         }
203
204         // Gets the value of the specified column as a SqlSingle.
205         public SqlSingle GetSqlSingle(int columnIndex)
206         {
207                         float f = GetFloat(columnIndex);
208             if(IsDBNull(columnIndex)) {
209                 return SqlSingle.Null;
210                         }
211             else {\r
212                 return new SqlSingle(f);\r
213             }
214         }
215
216         // Gets the value of the specified column as a SqlString.
217         public SqlString GetSqlString(int columnIndex)
218         {
219                         string str = GetString(columnIndex);
220             if(IsDBNull(columnIndex)) {
221                 return SqlString.Null;
222                         }
223             else {\r
224                 return new SqlString(str);\r
225             }
226         }
227
228                 // Gets the value of the specified column as a SqlGuid.
229         public SqlGuid GetSqlGuid(int columnIndex)
230         {
231                         object obj = GetValue(columnIndex);
232             if(IsDBNull(columnIndex)) {
233                 return SqlGuid.Null;
234                         }
235             else {\r
236                                 if (obj is byte[]) {\r
237                                         return new SqlGuid((byte[])obj);\r
238                                 }\r
239                                 else {\r
240                                         return new SqlGuid((string)obj);\r
241                                 }\r
242             }
243         }
244
245                 // Gets all the attribute columns in the current row.
246         public int GetSqlValues(Object[] values)
247         {
248             int columnCount = FieldCount;
249             int i = 0;
250             for (; i < values.Length && i < columnCount; i++) {
251                 values[i] = GetSqlValue(i);
252             }
253             return i;
254         }
255
256                 // Gets an Object that is a representation of the underlying SqlDbType Variant.
257         public Object GetSqlValue(int columnIndex)
258         {
259             try {
260                                 int jdbcType = ResultsMetaData.getColumnType(columnIndex + 1);
261                                 SqlDbType sqlDbType = SqlConvert.JdbcTypeToSqlDbType(jdbcType);
262
263                                 switch (sqlDbType) {\r
264                                         case SqlDbType.BigInt : return GetSqlInt64(columnIndex);\r
265                                         case SqlDbType.Binary : return GetSqlBinary(columnIndex);\r
266                                         case SqlDbType.Bit : return GetSqlBoolean(columnIndex);\r
267                                         case SqlDbType.Char : return GetSqlString(columnIndex);\r
268                                         case SqlDbType.DateTime : return GetSqlDateTime(columnIndex);\r
269                                         case SqlDbType.Decimal : return GetSqlDecimal(columnIndex);\r
270                                         case SqlDbType.Float : return GetSqlDouble(columnIndex);\r
271                                         case SqlDbType.Image : return GetSqlBinary(columnIndex);\r
272                                         case SqlDbType.Int : return GetSqlInt32(columnIndex);\r
273                                         case SqlDbType.Money : return GetSqlDecimal(columnIndex);\r
274                                         case SqlDbType.NChar : return GetSqlString(columnIndex);\r
275                                         case SqlDbType.NText : return GetSqlString(columnIndex);\r
276                                         case SqlDbType.NVarChar : return GetSqlString(columnIndex);\r
277                                         case SqlDbType.Real : return GetSqlSingle(columnIndex);\r
278                                         case SqlDbType.UniqueIdentifier : return GetSqlGuid(columnIndex);\r
279                                         case SqlDbType.SmallDateTime : return GetSqlDateTime(columnIndex);\r
280                                         case SqlDbType.SmallInt : return GetSqlInt16(columnIndex);\r
281                                         case SqlDbType.SmallMoney : return GetSqlDecimal(columnIndex);\r
282                                         case SqlDbType.Text : return GetSqlString(columnIndex);\r
283                                         case SqlDbType.Timestamp : return GetSqlDateTime(columnIndex);\r
284                                         case SqlDbType.TinyInt : return GetSqlByte(columnIndex);\r
285                                         case SqlDbType.VarBinary : return GetSqlBinary(columnIndex);\r
286                                         case SqlDbType.VarChar : return GetSqlString(columnIndex);\r
287                                         case SqlDbType.Variant : return GetValue(columnIndex);\r
288                                         default : return GetValue(columnIndex);\r
289                                 }
290             }
291             catch (SQLException exp) {
292                 throw new Exception(exp.Message);
293             }
294         }
295
296                 #endregion // Methods
297     }
298 }