OleDbDataReader.cs 18 KB

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