OdbcDataReader.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952
  1. //
  2. // System.Data.Odbc.OdbcDataReader
  3. //
  4. // Author:
  5. // Brian Ritchie ([email protected])
  6. // Daniel Morgan <[email protected]>
  7. // Sureshkumar T <[email protected]> (2004)
  8. //
  9. // Copyright (C) Brian Ritchie, 2002
  10. // Copyright (C) Daniel Morgan, 2002
  11. //
  12. //
  13. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  14. //
  15. // Permission is hereby granted, free of charge, to any person obtaining
  16. // a copy of this software and associated documentation files (the
  17. // "Software"), to deal in the Software without restriction, including
  18. // without limitation the rights to use, copy, modify, merge, publish,
  19. // distribute, sublicense, and/or sell copies of the Software, and to
  20. // permit persons to whom the Software is furnished to do so, subject to
  21. // the following conditions:
  22. //
  23. // The above copyright notice and this permission notice shall be
  24. // included in all copies or substantial portions of the Software.
  25. //
  26. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  27. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  28. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  29. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  30. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  31. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  32. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  33. //
  34. using System.Collections;
  35. using System.ComponentModel;
  36. using System.Data;
  37. using System.Data.Common;
  38. #if NET_2_0
  39. using System.Data.ProviderBase;
  40. #endif // NET_2_0
  41. using System.Text;
  42. namespace System.Data.Odbc
  43. {
  44. #if NET_2_0
  45. public sealed class OdbcDataReader : DbDataReaderBase
  46. #else
  47. public sealed class OdbcDataReader : MarshalByRefObject, IDataReader, IDisposable, IDataRecord, IEnumerable
  48. #endif
  49. {
  50. #region Fields
  51. private OdbcCommand command;
  52. private bool open;
  53. private int currentRow;
  54. private OdbcColumn[] cols;
  55. private IntPtr hstmt;
  56. #if ONLY_1_1
  57. private CommandBehavior behavior;
  58. #endif // ONLY_1_1
  59. #endregion
  60. #region Constructors
  61. internal OdbcDataReader (OdbcCommand command, CommandBehavior behavior)
  62. #if NET_2_0
  63. : base (behavior)
  64. #endif // NET_2_0
  65. {
  66. this.command = command;
  67. #if ONLY_1_1
  68. this.CommandBehavior=behavior;
  69. #endif // ONLY_1_1
  70. open = true;
  71. currentRow = -1;
  72. hstmt=command.hStmt;
  73. // Init columns array;
  74. short colcount=0;
  75. libodbc.SQLNumResultCols(hstmt, ref colcount);
  76. cols=new OdbcColumn[colcount];
  77. GetSchemaTable ();
  78. }
  79. #endregion
  80. #region Properties
  81. #if ONLY_1_1
  82. private CommandBehavior CommandBehavior
  83. {
  84. get { return behavior; }
  85. set { value = behavior; }
  86. }
  87. #endif // ONLY_1_1
  88. #if NET_2_0
  89. [MonoTODO]
  90. public override int VisibleFieldCount
  91. {
  92. get { throw new NotImplementedException (); }
  93. }
  94. [MonoTODO]
  95. protected override bool IsValidRow
  96. {
  97. get { throw new NotImplementedException (); }
  98. }
  99. #endif // NET_2_0
  100. public
  101. #if NET_2_0
  102. override
  103. #endif // NET_2_0
  104. int Depth {
  105. get {
  106. return 0; // no nested selects supported
  107. }
  108. }
  109. public
  110. #if NET_2_0
  111. override
  112. #endif // NET_2_0
  113. int FieldCount {
  114. get {
  115. return cols.Length;
  116. }
  117. }
  118. public
  119. #if NET_2_0
  120. override
  121. #endif // NET_2_0
  122. bool IsClosed {
  123. get {
  124. return !open;
  125. }
  126. }
  127. public
  128. #if NET_2_0
  129. override
  130. #endif // NET_2_0
  131. object this[string name] {
  132. get {
  133. int pos;
  134. if (currentRow == -1)
  135. throw new InvalidOperationException ();
  136. pos = ColIndex(name);
  137. if (pos == -1)
  138. throw new IndexOutOfRangeException ();
  139. return this[pos];
  140. }
  141. }
  142. public
  143. #if NET_2_0
  144. override
  145. #endif // NET_2_0
  146. object this[int index] {
  147. get {
  148. return (object) GetValue (index);
  149. }
  150. }
  151. [MonoTODO]
  152. public
  153. #if NET_2_0
  154. override
  155. #endif // NET_2_0
  156. int RecordsAffected {
  157. get {
  158. return -1;
  159. }
  160. }
  161. [MonoTODO]
  162. public
  163. #if NET_2_0
  164. override
  165. #endif // NET_2_0
  166. bool HasRows {
  167. get { throw new NotImplementedException(); }
  168. }
  169. #endregion
  170. #region Methods
  171. private int ColIndex(string colname)
  172. {
  173. int i=0;
  174. foreach (OdbcColumn col in cols)
  175. {
  176. if (col != null && col.ColumnName==colname)
  177. return i;
  178. i++;
  179. }
  180. return -1;
  181. }
  182. // Dynamically load column descriptions as needed.
  183. private OdbcColumn GetColumn(int ordinal)
  184. {
  185. if (cols[ordinal]==null)
  186. {
  187. short bufsize=255;
  188. byte[] colname_buffer=new byte[bufsize];
  189. string colname;
  190. short colname_size=0;
  191. uint ColSize=0;
  192. short DecDigits=0, Nullable=0, dt=0;
  193. OdbcReturn ret=libodbc.SQLDescribeCol(hstmt, Convert.ToUInt16(ordinal+1),
  194. colname_buffer, bufsize, ref colname_size, ref dt, ref ColSize,
  195. ref DecDigits, ref Nullable);
  196. if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
  197. throw new OdbcException(new OdbcError("SQLDescribeCol",OdbcHandleType.Stmt,hstmt));
  198. colname=System.Text.Encoding.Default.GetString(colname_buffer);
  199. colname=colname.Replace((char) 0,' ').Trim();
  200. OdbcColumn c=new OdbcColumn(colname, (SQL_TYPE) dt);
  201. c.AllowDBNull=(Nullable!=0);
  202. c.Digits=DecDigits;
  203. if (c.IsStringType)
  204. c.MaxLength=(int)ColSize;
  205. cols[ordinal]=c;
  206. }
  207. return cols[ordinal];
  208. }
  209. public
  210. #if NET_2_0
  211. override
  212. #endif // NET_2_0
  213. void Close ()
  214. {
  215. // FIXME : have to implement output parameter binding
  216. OdbcReturn ret = libodbc.SQLFreeStmt (hstmt, libodbc.SQLFreeStmtOptions.Close);
  217. if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
  218. throw new OdbcException(new OdbcError("SQLCloseCursor",OdbcHandleType.Stmt,hstmt));
  219. open = false;
  220. currentRow = -1;
  221. ret = libodbc.SQLFreeHandle( (ushort) OdbcHandleType.Stmt, hstmt);
  222. if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
  223. throw new OdbcException(new OdbcError("SQLFreeHandle",OdbcHandleType.Stmt,hstmt));
  224. if ((this.CommandBehavior & CommandBehavior.CloseConnection)==CommandBehavior.CloseConnection)
  225. this.command.Connection.Close();
  226. }
  227. ~OdbcDataReader ()
  228. {
  229. if (open)
  230. Close ();
  231. }
  232. public
  233. #if NET_2_0
  234. override
  235. #endif // NET_2_0
  236. bool GetBoolean (int ordinal)
  237. {
  238. return (bool) GetValue(ordinal);
  239. }
  240. public
  241. #if NET_2_0
  242. override
  243. #endif // NET_2_0
  244. byte GetByte (int ordinal)
  245. {
  246. return (byte) Convert.ToByte(GetValue(ordinal));
  247. }
  248. public
  249. #if NET_2_0
  250. override
  251. #endif // NET_2_0
  252. long GetBytes (int ordinal, long dataIndex, byte[] buffer, int bufferIndex, int length)
  253. {
  254. OdbcReturn ret = OdbcReturn.Error;
  255. bool copyBuffer = false;
  256. int returnVal = 0, outsize = 0;
  257. byte [] tbuff = new byte [length+1];
  258. length = buffer == null ? 0 : length;
  259. ret=libodbc.SQLGetData (hstmt, (ushort) (ordinal+1), SQL_C_TYPE.BINARY, tbuff, length,
  260. ref outsize);
  261. if (ret == OdbcReturn.NoData)
  262. return 0;
  263. if ( (ret != OdbcReturn.Success) && (ret != OdbcReturn.SuccessWithInfo))
  264. throw new OdbcException (new OdbcError ("SQLGetData", OdbcHandleType.Stmt, hstmt));
  265. OdbcError odbcErr = null;
  266. if ( (ret == OdbcReturn.SuccessWithInfo))
  267. odbcErr = new OdbcError ("SQLGetData", OdbcHandleType.Stmt, hstmt);
  268. if (buffer == null)
  269. return outsize; //if buffer is null,return length of the field
  270. if (ret == OdbcReturn.SuccessWithInfo) {
  271. if (outsize == (int) OdbcLengthIndicator.NoTotal)
  272. copyBuffer = true;
  273. else if (outsize == (int) OdbcLengthIndicator.NullData) {
  274. copyBuffer = false;
  275. returnVal = -1;
  276. } else {
  277. string sqlstate = odbcErr.SQLState;
  278. //SQLState: String Data, Right truncated
  279. if (sqlstate != libodbc.SQLSTATE_RIGHT_TRUNC)
  280. throw new OdbcException ( odbcErr);
  281. copyBuffer = true;
  282. }
  283. } else {
  284. copyBuffer = outsize == -1 ? false : true;
  285. returnVal = outsize;
  286. }
  287. if (copyBuffer) {
  288. int i = 0;
  289. while (tbuff [i] != libodbc.C_NULL) {
  290. buffer [bufferIndex + i] = tbuff [i];
  291. i++;
  292. }
  293. returnVal = i;
  294. }
  295. return returnVal;
  296. }
  297. [MonoTODO]
  298. public
  299. #if NET_2_0
  300. override
  301. #endif // NET_2_0
  302. char GetChar (int ordinal)
  303. {
  304. throw new NotImplementedException ();
  305. }
  306. [MonoTODO]
  307. public
  308. #if NET_2_0
  309. override
  310. #endif // NET_2_0
  311. long GetChars (int ordinal, long dataIndex, char[] buffer, int bufferIndex, int length)
  312. {
  313. throw new NotImplementedException ();
  314. }
  315. [MonoTODO]
  316. [EditorBrowsableAttribute (EditorBrowsableState.Never)]
  317. public
  318. #if NET_2_0
  319. new
  320. #endif // NET_2_0
  321. IDataReader GetData (int ordinal)
  322. {
  323. throw new NotImplementedException ();
  324. }
  325. public
  326. #if NET_2_0
  327. override
  328. #endif // NET_2_0
  329. string GetDataTypeName (int index)
  330. {
  331. return GetColumn(index).OdbcType.ToString();
  332. }
  333. public DateTime GetDate(int ordinal) {
  334. return GetDateTime(ordinal);
  335. }
  336. public
  337. #if NET_2_0
  338. override
  339. #endif // NET_2_0
  340. DateTime GetDateTime (int ordinal)
  341. {
  342. return (DateTime) GetValue(ordinal);
  343. }
  344. [MonoTODO]
  345. public
  346. #if NET_2_0
  347. override
  348. #endif // NET_2_0
  349. decimal GetDecimal (int ordinal)
  350. {
  351. throw new NotImplementedException ();
  352. }
  353. public
  354. #if NET_2_0
  355. override
  356. #endif // NET_2_0
  357. double GetDouble (int ordinal)
  358. {
  359. return (double) GetValue(ordinal);
  360. }
  361. public
  362. #if NET_2_0
  363. override
  364. #endif // NET_2_0
  365. Type GetFieldType (int index)
  366. {
  367. return GetColumn(index).DataType;
  368. }
  369. public
  370. #if NET_2_0
  371. override
  372. #endif // NET_2_0
  373. float GetFloat (int ordinal)
  374. {
  375. return (float) GetValue(ordinal);
  376. }
  377. [MonoTODO]
  378. public
  379. #if NET_2_0
  380. override
  381. #endif // NET_2_0
  382. Guid GetGuid (int ordinal)
  383. {
  384. throw new NotImplementedException ();
  385. }
  386. public
  387. #if NET_2_0
  388. override
  389. #endif // NET_2_0
  390. short GetInt16 (int ordinal)
  391. {
  392. return (short) GetValue(ordinal);
  393. }
  394. public
  395. #if NET_2_0
  396. override
  397. #endif // NET_2_0
  398. int GetInt32 (int ordinal)
  399. {
  400. return (int) GetValue(ordinal);
  401. }
  402. public
  403. #if NET_2_0
  404. override
  405. #endif // NET_2_0
  406. long GetInt64 (int ordinal)
  407. {
  408. return (long) GetValue(ordinal);
  409. }
  410. public
  411. #if NET_2_0
  412. override
  413. #endif // NET_2_0
  414. string GetName (int index)
  415. {
  416. return GetColumn(index).ColumnName;
  417. }
  418. public
  419. #if NET_2_0
  420. override
  421. #endif // NET_2_0
  422. int GetOrdinal (string name)
  423. {
  424. int i=ColIndex(name);
  425. if (i==-1)
  426. throw new IndexOutOfRangeException ();
  427. else
  428. return i;
  429. }
  430. [MonoTODO]
  431. public
  432. #if NET_2_0
  433. override
  434. #endif // NET_2_0
  435. DataTable GetSchemaTable()
  436. {
  437. DataTable dataTableSchema = null;
  438. // Only Results from SQL SELECT Queries
  439. // get a DataTable for schema of the result
  440. // otherwise, DataTable is null reference
  441. if(cols.Length > 0)
  442. {
  443. string [] keys = GetPrimaryKeys ();
  444. dataTableSchema = new DataTable ();
  445. dataTableSchema.Columns.Add ("ColumnName", typeof (string));
  446. dataTableSchema.Columns.Add ("ColumnOrdinal", typeof (int));
  447. dataTableSchema.Columns.Add ("ColumnSize", typeof (int));
  448. dataTableSchema.Columns.Add ("NumericPrecision", typeof (int));
  449. dataTableSchema.Columns.Add ("NumericScale", typeof (int));
  450. dataTableSchema.Columns.Add ("IsUnique", typeof (bool));
  451. dataTableSchema.Columns.Add ("IsKey", typeof (bool));
  452. DataColumn dc = dataTableSchema.Columns["IsKey"];
  453. dc.AllowDBNull = true; // IsKey can have a DBNull
  454. dataTableSchema.Columns.Add ("BaseCatalogName", typeof (string));
  455. dataTableSchema.Columns.Add ("BaseColumnName", typeof (string));
  456. dataTableSchema.Columns.Add ("BaseSchemaName", typeof (string));
  457. dataTableSchema.Columns.Add ("BaseTableName", typeof (string));
  458. dataTableSchema.Columns.Add ("DataType", typeof(Type));
  459. dataTableSchema.Columns.Add ("AllowDBNull", typeof (bool));
  460. dataTableSchema.Columns.Add ("ProviderType", typeof (int));
  461. dataTableSchema.Columns.Add ("IsAliased", typeof (bool));
  462. dataTableSchema.Columns.Add ("IsExpression", typeof (bool));
  463. dataTableSchema.Columns.Add ("IsIdentity", typeof (bool));
  464. dataTableSchema.Columns.Add ("IsAutoIncrement", typeof (bool));
  465. dataTableSchema.Columns.Add ("IsRowVersion", typeof (bool));
  466. dataTableSchema.Columns.Add ("IsHidden", typeof (bool));
  467. dataTableSchema.Columns.Add ("IsLong", typeof (bool));
  468. dataTableSchema.Columns.Add ("IsReadOnly", typeof (bool));
  469. DataRow schemaRow;
  470. for (int i = 0; i < cols.Length; i += 1 )
  471. {
  472. OdbcColumn col=GetColumn(i);
  473. schemaRow = dataTableSchema.NewRow ();
  474. dataTableSchema.Rows.Add (schemaRow);
  475. schemaRow["ColumnName"] = col.ColumnName;
  476. schemaRow["ColumnOrdinal"] = i + 1;
  477. schemaRow["ColumnSize"] = col.MaxLength;
  478. schemaRow["NumericPrecision"] = 0;
  479. schemaRow["NumericScale"] = 0;
  480. // TODO: need to get KeyInfo
  481. schemaRow["IsUnique"] = false;
  482. schemaRow["IsKey"] = DBNull.Value;
  483. for (int j=0; j < keys.Length; j++) {
  484. if (keys [j] == col.ColumnName) {
  485. schemaRow ["IsUnique"] = true;
  486. schemaRow ["IsKey"] = true;
  487. }
  488. }
  489. schemaRow["BaseCatalogName"] = "";
  490. schemaRow["BaseColumnName"] = col.ColumnName;
  491. schemaRow["BaseSchemaName"] = "";
  492. schemaRow["BaseTableName"] = "";
  493. schemaRow["DataType"] = col.DataType;
  494. schemaRow["AllowDBNull"] = col.AllowDBNull;
  495. schemaRow["ProviderType"] = (int) col.OdbcType;
  496. // TODO: all of these
  497. schemaRow["IsAliased"] = false;
  498. schemaRow["IsExpression"] = false;
  499. schemaRow["IsIdentity"] = false;
  500. schemaRow["IsAutoIncrement"] = false;
  501. schemaRow["IsRowVersion"] = false;
  502. schemaRow["IsHidden"] = false;
  503. schemaRow["IsLong"] = false;
  504. schemaRow["IsReadOnly"] = false;
  505. // FIXME: according to Brian,
  506. // this does not work on MS .NET
  507. // however, we need it for Mono
  508. // for now
  509. schemaRow.AcceptChanges();
  510. }
  511. dataTableSchema.AcceptChanges();
  512. }
  513. return dataTableSchema;
  514. }
  515. public
  516. #if NET_2_0
  517. override
  518. #endif // NET_2_0
  519. string GetString (int ordinal)
  520. {
  521. return (string) GetValue(ordinal);
  522. }
  523. [MonoTODO]
  524. public TimeSpan GetTime (int ordinal)
  525. {
  526. throw new NotImplementedException ();
  527. }
  528. public
  529. #if NET_2_0
  530. override
  531. #endif // NET_2_0
  532. object GetValue (int ordinal)
  533. {
  534. if (currentRow == -1)
  535. throw new IndexOutOfRangeException ();
  536. if (ordinal>cols.Length-1 || ordinal<0)
  537. throw new IndexOutOfRangeException ();
  538. OdbcReturn ret;
  539. int outsize=0, bufsize;
  540. byte[] buffer;
  541. OdbcColumn col=GetColumn(ordinal);
  542. object DataValue=null;
  543. ushort ColIndex=Convert.ToUInt16(ordinal+1);
  544. // Check cached values
  545. if (col.Value==null)
  546. {
  547. // odbc help file
  548. // mk:@MSITStore:C:\program%20files\Microsoft%20Data%20Access%20SDK\Docs\odbc.chm::/htm/odbcc_data_types.htm
  549. switch (col.OdbcType)
  550. {
  551. case OdbcType.Decimal:
  552. bufsize=50;
  553. buffer=new byte[bufsize]; // According to sqlext.h, use SQL_CHAR for decima.
  554. // 2005 03 10 : this now works with unixodbc with numeric c type.
  555. ret=libodbc.SQLGetData(hstmt, ColIndex, col.SqlCType, buffer, bufsize, ref outsize);
  556. byte[] temp = new byte[outsize];
  557. for (int i=0;i<outsize;i++)
  558. temp[i]=buffer[i];
  559. if (outsize!=-1)
  560. DataValue=Decimal.Parse(System.Text.Encoding.Default.GetString(temp));
  561. break;
  562. case OdbcType.TinyInt:
  563. short short_data=0;
  564. ret=libodbc.SQLGetData(hstmt, ColIndex, col.SqlCType, ref short_data, 0, ref outsize);
  565. DataValue=System.Convert.ToByte(short_data);
  566. break;
  567. case OdbcType.Int:
  568. int int_data=0;
  569. ret=libodbc.SQLGetData(hstmt, ColIndex, col.SqlCType, ref int_data, 0, ref outsize);
  570. DataValue=int_data;
  571. break;
  572. case OdbcType.SmallInt:
  573. short sint_data=0;
  574. ret=libodbc.SQLGetData(hstmt, ColIndex, col.SqlCType, ref sint_data, 0, ref outsize);
  575. DataValue=sint_data;
  576. break;
  577. case OdbcType.BigInt:
  578. long long_data=0;
  579. ret=libodbc.SQLGetData(hstmt, ColIndex, col.SqlCType, ref long_data, 0, ref outsize);
  580. DataValue=long_data;
  581. break;
  582. case OdbcType.NVarChar:
  583. bufsize=col.MaxLength*2+1; // Unicode is double byte
  584. buffer=new byte[bufsize];
  585. ret=libodbc.SQLGetData(hstmt, ColIndex, col.SqlCType, buffer, bufsize, ref outsize);
  586. if (outsize!=-1)
  587. DataValue=System.Text.Encoding.Unicode.GetString(buffer,0,outsize);
  588. break;
  589. case OdbcType.VarChar:
  590. bufsize=col.MaxLength+1;
  591. buffer=new byte[bufsize]; // According to sqlext.h, use SQL_CHAR for both char and varchar
  592. ret=libodbc.SQLGetData(hstmt, ColIndex, col.SqlCType, buffer, bufsize, ref outsize);
  593. if (outsize!=-1)
  594. DataValue=System.Text.Encoding.Default.GetString(buffer,0,outsize);
  595. break;
  596. case OdbcType.Real:
  597. float float_data=0;
  598. ret=libodbc.SQLGetData(hstmt, ColIndex, col.SqlCType, ref float_data, 0, ref outsize);
  599. DataValue=float_data;
  600. break;
  601. case OdbcType.Double:
  602. double double_data=0;
  603. ret=libodbc.SQLGetData(hstmt, ColIndex, col.SqlCType, ref double_data, 0, ref outsize);
  604. DataValue=double_data;
  605. break;
  606. case OdbcType.Timestamp:
  607. case OdbcType.DateTime:
  608. case OdbcType.Date:
  609. case OdbcType.Time:
  610. OdbcTimestamp ts_data=new OdbcTimestamp();
  611. ret=libodbc.SQLGetData(hstmt, ColIndex, col.SqlCType, ref ts_data, 0, ref outsize);
  612. if (outsize!=-1) // This means SQL_NULL_DATA
  613. DataValue=new DateTime(ts_data.year,ts_data.month,ts_data.day,ts_data.hour,
  614. ts_data.minute,ts_data.second,Convert.ToInt32(ts_data.fraction));
  615. break;
  616. case OdbcType.Binary :
  617. case OdbcType.Image :
  618. bufsize = col.MaxLength + 1;
  619. buffer = new byte [bufsize];
  620. long read = GetBytes (ordinal, 0, buffer, 0, bufsize);
  621. ret = OdbcReturn.Success;
  622. DataValue = buffer;
  623. break;
  624. default:
  625. bufsize=255;
  626. buffer=new byte[bufsize];
  627. ret=libodbc.SQLGetData(hstmt, ColIndex, SQL_C_TYPE.CHAR, buffer, bufsize, ref outsize);
  628. DataValue=System.Text.Encoding.Default.GetString(buffer);
  629. break;
  630. }
  631. if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
  632. throw new OdbcException(new OdbcError("SQLGetData",OdbcHandleType.Stmt,hstmt));
  633. if (outsize==-1) // This means SQL_NULL_DATA
  634. col.Value=DBNull.Value;
  635. else
  636. col.Value=DataValue;
  637. }
  638. return col.Value;
  639. }
  640. public
  641. #if NET_2_0
  642. override
  643. #endif // NET_2_0
  644. int GetValues (object[] values)
  645. {
  646. int numValues = 0;
  647. // copy values
  648. for (int i = 0; i < values.Length; i++) {
  649. if (i < FieldCount) {
  650. values[i] = GetValue(i);
  651. }
  652. else {
  653. values[i] = null;
  654. }
  655. }
  656. // get number of object instances in array
  657. if (values.Length < FieldCount)
  658. numValues = values.Length;
  659. else if (values.Length == FieldCount)
  660. numValues = FieldCount;
  661. else
  662. numValues = FieldCount;
  663. return numValues;
  664. }
  665. #if ONLY_1_1
  666. [MonoTODO]
  667. IDataReader IDataRecord.GetData (int ordinal)
  668. {
  669. throw new NotImplementedException ();
  670. }
  671. [MonoTODO]
  672. void IDisposable.Dispose ()
  673. {
  674. }
  675. IEnumerator IEnumerable.GetEnumerator ()
  676. {
  677. return new DbEnumerator (this);
  678. }
  679. #endif // ONLY_1_1
  680. public
  681. #if NET_2_0
  682. override
  683. #endif // NET_2_0
  684. bool IsDBNull (int ordinal)
  685. {
  686. return (GetValue(ordinal) is DBNull);
  687. }
  688. /// <remarks>
  689. /// Move to the next result set.
  690. /// </remarks>
  691. public
  692. #if NET_2_0
  693. override
  694. #endif // NET_2_0
  695. bool NextResult ()
  696. {
  697. OdbcReturn ret = OdbcReturn.Success;
  698. ret = libodbc.SQLMoreResults (hstmt);
  699. if (ret == OdbcReturn.Success) {
  700. short colcount = 0;
  701. libodbc.SQLNumResultCols (hstmt, ref colcount);
  702. cols = new OdbcColumn [colcount];
  703. GetSchemaTable ();
  704. }
  705. return (ret==OdbcReturn.Success);
  706. }
  707. /// <remarks>
  708. /// Load the next row in the current result set.
  709. /// </remarks>
  710. public bool NextRow ()
  711. {
  712. OdbcReturn ret=libodbc.SQLFetch (hstmt);
  713. if (ret != OdbcReturn.Success)
  714. currentRow = -1;
  715. else
  716. currentRow++;
  717. // Clear cached values from last record
  718. foreach (OdbcColumn col in cols)
  719. {
  720. if (col != null)
  721. col.Value = null;
  722. }
  723. return (ret == OdbcReturn.Success);
  724. }
  725. private int GetColumnAttribute (int column, FieldIdentifier fieldId)
  726. {
  727. OdbcReturn ret = OdbcReturn.Error;
  728. byte [] buffer = new byte [255];
  729. int outsize = 0;
  730. int val = 0;
  731. ret = libodbc.SQLColAttribute (hstmt, column, fieldId,
  732. buffer, buffer.Length,
  733. ref outsize, ref val);
  734. if (ret != OdbcReturn.Success && ret != OdbcReturn.SuccessWithInfo)
  735. throw new OdbcException (new OdbcError ("SQLColAttribute",
  736. OdbcHandleType.Stmt,
  737. hstmt)
  738. );
  739. return val;
  740. }
  741. private string GetColumnAttributeStr (int column, FieldIdentifier fieldId)
  742. {
  743. OdbcReturn ret = OdbcReturn.Error;
  744. byte [] buffer = new byte [255];
  745. int outsize = 0;
  746. int val = 0;
  747. ret = libodbc.SQLColAttribute (hstmt, column, fieldId,
  748. buffer, buffer.Length,
  749. ref outsize, ref val);
  750. if (ret != OdbcReturn.Success && ret != OdbcReturn.SuccessWithInfo)
  751. throw new OdbcException (new OdbcError ("SQLColAttribute",
  752. OdbcHandleType.Stmt,
  753. hstmt)
  754. );
  755. string value = Encoding.Default.GetString (buffer);
  756. return value;
  757. }
  758. private string [] GetPrimaryKeys ()
  759. {
  760. if (cols.Length <= 0)
  761. return new string [0];
  762. string [] keys = new string [cols.Length];
  763. IntPtr handle = IntPtr.Zero;
  764. OdbcReturn ret = OdbcReturn.Error;
  765. try {
  766. ret=libodbc.SQLAllocHandle(OdbcHandleType.Stmt,
  767. command.Connection.hDbc, ref handle);
  768. if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
  769. throw new OdbcException(new OdbcError("SQLAllocHandle",
  770. OdbcHandleType.Dbc,
  771. command.Connection.hDbc));
  772. string tableName = GetColumnAttributeStr (1, FieldIdentifier.TableName);
  773. string schemaName = GetColumnAttributeStr (1, FieldIdentifier.SchemaName);
  774. string catalogName = GetColumnAttributeStr (1, FieldIdentifier.CatelogName);
  775. ret = libodbc.SQLPrimaryKeys (handle, catalogName, -3,
  776. schemaName, -3,
  777. tableName, -3);
  778. if (ret != OdbcReturn.Success && ret != OdbcReturn.SuccessWithInfo)
  779. throw new OdbcException (new OdbcError ("SQLPrimaryKeys", OdbcHandleType.Stmt, handle));
  780. int length = 0;
  781. byte [] primaryKey = new byte [255];
  782. ret = libodbc.SQLBindCol (handle, 4, SQL_C_TYPE.CHAR, primaryKey, primaryKey.Length, ref length);
  783. if (ret != OdbcReturn.Success && ret != OdbcReturn.SuccessWithInfo)
  784. throw new OdbcException (new OdbcError ("SQLBindCol", OdbcHandleType.Stmt, handle));
  785. int i = 0;
  786. while (true) {
  787. ret = libodbc.SQLFetch (handle);
  788. if (ret != OdbcReturn.Success && ret != OdbcReturn.SuccessWithInfo)
  789. break;
  790. string pkey = Encoding.Default.GetString (primaryKey);
  791. keys [i++] = pkey;
  792. }
  793. } catch (OdbcException){
  794. // FIXME: Try using SQLStatistics
  795. } finally {
  796. if (handle != IntPtr.Zero) {
  797. ret = libodbc.SQLFreeStmt (handle, libodbc.SQLFreeStmtOptions.Close);
  798. if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
  799. throw new OdbcException(new OdbcError("SQLFreeStmt",OdbcHandleType.Stmt,handle));
  800. ret = libodbc.SQLFreeHandle( (ushort) OdbcHandleType.Stmt, handle);
  801. if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
  802. throw new OdbcException(new OdbcError("SQLFreeHandle",OdbcHandleType.Stmt,handle));
  803. }
  804. }
  805. return keys;
  806. }
  807. public
  808. #if NET_2_0
  809. override
  810. #endif // NET_2_0
  811. bool Read ()
  812. {
  813. return NextRow ();
  814. }
  815. #if NET_2_0
  816. [MonoTODO]
  817. public override object GetProviderSpecificValue (int i)
  818. {
  819. throw new NotImplementedException ();
  820. }
  821. [MonoTODO]
  822. public override int GetProviderSpecificValues (object[] values)
  823. {
  824. throw new NotImplementedException ();
  825. }
  826. [MonoTODO]
  827. public override Type GetFieldProviderSpecificType (int i)
  828. {
  829. throw new NotImplementedException ();
  830. }
  831. #endif // NET_2_0
  832. #endregion
  833. }
  834. }