OdbcDataReader.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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, IDisposable, IDataRecord, IEnumerable
  17. {
  18. #region Fields
  19. private OdbcCommand command;
  20. private bool open;
  21. private int currentRow;
  22. private OdbcColumn[] cols;
  23. private IntPtr hstmt;
  24. #endregion
  25. #region Constructors
  26. internal OdbcDataReader (OdbcCommand command)
  27. {
  28. this.command = command;
  29. this.command.Connection.DataReader = this;
  30. open = true;
  31. currentRow = -1;
  32. hstmt=command.hStmt;
  33. // Init columns array;
  34. short colcount=0;
  35. libodbc.SQLNumResultCols(hstmt, ref colcount);
  36. cols=new OdbcColumn[colcount];
  37. }
  38. #endregion
  39. #region Properties
  40. public int Depth {
  41. get {
  42. return 0; // no nested selects supported
  43. }
  44. }
  45. public int FieldCount {
  46. get {
  47. return cols.Length;
  48. }
  49. }
  50. public bool IsClosed {
  51. get {
  52. return !open;
  53. }
  54. }
  55. public object this[string name] {
  56. get {
  57. int pos;
  58. if (currentRow == -1)
  59. throw new InvalidOperationException ();
  60. pos = ColIndex(name);
  61. if (pos == -1)
  62. throw new IndexOutOfRangeException ();
  63. return this[pos];
  64. }
  65. }
  66. public object this[int index] {
  67. get {
  68. return (object) GetValue (index);
  69. }
  70. }
  71. public int RecordsAffected {
  72. get {
  73. return -1;
  74. }
  75. }
  76. #endregion
  77. #region Methods
  78. private int ColIndex(string colname)
  79. {
  80. int i=0;
  81. foreach (OdbcColumn col in cols)
  82. {
  83. if (col.ColumnName==colname)
  84. return i;
  85. i++;
  86. }
  87. return 0;
  88. }
  89. // Dynamically load column descriptions as needed.
  90. private OdbcColumn GetColumn(int ordinal)
  91. {
  92. if (cols[ordinal]==null)
  93. {
  94. short bufsize=255;
  95. byte[] colname_buffer=new byte[bufsize];
  96. string colname;
  97. short colname_size=0;
  98. OdbcType DataType=OdbcType.Int;
  99. short ColSize=0, DecDigits=0, Nullable=0;
  100. libodbc.SQLDescribeCol(hstmt, Convert.ToUInt16(ordinal+1),
  101. colname_buffer, bufsize, ref colname_size, ref DataType, ref ColSize,
  102. ref DecDigits, ref Nullable);
  103. colname=System.Text.Encoding.Default.GetString(colname_buffer);
  104. colname=colname.Replace((char) 0,' ').Trim();
  105. OdbcColumn c=new OdbcColumn(colname, DataType);
  106. c.AllowDBNull=(Nullable!=0);
  107. c.Digits=DecDigits;
  108. if (c.IsStringType)
  109. c.MaxLength=ColSize;
  110. cols[ordinal]=c;
  111. }
  112. return cols[ordinal];
  113. }
  114. public void Close ()
  115. {
  116. // libodbc.SQLFreeHandle((ushort) OdbcHandleType.Stmt, hstmt);
  117. OdbcReturn ret=libodbc.SQLCloseCursor(hstmt);
  118. libodbchelper.DisplayError("SQLCancel",ret);
  119. open = false;
  120. currentRow = -1;
  121. this.command.Connection.DataReader = null;
  122. }
  123. ~OdbcDataReader ()
  124. {
  125. if (open)
  126. Close ();
  127. }
  128. public bool GetBoolean (int ordinal)
  129. {
  130. return (bool) GetValue(ordinal);
  131. }
  132. [MonoTODO]
  133. public byte GetByte (int ordinal)
  134. {
  135. throw new NotImplementedException ();
  136. }
  137. [MonoTODO]
  138. public long GetBytes (int ordinal, long dataIndex, byte[] buffer, int bufferIndex, int length)
  139. {
  140. throw new NotImplementedException ();
  141. }
  142. [MonoTODO]
  143. public char GetChar (int ordinal)
  144. {
  145. throw new NotImplementedException ();
  146. }
  147. [MonoTODO]
  148. public long GetChars (int ordinal, long dataIndex, char[] buffer, int bufferIndex, int length)
  149. {
  150. throw new NotImplementedException ();
  151. }
  152. [MonoTODO]
  153. public OdbcDataReader GetData (int ordinal)
  154. {
  155. throw new NotImplementedException ();
  156. }
  157. public string GetDataTypeName (int index)
  158. {
  159. return GetColumn(index).OdbcType.ToString();
  160. }
  161. public DateTime GetDateTime (int ordinal)
  162. {
  163. return (DateTime) GetValue(ordinal);
  164. }
  165. [MonoTODO]
  166. public decimal GetDecimal (int ordinal)
  167. {
  168. throw new NotImplementedException ();
  169. }
  170. public double GetDouble (int ordinal)
  171. {
  172. return (double) GetValue(ordinal);
  173. }
  174. public Type GetFieldType (int index)
  175. {
  176. return GetColumn(index).DataType;
  177. }
  178. public float GetFloat (int ordinal)
  179. {
  180. return (float) GetValue(ordinal);
  181. }
  182. [MonoTODO]
  183. public Guid GetGuid (int ordinal)
  184. {
  185. throw new NotImplementedException ();
  186. }
  187. public short GetInt16 (int ordinal)
  188. {
  189. return (short) GetValue(ordinal);
  190. }
  191. public int GetInt32 (int ordinal)
  192. {
  193. return (int) GetValue(ordinal);
  194. }
  195. public long GetInt64 (int ordinal)
  196. {
  197. return (long) GetValue(ordinal);
  198. }
  199. public string GetName (int index)
  200. {
  201. if (currentRow == -1)
  202. return null;
  203. return GetColumn(index).ColumnName;
  204. }
  205. public int GetOrdinal (string name)
  206. {
  207. if (currentRow == -1)
  208. throw new IndexOutOfRangeException ();
  209. int i=ColIndex(name);
  210. if (i==-1)
  211. throw new IndexOutOfRangeException ();
  212. else
  213. return i;
  214. }
  215. [MonoTODO]
  216. public DataTable GetSchemaTable()
  217. {
  218. DataTable dataTableSchema = null;
  219. // Only Results from SQL SELECT Queries
  220. // get a DataTable for schema of the result
  221. // otherwise, DataTable is null reference
  222. if(cols.Length > 0)
  223. {
  224. dataTableSchema = new DataTable ();
  225. dataTableSchema.Columns.Add ("ColumnName", typeof (string));
  226. dataTableSchema.Columns.Add ("ColumnOrdinal", typeof (int));
  227. dataTableSchema.Columns.Add ("ColumnSize", typeof (int));
  228. dataTableSchema.Columns.Add ("NumericPrecision", typeof (int));
  229. dataTableSchema.Columns.Add ("NumericScale", typeof (int));
  230. dataTableSchema.Columns.Add ("IsUnique", typeof (bool));
  231. dataTableSchema.Columns.Add ("IsKey", typeof (bool));
  232. DataColumn dc = dataTableSchema.Columns["IsKey"];
  233. dc.AllowDBNull = true; // IsKey can have a DBNull
  234. dataTableSchema.Columns.Add ("BaseCatalogName", typeof (string));
  235. dataTableSchema.Columns.Add ("BaseColumnName", typeof (string));
  236. dataTableSchema.Columns.Add ("BaseSchemaName", typeof (string));
  237. dataTableSchema.Columns.Add ("BaseTableName", typeof (string));
  238. dataTableSchema.Columns.Add ("DataType", typeof(Type));
  239. dataTableSchema.Columns.Add ("AllowDBNull", typeof (bool));
  240. dataTableSchema.Columns.Add ("ProviderType", typeof (int));
  241. dataTableSchema.Columns.Add ("IsAliased", typeof (bool));
  242. dataTableSchema.Columns.Add ("IsExpression", typeof (bool));
  243. dataTableSchema.Columns.Add ("IsIdentity", typeof (bool));
  244. dataTableSchema.Columns.Add ("IsAutoIncrement", typeof (bool));
  245. dataTableSchema.Columns.Add ("IsRowVersion", typeof (bool));
  246. dataTableSchema.Columns.Add ("IsHidden", typeof (bool));
  247. dataTableSchema.Columns.Add ("IsLong", typeof (bool));
  248. dataTableSchema.Columns.Add ("IsReadOnly", typeof (bool));
  249. DataRow schemaRow;
  250. for (int i = 0; i < cols.Length; i += 1 )
  251. {
  252. OdbcColumn col=GetColumn(i);
  253. schemaRow = dataTableSchema.NewRow ();
  254. dataTableSchema.Rows.Add (schemaRow);
  255. schemaRow["ColumnName"] = col.ColumnName;
  256. schemaRow["ColumnOrdinal"] = i + 1;
  257. schemaRow["ColumnSize"] = col.MaxLength;
  258. schemaRow["NumericPrecision"] = 0;
  259. schemaRow["NumericScale"] = 0;
  260. // TODO: need to get KeyInfo
  261. schemaRow["IsUnique"] = false;
  262. schemaRow["IsKey"] = DBNull.Value;
  263. schemaRow["BaseCatalogName"] = "";
  264. schemaRow["BaseColumnName"] = col.ColumnName;
  265. schemaRow["BaseSchemaName"] = "";
  266. schemaRow["BaseTableName"] = "";
  267. schemaRow["DataType"] = col.DataType;
  268. schemaRow["AllowDBNull"] = col.AllowDBNull;
  269. schemaRow["ProviderType"] = (int) col.OdbcType;
  270. // TODO: all of these
  271. schemaRow["IsAliased"] = false;
  272. schemaRow["IsExpression"] = false;
  273. schemaRow["IsIdentity"] = false;
  274. schemaRow["IsAutoIncrement"] = false;
  275. schemaRow["IsRowVersion"] = false;
  276. schemaRow["IsHidden"] = false;
  277. schemaRow["IsLong"] = false;
  278. schemaRow["IsReadOnly"] = false;
  279. schemaRow.AcceptChanges();
  280. }
  281. }
  282. return dataTableSchema;
  283. }
  284. public string GetString (int ordinal)
  285. {
  286. return (string) GetValue(ordinal);
  287. }
  288. [MonoTODO]
  289. public TimeSpan GetTimeSpan (int ordinal)
  290. {
  291. throw new NotImplementedException ();
  292. }
  293. public object GetValue (int ordinal)
  294. {
  295. if (currentRow == -1)
  296. throw new IndexOutOfRangeException ();
  297. if (ordinal>cols.Length-1 || ordinal<0)
  298. throw new IndexOutOfRangeException ();
  299. OdbcReturn ret;
  300. int outsize=0, bufsize;
  301. byte[] buffer;
  302. OdbcColumn col=GetColumn(ordinal);
  303. object DataValue=null;
  304. ushort ColIndex=Convert.ToUInt16(ordinal+1);
  305. // Check cached values
  306. if (col.Value==null)
  307. {
  308. // odbc help file
  309. // mk:@MSITStore:C:\program%20files\Microsoft%20Data%20Access%20SDK\Docs\odbc.chm::/htm/odbcc_data_types.htm
  310. switch (col.OdbcType)
  311. {
  312. case OdbcType.Decimal:
  313. bufsize=50;
  314. buffer=new byte[bufsize]; // According to sqlext.h, use SQL_CHAR for decimal
  315. ret=libodbc.SQLGetData(hstmt, ColIndex, OdbcType.Char, buffer, bufsize, ref outsize);
  316. if (outsize!=-1)
  317. DataValue=Decimal.Parse(System.Text.Encoding.Default.GetString(buffer));
  318. break;
  319. case OdbcType.TinyInt:
  320. short short_data=0;
  321. ret=libodbc.SQLGetData(hstmt, ColIndex, OdbcType.TinyInt, ref short_data, 0, ref outsize);
  322. DataValue=short_data;
  323. break;
  324. case OdbcType.Int:
  325. int int_data=0;
  326. ret=libodbc.SQLGetData(hstmt, ColIndex, OdbcType.Int, ref int_data, 0, ref outsize);
  327. DataValue=int_data;
  328. break;
  329. case OdbcType.BigInt:
  330. long long_data=0;
  331. ret=libodbc.SQLGetData(hstmt, ColIndex, OdbcType.BigInt, ref long_data, 0, ref outsize);
  332. DataValue=long_data;
  333. break;
  334. case OdbcType.NVarChar:
  335. bufsize=col.MaxLength*2+1; // Unicode is double byte
  336. buffer=new byte[bufsize];
  337. ret=libodbc.SQLGetData(hstmt, ColIndex, OdbcType.NVarChar, buffer, bufsize, ref outsize);
  338. if (outsize!=-1)
  339. DataValue=System.Text.Encoding.Unicode.GetString(buffer,0,outsize);
  340. break;
  341. case OdbcType.VarChar:
  342. bufsize=col.MaxLength+1;
  343. buffer=new byte[bufsize]; // According to sqlext.h, use SQL_CHAR for both char and varchar
  344. ret=libodbc.SQLGetData(hstmt, ColIndex, OdbcType.Char, buffer, bufsize, ref outsize);
  345. if (outsize!=-1)
  346. DataValue=System.Text.Encoding.Default.GetString(buffer,0,outsize);
  347. break;
  348. case OdbcType.Real:
  349. float float_data=0;
  350. ret=libodbc.SQLGetData(hstmt, ColIndex, OdbcType.Real, ref float_data, 0, ref outsize);
  351. DataValue=float_data;
  352. break;
  353. case OdbcType.Timestamp:
  354. case OdbcType.DateTime:
  355. OdbcTimestamp ts_data=new OdbcTimestamp();
  356. ret=libodbc.SQLGetData(hstmt, ColIndex, OdbcType.DateTime, ref ts_data, 0, ref outsize);
  357. if (outsize!=-1) // This means SQL_NULL_DATA
  358. DataValue=new DateTime(ts_data.year,ts_data.month,ts_data.day,ts_data.hour,
  359. ts_data.minute,ts_data.second,Convert.ToInt32(ts_data.fraction));
  360. break;
  361. default:
  362. //Console.WriteLine("Fetching unsupported data type as string: "+col.OdbcType.ToString());
  363. bufsize=255;
  364. buffer=new byte[bufsize];
  365. ret=libodbc.SQLGetData(hstmt, ColIndex, OdbcType.Char, buffer, bufsize, ref outsize);
  366. DataValue=System.Text.Encoding.Default.GetString(buffer);
  367. break;
  368. }
  369. if (outsize==-1) // This means SQL_NULL_DATA
  370. col.Value=DBNull.Value;
  371. else
  372. {
  373. libodbchelper.DisplayError("SQLGetData("+col.OdbcType.ToString()+")",ret);
  374. col.Value=DataValue;
  375. }
  376. }
  377. return col.Value;
  378. }
  379. [MonoTODO]
  380. public int GetValues (object[] values)
  381. {
  382. throw new NotImplementedException ();
  383. }
  384. [MonoTODO]
  385. IDataReader IDataRecord.GetData (int ordinal)
  386. {
  387. throw new NotImplementedException ();
  388. }
  389. [MonoTODO]
  390. void IDisposable.Dispose ()
  391. {
  392. }
  393. [MonoTODO]
  394. IEnumerator IEnumerable.GetEnumerator ()
  395. {
  396. throw new NotImplementedException ();
  397. }
  398. public bool IsDBNull (int ordinal)
  399. {
  400. return (GetValue(ordinal) is DBNull);
  401. }
  402. public bool NextResult ()
  403. {
  404. OdbcReturn ret=libodbc.SQLFetch(hstmt);
  405. if (ret!=OdbcReturn.Success)
  406. currentRow=-1;
  407. else
  408. currentRow++;
  409. // Clear cached values from last record
  410. foreach (OdbcColumn col in cols)
  411. {
  412. if (col!=null)
  413. col.Value=null;
  414. }
  415. return (ret==OdbcReturn.Success);
  416. }
  417. public bool Read ()
  418. {
  419. return NextResult();
  420. }
  421. #endregion
  422. }
  423. }