83dedecd0c46e6fae9ca45c9621171c219e72423
[mono.git] / mcs / class / System.Data / System.Data.SqlClient.jvm / SqlDataReader.cs
1 //
2 // System.Data.SqlClient.SqlDataReader
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)
57                 {
58                         return new SqlException(message, e, (SqlConnection)_command.Connection);                
59                 }
60
61                 protected sealed override SystemException CreateException(java.io.IOException e)
62                 {
63                         return new SqlException(e, (SqlConnection)_command.Connection);
64                 }
65
66                 public override String GetDataTypeName(int columnIndex)
67                 {
68                         try {
69                                 string jdbcTypeName = Results.getMetaData().getColumnTypeName(columnIndex + 1);
70                                 
71                                 return SqlConvert.JdbcTypeNameToDbTypeName(jdbcTypeName);
72                         }
73                         catch (SQLException e) {
74                                 throw CreateException(e);
75                         }
76                 }
77
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);
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 {
115                 return new SqlByte(byt);
116             }
117         }
118
119 #if NET_2_0
120
121                 public virtual SqlBytes GetSqlBytes (int columnIndex)
122                 {
123                         byte [] bytes = GetBytes (columnIndex);
124                         if (IsDBNull (columnIndex)) {
125                                 return SqlBytes.Null;
126                         }
127                         else {
128                                 return new SqlBytes (bytes);
129                         }
130                 }
131
132                 public virtual SqlChars GetSqlChars (int columnIndex)
133                 {
134                         SqlString sqlStr = GetSqlString (columnIndex);
135                         if (sqlStr.IsNull) {
136                                 return SqlChars.Null;
137                         }
138                         else {
139                                 return new SqlChars (sqlStr);
140                         }
141                 }
142
143                 [MonoNotSupported("SqlXml is not fully implemented")]
144                 public virtual SqlXml GetSqlXml (int columnIndex)
145                 {
146                         throw new NotImplementedException ();
147                 }
148
149 #endif
150
151         // Gets the value of the specified column as a SqlDecimal.
152         public SqlDecimal GetSqlDecimal(int columnIndex)
153         {
154                         decimal dec = GetDecimal(columnIndex);
155             if(IsDBNull(columnIndex)) {
156                 return SqlDecimal.Null;
157                         }
158             else {
159                 return new SqlDecimal(dec);
160             }
161         }
162
163         // Gets the value of the specified column as a SqlDateTime.
164         public SqlDateTime GetSqlDateTime(int columnIndex)
165         {
166                         DateTime dateTime = GetDateTime(columnIndex);
167             if(IsDBNull(columnIndex)) {
168                 return SqlDateTime.Null;
169                         }
170             else {
171                 return new SqlDateTime(dateTime);
172             }
173         }
174
175         // Gets the value of the specified column as a SqlDouble.
176         public SqlDouble GetSqlDouble(int columnIndex)
177         {
178                         double doubl = GetDouble(columnIndex);
179             if(IsDBNull(columnIndex)) {
180                 return SqlDouble.Null;
181                         }
182             else {
183                 return new SqlDouble(doubl);
184             }
185         }
186
187         // Gets the value of the specified column as a SqlInt16.
188         public SqlInt16 GetSqlInt16(int columnIndex)
189         {
190                         short s = GetInt16(columnIndex);
191             if(IsDBNull(columnIndex)) {
192                 return SqlInt16.Null;
193                         }
194             else {
195                 return new SqlInt16(s);
196             }
197         }
198
199         // Gets the value of the specified column as a SqlInt32.
200         public SqlInt32 GetSqlInt32(int columnIndex)
201         {
202                         int i = GetInt32(columnIndex);
203             if(IsDBNull(columnIndex)) {
204                 return SqlInt32.Null;
205                         }
206             else {
207                 return new SqlInt32(i);
208             }
209         }
210
211         // Gets the value of the specified column as a SqlInt64.
212         public SqlInt64 GetSqlInt64(int columnIndex)
213         {
214                         long l = GetInt64(columnIndex);
215             if(IsDBNull(columnIndex)) {
216                 return SqlInt64.Null;
217                         }
218             else {
219                 return new SqlInt64(l);
220             }
221         }
222
223         // Gets the value of the specified column as a SqlMoney.
224         public SqlMoney GetSqlMoney(int columnIndex)
225         {
226                         decimal dec = GetDecimal(columnIndex);
227             if(IsDBNull(columnIndex)) {
228                 return SqlMoney.Null;
229                         }
230                         else {
231                                 return new SqlMoney(dec);
232                         }
233         }
234
235         // Gets the value of the specified column as a SqlSingle.
236         public SqlSingle GetSqlSingle(int columnIndex)
237         {
238                         float f = GetFloat(columnIndex);
239             if(IsDBNull(columnIndex)) {
240                 return SqlSingle.Null;
241                         }
242             else {
243                 return new SqlSingle(f);
244             }
245         }
246
247         // Gets the value of the specified column as a SqlString.
248         public SqlString GetSqlString(int columnIndex)
249         {
250                         string str = GetString(columnIndex);
251             if(IsDBNull(columnIndex)) {
252                 return SqlString.Null;
253                         }
254             else {
255                 return new SqlString(str);
256             }
257         }
258
259                 // Gets the value of the specified column as a SqlGuid.
260         public SqlGuid GetSqlGuid(int columnIndex)
261         {
262                         object obj = GetValue(columnIndex);
263             if(IsDBNull(columnIndex)) {
264                 return SqlGuid.Null;
265                         }
266             else {
267                                 if (obj is byte[]) {
268                                         return new SqlGuid((byte[])obj);
269                                 }
270                                 else {
271                                         return new SqlGuid((string)obj);
272                                 }
273             }
274         }
275
276                 // Gets all the attribute columns in the current row.
277         public int GetSqlValues(Object[] values)
278         {
279             int columnCount = FieldCount;
280             int i = 0;
281             for (; i < values.Length && i < columnCount; i++) {
282                 values[i] = GetSqlValue(i);
283             }
284             return i;
285         }
286
287                 // Gets an Object that is a representation of the underlying SqlDbType Variant.
288         public Object GetSqlValue(int columnIndex)
289         {
290             try {
291                                 int jdbcType = ResultsMetaData.getColumnType(columnIndex + 1);
292                                 SqlDbType sqlDbType = SqlConvert.JdbcTypeToSqlDbType(jdbcType);
293
294                                 switch (sqlDbType) {
295                                         case SqlDbType.BigInt : return GetSqlInt64(columnIndex);
296                                         case SqlDbType.Binary : return GetSqlBinary(columnIndex);
297                                         case SqlDbType.Bit : return GetSqlBoolean(columnIndex);
298                                         case SqlDbType.Char : return GetSqlString(columnIndex);
299                                         case SqlDbType.DateTime : return GetSqlDateTime(columnIndex);
300                                         case SqlDbType.Decimal : return GetSqlDecimal(columnIndex);
301                                         case SqlDbType.Float : return GetSqlDouble(columnIndex);
302                                         case SqlDbType.Image : return GetSqlBinary(columnIndex);
303                                         case SqlDbType.Int : return GetSqlInt32(columnIndex);
304                                         case SqlDbType.Money : return GetSqlDecimal(columnIndex);
305                                         case SqlDbType.NChar : return GetSqlString(columnIndex);
306                                         case SqlDbType.NText : return GetSqlString(columnIndex);
307                                         case SqlDbType.NVarChar : return GetSqlString(columnIndex);
308                                         case SqlDbType.Real : return GetSqlSingle(columnIndex);
309                                         case SqlDbType.UniqueIdentifier : return GetSqlGuid(columnIndex);
310                                         case SqlDbType.SmallDateTime : return GetSqlDateTime(columnIndex);
311                                         case SqlDbType.SmallInt : return GetSqlInt16(columnIndex);
312                                         case SqlDbType.SmallMoney : return GetSqlDecimal(columnIndex);
313                                         case SqlDbType.Text : return GetSqlString(columnIndex);
314                                         case SqlDbType.Timestamp : return GetSqlDateTime(columnIndex);
315                                         case SqlDbType.TinyInt : return GetSqlByte(columnIndex);
316                                         case SqlDbType.VarBinary : return GetSqlBinary(columnIndex);
317                                         case SqlDbType.VarChar : return GetSqlString(columnIndex);
318                                         case SqlDbType.Variant : return GetValue(columnIndex);
319                                         default : return GetValue(columnIndex);
320                                 }
321             }
322             catch (SQLException exp) {
323                 throw new Exception(exp.Message);
324             }
325         }
326
327 #if NET_2_0
328                 protected bool IsCommandBehavior (CommandBehavior condition)
329                 {
330                         return (_command.Behavior & condition) == condition;
331                 }
332 #endif
333                 #endregion // Methods
334     }
335 }