OleDbDataReader.cs 17 KB

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