OdbcDataReader.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. //
  2. // System.Data.Odbc.OdbcDataReader
  3. //
  4. // Author:
  5. // Brian Ritchie ([email protected])
  6. //
  7. // Copyright (C) Brian Ritchie, 2002
  8. //
  9. using System.Collections;
  10. using System.ComponentModel;
  11. using System.Data;
  12. using System.Data.Common;
  13. using System.Runtime.InteropServices;
  14. namespace System.Data.Odbc
  15. {
  16. public sealed class OdbcDataReader : MarshalByRefObject, IDataReader,
  17. IDisposable, IDataRecord, IEnumerable
  18. {
  19. #region Fields
  20. private OdbcCommand command;
  21. private bool open;
  22. private int currentRow;
  23. private DataColumn[] cols;
  24. private int hstmt;
  25. #endregion
  26. #region Constructors
  27. internal OdbcDataReader (OdbcCommand command)
  28. {
  29. this.command = command;
  30. this.command.Connection.DataReader = this;
  31. open = true;
  32. currentRow = -1;
  33. hstmt=command.hStmt;
  34. LoadColumns();
  35. }
  36. #endregion
  37. #region Properties
  38. public int Depth {
  39. get {
  40. return 0; // no nested selects supported
  41. }
  42. }
  43. public int FieldCount {
  44. get {
  45. return cols.Length;
  46. }
  47. }
  48. public bool IsClosed {
  49. get {
  50. return !open;
  51. }
  52. }
  53. public DataColumn[] Columns
  54. {
  55. get {
  56. return cols;
  57. }
  58. }
  59. public object this[string name] {
  60. get {
  61. ushort pos;
  62. if (currentRow == -1)
  63. throw new InvalidOperationException ();
  64. pos = ColIndex(name);
  65. if (pos == -1)
  66. throw new IndexOutOfRangeException ();
  67. return this[pos];
  68. }
  69. }
  70. public object this[int index] {
  71. get {
  72. return (object) GetODBCData (index);
  73. }
  74. }
  75. public int RecordsAffected {
  76. get {
  77. return -1;
  78. }
  79. }
  80. #endregion
  81. #region Methods
  82. private Type SQLTypeToCILType(short DataType)
  83. {
  84. switch (DataType)
  85. {
  86. case 12:
  87. case 1:
  88. return typeof(string);
  89. case 4:
  90. return typeof(int);
  91. case 5:
  92. return typeof(short);
  93. case 2:
  94. case 3:
  95. case 6:
  96. case 7:
  97. case 8:
  98. return typeof(float);
  99. case 90:
  100. case 91:
  101. case 92:
  102. case 9:
  103. return typeof(DateTime);
  104. default:
  105. Console.WriteLine("WARNING: Unknown type {0}", DataType);
  106. return typeof(string);
  107. }
  108. }
  109. private short CILTypeToSQLType(Type type)
  110. {
  111. if (type==typeof(int))
  112. return 4;
  113. else if (type==typeof(string))
  114. return 12;
  115. else
  116. return 12;
  117. }
  118. private void LoadColumns()
  119. {
  120. ArrayList colsArray=new ArrayList();
  121. short colcount=0;
  122. short bufsize=255;
  123. byte[] colname_buffer=new byte[bufsize];
  124. string colname;
  125. short colname_size=0;
  126. short DataType=0, ColSize=0, DecDigits=0, Nullable=0;
  127. libodbc.SQLNumResultCols(hstmt, ref colcount);
  128. for (ushort i=1;i<=colcount;i++)
  129. {
  130. libodbc.SQLDescribeCol(hstmt, i, colname_buffer, bufsize, ref
  131. colname_size, ref DataType, ref ColSize, ref DecDigits, ref Nullable);
  132. colname=System.Text.Encoding.Default.GetString(colname_buffer);
  133. DataColumn c=new DataColumn(colname, SQLTypeToCILType(DataType));
  134. c.AllowDBNull=(Nullable!=0);
  135. if (c.DataType==typeof(string))
  136. c.MaxLength=ColSize;
  137. colsArray.Add(c);
  138. }
  139. cols=(DataColumn[]) colsArray.ToArray(typeof(DataColumn));
  140. }
  141. private ushort ColIndex(string colname)
  142. {
  143. ushort i=0;
  144. foreach (DataColumn col in cols)
  145. {
  146. if (col.ColumnName==colname)
  147. return i;
  148. i++;
  149. }
  150. return 0;
  151. }
  152. private object GetODBCData(int colindex)
  153. {
  154. return GetODBCData(Convert.ToUInt16(colindex));
  155. }
  156. private object GetODBCData(ushort colindex)
  157. {
  158. OdbcReturn ret;
  159. int outsize=0;
  160. DataColumn col=cols[colindex];
  161. colindex+=1;
  162. if (col.DataType==typeof(int))
  163. {
  164. int data=0;
  165. ret=libodbc.SQLGetData(hstmt, colindex, 4, ref data, 0, ref outsize);
  166. libodbc.DisplayError("SQLGetData(int)",ret);
  167. return data;
  168. }
  169. else if (col.DataType==typeof(string))
  170. {
  171. byte[] strbuffer=new byte[255];
  172. ret=libodbc.SQLGetData(hstmt, colindex, 1, strbuffer, 255, ref outsize);
  173. libodbc.DisplayError("SQLGetData("+col.ColumnName+","+colindex.ToString()+")",ret);
  174. return System.Text.Encoding.Default.GetString(strbuffer);
  175. }
  176. else if (col.DataType==typeof(float))
  177. {
  178. float data=0;
  179. ret=libodbc.SQLGetData(hstmt, colindex, 7, ref data, 0, ref outsize);
  180. return data;
  181. }
  182. else if (col.DataType==typeof(DateTime))
  183. {
  184. OdbcTimestamp data=new OdbcTimestamp();
  185. ret=libodbc.SQLGetData(hstmt, colindex, 91, ref data, 0, ref outsize);
  186. return new
  187. DateTime(data.year,data.month,data.day,data.hour,data.minute,data.second,Convert.ToInt32(data.fraction));
  188. }
  189. else return "";
  190. }
  191. public void Close ()
  192. {
  193. // libodbc.SQLFreeHandle((ushort) OdbcHandleType.Stmt, hstmt);
  194. OdbcReturn ret=libodbc.SQLCloseCursor(hstmt);
  195. libodbc.DisplayError("SQLCancel",ret);
  196. open = false;
  197. currentRow = -1;
  198. this.command.Connection.DataReader = null;
  199. }
  200. ~OdbcDataReader ()
  201. {
  202. if (open)
  203. Close ();
  204. }
  205. public bool GetBoolean (int ordinal)
  206. {
  207. throw new NotImplementedException ();
  208. }
  209. public byte GetByte (int ordinal)
  210. {
  211. throw new NotImplementedException ();
  212. }
  213. [MonoTODO]
  214. public long GetBytes (int ordinal, long dataIndex, byte[] buffer, int
  215. bufferIndex, int length)
  216. {
  217. throw new NotImplementedException ();
  218. }
  219. public char GetChar (int ordinal)
  220. {
  221. throw new NotImplementedException ();
  222. }
  223. [MonoTODO]
  224. public long GetChars (int ordinal, long dataIndex, char[] buffer, int
  225. bufferIndex, int length)
  226. {
  227. throw new NotImplementedException ();
  228. }
  229. [MonoTODO]
  230. public OdbcDataReader GetData (int ordinal)
  231. {
  232. throw new NotImplementedException ();
  233. }
  234. public string GetDataTypeName (int index)
  235. {
  236. return "";
  237. }
  238. public DateTime GetDateTime (int ordinal)
  239. {
  240. throw new NotImplementedException ();
  241. }
  242. [MonoTODO]
  243. public decimal GetDecimal (int ordinal)
  244. {
  245. throw new NotImplementedException ();
  246. }
  247. public double GetDouble (int ordinal)
  248. {
  249. throw new NotImplementedException ();
  250. }
  251. [MonoTODO]
  252. public Type GetFieldType (int index)
  253. {
  254. throw new NotImplementedException ();
  255. }
  256. public float GetFloat (int ordinal)
  257. {
  258. throw new NotImplementedException ();
  259. }
  260. [MonoTODO]
  261. public Guid GetGuid (int ordinal)
  262. {
  263. throw new NotImplementedException ();
  264. }
  265. public short GetInt16 (int ordinal)
  266. {
  267. throw new NotImplementedException ();
  268. }
  269. public int GetInt32 (int ordinal)
  270. {
  271. throw new NotImplementedException ();
  272. }
  273. public long GetInt64 (int ordinal)
  274. {
  275. throw new NotImplementedException ();
  276. }
  277. public string GetName (int index)
  278. {
  279. if (currentRow == -1)
  280. return null;
  281. return cols[index].ColumnName;
  282. }
  283. public int GetOrdinal (string name)
  284. {
  285. if (currentRow == -1)
  286. throw new IndexOutOfRangeException ();
  287. int i=ColIndex(name);
  288. if (i==-1)
  289. throw new IndexOutOfRangeException ();
  290. else
  291. return i;
  292. }
  293. public DataTable GetSchemaTable ()
  294. {
  295. DataTable table = new DataTable ();
  296. // FIXME: implement
  297. return table;
  298. }
  299. public string GetString (int ordinal)
  300. {
  301. throw new NotImplementedException ();
  302. }
  303. [MonoTODO]
  304. public TimeSpan GetTimeSpan (int ordinal)
  305. {
  306. throw new NotImplementedException ();
  307. }
  308. public object GetValue (int ordinal)
  309. {
  310. if (currentRow == -1)
  311. throw new IndexOutOfRangeException ();
  312. if (ordinal>cols.Length-1 || ordinal<0)
  313. throw new IndexOutOfRangeException ();
  314. return (object) GetODBCData(ordinal);
  315. }
  316. [MonoTODO]
  317. public int GetValues (object[] values)
  318. {
  319. throw new NotImplementedException ();
  320. }
  321. [MonoTODO]
  322. IDataReader IDataRecord.GetData (int ordinal)
  323. {
  324. throw new NotImplementedException ();
  325. }
  326. [MonoTODO]
  327. void IDisposable.Dispose ()
  328. {
  329. throw new NotImplementedException ();
  330. }
  331. [MonoTODO]
  332. IEnumerator IEnumerable.GetEnumerator ()
  333. {
  334. throw new NotImplementedException ();
  335. }
  336. public bool IsDBNull (int ordinal)
  337. {
  338. throw new NotImplementedException ();
  339. }
  340. public bool NextResult ()
  341. {
  342. OdbcReturn ret=libodbc.SQLFetch(hstmt);
  343. return (ret==OdbcReturn.Success);
  344. }
  345. public bool Read ()
  346. {
  347. return NextResult();
  348. }
  349. #endregion
  350. }
  351. }