OleDbDataReader.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  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. Console.WriteLine("Error: current result -1");
  347. return null;
  348. }
  349. dataTableSchema = new DataTable ();
  350. dataTableSchema.Columns.Add ("ColumnName", typeof (string));
  351. dataTableSchema.Columns.Add ("ColumnOrdinal", typeof (int));
  352. dataTableSchema.Columns.Add ("ColumnSize", typeof (int));
  353. dataTableSchema.Columns.Add ("NumericPrecision", typeof (int));
  354. dataTableSchema.Columns.Add ("NumericScale", typeof (int));
  355. dataTableSchema.Columns.Add ("IsUnique", typeof (bool));
  356. dataTableSchema.Columns.Add ("IsKey", typeof (bool));
  357. DataColumn dc = dataTableSchema.Columns["IsKey"];
  358. dc.AllowDBNull = true; // IsKey can have a DBNull
  359. dataTableSchema.Columns.Add ("BaseCatalogName", typeof (string));
  360. dataTableSchema.Columns.Add ("BaseColumnName", typeof (string));
  361. dataTableSchema.Columns.Add ("BaseSchemaName", typeof (string));
  362. dataTableSchema.Columns.Add ("BaseTableName", typeof (string));
  363. dataTableSchema.Columns.Add ("DataType", typeof(Type));
  364. dataTableSchema.Columns.Add ("AllowDBNull", typeof (bool));
  365. dataTableSchema.Columns.Add ("ProviderType", typeof (int));
  366. dataTableSchema.Columns.Add ("IsAliased", typeof (bool));
  367. dataTableSchema.Columns.Add ("IsExpression", typeof (bool));
  368. dataTableSchema.Columns.Add ("IsIdentity", typeof (bool));
  369. dataTableSchema.Columns.Add ("IsAutoIncrement", typeof (bool));
  370. dataTableSchema.Columns.Add ("IsRowVersion", typeof (bool));
  371. dataTableSchema.Columns.Add ("IsHidden", typeof (bool));
  372. dataTableSchema.Columns.Add ("IsLong", typeof (bool));
  373. dataTableSchema.Columns.Add ("IsReadOnly", typeof (bool));
  374. DataRow schemaRow;
  375. DbType dbType;
  376. Type typ;
  377. for (int i = 0; i < this.FieldCount; i += 1 ) {
  378. schemaRow = dataTableSchema.NewRow ();
  379. attrs = libgda.gda_data_model_describe_column ((IntPtr) gdaResults[currentResult],
  380. i);
  381. if (attrs == IntPtr.Zero){
  382. // FIXME: throw exception
  383. Console.WriteLine("Error: attrs null");
  384. return null;
  385. }
  386. gdaType = libgda.gda_field_attributes_get_gdatype (attrs);
  387. columnSize = libgda.gda_field_attributes_get_defined_size (attrs);
  388. libgda.gda_field_attributes_free (attrs);
  389. schemaRow["ColumnName"] = this.GetName(i);
  390. schemaRow["ColumnOrdinal"] = i + 1;
  391. schemaRow["ColumnSize"] = (int) columnSize;
  392. schemaRow["NumericPrecision"] = 0;
  393. schemaRow["NumericScale"] = 0;
  394. // TODO: need to get KeyInfo
  395. //if((cmdBehavior & CommandBehavior.KeyInfo) == CommandBehavior.KeyInfo) {
  396. // bool IsUnique, IsKey;
  397. // GetKeyInfo(field[i].Name, out IsUnique, out IsKey);
  398. //}
  399. //else {
  400. schemaRow["IsUnique"] = false;
  401. schemaRow["IsKey"] = DBNull.Value;
  402. //}
  403. schemaRow["BaseCatalogName"] = "";
  404. schemaRow["BaseColumnName"] = this.GetName(i);
  405. schemaRow["BaseSchemaName"] = "";
  406. schemaRow["BaseTableName"] = "";
  407. schemaRow["DataType"] = this.GetFieldType(i);
  408. schemaRow["AllowDBNull"] = false;
  409. schemaRow["ProviderType"] = (int) gdaType;
  410. schemaRow["IsAliased"] = false;
  411. schemaRow["IsExpression"] = false;
  412. schemaRow["IsIdentity"] = false;
  413. schemaRow["IsAutoIncrement"] = false;
  414. schemaRow["IsRowVersion"] = false;
  415. schemaRow["IsHidden"] = false;
  416. schemaRow["IsLong"] = false;
  417. schemaRow["IsReadOnly"] = false;
  418. schemaRow.AcceptChanges();
  419. dataTableSchema.Rows.Add (schemaRow);
  420. }
  421. #if DEBUG_OleDbDataReader
  422. Console.WriteLine("********** DEBUG Table Schema BEGIN ************");
  423. foreach (DataRow myRow in dataTableSchema.Rows) {
  424. foreach (DataColumn myCol in dataTableSchema.Columns)
  425. Console.WriteLine(myCol.ColumnName + " = " + myRow[myCol]);
  426. Console.WriteLine();
  427. }
  428. Console.WriteLine("********** DEBUG Table Schema END ************");
  429. #endif // DEBUG_OleDbDataReader
  430. }
  431. return dataTableSchema;
  432. }
  433. public string GetString (int ordinal)
  434. {
  435. IntPtr value;
  436. if (currentResult == -1)
  437. throw new InvalidCastException ();
  438. value = libgda.gda_data_model_get_value_at ((IntPtr) gdaResults[currentResult],
  439. ordinal, currentRow);
  440. if (value == IntPtr.Zero)
  441. throw new InvalidCastException ();
  442. if (libgda.gda_value_get_type (value) != GdaValueType.String)
  443. throw new InvalidCastException ();
  444. return libgda.gda_value_get_string (value);
  445. }
  446. [MonoTODO]
  447. public TimeSpan GetTimeSpan (int ordinal)
  448. {
  449. throw new NotImplementedException ();
  450. }
  451. public object GetValue (int ordinal)
  452. {
  453. IntPtr value;
  454. GdaValueType type;
  455. if (currentResult == -1)
  456. throw new IndexOutOfRangeException ();
  457. value = libgda.gda_data_model_get_value_at ((IntPtr) gdaResults[currentResult],
  458. ordinal, currentRow);
  459. if (value == IntPtr.Zero)
  460. throw new IndexOutOfRangeException ();
  461. type = libgda.gda_value_get_type (value);
  462. switch (type) {
  463. case GdaValueType.Bigint : return GetInt64 (ordinal);
  464. case GdaValueType.Boolean : return GetBoolean (ordinal);
  465. case GdaValueType.Date : return GetDateTime (ordinal);
  466. case GdaValueType.Double : return GetDouble (ordinal);
  467. case GdaValueType.Integer : return GetInt32 (ordinal);
  468. case GdaValueType.Single : return GetFloat (ordinal);
  469. case GdaValueType.Smallint : return GetByte (ordinal);
  470. case GdaValueType.String : return GetString (ordinal);
  471. case GdaValueType.Time : return GetDateTime (ordinal);
  472. case GdaValueType.Timestamp : return GetDateTime (ordinal);
  473. case GdaValueType.Tinyint : return GetByte (ordinal);
  474. }
  475. return (object) libgda.gda_value_stringify (value);
  476. }
  477. [MonoTODO]
  478. public int GetValues (object[] values)
  479. {
  480. throw new NotImplementedException ();
  481. }
  482. [MonoTODO]
  483. IDataReader IDataRecord.GetData (int ordinal)
  484. {
  485. throw new NotImplementedException ();
  486. }
  487. IEnumerator IEnumerable.GetEnumerator ()
  488. {
  489. return new DbEnumerator (this);
  490. }
  491. public bool IsDBNull (int ordinal)
  492. {
  493. IntPtr value;
  494. if (currentResult == -1)
  495. throw new IndexOutOfRangeException ();
  496. value = libgda.gda_data_model_get_value_at ((IntPtr) gdaResults[currentResult],
  497. ordinal, currentRow);
  498. if (value == IntPtr.Zero)
  499. throw new IndexOutOfRangeException ();
  500. return libgda.gda_value_is_null (value);
  501. }
  502. public bool NextResult ()
  503. {
  504. int i = currentResult + 1;
  505. if (i >= 0 && i < gdaResults.Count) {
  506. currentResult++;
  507. return true;
  508. }
  509. return false;
  510. }
  511. public bool Read ()
  512. {
  513. if (currentResult < 0 ||
  514. currentResult >= gdaResults.Count)
  515. return false;
  516. currentRow++;
  517. if (currentRow <
  518. libgda.gda_data_model_get_n_rows ((IntPtr) gdaResults[currentResult]))
  519. return true;
  520. return false;
  521. }
  522. #endregion
  523. #region Destructors
  524. private void Dispose (bool disposing) {
  525. if (!this.disposed) {
  526. if (disposing) {
  527. // release any managed resources
  528. command = null;
  529. }
  530. // release any unmanaged resources
  531. if (gdaResults != null) {
  532. gdaResults.Clear ();
  533. gdaResults = null;
  534. }
  535. // close any handles
  536. if (open)
  537. Close ();
  538. this.disposed = true;
  539. }
  540. }
  541. void IDisposable.Dispose() {
  542. Dispose (true);
  543. }
  544. ~OleDbDataReader() {
  545. Dispose (false);
  546. }
  547. #endregion // Destructors
  548. }
  549. }