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;
38 DataTable schemaTable;
40 int recordsAffected = -1;
41 OciStatementType statementType;
42 OciStatementHandle statement;
48 internal OracleDataReader (OracleCommand command, OciStatementHandle statement)
50 this.command = command;
52 this.isClosed = false;
53 this.schemaTable = ConstructSchemaTable ();
54 this.statement = statement;
55 this.statementType = statement.GetStatementType ();
63 #endregion // Constructors
71 public int FieldCount {
72 get { return statement.ColumnCount; }
76 get { return hasRows; }
79 public bool IsClosed {
80 get { return isClosed; }
83 public object this [string name] {
84 get { return GetValue (GetOrdinal (name)); }
87 public object this [int i] {
88 get { return GetValue (i); }
91 public int RecordsAffected {
93 if (statementType == OciStatementType.Select)
96 return GetRecordsAffected ();
100 #endregion // Properties
107 command.CloseDataReader ();
111 private static DataTable ConstructSchemaTable ()
113 Type booleanType = Type.GetType ("System.Boolean");
114 Type stringType = Type.GetType ("System.String");
115 Type intType = Type.GetType ("System.Int32");
116 Type typeType = Type.GetType ("System.Type");
117 Type shortType = Type.GetType ("System.Int16");
119 DataTable schemaTable = new DataTable ("SchemaTable");
120 schemaTable.Columns.Add ("ColumnName", stringType);
121 schemaTable.Columns.Add ("ColumnOrdinal", intType);
122 schemaTable.Columns.Add ("ColumnSize", intType);
123 schemaTable.Columns.Add ("NumericPrecision", shortType);
124 schemaTable.Columns.Add ("NumericScale", shortType);
125 schemaTable.Columns.Add ("DataType", typeType);
126 schemaTable.Columns.Add ("IsLong", booleanType);
127 schemaTable.Columns.Add ("AllowDBNull", booleanType);
128 schemaTable.Columns.Add ("IsUnique", booleanType);
129 schemaTable.Columns.Add ("IsKey", booleanType);
130 schemaTable.Columns.Add ("IsReadOnly", booleanType);
131 schemaTable.Columns.Add ("BaseSchemaTable", stringType);
132 schemaTable.Columns.Add ("BaseCatalogName", stringType);
133 schemaTable.Columns.Add ("BaseTableName", stringType);
134 schemaTable.Columns.Add ("BaseColumnName", stringType);
135 schemaTable.Columns.Add ("BaseSchemaName", stringType);
140 private void Dispose (bool disposing)
144 schemaTable.Dispose ();
151 public void Dispose ()
154 GC.SuppressFinalize (this);
157 public bool GetBoolean (int i)
159 throw new NotSupportedException ();
162 public byte GetByte (int i)
164 throw new NotSupportedException ();
167 public long GetBytes (int i, long fieldOffset, byte[] buffer2, int bufferoffset, int length)
169 object value = GetValue (i);
170 if (!(value is byte[]))
171 throw new InvalidCastException ();
172 Array.Copy ((byte[]) value, (int) fieldOffset, buffer2, bufferoffset, length);
173 return ((byte[]) value).Length - fieldOffset;
176 public char GetChar (int i)
178 throw new NotSupportedException ();
181 public long GetChars (int i, long fieldOffset, char[] buffer2, int bufferoffset, int length)
183 object value = GetValue (i);
184 if (!(value is char[]))
185 throw new InvalidCastException ();
186 Array.Copy ((char[]) value, (int) fieldOffset, buffer2, bufferoffset, length);
187 return ((char[]) value).Length - fieldOffset;
191 public IDataReader GetData (int i)
193 throw new NotImplementedException ();
196 public string GetDataTypeName (int i)
198 return (string) dataTypeNames [i];
201 public DateTime GetDateTime (int i)
203 object value = GetValue (i);
204 if (!(value is DateTime))
205 throw new InvalidCastException ();
206 return (DateTime) value;
209 public decimal GetDecimal (int i)
211 object value = GetValue (i);
212 if (!(value is decimal))
213 throw new InvalidCastException ();
214 return (decimal) value;
217 public double GetDouble (int i)
219 object value = GetValue (i);
220 if (!(value is double))
221 throw new InvalidCastException ();
222 return (double) value;
225 public Type GetFieldType (int i)
227 // FIXME: "DataType" need to implement
228 //OciColumnInfo columnInfo = command.StatementHandle.DescribeColumn (i);
229 //Type fieldType = OciGlue.OciDataTypeToDbType (columnInfo.DataType);
231 return typeof(string);
234 public float GetFloat (int i)
236 object value = GetValue (i);
237 if (!(value is float))
238 throw new InvalidCastException ();
239 return (float) value;
242 public Guid GetGuid (int i)
244 throw new NotSupportedException ();
247 public short GetInt16 (int i)
249 throw new NotSupportedException ();
252 public int GetInt32 (int i)
254 object value = GetValue (i);
256 throw new InvalidCastException ();
260 public long GetInt64 (int i)
262 object value = GetValue (i);
263 if (!(value is long))
264 throw new InvalidCastException ();
268 public string GetName (int i)
270 return statement.GetParameter (i).GetName ();
274 public OracleBFile GetOracleBFile (int i)
276 throw new NotImplementedException ();
280 public OracleBinary GetOracleBinary (int i)
282 throw new NotImplementedException ();
285 public OracleLob GetOracleLob (int i)
287 OracleLob output = ((OciDefineHandle) statement.Values [i]).GetOracleLob ();
288 output.connection = command.Connection;
292 public OracleNumber GetOracleNumber (int i)
294 return new OracleNumber ((decimal) GetValue (i));
297 public int GetOrdinal (string name)
301 for (i = 0; i < statement.ColumnCount; i += 1) {
302 if (String.Compare (statement.GetParameter(i).GetName(), name, false) == 0)
306 for (i = 0; i < statement.ColumnCount; i += 1) {
307 if (String.Compare (statement.GetParameter(i).GetName(), name, true) == 0)
311 throw new IndexOutOfRangeException ();
314 private int GetRecordsAffected ()
316 if (recordsAffected == -1)
317 recordsAffected = statement.GetAttributeInt32 (OciAttributeType.RowCount, command.ErrorHandle);
318 return recordsAffected;
321 public DataTable GetSchemaTable ()
323 if (schemaTable.Rows != null && schemaTable.Rows.Count > 0)
326 dataTypeNames = new ArrayList ();
328 for (int i = 0; i < statement.ColumnCount; i += 1) {
329 DataRow row = schemaTable.NewRow ();
331 OciParameterDescriptor parameter = statement.GetParameter (i);
333 dataTypeNames.Add(parameter.GetDataTypeName());
335 row ["ColumnName"] = parameter.GetName ();
336 row ["ColumnOrdinal"] = i + 1;
337 row ["ColumnSize"] = parameter.GetDataSize ();
338 row ["NumericPrecision"] = parameter.GetPrecision ();
339 row ["NumericScale"] = parameter.GetScale ();
340 // FIXME: "DataType" need to implement
341 //row ["DataType"] = OciGlue.OciDataTypeToDbType (columnInfo.DataType);
342 row ["DataType"] = typeof(string);
343 row ["AllowDBNull"] = parameter.GetIsNull ();
344 row ["BaseColumnName"] = parameter.GetName ();
345 row ["IsReadOnly"] = true;
347 schemaTable.Rows.Add (row);
353 public string GetString (int i)
355 object value = GetValue (i);
356 if (!(value is string))
357 throw new InvalidCastException ();
358 return (string) value;
361 public TimeSpan GetTimeSpan (int i)
363 object value = GetValue (i);
364 if (!(value is TimeSpan))
365 throw new InvalidCastException ();
366 return (TimeSpan) value;
369 public object GetValue (int i)
371 OciDefineHandle defineHandle = (OciDefineHandle) statement.Values [i];
376 return defineHandle.GetValue ();
379 public int GetValues (object[] values)
381 int len = values.Length;
382 int count = statement.ColumnCount;
390 for (int i = 0; i < retval; i += 1)
391 values [i] = GetValue (i);
396 IEnumerator IEnumerable.GetEnumerator ()
398 return new DbEnumerator (this);
401 public bool IsDBNull (int i)
403 return ((OciDefineHandle) statement.Values [i]).IsNull;
407 public bool NextResult ()
409 // FIXME: get next result
410 //throw new NotImplementedException ();
416 bool retval = statement.Fetch ();
418 //return command.StatementHandle.Fetch ();
421 #endregion // Methods