OleDbDataReader.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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. [MonoTODO]
  43. get {
  44. throw new NotImplementedException ();
  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. [MonoTODO]
  63. get {
  64. throw new NotImplementedException ();
  65. }
  66. }
  67. public object this[int index] {
  68. get {
  69. return (object) GetValue (index);
  70. }
  71. }
  72. public int RecordsAffected {
  73. get {
  74. int total_rows;
  75. if (currentResult < 0 ||
  76. currentResult >= gdaResults.Count)
  77. return 0;
  78. total_rows = libgda.gda_data_model_get_n_rows (
  79. (IntPtr) gdaResults[currentResult]);
  80. if (total_rows > 0) {
  81. if (FieldCount > 0) {
  82. // It's a SELECT statement
  83. return -1;
  84. }
  85. }
  86. return FieldCount > 0 ? -1 : total_rows;
  87. }
  88. }
  89. #endregion
  90. #region Methods
  91. public void Close ()
  92. {
  93. for (int i = 0; i < gdaResults.Count; i++) {
  94. IntPtr obj = (IntPtr) gdaResults[i];
  95. libgda.FreeObject (obj);
  96. gdaResults = null;
  97. }
  98. gdaResults.Clear ();
  99. open = false;
  100. currentResult = -1;
  101. currentRow = -1;
  102. }
  103. ~OleDbDataReader ()
  104. {
  105. if (open)
  106. Close ();
  107. }
  108. public bool GetBoolean (int ordinal)
  109. {
  110. IntPtr value;
  111. if (currentResult == -1)
  112. throw new InvalidCastException ();
  113. value = libgda.gda_data_model_get_value_at ((IntPtr) gdaResults[currentResult],
  114. ordinal, currentRow);
  115. if (value == IntPtr.Zero)
  116. throw new InvalidCastException ();
  117. if (libgda.gda_value_get_vtype (value) != GdaValueType.Boolean)
  118. throw new InvalidCastException ();
  119. return libgda.gda_value_get_boolean (value);
  120. }
  121. public byte GetByte (int ordinal)
  122. {
  123. IntPtr value;
  124. if (currentResult == -1)
  125. throw new InvalidCastException ();
  126. value = libgda.gda_data_model_get_value_at ((IntPtr) gdaResults[currentResult],
  127. ordinal, currentRow);
  128. if (value == IntPtr.Zero)
  129. throw new InvalidCastException ();
  130. if (libgda.gda_value_get_vtype (value) != GdaValueType.Tinyint)
  131. throw new InvalidCastException ();
  132. return libgda.gda_value_get_tinyint (value);
  133. }
  134. [MonoTODO]
  135. public long GetBytes (int ordinal, long dataIndex, byte[] buffer, int bufferIndex, int length)
  136. {
  137. throw new NotImplementedException ();
  138. }
  139. public char GetChar (int ordinal)
  140. {
  141. IntPtr value;
  142. if (currentResult == -1)
  143. throw new InvalidCastException ();
  144. value = libgda.gda_data_model_get_value_at ((IntPtr) gdaResults[currentResult],
  145. ordinal, currentRow);
  146. if (value == IntPtr.Zero)
  147. throw new InvalidCastException ();
  148. if (libgda.gda_value_get_vtype (value) != GdaValueType.Tinyint)
  149. throw new InvalidCastException ();
  150. return (char) libgda.gda_value_get_tinyint (value);
  151. }
  152. [MonoTODO]
  153. public long GetChars (int ordinal, long dataIndex, char[] buffer, int bufferIndex, int length)
  154. {
  155. throw new NotImplementedException ();
  156. }
  157. [MonoTODO]
  158. public OleDbDataReader GetData (int ordinal)
  159. {
  160. throw new NotImplementedException ();
  161. }
  162. public string GetDataTypeName (int index)
  163. {
  164. IntPtr attrs;
  165. GdaValueType type;
  166. if (currentResult == -1)
  167. return "unknown";
  168. attrs = libgda.gda_data_model_describe_column ((IntPtr) gdaResults[currentResult],
  169. index);
  170. if (attrs == IntPtr.Zero)
  171. return "unknown";
  172. type = libgda.gda_field_attributes_get_gdatype (attrs);
  173. libgda.gda_field_attributes_free (attrs);
  174. return libgda.gda_type_to_string (type);
  175. }
  176. public DateTime GetDateTime (int ordinal)
  177. {
  178. IntPtr value;
  179. DateTime dt;
  180. if (currentResult == -1)
  181. throw new InvalidCastException ();
  182. value = libgda.gda_data_model_get_value_at ((IntPtr) gdaResults[currentResult],
  183. ordinal, currentRow);
  184. if (value == IntPtr.Zero)
  185. throw new InvalidCastException ();
  186. if (libgda.gda_value_get_vtype (value) == GdaValueType.Date) {
  187. GdaDate gdt;
  188. gdt = (GdaDate) Marshal.PtrToStructure (libgda.gda_value_get_date (value),
  189. typeof (GdaDate));
  190. return new DateTime ((int) gdt.year, (int) gdt.month, (int) gdt.day);
  191. } else if (libgda.gda_value_get_vtype (value) == GdaValueType.Time) {
  192. GdaTime gdt;
  193. gdt = (GdaTime) Marshal.PtrToStructure (libgda.gda_value_get_time (value),
  194. typeof (GdaTime));
  195. return new DateTime (0, 0, 0, (int) gdt.hour, (int) gdt.minute, (int) gdt.second, 0);
  196. } else if (libgda.gda_value_get_vtype (value) == GdaValueType.Timestamp) {
  197. GdaTimestamp gdt;
  198. gdt = (GdaTimestamp) Marshal.PtrToStructure (libgda.gda_value_get_timestamp (value),
  199. typeof (GdaTimestamp));
  200. return new DateTime ((int) gdt.year, (int) gdt.month, (int) gdt.day,
  201. (int) gdt.hour, (int) gdt.minute, (int) gdt.second,
  202. (int) gdt.fraction);
  203. }
  204. throw new InvalidCastException ();
  205. }
  206. [MonoTODO]
  207. public decimal GetDecimal (int ordinal)
  208. {
  209. throw new NotImplementedException ();
  210. }
  211. public double GetDouble (int ordinal)
  212. {
  213. IntPtr value;
  214. if (currentResult == -1)
  215. throw new InvalidCastException ();
  216. value = libgda.gda_data_model_get_value_at ((IntPtr) gdaResults[currentResult],
  217. ordinal, currentRow);
  218. if (value == IntPtr.Zero)
  219. throw new InvalidCastException ();
  220. if (libgda.gda_value_get_vtype (value) != GdaValueType.Double)
  221. throw new InvalidCastException ();
  222. return libgda.gda_value_get_double (value);
  223. }
  224. [MonoTODO]
  225. public Type GetFieldType (int index)
  226. {
  227. throw new NotImplementedException ();
  228. }
  229. public float GetFloat (int ordinal)
  230. {
  231. IntPtr value;
  232. if (currentResult == -1)
  233. throw new InvalidCastException ();
  234. value = libgda.gda_data_model_get_value_at ((IntPtr) gdaResults[currentResult],
  235. ordinal, currentRow);
  236. if (value == IntPtr.Zero)
  237. throw new InvalidCastException ();
  238. if (libgda.gda_value_get_vtype (value) != GdaValueType.Single)
  239. throw new InvalidCastException ();
  240. return libgda.gda_value_get_single (value);
  241. }
  242. [MonoTODO]
  243. public Guid GetGuid (int ordinal)
  244. {
  245. throw new NotImplementedException ();
  246. }
  247. public short GetInt16 (int ordinal)
  248. {
  249. IntPtr value;
  250. if (currentResult == -1)
  251. throw new InvalidCastException ();
  252. value = libgda.gda_data_model_get_value_at ((IntPtr) gdaResults[currentResult],
  253. ordinal, currentRow);
  254. if (value == IntPtr.Zero)
  255. throw new InvalidCastException ();
  256. if (libgda.gda_value_get_vtype (value) != GdaValueType.Smallint)
  257. throw new InvalidCastException ();
  258. return (short) libgda.gda_value_get_smallint (value);
  259. }
  260. public int GetInt32 (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_vtype (value) != GdaValueType.Integer)
  270. throw new InvalidCastException ();
  271. return libgda.gda_value_get_integer (value);
  272. }
  273. public long GetInt64 (int ordinal)
  274. {
  275. IntPtr value;
  276. if (currentResult == -1)
  277. throw new InvalidCastException ();
  278. value = libgda.gda_data_model_get_value_at ((IntPtr) gdaResults[currentResult],
  279. ordinal, currentRow);
  280. if (value == IntPtr.Zero)
  281. throw new InvalidCastException ();
  282. if (libgda.gda_value_get_vtype (value) != GdaValueType.Bigint)
  283. throw new InvalidCastException ();
  284. return libgda.gda_value_get_bigint (value);
  285. }
  286. public string GetName (int index)
  287. {
  288. if (currentResult == -1)
  289. return null;
  290. return libgda.gda_data_model_get_column_title (
  291. (IntPtr) gdaResults[currentResult], index);
  292. }
  293. public int GetOrdinal (string name)
  294. {
  295. if (currentResult == -1)
  296. throw new IndexOutOfRangeException ();
  297. for (int i = 0; i < FieldCount; i++) {
  298. if (GetName (i) == name)
  299. return i;
  300. }
  301. throw new IndexOutOfRangeException ();
  302. }
  303. [MonoTODO]
  304. public DataTable GetSchemaTable ()
  305. {
  306. throw new NotImplementedException ();
  307. }
  308. public string GetString (int ordinal)
  309. {
  310. IntPtr value;
  311. if (currentResult == -1)
  312. throw new InvalidCastException ();
  313. value = libgda.gda_data_model_get_value_at ((IntPtr) gdaResults[currentResult],
  314. ordinal, currentRow);
  315. if (value == IntPtr.Zero)
  316. throw new InvalidCastException ();
  317. if (libgda.gda_value_get_vtype (value) != GdaValueType.String)
  318. throw new InvalidCastException ();
  319. return libgda.gda_value_get_string (value);
  320. }
  321. [MonoTODO]
  322. public TimeSpan GetTimeSpan (int ordinal)
  323. {
  324. throw new NotImplementedException ();
  325. }
  326. public object GetValue (int ordinal)
  327. {
  328. IntPtr value;
  329. GdaValueType type;
  330. if (currentResult == -1)
  331. throw new IndexOutOfRangeException ();
  332. value = libgda.gda_data_model_get_value_at ((IntPtr) gdaResults[currentResult],
  333. ordinal, currentRow);
  334. if (value == IntPtr.Zero)
  335. throw new IndexOutOfRangeException ();
  336. type = libgda.gda_value_get_vtype (value);
  337. switch (type) {
  338. case GdaValueType.Bigint : return GetInt64 (ordinal);
  339. case GdaValueType.Boolean : return GetBoolean (ordinal);
  340. case GdaValueType.Date : return GetDateTime (ordinal);
  341. case GdaValueType.Double : return GetDouble (ordinal);
  342. case GdaValueType.Integer : return GetInt32 (ordinal);
  343. case GdaValueType.Single : return GetFloat (ordinal);
  344. case GdaValueType.Smallint : return GetByte (ordinal);
  345. case GdaValueType.String : return GetString (ordinal);
  346. case GdaValueType.Time : return GetDateTime (ordinal);
  347. case GdaValueType.Timestamp : return GetDateTime (ordinal);
  348. case GdaValueType.Tinyint : return GetByte (ordinal);
  349. }
  350. return (object) libgda.gda_value_stringify (value);
  351. }
  352. [MonoTODO]
  353. public int GetValues (object[] values)
  354. {
  355. throw new NotImplementedException ();
  356. }
  357. [MonoTODO]
  358. IDataReader IDataRecord.GetData (int ordinal)
  359. {
  360. throw new NotImplementedException ();
  361. }
  362. [MonoTODO]
  363. void IDisposable.Dispose ()
  364. {
  365. throw new NotImplementedException ();
  366. }
  367. [MonoTODO]
  368. IEnumerator IEnumerable.GetEnumerator ()
  369. {
  370. throw new NotImplementedException ();
  371. }
  372. [MonoTODO]
  373. public bool IsDBNull (int ordinal)
  374. {
  375. throw new NotImplementedException ();
  376. }
  377. public bool NextResult ()
  378. {
  379. int i = currentResult + 1;
  380. if (i >= 0 && i < gdaResults.Count) {
  381. currentResult++;
  382. return true;
  383. }
  384. return false;
  385. }
  386. public bool Read ()
  387. {
  388. if (currentResult < 0 ||
  389. currentResult >= gdaResults.Count)
  390. return false;
  391. currentRow++;
  392. if (currentRow <
  393. libgda.gda_data_model_get_n_rows ((IntPtr) gdaResults[currentResult]))
  394. return true;
  395. return false;
  396. }
  397. #endregion
  398. }
  399. }