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