OleDbDataReader.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  1. //
  2. // System.Data.OleDb.OleDbDataReader
  3. //
  4. // Author:
  5. // Rodrigo Moya ([email protected])
  6. // Tim Coleman ([email protected])
  7. //
  8. // Copyright (C) Rodrigo Moya, 2002
  9. // Copyright (C) Tim Coleman, 2002
  10. //
  11. using System.Collections;
  12. using System.ComponentModel;
  13. using System.Data;
  14. using System.Data.Common;
  15. using System.Runtime.InteropServices;
  16. namespace System.Data.OleDb
  17. {
  18. public sealed class OleDbDataReader : MarshalByRefObject, IDataReader, IDisposable, IDataRecord, IEnumerable
  19. {
  20. #region Fields
  21. private OleDbCommand command;
  22. private bool open;
  23. private ArrayList gdaResults;
  24. private int currentResult;
  25. private int currentRow;
  26. private bool disposed = false;
  27. #endregion
  28. #region Constructors
  29. internal OleDbDataReader (OleDbCommand command, ArrayList results)
  30. {
  31. this.command = command;
  32. open = true;
  33. if (results != null)
  34. gdaResults = results;
  35. else
  36. gdaResults = new ArrayList ();
  37. currentResult = -1;
  38. currentRow = -1;
  39. }
  40. #endregion
  41. #region Properties
  42. public int Depth {
  43. get {
  44. return 0; // no nested selects supported
  45. }
  46. }
  47. public int FieldCount {
  48. get {
  49. if (currentResult < 0 ||
  50. currentResult >= gdaResults.Count)
  51. return 0;
  52. return libgda.gda_data_model_get_n_columns (
  53. (IntPtr) gdaResults[currentResult]);
  54. }
  55. }
  56. public bool IsClosed {
  57. get {
  58. return !open;
  59. }
  60. }
  61. public object this[string name] {
  62. get {
  63. int pos;
  64. if (currentResult == -1)
  65. throw new InvalidOperationException ();
  66. pos = libgda.gda_data_model_get_column_position (
  67. (IntPtr) gdaResults[currentResult],
  68. name);
  69. if (pos == -1)
  70. throw new IndexOutOfRangeException ();
  71. return this[pos];
  72. }
  73. }
  74. public object this[int index] {
  75. get {
  76. return (object) GetValue (index);
  77. }
  78. }
  79. public int RecordsAffected {
  80. get {
  81. int total_rows;
  82. if (currentResult < 0 ||
  83. currentResult >= gdaResults.Count)
  84. return 0;
  85. total_rows = libgda.gda_data_model_get_n_rows (
  86. (IntPtr) gdaResults[currentResult]);
  87. if (total_rows > 0) {
  88. if (FieldCount > 0) {
  89. // It's a SELECT statement
  90. return -1;
  91. }
  92. }
  93. return FieldCount > 0 ? -1 : total_rows;
  94. }
  95. }
  96. [MonoTODO]
  97. public bool HasRows {
  98. get {
  99. throw new NotImplementedException ();
  100. }
  101. }
  102. #endregion
  103. #region Methods
  104. public void Close ()
  105. {
  106. for (int i = 0; i < gdaResults.Count; i++) {
  107. IntPtr obj = (IntPtr) gdaResults[i];
  108. libgda.FreeObject (obj);
  109. }
  110. gdaResults.Clear ();
  111. gdaResults = null;
  112. open = false;
  113. currentResult = -1;
  114. currentRow = -1;
  115. }
  116. public bool GetBoolean (int ordinal)
  117. {
  118. IntPtr value;
  119. if (currentResult == -1)
  120. throw new InvalidCastException ();
  121. value = libgda.gda_data_model_get_value_at ((IntPtr) gdaResults[currentResult],
  122. ordinal, currentRow);
  123. if (value == IntPtr.Zero)
  124. throw new InvalidCastException ();
  125. if (libgda.gda_value_get_type (value) != GdaValueType.Boolean)
  126. throw new InvalidCastException ();
  127. return libgda.gda_value_get_boolean (value);
  128. }
  129. public byte GetByte (int ordinal)
  130. {
  131. IntPtr value;
  132. if (currentResult == -1)
  133. throw new InvalidCastException ();
  134. value = libgda.gda_data_model_get_value_at ((IntPtr) gdaResults[currentResult],
  135. ordinal, currentRow);
  136. if (value == IntPtr.Zero)
  137. throw new InvalidCastException ();
  138. if (libgda.gda_value_get_type (value) != GdaValueType.Tinyint)
  139. throw new InvalidCastException ();
  140. return libgda.gda_value_get_tinyint (value);
  141. }
  142. [MonoTODO]
  143. public long GetBytes (int ordinal, long dataIndex, byte[] buffer, int bufferIndex, int length)
  144. {
  145. throw new NotImplementedException ();
  146. }
  147. [EditorBrowsableAttribute (EditorBrowsableState.Never)]
  148. public char GetChar (int ordinal)
  149. {
  150. IntPtr value;
  151. if (currentResult == -1)
  152. throw new InvalidCastException ();
  153. value = libgda.gda_data_model_get_value_at ((IntPtr) gdaResults[currentResult],
  154. ordinal, currentRow);
  155. if (value == IntPtr.Zero)
  156. throw new InvalidCastException ();
  157. if (libgda.gda_value_get_type (value) != GdaValueType.Tinyint)
  158. throw new InvalidCastException ();
  159. return (char) libgda.gda_value_get_tinyint (value);
  160. }
  161. [MonoTODO]
  162. public long GetChars (int ordinal, long dataIndex, char[] buffer, int bufferIndex, int length)
  163. {
  164. throw new NotImplementedException ();
  165. }
  166. [MonoTODO]
  167. public OleDbDataReader GetData (int ordinal)
  168. {
  169. throw new NotImplementedException ();
  170. }
  171. public string GetDataTypeName (int index)
  172. {
  173. IntPtr attrs;
  174. GdaValueType type;
  175. if (currentResult == -1)
  176. return "unknown";
  177. attrs = libgda.gda_data_model_describe_column ((IntPtr) gdaResults[currentResult],
  178. index);
  179. if (attrs == IntPtr.Zero)
  180. return "unknown";
  181. type = libgda.gda_field_attributes_get_gdatype (attrs);
  182. libgda.gda_field_attributes_free (attrs);
  183. return libgda.gda_type_to_string (type);
  184. }
  185. public DateTime GetDateTime (int ordinal)
  186. {
  187. IntPtr value;
  188. DateTime dt;
  189. if (currentResult == -1)
  190. throw new InvalidCastException ();
  191. value = libgda.gda_data_model_get_value_at ((IntPtr) gdaResults[currentResult],
  192. ordinal, currentRow);
  193. if (value == IntPtr.Zero)
  194. throw new InvalidCastException ();
  195. if (libgda.gda_value_get_type (value) == GdaValueType.Date) {
  196. GdaDate gdt;
  197. gdt = (GdaDate) Marshal.PtrToStructure (libgda.gda_value_get_date (value),
  198. typeof (GdaDate));
  199. return new DateTime ((int) gdt.year, (int) gdt.month, (int) gdt.day);
  200. } else if (libgda.gda_value_get_type (value) == GdaValueType.Time) {
  201. GdaTime gdt;
  202. gdt = (GdaTime) Marshal.PtrToStructure (libgda.gda_value_get_time (value),
  203. typeof (GdaTime));
  204. return new DateTime (0, 0, 0, (int) gdt.hour, (int) gdt.minute, (int) gdt.second, 0);
  205. } else if (libgda.gda_value_get_type (value) == GdaValueType.Timestamp) {
  206. GdaTimestamp gdt;
  207. gdt = (GdaTimestamp) Marshal.PtrToStructure (libgda.gda_value_get_timestamp (value),
  208. typeof (GdaTimestamp));
  209. return new DateTime ((int) gdt.year, (int) gdt.month, (int) gdt.day,
  210. (int) gdt.hour, (int) gdt.minute, (int) gdt.second,
  211. (int) gdt.fraction);
  212. }
  213. throw new InvalidCastException ();
  214. }
  215. [MonoTODO]
  216. public decimal GetDecimal (int ordinal)
  217. {
  218. throw new NotImplementedException ();
  219. }
  220. public double GetDouble (int ordinal)
  221. {
  222. IntPtr value;
  223. if (currentResult == -1)
  224. throw new InvalidCastException ();
  225. value = libgda.gda_data_model_get_value_at ((IntPtr) gdaResults[currentResult],
  226. ordinal, currentRow);
  227. if (value == IntPtr.Zero)
  228. throw new InvalidCastException ();
  229. if (libgda.gda_value_get_type (value) != GdaValueType.Double)
  230. throw new InvalidCastException ();
  231. return libgda.gda_value_get_double (value);
  232. }
  233. [MonoTODO]
  234. public Type GetFieldType (int index)
  235. {
  236. IntPtr value;
  237. GdaValueType type;
  238. if (currentResult == -1)
  239. throw new IndexOutOfRangeException ();
  240. value = libgda.gda_data_model_get_value_at ((IntPtr) gdaResults[currentResult],
  241. index, currentRow);
  242. if (value == IntPtr.Zero)
  243. throw new IndexOutOfRangeException ();
  244. type = libgda.gda_value_get_type (value);
  245. switch (type) {
  246. case GdaValueType.Bigint : return typeof (long);
  247. case GdaValueType.Boolean : return typeof (bool);
  248. case GdaValueType.Date : return typeof (DateTime);
  249. case GdaValueType.Double : return typeof (double);
  250. case GdaValueType.Integer : return typeof (int);
  251. case GdaValueType.Single : return typeof (float);
  252. case GdaValueType.Smallint : return typeof (byte);
  253. case GdaValueType.String : return typeof (string);
  254. case GdaValueType.Time : return typeof (DateTime);
  255. case GdaValueType.Timestamp : return typeof (DateTime);
  256. case GdaValueType.Tinyint : return typeof (byte);
  257. }
  258. return typeof(string); // default
  259. }
  260. public float GetFloat (int ordinal)
  261. {
  262. IntPtr value;
  263. if (currentResult == -1)
  264. throw new InvalidCastException ();
  265. value = libgda.gda_data_model_get_value_at ((IntPtr) gdaResults[currentResult],
  266. ordinal, currentRow);
  267. if (value == IntPtr.Zero)
  268. throw new InvalidCastException ();
  269. if (libgda.gda_value_get_type (value) != GdaValueType.Single)
  270. throw new InvalidCastException ();
  271. return libgda.gda_value_get_single (value);
  272. }
  273. [MonoTODO]
  274. public Guid GetGuid (int ordinal)
  275. {
  276. throw new NotImplementedException ();
  277. }
  278. public short GetInt16 (int ordinal)
  279. {
  280. IntPtr value;
  281. if (currentResult == -1)
  282. throw new InvalidCastException ();
  283. value = libgda.gda_data_model_get_value_at ((IntPtr) gdaResults[currentResult],
  284. ordinal, currentRow);
  285. if (value == IntPtr.Zero)
  286. throw new InvalidCastException ();
  287. if (libgda.gda_value_get_type (value) != GdaValueType.Smallint)
  288. throw new InvalidCastException ();
  289. return (short) libgda.gda_value_get_smallint (value);
  290. }
  291. public int GetInt32 (int ordinal)
  292. {
  293. IntPtr value;
  294. if (currentResult == -1)
  295. throw new InvalidCastException ();
  296. value = libgda.gda_data_model_get_value_at ((IntPtr) gdaResults[currentResult],
  297. ordinal, currentRow);
  298. if (value == IntPtr.Zero)
  299. throw new InvalidCastException ();
  300. if (libgda.gda_value_get_type (value) != GdaValueType.Integer)
  301. throw new InvalidCastException ();
  302. return libgda.gda_value_get_integer (value);
  303. }
  304. public long GetInt64 (int ordinal)
  305. {
  306. IntPtr value;
  307. if (currentResult == -1)
  308. throw new InvalidCastException ();
  309. value = libgda.gda_data_model_get_value_at ((IntPtr) gdaResults[currentResult],
  310. ordinal, currentRow);
  311. if (value == IntPtr.Zero)
  312. throw new InvalidCastException ();
  313. if (libgda.gda_value_get_type (value) != GdaValueType.Bigint)
  314. throw new InvalidCastException ();
  315. return libgda.gda_value_get_bigint (value);
  316. }
  317. public string GetName (int index)
  318. {
  319. if (currentResult == -1)
  320. return null;
  321. return libgda.gda_data_model_get_column_title (
  322. (IntPtr) gdaResults[currentResult], index);
  323. }
  324. public int GetOrdinal (string name)
  325. {
  326. if (currentResult == -1)
  327. throw new IndexOutOfRangeException ();
  328. for (int i = 0; i < FieldCount; i++) {
  329. if (GetName (i) == name)
  330. return i;
  331. }
  332. throw new IndexOutOfRangeException ();
  333. }
  334. public DataTable GetSchemaTable ()
  335. {
  336. DataTable dataTableSchema = null;
  337. // Only Results from SQL SELECT Queries
  338. // get a DataTable for schema of the result
  339. // otherwise, DataTable is null reference
  340. if(this.FieldCount > 0) {
  341. IntPtr attrs;
  342. GdaValueType gdaType;
  343. long columnSize = 0;
  344. if (currentResult == -1) {
  345. // FIXME: throw an exception?
  346. #if DEBUG_OleDbDataReader
  347. Console.WriteLine("Error: current result -1");
  348. #endif
  349. return null;
  350. }
  351. dataTableSchema = new DataTable ();
  352. dataTableSchema.Columns.Add ("ColumnName", typeof (string));
  353. dataTableSchema.Columns.Add ("ColumnOrdinal", typeof (int));
  354. dataTableSchema.Columns.Add ("ColumnSize", typeof (int));
  355. dataTableSchema.Columns.Add ("NumericPrecision", typeof (int));
  356. dataTableSchema.Columns.Add ("NumericScale", typeof (int));
  357. dataTableSchema.Columns.Add ("IsUnique", typeof (bool));
  358. dataTableSchema.Columns.Add ("IsKey", typeof (bool));
  359. DataColumn dc = dataTableSchema.Columns["IsKey"];
  360. dc.AllowDBNull = true; // IsKey can have a DBNull
  361. dataTableSchema.Columns.Add ("BaseCatalogName", typeof (string));
  362. dataTableSchema.Columns.Add ("BaseColumnName", typeof (string));
  363. dataTableSchema.Columns.Add ("BaseSchemaName", typeof (string));
  364. dataTableSchema.Columns.Add ("BaseTableName", typeof (string));
  365. dataTableSchema.Columns.Add ("DataType", typeof(Type));
  366. dataTableSchema.Columns.Add ("AllowDBNull", typeof (bool));
  367. dataTableSchema.Columns.Add ("ProviderType", typeof (int));
  368. dataTableSchema.Columns.Add ("IsAliased", typeof (bool));
  369. dataTableSchema.Columns.Add ("IsExpression", typeof (bool));
  370. dataTableSchema.Columns.Add ("IsIdentity", typeof (bool));
  371. dataTableSchema.Columns.Add ("IsAutoIncrement", typeof (bool));
  372. dataTableSchema.Columns.Add ("IsRowVersion", typeof (bool));
  373. dataTableSchema.Columns.Add ("IsHidden", typeof (bool));
  374. dataTableSchema.Columns.Add ("IsLong", typeof (bool));
  375. dataTableSchema.Columns.Add ("IsReadOnly", typeof (bool));
  376. DataRow schemaRow;
  377. DbType dbType;
  378. Type typ;
  379. for (int i = 0; i < this.FieldCount; i += 1 ) {
  380. schemaRow = dataTableSchema.NewRow ();
  381. attrs = libgda.gda_data_model_describe_column ((IntPtr) gdaResults[currentResult],
  382. i);
  383. if (attrs == IntPtr.Zero){
  384. // FIXME: throw exception
  385. #if DEBUG_OleDbDataReader
  386. Console.WriteLine("Error: attrs null");
  387. #endif
  388. return null;
  389. }
  390. gdaType = libgda.gda_field_attributes_get_gdatype (attrs);
  391. columnSize = libgda.gda_field_attributes_get_defined_size (attrs);
  392. libgda.gda_field_attributes_free (attrs);
  393. schemaRow["ColumnName"] = this.GetName(i);
  394. schemaRow["ColumnOrdinal"] = i + 1;
  395. schemaRow["ColumnSize"] = (int) columnSize;
  396. schemaRow["NumericPrecision"] = 0;
  397. schemaRow["NumericScale"] = 0;
  398. // TODO: need to get KeyInfo
  399. //if((cmdBehavior & CommandBehavior.KeyInfo) == CommandBehavior.KeyInfo) {
  400. // bool IsUnique, IsKey;
  401. // GetKeyInfo(field[i].Name, out IsUnique, out IsKey);
  402. //}
  403. //else {
  404. schemaRow["IsUnique"] = false;
  405. schemaRow["IsKey"] = DBNull.Value;
  406. //}
  407. schemaRow["BaseCatalogName"] = "";
  408. schemaRow["BaseColumnName"] = this.GetName(i);
  409. schemaRow["BaseSchemaName"] = "";
  410. schemaRow["BaseTableName"] = "";
  411. schemaRow["DataType"] = this.GetFieldType(i);
  412. schemaRow["AllowDBNull"] = false;
  413. schemaRow["ProviderType"] = (int) gdaType;
  414. schemaRow["IsAliased"] = false;
  415. schemaRow["IsExpression"] = false;
  416. schemaRow["IsIdentity"] = false;
  417. schemaRow["IsAutoIncrement"] = false;
  418. schemaRow["IsRowVersion"] = false;
  419. schemaRow["IsHidden"] = false;
  420. schemaRow["IsLong"] = false;
  421. schemaRow["IsReadOnly"] = false;
  422. schemaRow.AcceptChanges();
  423. dataTableSchema.Rows.Add (schemaRow);
  424. }
  425. #if DEBUG_OleDbDataReader
  426. Console.WriteLine("********** DEBUG Table Schema BEGIN ************");
  427. foreach (DataRow myRow in dataTableSchema.Rows) {
  428. foreach (DataColumn myCol in dataTableSchema.Columns)
  429. Console.WriteLine(myCol.ColumnName + " = " + myRow[myCol]);
  430. Console.WriteLine();
  431. }
  432. Console.WriteLine("********** DEBUG Table Schema END ************");
  433. #endif // DEBUG_OleDbDataReader
  434. }
  435. return dataTableSchema;
  436. }
  437. public string GetString (int ordinal)
  438. {
  439. IntPtr value;
  440. if (currentResult == -1)
  441. throw new InvalidCastException ();
  442. value = libgda.gda_data_model_get_value_at ((IntPtr) gdaResults[currentResult],
  443. ordinal, currentRow);
  444. if (value == IntPtr.Zero)
  445. throw new InvalidCastException ();
  446. if (libgda.gda_value_get_type (value) != GdaValueType.String)
  447. throw new InvalidCastException ();
  448. return libgda.gda_value_get_string (value);
  449. }
  450. [MonoTODO]
  451. public TimeSpan GetTimeSpan (int ordinal)
  452. {
  453. throw new NotImplementedException ();
  454. }
  455. public object GetValue (int ordinal)
  456. {
  457. IntPtr value;
  458. GdaValueType type;
  459. if (currentResult == -1)
  460. throw new IndexOutOfRangeException ();
  461. value = libgda.gda_data_model_get_value_at ((IntPtr) gdaResults[currentResult],
  462. ordinal, currentRow);
  463. if (value == IntPtr.Zero)
  464. throw new IndexOutOfRangeException ();
  465. type = libgda.gda_value_get_type (value);
  466. switch (type) {
  467. case GdaValueType.Bigint : return GetInt64 (ordinal);
  468. case GdaValueType.Boolean : return GetBoolean (ordinal);
  469. case GdaValueType.Date : return GetDateTime (ordinal);
  470. case GdaValueType.Double : return GetDouble (ordinal);
  471. case GdaValueType.Integer : return GetInt32 (ordinal);
  472. case GdaValueType.Single : return GetFloat (ordinal);
  473. case GdaValueType.Smallint : return GetByte (ordinal);
  474. case GdaValueType.String : return GetString (ordinal);
  475. case GdaValueType.Time : return GetDateTime (ordinal);
  476. case GdaValueType.Timestamp : return GetDateTime (ordinal);
  477. case GdaValueType.Tinyint : return GetByte (ordinal);
  478. }
  479. return (object) libgda.gda_value_stringify (value);
  480. }
  481. [MonoTODO]
  482. public int GetValues (object[] values)
  483. {
  484. throw new NotImplementedException ();
  485. }
  486. [MonoTODO]
  487. IDataReader IDataRecord.GetData (int ordinal)
  488. {
  489. throw new NotImplementedException ();
  490. }
  491. IEnumerator IEnumerable.GetEnumerator ()
  492. {
  493. return new DbEnumerator (this);
  494. }
  495. public bool IsDBNull (int ordinal)
  496. {
  497. IntPtr value;
  498. if (currentResult == -1)
  499. throw new IndexOutOfRangeException ();
  500. value = libgda.gda_data_model_get_value_at ((IntPtr) gdaResults[currentResult],
  501. ordinal, currentRow);
  502. if (value == IntPtr.Zero)
  503. throw new IndexOutOfRangeException ();
  504. return libgda.gda_value_is_null (value);
  505. }
  506. public bool NextResult ()
  507. {
  508. int i = currentResult + 1;
  509. if (i >= 0 && i < gdaResults.Count) {
  510. currentResult++;
  511. return true;
  512. }
  513. return false;
  514. }
  515. public bool Read ()
  516. {
  517. if (currentResult < 0 ||
  518. currentResult >= gdaResults.Count)
  519. return false;
  520. currentRow++;
  521. if (currentRow <
  522. libgda.gda_data_model_get_n_rows ((IntPtr) gdaResults[currentResult]))
  523. return true;
  524. return false;
  525. }
  526. #endregion
  527. #region Destructors
  528. private void Dispose (bool disposing) {
  529. if (!this.disposed) {
  530. if (disposing) {
  531. // release any managed resources
  532. command = null;
  533. }
  534. // release any unmanaged resources
  535. if (gdaResults != null) {
  536. gdaResults.Clear ();
  537. gdaResults = null;
  538. }
  539. // close any handles
  540. if (open)
  541. Close ();
  542. this.disposed = true;
  543. }
  544. }
  545. void IDisposable.Dispose() {
  546. Dispose (true);
  547. }
  548. ~OleDbDataReader() {
  549. Dispose (false);
  550. }
  551. #endregion // Destructors
  552. }
  553. }