OdbcDataReader.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976
  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) {
  177. if (col.ColumnName == colname)
  178. return i;
  179. if (String.Compare (col.ColumnName, colname, true) == 0)
  180. return i;
  181. }
  182. i++;
  183. }
  184. return -1;
  185. }
  186. // Dynamically load column descriptions as needed.
  187. private OdbcColumn GetColumn(int ordinal)
  188. {
  189. if (cols[ordinal]==null)
  190. {
  191. short bufsize=255;
  192. byte[] colname_buffer=new byte[bufsize];
  193. string colname;
  194. short colname_size=0;
  195. uint ColSize=0;
  196. short DecDigits=0, Nullable=0, dt=0;
  197. OdbcReturn ret=libodbc.SQLDescribeCol(hstmt, Convert.ToUInt16(ordinal+1),
  198. colname_buffer, bufsize, ref colname_size, ref dt, ref ColSize,
  199. ref DecDigits, ref Nullable);
  200. if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
  201. throw new OdbcException(new OdbcError("SQLDescribeCol",OdbcHandleType.Stmt,hstmt));
  202. colname=System.Text.Encoding.Default.GetString(colname_buffer);
  203. colname=colname.Replace((char) 0,' ').Trim();
  204. OdbcColumn c=new OdbcColumn(colname, (SQL_TYPE) dt);
  205. c.AllowDBNull=(Nullable!=0);
  206. c.Digits=DecDigits;
  207. if (c.IsStringType)
  208. c.MaxLength=(int)ColSize;
  209. cols[ordinal]=c;
  210. }
  211. return cols[ordinal];
  212. }
  213. public
  214. #if NET_2_0
  215. override
  216. #endif // NET_2_0
  217. void Close ()
  218. {
  219. // FIXME : have to implement output parameter binding
  220. OdbcReturn ret = libodbc.SQLFreeStmt (hstmt, libodbc.SQLFreeStmtOptions.Close);
  221. if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
  222. throw new OdbcException(new OdbcError("SQLCloseCursor",OdbcHandleType.Stmt,hstmt));
  223. open = false;
  224. currentRow = -1;
  225. ret = libodbc.SQLFreeHandle( (ushort) OdbcHandleType.Stmt, hstmt);
  226. if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
  227. throw new OdbcException(new OdbcError("SQLFreeHandle",OdbcHandleType.Stmt,hstmt));
  228. if ((this.CommandBehavior & CommandBehavior.CloseConnection)==CommandBehavior.CloseConnection)
  229. this.command.Connection.Close();
  230. }
  231. ~OdbcDataReader ()
  232. {
  233. if (open)
  234. Close ();
  235. }
  236. public
  237. #if NET_2_0
  238. override
  239. #endif // NET_2_0
  240. bool GetBoolean (int ordinal)
  241. {
  242. return (bool) GetValue(ordinal);
  243. }
  244. public
  245. #if NET_2_0
  246. override
  247. #endif // NET_2_0
  248. byte GetByte (int ordinal)
  249. {
  250. return (byte) Convert.ToByte(GetValue(ordinal));
  251. }
  252. public
  253. #if NET_2_0
  254. override
  255. #endif // NET_2_0
  256. long GetBytes (int ordinal, long dataIndex, byte[] buffer, int bufferIndex, int length)
  257. {
  258. OdbcReturn ret = OdbcReturn.Error;
  259. bool copyBuffer = false;
  260. int returnVal = 0, outsize = 0;
  261. byte [] tbuff = new byte [length+1];
  262. length = buffer == null ? 0 : length;
  263. ret=libodbc.SQLGetData (hstmt, (ushort) (ordinal+1), SQL_C_TYPE.BINARY, tbuff, length,
  264. ref outsize);
  265. if (ret == OdbcReturn.NoData)
  266. return 0;
  267. if ( (ret != OdbcReturn.Success) && (ret != OdbcReturn.SuccessWithInfo))
  268. throw new OdbcException (new OdbcError ("SQLGetData", OdbcHandleType.Stmt, hstmt));
  269. OdbcError odbcErr = null;
  270. if ( (ret == OdbcReturn.SuccessWithInfo))
  271. odbcErr = new OdbcError ("SQLGetData", OdbcHandleType.Stmt, hstmt);
  272. if (buffer == null)
  273. return outsize; //if buffer is null,return length of the field
  274. if (ret == OdbcReturn.SuccessWithInfo) {
  275. if (outsize == (int) OdbcLengthIndicator.NoTotal)
  276. copyBuffer = true;
  277. else if (outsize == (int) OdbcLengthIndicator.NullData) {
  278. copyBuffer = false;
  279. returnVal = -1;
  280. } else {
  281. string sqlstate = odbcErr.SQLState;
  282. //SQLState: String Data, Right truncated
  283. if (sqlstate != libodbc.SQLSTATE_RIGHT_TRUNC)
  284. throw new OdbcException ( odbcErr);
  285. copyBuffer = true;
  286. }
  287. } else {
  288. copyBuffer = outsize == -1 ? false : true;
  289. returnVal = outsize;
  290. }
  291. if (copyBuffer) {
  292. int i = 0;
  293. while (tbuff [i] != libodbc.C_NULL) {
  294. buffer [bufferIndex + i] = tbuff [i];
  295. i++;
  296. }
  297. returnVal = i;
  298. }
  299. return returnVal;
  300. }
  301. [MonoTODO]
  302. public
  303. #if NET_2_0
  304. override
  305. #endif // NET_2_0
  306. char GetChar (int ordinal)
  307. {
  308. throw new NotImplementedException ();
  309. }
  310. [MonoTODO]
  311. public
  312. #if NET_2_0
  313. override
  314. #endif // NET_2_0
  315. long GetChars (int ordinal, long dataIndex, char[] buffer, int bufferIndex, int length)
  316. {
  317. throw new NotImplementedException ();
  318. }
  319. [MonoTODO]
  320. [EditorBrowsableAttribute (EditorBrowsableState.Never)]
  321. public
  322. #if NET_2_0
  323. new
  324. #endif // NET_2_0
  325. IDataReader GetData (int ordinal)
  326. {
  327. throw new NotImplementedException ();
  328. }
  329. public
  330. #if NET_2_0
  331. override
  332. #endif // NET_2_0
  333. string GetDataTypeName (int index)
  334. {
  335. return GetColumn(index).OdbcType.ToString();
  336. }
  337. public DateTime GetDate(int ordinal) {
  338. return GetDateTime(ordinal);
  339. }
  340. public
  341. #if NET_2_0
  342. override
  343. #endif // NET_2_0
  344. DateTime GetDateTime (int ordinal)
  345. {
  346. return (DateTime) GetValue(ordinal);
  347. }
  348. [MonoTODO]
  349. public
  350. #if NET_2_0
  351. override
  352. #endif // NET_2_0
  353. decimal GetDecimal (int ordinal)
  354. {
  355. throw new NotImplementedException ();
  356. }
  357. public
  358. #if NET_2_0
  359. override
  360. #endif // NET_2_0
  361. double GetDouble (int ordinal)
  362. {
  363. return (double) GetValue(ordinal);
  364. }
  365. public
  366. #if NET_2_0
  367. override
  368. #endif // NET_2_0
  369. Type GetFieldType (int index)
  370. {
  371. return GetColumn(index).DataType;
  372. }
  373. public
  374. #if NET_2_0
  375. override
  376. #endif // NET_2_0
  377. float GetFloat (int ordinal)
  378. {
  379. return (float) GetValue(ordinal);
  380. }
  381. [MonoTODO]
  382. public
  383. #if NET_2_0
  384. override
  385. #endif // NET_2_0
  386. Guid GetGuid (int ordinal)
  387. {
  388. throw new NotImplementedException ();
  389. }
  390. public
  391. #if NET_2_0
  392. override
  393. #endif // NET_2_0
  394. short GetInt16 (int ordinal)
  395. {
  396. return (short) GetValue(ordinal);
  397. }
  398. public
  399. #if NET_2_0
  400. override
  401. #endif // NET_2_0
  402. int GetInt32 (int ordinal)
  403. {
  404. return (int) GetValue(ordinal);
  405. }
  406. public
  407. #if NET_2_0
  408. override
  409. #endif // NET_2_0
  410. long GetInt64 (int ordinal)
  411. {
  412. return (long) GetValue(ordinal);
  413. }
  414. public
  415. #if NET_2_0
  416. override
  417. #endif // NET_2_0
  418. string GetName (int index)
  419. {
  420. return GetColumn(index).ColumnName;
  421. }
  422. public
  423. #if NET_2_0
  424. override
  425. #endif // NET_2_0
  426. int GetOrdinal (string name)
  427. {
  428. int i=ColIndex(name);
  429. if (i==-1)
  430. throw new IndexOutOfRangeException ();
  431. else
  432. return i;
  433. }
  434. [MonoTODO]
  435. public
  436. #if NET_2_0
  437. override
  438. #endif // NET_2_0
  439. DataTable GetSchemaTable()
  440. {
  441. // FIXME :
  442. // * Map OdbcType to System.Type and assign to DataType.
  443. // This will eliminate the need for IsStringType in
  444. // OdbcColumn.
  445. // * Cache this DataTable so that it is not contacting
  446. // datasource everytime for the same result set.
  447. DataTable dataTableSchema = null;
  448. // Only Results from SQL SELECT Queries
  449. // get a DataTable for schema of the result
  450. // otherwise, DataTable is null reference
  451. if(cols.Length > 0)
  452. {
  453. string [] keys = GetPrimaryKeys ();
  454. dataTableSchema = new DataTable ();
  455. dataTableSchema.Columns.Add ("ColumnName", typeof (string));
  456. dataTableSchema.Columns.Add ("ColumnOrdinal", typeof (int));
  457. dataTableSchema.Columns.Add ("ColumnSize", typeof (int));
  458. dataTableSchema.Columns.Add ("NumericPrecision", typeof (int));
  459. dataTableSchema.Columns.Add ("NumericScale", typeof (int));
  460. dataTableSchema.Columns.Add ("IsUnique", typeof (bool));
  461. dataTableSchema.Columns.Add ("IsKey", typeof (bool));
  462. DataColumn dc = dataTableSchema.Columns["IsKey"];
  463. dc.AllowDBNull = true; // IsKey can have a DBNull
  464. dataTableSchema.Columns.Add ("BaseCatalogName", typeof (string));
  465. dataTableSchema.Columns.Add ("BaseColumnName", typeof (string));
  466. dataTableSchema.Columns.Add ("BaseSchemaName", typeof (string));
  467. dataTableSchema.Columns.Add ("BaseTableName", typeof (string));
  468. dataTableSchema.Columns.Add ("DataType", typeof(Type));
  469. dataTableSchema.Columns.Add ("AllowDBNull", typeof (bool));
  470. dataTableSchema.Columns.Add ("ProviderType", typeof (int));
  471. dataTableSchema.Columns.Add ("IsAliased", typeof (bool));
  472. dataTableSchema.Columns.Add ("IsExpression", typeof (bool));
  473. dataTableSchema.Columns.Add ("IsIdentity", typeof (bool));
  474. dataTableSchema.Columns.Add ("IsAutoIncrement", typeof (bool));
  475. dataTableSchema.Columns.Add ("IsRowVersion", typeof (bool));
  476. dataTableSchema.Columns.Add ("IsHidden", typeof (bool));
  477. dataTableSchema.Columns.Add ("IsLong", typeof (bool));
  478. dataTableSchema.Columns.Add ("IsReadOnly", typeof (bool));
  479. DataRow schemaRow;
  480. for (int i = 0; i < cols.Length; i += 1 )
  481. {
  482. OdbcColumn col=GetColumn(i);
  483. schemaRow = dataTableSchema.NewRow ();
  484. dataTableSchema.Rows.Add (schemaRow);
  485. schemaRow["ColumnName"] = col.ColumnName;
  486. schemaRow["ColumnOrdinal"] = i;
  487. schemaRow["ColumnSize"] = col.MaxLength;
  488. schemaRow["NumericPrecision"] = GetColumnAttribute (i+1, FieldIdentifier.Precision);
  489. schemaRow["NumericScale"] = GetColumnAttribute (i+1, FieldIdentifier.Scale);
  490. schemaRow["IsUnique"] = false;
  491. schemaRow["IsKey"] = DBNull.Value;
  492. for (int j=0; j < keys.Length; j++) {
  493. if (keys [j] == col.ColumnName) {
  494. schemaRow ["IsUnique"] = true;
  495. schemaRow ["IsKey"] = true;
  496. }
  497. }
  498. schemaRow["BaseCatalogName"] = "";
  499. schemaRow["BaseColumnName"] = "";
  500. schemaRow["BaseSchemaName"] = "";
  501. schemaRow["BaseTableName"] = "";
  502. try {
  503. schemaRow["BaseColumnName"] = GetColumnAttributeStr (i+1, FieldIdentifier.BaseColumnName);
  504. schemaRow["BaseTableName"] = GetColumnAttributeStr (i+1, FieldIdentifier.BaseTableName);
  505. } catch (Exception e) {
  506. // ignore these properties as they are optional.
  507. }
  508. schemaRow["DataType"] = col.DataType;
  509. schemaRow["AllowDBNull"] = col.AllowDBNull;
  510. schemaRow["ProviderType"] = (int) col.OdbcType;
  511. // TODO: all of these
  512. schemaRow["IsAliased"] = false;
  513. schemaRow["IsExpression"] = false;
  514. schemaRow["IsIdentity"] = false;
  515. schemaRow["IsAutoIncrement"] = GetColumnAttribute (i+1, FieldIdentifier.AutoUniqueValue) == 1;
  516. schemaRow["IsRowVersion"] = false;
  517. schemaRow["IsHidden"] = false;
  518. schemaRow["IsLong"] = false;
  519. schemaRow["IsReadOnly"] = false;
  520. // FIXME: according to Brian,
  521. // this does not work on MS .NET
  522. // however, we need it for Mono
  523. // for now
  524. schemaRow.AcceptChanges();
  525. }
  526. dataTableSchema.AcceptChanges();
  527. }
  528. return dataTableSchema;
  529. }
  530. public
  531. #if NET_2_0
  532. override
  533. #endif // NET_2_0
  534. string GetString (int ordinal)
  535. {
  536. return (string) GetValue(ordinal);
  537. }
  538. [MonoTODO]
  539. public TimeSpan GetTime (int ordinal)
  540. {
  541. throw new NotImplementedException ();
  542. }
  543. public
  544. #if NET_2_0
  545. override
  546. #endif // NET_2_0
  547. object GetValue (int ordinal)
  548. {
  549. if (currentRow == -1)
  550. throw new IndexOutOfRangeException ();
  551. if (ordinal>cols.Length-1 || ordinal<0)
  552. throw new IndexOutOfRangeException ();
  553. OdbcReturn ret;
  554. int outsize=0, bufsize;
  555. byte[] buffer;
  556. OdbcColumn col=GetColumn(ordinal);
  557. object DataValue=null;
  558. ushort ColIndex=Convert.ToUInt16(ordinal+1);
  559. // Check cached values
  560. if (col.Value==null)
  561. {
  562. // odbc help file
  563. // mk:@MSITStore:C:\program%20files\Microsoft%20Data%20Access%20SDK\Docs\odbc.chm::/htm/odbcc_data_types.htm
  564. switch (col.OdbcType)
  565. {
  566. case OdbcType.Numeric:
  567. case OdbcType.Decimal:
  568. bufsize=50;
  569. buffer=new byte[bufsize]; // According to sqlext.h, use SQL_CHAR for decimal.
  570. ret=libodbc.SQLGetData(hstmt, ColIndex, col.SqlCType, buffer, bufsize, ref outsize);
  571. if (outsize!=-1) {
  572. byte[] temp = new byte[outsize];
  573. for (int i=0;i<outsize;i++)
  574. temp[i]=buffer[i];
  575. DataValue=Decimal.Parse(System.Text.Encoding.Default.GetString(temp));
  576. }
  577. break;
  578. case OdbcType.TinyInt:
  579. short short_data=0;
  580. ret=libodbc.SQLGetData(hstmt, ColIndex, col.SqlCType, ref short_data, 0, ref outsize);
  581. DataValue=System.Convert.ToByte(short_data);
  582. break;
  583. case OdbcType.Int:
  584. int int_data=0;
  585. ret=libodbc.SQLGetData(hstmt, ColIndex, col.SqlCType, ref int_data, 0, ref outsize);
  586. DataValue=int_data;
  587. break;
  588. case OdbcType.SmallInt:
  589. short sint_data=0;
  590. ret=libodbc.SQLGetData(hstmt, ColIndex, col.SqlCType, ref sint_data, 0, ref outsize);
  591. DataValue=sint_data;
  592. break;
  593. case OdbcType.BigInt:
  594. long long_data=0;
  595. ret=libodbc.SQLGetData(hstmt, ColIndex, col.SqlCType, ref long_data, 0, ref outsize);
  596. DataValue=long_data;
  597. break;
  598. case OdbcType.NVarChar:
  599. bufsize=col.MaxLength*2+1; // Unicode is double byte
  600. buffer=new byte[bufsize];
  601. ret=libodbc.SQLGetData(hstmt, ColIndex, col.SqlCType, buffer, bufsize, ref outsize);
  602. if (outsize!=-1)
  603. DataValue=System.Text.Encoding.Unicode.GetString(buffer,0,outsize);
  604. break;
  605. case OdbcType.VarChar:
  606. bufsize=col.MaxLength+1;
  607. buffer=new byte[bufsize]; // According to sqlext.h, use SQL_CHAR for both char and varchar
  608. ret=libodbc.SQLGetData(hstmt, ColIndex, col.SqlCType, buffer, bufsize, ref outsize);
  609. if (outsize!=-1)
  610. DataValue=System.Text.Encoding.Default.GetString(buffer,0,outsize);
  611. break;
  612. case OdbcType.Real:
  613. float float_data=0;
  614. ret=libodbc.SQLGetData(hstmt, ColIndex, col.SqlCType, ref float_data, 0, ref outsize);
  615. DataValue=float_data;
  616. break;
  617. case OdbcType.Double:
  618. double double_data=0;
  619. ret=libodbc.SQLGetData(hstmt, ColIndex, col.SqlCType, ref double_data, 0, ref outsize);
  620. DataValue=double_data;
  621. break;
  622. case OdbcType.Timestamp:
  623. case OdbcType.DateTime:
  624. case OdbcType.Date:
  625. case OdbcType.Time:
  626. OdbcTimestamp ts_data=new OdbcTimestamp();
  627. ret=libodbc.SQLGetData(hstmt, ColIndex, col.SqlCType, ref ts_data, 0, ref outsize);
  628. if (outsize!=-1) // This means SQL_NULL_DATA
  629. DataValue=new DateTime(ts_data.year,ts_data.month,ts_data.day,ts_data.hour,
  630. ts_data.minute,ts_data.second,Convert.ToInt32(ts_data.fraction));
  631. break;
  632. case OdbcType.Binary :
  633. case OdbcType.Image :
  634. bufsize = col.MaxLength + 1;
  635. buffer = new byte [bufsize];
  636. long read = GetBytes (ordinal, 0, buffer, 0, bufsize);
  637. ret = OdbcReturn.Success;
  638. DataValue = buffer;
  639. break;
  640. default:
  641. bufsize=255;
  642. buffer=new byte[bufsize];
  643. ret=libodbc.SQLGetData(hstmt, ColIndex, SQL_C_TYPE.CHAR, buffer, bufsize, ref outsize);
  644. if (outsize != (int) OdbcLengthIndicator.NullData)
  645. if (! (ret == OdbcReturn.SuccessWithInfo
  646. && outsize == (int) OdbcLengthIndicator.NoTotal))
  647. DataValue=System.Text.Encoding.Default.GetString(buffer, 0, outsize);
  648. break;
  649. }
  650. if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
  651. throw new OdbcException(new OdbcError("SQLGetData",OdbcHandleType.Stmt,hstmt));
  652. if (outsize==-1) // This means SQL_NULL_DATA
  653. col.Value=DBNull.Value;
  654. else
  655. col.Value=DataValue;
  656. }
  657. return col.Value;
  658. }
  659. public
  660. #if NET_2_0
  661. override
  662. #endif // NET_2_0
  663. int GetValues (object[] values)
  664. {
  665. int numValues = 0;
  666. // copy values
  667. for (int i = 0; i < values.Length; i++) {
  668. if (i < FieldCount) {
  669. values[i] = GetValue(i);
  670. }
  671. else {
  672. values[i] = null;
  673. }
  674. }
  675. // get number of object instances in array
  676. if (values.Length < FieldCount)
  677. numValues = values.Length;
  678. else if (values.Length == FieldCount)
  679. numValues = FieldCount;
  680. else
  681. numValues = FieldCount;
  682. return numValues;
  683. }
  684. #if ONLY_1_1
  685. [MonoTODO]
  686. IDataReader IDataRecord.GetData (int ordinal)
  687. {
  688. throw new NotImplementedException ();
  689. }
  690. [MonoTODO]
  691. void IDisposable.Dispose ()
  692. {
  693. }
  694. IEnumerator IEnumerable.GetEnumerator ()
  695. {
  696. return new DbEnumerator (this);
  697. }
  698. #endif // ONLY_1_1
  699. public
  700. #if NET_2_0
  701. override
  702. #endif // NET_2_0
  703. bool IsDBNull (int ordinal)
  704. {
  705. return (GetValue(ordinal) is DBNull);
  706. }
  707. /// <remarks>
  708. /// Move to the next result set.
  709. /// </remarks>
  710. public
  711. #if NET_2_0
  712. override
  713. #endif // NET_2_0
  714. bool NextResult ()
  715. {
  716. OdbcReturn ret = OdbcReturn.Success;
  717. ret = libodbc.SQLMoreResults (hstmt);
  718. if (ret == OdbcReturn.Success) {
  719. short colcount = 0;
  720. libodbc.SQLNumResultCols (hstmt, ref colcount);
  721. cols = new OdbcColumn [colcount];
  722. GetSchemaTable ();
  723. }
  724. return (ret==OdbcReturn.Success);
  725. }
  726. /// <remarks>
  727. /// Load the next row in the current result set.
  728. /// </remarks>
  729. public bool NextRow ()
  730. {
  731. OdbcReturn ret=libodbc.SQLFetch (hstmt);
  732. if (ret != OdbcReturn.Success)
  733. currentRow = -1;
  734. else
  735. currentRow++;
  736. // Clear cached values from last record
  737. foreach (OdbcColumn col in cols)
  738. {
  739. if (col != null)
  740. col.Value = null;
  741. }
  742. return (ret == OdbcReturn.Success);
  743. }
  744. private int GetColumnAttribute (int column, FieldIdentifier fieldId)
  745. {
  746. OdbcReturn ret = OdbcReturn.Error;
  747. byte [] buffer = new byte [255];
  748. int outsize = 0;
  749. int val = 0;
  750. ret = libodbc.SQLColAttribute (hstmt, column, fieldId,
  751. buffer, buffer.Length,
  752. ref outsize, ref val);
  753. if (ret != OdbcReturn.Success && ret != OdbcReturn.SuccessWithInfo)
  754. throw new OdbcException (new OdbcError ("SQLColAttribute",
  755. OdbcHandleType.Stmt,
  756. hstmt)
  757. );
  758. return val;
  759. }
  760. private string GetColumnAttributeStr (int column, FieldIdentifier fieldId)
  761. {
  762. OdbcReturn ret = OdbcReturn.Error;
  763. byte [] buffer = new byte [255];
  764. int outsize = 0;
  765. int val = 0;
  766. ret = libodbc.SQLColAttribute (hstmt, column, fieldId,
  767. buffer, buffer.Length,
  768. ref outsize, ref val);
  769. if (ret != OdbcReturn.Success && ret != OdbcReturn.SuccessWithInfo)
  770. throw new OdbcException (new OdbcError ("SQLColAttribute",
  771. OdbcHandleType.Stmt,
  772. hstmt)
  773. );
  774. string value = "";
  775. if (outsize > 0)
  776. value = Encoding.Default.GetString (buffer, 0, outsize);
  777. return value;
  778. }
  779. private string [] GetPrimaryKeys ()
  780. {
  781. if (cols.Length <= 0)
  782. return new string [0];
  783. ArrayList keys = new ArrayList ();
  784. IntPtr handle = IntPtr.Zero;
  785. OdbcReturn ret = OdbcReturn.Error;
  786. try {
  787. ret=libodbc.SQLAllocHandle(OdbcHandleType.Stmt,
  788. command.Connection.hDbc, ref handle);
  789. if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
  790. throw new OdbcException(new OdbcError("SQLAllocHandle",
  791. OdbcHandleType.Dbc,
  792. command.Connection.hDbc));
  793. string tableName = GetColumnAttributeStr (1, FieldIdentifier.TableName);
  794. string schemaName = GetColumnAttributeStr (1, FieldIdentifier.SchemaName);
  795. string catalogName = GetColumnAttributeStr (1, FieldIdentifier.CatelogName);
  796. ret = libodbc.SQLPrimaryKeys (handle, catalogName, -3,
  797. schemaName, -3,
  798. tableName, -3);
  799. if (ret != OdbcReturn.Success && ret != OdbcReturn.SuccessWithInfo)
  800. throw new OdbcException (new OdbcError ("SQLPrimaryKeys", OdbcHandleType.Stmt, handle));
  801. int length = 0;
  802. byte [] primaryKey = new byte [255];
  803. ret = libodbc.SQLBindCol (handle, 4, SQL_C_TYPE.CHAR, primaryKey, primaryKey.Length, ref length);
  804. if (ret != OdbcReturn.Success && ret != OdbcReturn.SuccessWithInfo)
  805. throw new OdbcException (new OdbcError ("SQLBindCol", OdbcHandleType.Stmt, handle));
  806. int i = 0;
  807. while (true) {
  808. ret = libodbc.SQLFetch (handle);
  809. if (ret != OdbcReturn.Success && ret != OdbcReturn.SuccessWithInfo)
  810. break;
  811. string pkey = Encoding.Default.GetString (primaryKey, 0, length);
  812. keys.Add (pkey);
  813. }
  814. } catch (OdbcException){
  815. // FIXME: Try using SQLStatistics
  816. } finally {
  817. if (handle != IntPtr.Zero) {
  818. ret = libodbc.SQLFreeStmt (handle, libodbc.SQLFreeStmtOptions.Close);
  819. if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
  820. throw new OdbcException(new OdbcError("SQLFreeStmt",OdbcHandleType.Stmt,handle));
  821. ret = libodbc.SQLFreeHandle( (ushort) OdbcHandleType.Stmt, handle);
  822. if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
  823. throw new OdbcException(new OdbcError("SQLFreeHandle",OdbcHandleType.Stmt,handle));
  824. }
  825. }
  826. return (string []) keys.ToArray (typeof (string));
  827. }
  828. public
  829. #if NET_2_0
  830. override
  831. #endif // NET_2_0
  832. bool Read ()
  833. {
  834. return NextRow ();
  835. }
  836. #if NET_2_0
  837. [MonoTODO]
  838. public override object GetProviderSpecificValue (int i)
  839. {
  840. throw new NotImplementedException ();
  841. }
  842. [MonoTODO]
  843. public override int GetProviderSpecificValues (object[] values)
  844. {
  845. throw new NotImplementedException ();
  846. }
  847. [MonoTODO]
  848. public override Type GetFieldProviderSpecificType (int i)
  849. {
  850. throw new NotImplementedException ();
  851. }
  852. #endif // NET_2_0
  853. #endregion
  854. }
  855. }