OleDbDataReader.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  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. throw new NotImplementedException ();
  236. }
  237. public float GetFloat (int ordinal)
  238. {
  239. IntPtr value;
  240. if (currentResult == -1)
  241. throw new InvalidCastException ();
  242. value = libgda.gda_data_model_get_value_at ((IntPtr) gdaResults[currentResult],
  243. ordinal, currentRow);
  244. if (value == IntPtr.Zero)
  245. throw new InvalidCastException ();
  246. if (libgda.gda_value_get_type (value) != GdaValueType.Single)
  247. throw new InvalidCastException ();
  248. return libgda.gda_value_get_single (value);
  249. }
  250. [MonoTODO]
  251. public Guid GetGuid (int ordinal)
  252. {
  253. throw new NotImplementedException ();
  254. }
  255. public short GetInt16 (int ordinal)
  256. {
  257. IntPtr value;
  258. if (currentResult == -1)
  259. throw new InvalidCastException ();
  260. value = libgda.gda_data_model_get_value_at ((IntPtr) gdaResults[currentResult],
  261. ordinal, currentRow);
  262. if (value == IntPtr.Zero)
  263. throw new InvalidCastException ();
  264. if (libgda.gda_value_get_type (value) != GdaValueType.Smallint)
  265. throw new InvalidCastException ();
  266. return (short) libgda.gda_value_get_smallint (value);
  267. }
  268. public int GetInt32 (int ordinal)
  269. {
  270. IntPtr value;
  271. if (currentResult == -1)
  272. throw new InvalidCastException ();
  273. value = libgda.gda_data_model_get_value_at ((IntPtr) gdaResults[currentResult],
  274. ordinal, currentRow);
  275. if (value == IntPtr.Zero)
  276. throw new InvalidCastException ();
  277. if (libgda.gda_value_get_type (value) != GdaValueType.Integer)
  278. throw new InvalidCastException ();
  279. return libgda.gda_value_get_integer (value);
  280. }
  281. public long GetInt64 (int ordinal)
  282. {
  283. IntPtr value;
  284. if (currentResult == -1)
  285. throw new InvalidCastException ();
  286. value = libgda.gda_data_model_get_value_at ((IntPtr) gdaResults[currentResult],
  287. ordinal, currentRow);
  288. if (value == IntPtr.Zero)
  289. throw new InvalidCastException ();
  290. if (libgda.gda_value_get_type (value) != GdaValueType.Bigint)
  291. throw new InvalidCastException ();
  292. return libgda.gda_value_get_bigint (value);
  293. }
  294. public string GetName (int index)
  295. {
  296. if (currentResult == -1)
  297. return null;
  298. return libgda.gda_data_model_get_column_title (
  299. (IntPtr) gdaResults[currentResult], index);
  300. }
  301. public int GetOrdinal (string name)
  302. {
  303. if (currentResult == -1)
  304. throw new IndexOutOfRangeException ();
  305. for (int i = 0; i < FieldCount; i++) {
  306. if (GetName (i) == name)
  307. return i;
  308. }
  309. throw new IndexOutOfRangeException ();
  310. }
  311. public DataTable GetSchemaTable ()
  312. {
  313. DataTable table = new DataTable ();
  314. // FIXME: implement
  315. return table;
  316. }
  317. public string GetString (int ordinal)
  318. {
  319. IntPtr value;
  320. if (currentResult == -1)
  321. throw new InvalidCastException ();
  322. value = libgda.gda_data_model_get_value_at ((IntPtr) gdaResults[currentResult],
  323. ordinal, currentRow);
  324. if (value == IntPtr.Zero)
  325. throw new InvalidCastException ();
  326. if (libgda.gda_value_get_type (value) != GdaValueType.String)
  327. throw new InvalidCastException ();
  328. return libgda.gda_value_get_string (value);
  329. }
  330. [MonoTODO]
  331. public TimeSpan GetTimeSpan (int ordinal)
  332. {
  333. throw new NotImplementedException ();
  334. }
  335. public object GetValue (int ordinal)
  336. {
  337. IntPtr value;
  338. GdaValueType type;
  339. if (currentResult == -1)
  340. throw new IndexOutOfRangeException ();
  341. value = libgda.gda_data_model_get_value_at ((IntPtr) gdaResults[currentResult],
  342. ordinal, currentRow);
  343. if (value == IntPtr.Zero)
  344. throw new IndexOutOfRangeException ();
  345. type = libgda.gda_value_get_type (value);
  346. switch (type) {
  347. case GdaValueType.Bigint : return GetInt64 (ordinal);
  348. case GdaValueType.Boolean : return GetBoolean (ordinal);
  349. case GdaValueType.Date : return GetDateTime (ordinal);
  350. case GdaValueType.Double : return GetDouble (ordinal);
  351. case GdaValueType.Integer : return GetInt32 (ordinal);
  352. case GdaValueType.Single : return GetFloat (ordinal);
  353. case GdaValueType.Smallint : return GetByte (ordinal);
  354. case GdaValueType.String : return GetString (ordinal);
  355. case GdaValueType.Time : return GetDateTime (ordinal);
  356. case GdaValueType.Timestamp : return GetDateTime (ordinal);
  357. case GdaValueType.Tinyint : return GetByte (ordinal);
  358. }
  359. return (object) libgda.gda_value_stringify (value);
  360. }
  361. [MonoTODO]
  362. public int GetValues (object[] values)
  363. {
  364. throw new NotImplementedException ();
  365. }
  366. [MonoTODO]
  367. IDataReader IDataRecord.GetData (int ordinal)
  368. {
  369. throw new NotImplementedException ();
  370. }
  371. [MonoTODO]
  372. void IDisposable.Dispose ()
  373. {
  374. throw new NotImplementedException ();
  375. }
  376. [MonoTODO]
  377. IEnumerator IEnumerable.GetEnumerator ()
  378. {
  379. throw new NotImplementedException ();
  380. }
  381. public bool IsDBNull (int ordinal)
  382. {
  383. IntPtr value;
  384. if (currentResult == -1)
  385. throw new IndexOutOfRangeException ();
  386. value = libgda.gda_data_model_get_value_at ((IntPtr) gdaResults[currentResult],
  387. ordinal, currentRow);
  388. if (value == IntPtr.Zero)
  389. throw new IndexOutOfRangeException ();
  390. return libgda.gda_value_is_null (value);
  391. }
  392. public bool NextResult ()
  393. {
  394. int i = currentResult + 1;
  395. if (i >= 0 && i < gdaResults.Count) {
  396. currentResult++;
  397. return true;
  398. }
  399. return false;
  400. }
  401. public bool Read ()
  402. {
  403. if (currentResult < 0 ||
  404. currentResult >= gdaResults.Count)
  405. return false;
  406. currentRow++;
  407. if (currentRow <
  408. libgda.gda_data_model_get_n_rows ((IntPtr) gdaResults[currentResult]))
  409. return true;
  410. return false;
  411. }
  412. #endregion
  413. }
  414. }