**** Merged from MCS ****
[mono-project.git] / mcs / class / System.Data.OracleClient / System.Data.OracleClient / OracleDataReader.cs
blobebd5a73762e25a0ce7cbe0972f2ae2865d4476cb
1 //
2 // OracleDataReader.cs
3 //
4 // Part of the Mono class libraries at
5 // mcs/class/System.Data.OracleClient/System.Data.OracleClient
6 //
7 // Assembly: System.Data.OracleClient.dll
8 // Namespace: System.Data.OracleClient
9 //
10 // Authors: Tim Coleman <tim@timcoleman.com>
11 // Daniel Morgan <danmorg@sc.rr.com>
13 // Copyright (C) Tim Coleman, 2003
14 // Copyright (C) Daniel Morgan, 2003
16 // Licensed under the MIT/X11 License.
19 using System;
20 using System.Collections;
21 using System.ComponentModel;
22 using System.Data;
23 using System.Data.Common;
24 using System.Data.OracleClient.Oci;
25 using System.Runtime.InteropServices;
27 namespace System.Data.OracleClient {
28 public sealed class OracleDataReader : MarshalByRefObject, IDataReader, IDisposable, IDataRecord, IEnumerable
30 #region Fields
32 OracleCommand command;
33 ArrayList dataTypeNames;
34 bool disposed = false;
35 bool isClosed;
36 bool hasRows;
37 DataTable schemaTable;
39 int recordsAffected = -1;
40 OciStatementType statementType;
41 OciStatementHandle statement;
43 #endregion // Fields
45 #region Constructors
47 internal OracleDataReader (OracleCommand command, OciStatementHandle statement)
49 this.command = command;
50 this.hasRows = false;
51 this.isClosed = false;
52 this.schemaTable = ConstructSchemaTable ();
53 this.statement = statement;
54 this.statementType = statement.GetStatementType ();
57 internal OracleDataReader (OracleCommand command, OciStatementHandle statement, bool extHasRows )
59 this.command = command;
60 this.hasRows = extHasRows;
61 this.isClosed = false;
62 this.schemaTable = ConstructSchemaTable ();
63 this.statement = statement;
64 this.statementType = statement.GetStatementType ();
68 ~OracleDataReader ()
70 Dispose (false);
73 #endregion // Constructors
75 #region Properties
77 public int Depth {
78 get { return 0; }
81 public int FieldCount {
82 get { return statement.ColumnCount; }
85 public bool HasRows {
86 get { return hasRows; }
89 public bool IsClosed {
90 get { return isClosed; }
93 public object this [string name] {
94 get { return GetValue (GetOrdinal (name)); }
97 public object this [int i] {
98 get { return GetValue (i); }
101 public int RecordsAffected {
102 get {
103 if (statementType == OciStatementType.Select)
104 return -1;
105 else
106 return GetRecordsAffected ();
110 #endregion // Properties
112 #region Methods
114 public void Close ()
116 statement.Dispose();
117 if (!isClosed)
118 command.CloseDataReader ();
119 isClosed = true;
122 private static DataTable ConstructSchemaTable ()
124 Type booleanType = Type.GetType ("System.Boolean");
125 Type stringType = Type.GetType ("System.String");
126 Type intType = Type.GetType ("System.Int32");
127 Type typeType = Type.GetType ("System.Type");
128 Type shortType = Type.GetType ("System.Int16");
130 DataTable schemaTable = new DataTable ("SchemaTable");
131 schemaTable.Columns.Add ("ColumnName", stringType);
132 schemaTable.Columns.Add ("ColumnOrdinal", intType);
133 schemaTable.Columns.Add ("ColumnSize", intType);
134 schemaTable.Columns.Add ("NumericPrecision", shortType);
135 schemaTable.Columns.Add ("NumericScale", shortType);
136 schemaTable.Columns.Add ("DataType", typeType);
137 schemaTable.Columns.Add ("IsLong", booleanType);
138 schemaTable.Columns.Add ("AllowDBNull", booleanType);
139 schemaTable.Columns.Add ("IsUnique", booleanType);
140 schemaTable.Columns.Add ("IsKey", booleanType);
141 schemaTable.Columns.Add ("IsReadOnly", booleanType);
142 schemaTable.Columns.Add ("BaseSchemaTable", stringType);
143 schemaTable.Columns.Add ("BaseCatalogName", stringType);
144 schemaTable.Columns.Add ("BaseTableName", stringType);
145 schemaTable.Columns.Add ("BaseColumnName", stringType);
146 schemaTable.Columns.Add ("BaseSchemaName", stringType);
148 return schemaTable;
151 private void Dispose (bool disposing)
153 if (!disposed) {
154 if (disposing) {
155 schemaTable.Dispose ();
156 Close ();
158 disposed = true;
162 public void Dispose ()
164 Dispose (true);
165 GC.SuppressFinalize (this);
168 public bool GetBoolean (int i)
170 throw new NotSupportedException ();
173 public byte GetByte (int i)
175 throw new NotSupportedException ();
178 public long GetBytes (int i, long fieldOffset, byte[] buffer2, int bufferoffset, int length)
180 object value = GetValue (i);
181 if (!(value is byte[]))
182 throw new InvalidCastException ();
183 Array.Copy ((byte[]) value, (int) fieldOffset, buffer2, bufferoffset, length);
184 return ((byte[]) value).Length - fieldOffset;
187 public char GetChar (int i)
189 throw new NotSupportedException ();
192 public long GetChars (int i, long fieldOffset, char[] buffer2, int bufferoffset, int length)
194 object value = GetValue (i);
195 if (!(value is char[]))
196 throw new InvalidCastException ();
197 Array.Copy ((char[]) value, (int) fieldOffset, buffer2, bufferoffset, length);
198 return ((char[]) value).Length - fieldOffset;
201 [MonoTODO]
202 public IDataReader GetData (int i)
204 throw new NotImplementedException ();
207 public string GetDataTypeName (int i)
209 return (string) dataTypeNames [i];
212 public DateTime GetDateTime (int i)
214 object value = GetValue (i);
215 if (!(value is DateTime))
216 throw new InvalidCastException ();
217 return (DateTime) value;
220 public decimal GetDecimal (int i)
222 object value = GetValue (i);
223 if (!(value is decimal))
224 throw new InvalidCastException ();
225 return (decimal) value;
228 public double GetDouble (int i)
230 object value = GetValue (i);
231 if (!(value is double))
232 throw new InvalidCastException ();
233 return (double) value;
236 public Type GetFieldType (int i)
238 // FIXME: "DataType" need to implement
239 //OciColumnInfo columnInfo = command.StatementHandle.DescribeColumn (i);
240 //Type fieldType = OciGlue.OciDataTypeToDbType (columnInfo.DataType);
241 //return fieldType;
242 return typeof(string);
245 public float GetFloat (int i)
247 object value = GetValue (i);
248 if (!(value is float))
249 throw new InvalidCastException ();
250 return (float) value;
253 public Guid GetGuid (int i)
255 throw new NotSupportedException ();
258 public short GetInt16 (int i)
260 throw new NotSupportedException ();
263 public int GetInt32 (int i)
265 object value = GetValue (i);
266 if (!(value is int))
267 throw new InvalidCastException ();
268 return (int) value;
271 public long GetInt64 (int i)
273 object value = GetValue (i);
274 if (!(value is long))
275 throw new InvalidCastException ();
276 return (long) value;
279 public string GetName (int i)
281 return statement.GetParameter (i).GetName ();
284 [MonoTODO]
285 public OracleBFile GetOracleBFile (int i)
287 throw new NotImplementedException ();
290 [MonoTODO]
291 public OracleBinary GetOracleBinary (int i)
293 throw new NotImplementedException ();
296 public OracleLob GetOracleLob (int i)
298 OracleLob output = ((OciDefineHandle) statement.Values [i]).GetOracleLob ();
299 output.connection = command.Connection;
300 return output;
303 public OracleNumber GetOracleNumber (int i)
305 return new OracleNumber ((decimal) GetValue (i));
308 [MonoTODO]
309 public OracleDateTime GetOracleDateTime (int i)
311 throw new NotImplementedException ();
314 [MonoTODO]
315 public OracleMonthSpan GetOracleMonthSpan (int i)
317 throw new NotImplementedException ();
320 [MonoTODO]
321 public OracleString GetOracleString (int i)
323 throw new NotImplementedException ();
326 [MonoTODO]
327 public object GetOracleValue (int i)
329 throw new NotImplementedException ();
332 [MonoTODO]
333 public int GetOracleValues (object[] values)
335 throw new NotImplementedException ();
338 [MonoTODO]
339 public OracleTimeSpan GetOracleTimeSpan (int i)
341 throw new NotImplementedException ();
344 public int GetOrdinal (string name)
346 int i;
348 for (i = 0; i < statement.ColumnCount; i += 1) {
349 if (String.Compare (statement.GetParameter(i).GetName(), name, false) == 0)
350 return i;
353 for (i = 0; i < statement.ColumnCount; i += 1) {
354 if (String.Compare (statement.GetParameter(i).GetName(), name, true) == 0)
355 return i;
358 throw new IndexOutOfRangeException ();
361 private int GetRecordsAffected ()
363 if (recordsAffected == -1)
364 recordsAffected = statement.GetAttributeInt32 (OciAttributeType.RowCount, command.ErrorHandle);
365 return recordsAffected;
368 public DataTable GetSchemaTable ()
370 if (schemaTable.Rows != null && schemaTable.Rows.Count > 0)
371 return schemaTable;
373 dataTypeNames = new ArrayList ();
375 for (int i = 0; i < statement.ColumnCount; i += 1) {
376 DataRow row = schemaTable.NewRow ();
378 OciParameterDescriptor parameter = statement.GetParameter (i);
380 dataTypeNames.Add(parameter.GetDataTypeName());
382 row ["ColumnName"] = parameter.GetName ();
383 row ["ColumnOrdinal"] = i + 1;
384 row ["ColumnSize"] = parameter.GetDataSize ();
385 row ["NumericPrecision"] = parameter.GetPrecision ();
386 row ["NumericScale"] = parameter.GetScale ();
387 // FIXME: "DataType" need to implement
388 //row ["DataType"] = OciGlue.OciDataTypeToDbType (columnInfo.DataType);
389 row ["DataType"] = typeof(string);
390 row ["AllowDBNull"] = parameter.GetIsNull ();
391 row ["BaseColumnName"] = parameter.GetName ();
392 row ["IsReadOnly"] = true;
394 schemaTable.Rows.Add (row);
397 return schemaTable;
400 public string GetString (int i)
402 object value = GetValue (i);
403 if (!(value is string))
404 throw new InvalidCastException ();
405 return (string) value;
408 public TimeSpan GetTimeSpan (int i)
410 object value = GetValue (i);
411 if (!(value is TimeSpan))
412 throw new InvalidCastException ();
413 return (TimeSpan) value;
416 public object GetValue (int i)
418 OciDefineHandle defineHandle = (OciDefineHandle) statement.Values [i];
420 if (IsDBNull (i))
421 return DBNull.Value;
423 return defineHandle.GetValue ();
426 public int GetValues (object[] values)
428 int len = values.Length;
429 int count = statement.ColumnCount;
430 int retval = 0;
432 if (len > count)
433 retval = count;
434 else
435 retval = len;
437 for (int i = 0; i < retval; i += 1)
438 values [i] = GetValue (i);
440 return retval;
443 IEnumerator IEnumerable.GetEnumerator ()
445 return new DbEnumerator (this);
448 public bool IsDBNull (int i)
450 return ((OciDefineHandle) statement.Values [i]).IsNull;
453 [MonoTODO]
454 public bool NextResult ()
456 // FIXME: get next result
457 //throw new NotImplementedException ();
458 return false;
461 public bool Read ()
463 bool retval = statement.Fetch ();
464 hasRows = retval;
465 return retval;
466 //return command.StatementHandle.Fetch ();
469 #endregion // Methods