OleDbDataReader.cs 17 KB

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