OdbcDataReader.cs 30 KB

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