4 // Part of the Mono class libraries at
5 // mcs/class/System.Data.OracleClient/System.Data.OracleClient
7 // Assembly: System.Data.OracleClient.dll
8 // Namespace: System.Data.OracleClient
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.
20 using System
.Collections
;
21 using System
.ComponentModel
;
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
32 OracleCommand command
;
33 ArrayList dataTypeNames
;
34 bool disposed
= false;
37 DataTable schemaTable
;
39 int recordsAffected
= -1;
40 OciStatementType statementType
;
41 OciStatementHandle statement
;
47 internal OracleDataReader (OracleCommand command
, OciStatementHandle statement
)
49 this.command
= command
;
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 ();
73 #endregion // Constructors
81 public int FieldCount
{
82 get { return statement.ColumnCount; }
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
{
103 if (statementType
== OciStatementType
.Select
)
106 return GetRecordsAffected ();
110 #endregion // Properties
118 command
.CloseDataReader ();
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
);
151 private void Dispose (bool disposing
)
155 schemaTable
.Dispose ();
162 public void Dispose ()
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
;
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);
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
);
267 throw new InvalidCastException ();
271 public long GetInt64 (int i
)
273 object value = GetValue (i
);
274 if (!(value is long))
275 throw new InvalidCastException ();
279 public string GetName (int i
)
281 return statement
.GetParameter (i
).GetName ();
285 public OracleBFile
GetOracleBFile (int i
)
287 throw new NotImplementedException ();
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
;
303 public OracleNumber
GetOracleNumber (int i
)
305 return new OracleNumber ((decimal) GetValue (i
));
309 public OracleDateTime
GetOracleDateTime (int i
)
311 throw new NotImplementedException ();
315 public OracleMonthSpan
GetOracleMonthSpan (int i
)
317 throw new NotImplementedException ();
321 public OracleString
GetOracleString (int i
)
323 throw new NotImplementedException ();
327 public object GetOracleValue (int i
)
329 throw new NotImplementedException ();
333 public int GetOracleValues (object[] values
)
335 throw new NotImplementedException ();
339 public OracleTimeSpan
GetOracleTimeSpan (int i
)
341 throw new NotImplementedException ();
344 public int GetOrdinal (string name
)
348 for (i
= 0; i
< statement
.ColumnCount
; i
+= 1) {
349 if (String
.Compare (statement
.GetParameter(i
).GetName(), name
, false) == 0)
353 for (i
= 0; i
< statement
.ColumnCount
; i
+= 1) {
354 if (String
.Compare (statement
.GetParameter(i
).GetName(), name
, true) == 0)
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)
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
);
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
];
423 return defineHandle
.GetValue ();
426 public int GetValues (object[] values
)
428 int len
= values
.Length
;
429 int count
= statement
.ColumnCount
;
437 for (int i
= 0; i
< retval
; i
+= 1)
438 values
[i
] = GetValue (i
);
443 IEnumerator IEnumerable
.GetEnumerator ()
445 return new DbEnumerator (this);
448 public bool IsDBNull (int i
)
450 return ((OciDefineHandle
) statement
.Values
[i
]).IsNull
;
454 public bool NextResult ()
456 // FIXME: get next result
457 //throw new NotImplementedException ();
463 bool retval
= statement
.Fetch ();
466 //return command.StatementHandle.Fetch ();
469 #endregion // Methods