SybaseDataReader.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  1. //
  2. // Mono.Data.SybaseClient.SybaseDataReader.cs
  3. //
  4. // Author:
  5. // Rodrigo Moya ([email protected])
  6. // Daniel Morgan ([email protected])
  7. // Tim Coleman ([email protected])
  8. //
  9. // (C) Ximian, Inc 2002
  10. // (C) Daniel Morgan 2002
  11. // Copyright (C) Tim Coleman, 2002
  12. //
  13. using Mono.Data.SybaseTypes;
  14. using Mono.Data.Tds.Protocol;
  15. using System;
  16. using System.Collections;
  17. using System.ComponentModel;
  18. using System.Data;
  19. using System.Data.Common;
  20. namespace Mono.Data.SybaseClient {
  21. public sealed class SybaseDataReader : MarshalByRefObject, IEnumerable, IDataReader, IDisposable, IDataRecord
  22. {
  23. #region Fields
  24. SybaseCommand command;
  25. ArrayList dataTypeNames;
  26. bool disposed = false;
  27. int fieldCount;
  28. bool isClosed;
  29. bool isSelect;
  30. bool moreResults;
  31. int resultsRead;
  32. int rowsRead;
  33. DataTable schemaTable;
  34. #endregion // Fields
  35. #region Constructors
  36. internal SybaseDataReader (SybaseCommand command)
  37. {
  38. this.command = command;
  39. schemaTable = ConstructSchemaTable ();
  40. resultsRead = 0;
  41. fieldCount = 0;
  42. isClosed = false;
  43. isSelect = (command.CommandText.Trim ().ToUpper ().StartsWith ("SELECT"));
  44. command.Tds.RecordsAffected = 0;
  45. NextResult ();
  46. }
  47. #endregion // Constructors
  48. #region Properties
  49. public int Depth {
  50. get { return 0; }
  51. }
  52. public int FieldCount {
  53. get { return fieldCount; }
  54. }
  55. public bool IsClosed {
  56. get { return isClosed; }
  57. }
  58. public object this [int i] {
  59. get { return GetValue (i); }
  60. }
  61. public object this [string name] {
  62. get { return GetValue (GetOrdinal (name)); }
  63. }
  64. public int RecordsAffected {
  65. get {
  66. if (isSelect)
  67. return -1;
  68. else
  69. return command.Tds.RecordsAffected;
  70. }
  71. }
  72. #endregion // Properties
  73. #region Methods
  74. public void Close ()
  75. {
  76. isClosed = true;
  77. command.CloseDataReader (moreResults);
  78. }
  79. private static DataTable ConstructSchemaTable ()
  80. {
  81. Type booleanType = Type.GetType ("System.Boolean");
  82. Type stringType = Type.GetType ("System.String");
  83. Type intType = Type.GetType ("System.Int32");
  84. Type typeType = Type.GetType ("System.Type");
  85. Type shortType = Type.GetType ("System.Int16");
  86. DataTable schemaTable = new DataTable ("SchemaTable");
  87. schemaTable.Columns.Add ("ColumnName", stringType);
  88. schemaTable.Columns.Add ("ColumnOrdinal", intType);
  89. schemaTable.Columns.Add ("ColumnSize", intType);
  90. schemaTable.Columns.Add ("NumericPrecision", shortType);
  91. schemaTable.Columns.Add ("NumericScale", shortType);
  92. schemaTable.Columns.Add ("IsUnique", booleanType);
  93. schemaTable.Columns.Add ("IsKey", booleanType);
  94. schemaTable.Columns.Add ("BaseServerName", stringType);
  95. schemaTable.Columns.Add ("BaseCatalogName", stringType);
  96. schemaTable.Columns.Add ("BaseColumnName", stringType);
  97. schemaTable.Columns.Add ("BaseSchemaName", stringType);
  98. schemaTable.Columns.Add ("BaseTableName", stringType);
  99. schemaTable.Columns.Add ("DataType", typeType);
  100. schemaTable.Columns.Add ("AllowDBNull", booleanType);
  101. schemaTable.Columns.Add ("ProviderType", intType);
  102. schemaTable.Columns.Add ("IsAliased", booleanType);
  103. schemaTable.Columns.Add ("IsExpression", booleanType);
  104. schemaTable.Columns.Add ("IsIdentity", booleanType);
  105. schemaTable.Columns.Add ("IsAutoIncrement", booleanType);
  106. schemaTable.Columns.Add ("IsRowVersion", booleanType);
  107. schemaTable.Columns.Add ("IsHidden", booleanType);
  108. schemaTable.Columns.Add ("IsLong", booleanType);
  109. schemaTable.Columns.Add ("IsReadOnly", booleanType);
  110. return schemaTable;
  111. }
  112. private void Dispose (bool disposing)
  113. {
  114. if (!disposed) {
  115. if (disposing) {
  116. schemaTable.Dispose ();
  117. Close ();
  118. }
  119. disposed = true;
  120. }
  121. }
  122. public bool GetBoolean (int i)
  123. {
  124. object value = GetValue (i);
  125. if (!(value is bool))
  126. throw new InvalidCastException ();
  127. return (bool) value;
  128. }
  129. public byte GetByte (int i)
  130. {
  131. object value = GetValue (i);
  132. if (!(value is byte))
  133. throw new InvalidCastException ();
  134. return (byte) value;
  135. }
  136. public long GetBytes (int i, long dataIndex, byte[] buffer, int bufferIndex, int length)
  137. {
  138. object value = GetValue (i);
  139. if (!(value is byte []))
  140. throw new InvalidCastException ();
  141. Array.Copy ((byte []) value, (int) dataIndex, buffer, bufferIndex, length);
  142. return ((byte []) value).Length - dataIndex;
  143. }
  144. public char GetChar (int i)
  145. {
  146. object value = GetValue (i);
  147. if (!(value is char))
  148. throw new InvalidCastException ();
  149. return (char) value;
  150. }
  151. public long GetChars (int i, long dataIndex, char[] buffer, int bufferIndex, int length)
  152. {
  153. object value = GetValue (i);
  154. if (!(value is char[]))
  155. throw new InvalidCastException ();
  156. Array.Copy ((char []) value, (int) dataIndex, buffer, bufferIndex, length);
  157. return ((char []) value).Length - dataIndex;
  158. }
  159. [MonoTODO ("Implement GetData")]
  160. public IDataReader GetData (int i)
  161. {
  162. throw new NotImplementedException ();
  163. }
  164. public string GetDataTypeName (int i)
  165. {
  166. return (string) dataTypeNames [i];
  167. }
  168. public DateTime GetDateTime (int i)
  169. {
  170. object value = GetValue (i);
  171. if (!(value is DateTime))
  172. throw new InvalidCastException ();
  173. return (DateTime) value;
  174. }
  175. public decimal GetDecimal (int i)
  176. {
  177. object value = GetValue (i);
  178. if (!(value is decimal))
  179. throw new InvalidCastException ();
  180. return (decimal) value;
  181. }
  182. public double GetDouble (int i)
  183. {
  184. object value = GetValue (i);
  185. if (!(value is double))
  186. throw new InvalidCastException ();
  187. return (double) value;
  188. }
  189. public Type GetFieldType (int i)
  190. {
  191. return (Type) schemaTable.Rows[i]["DataType"];
  192. }
  193. public float GetFloat (int i)
  194. {
  195. object value = GetValue (i);
  196. if (!(value is float))
  197. throw new InvalidCastException ();
  198. return (float) value;
  199. }
  200. public Guid GetGuid (int i)
  201. {
  202. object value = GetValue (i);
  203. if (!(value is Guid))
  204. throw new InvalidCastException ();
  205. return (Guid) value;
  206. }
  207. public short GetInt16 (int i)
  208. {
  209. object value = GetValue (i);
  210. if (!(value is short))
  211. throw new InvalidCastException ();
  212. return (short) value;
  213. }
  214. public int GetInt32 (int i)
  215. {
  216. object value = GetValue (i);
  217. if (!(value is int))
  218. throw new InvalidCastException ();
  219. return (int) value;
  220. }
  221. public long GetInt64 (int i)
  222. {
  223. object value = GetValue (i);
  224. if (!(value is long))
  225. throw new InvalidCastException ();
  226. return (long) value;
  227. }
  228. public string GetName (int i)
  229. {
  230. return (string) schemaTable.Rows[i]["ColumnName"];
  231. }
  232. public int GetOrdinal (string name)
  233. {
  234. foreach (DataRow schemaRow in schemaTable.Rows)
  235. if (((string) schemaRow ["ColumnName"]).Equals (name))
  236. return (int) schemaRow ["ColumnOrdinal"];
  237. foreach (DataRow schemaRow in schemaTable.Rows)
  238. if (String.Compare (((string) schemaRow ["ColumnName"]), name, true) == 0)
  239. return (int) schemaRow ["ColumnOrdinal"];
  240. throw new IndexOutOfRangeException ();
  241. }
  242. public DataTable GetSchemaTable ()
  243. {
  244. if (schemaTable.Rows != null && schemaTable.Rows.Count > 0)
  245. return schemaTable;
  246. if (!moreResults)
  247. return null;
  248. fieldCount = 0;
  249. dataTypeNames = new ArrayList ();
  250. foreach (TdsDataColumn schema in command.Tds.Columns) {
  251. DataRow row = schemaTable.NewRow ();
  252. row ["ColumnName"] = GetSchemaValue (schema, "ColumnName");
  253. row ["ColumnSize"] = GetSchemaValue (schema, "ColumnSize");
  254. row ["ColumnOrdinal"] = GetSchemaValue (schema, "ColumnOrdinal");
  255. row ["NumericPrecision"] = GetSchemaValue (schema, "NumericPrecision");
  256. row ["NumericScale"] = GetSchemaValue (schema, "NumericScale");
  257. row ["IsUnique"] = GetSchemaValue (schema, "IsUnique");
  258. row ["IsKey"] = GetSchemaValue (schema, "IsKey");
  259. row ["BaseServerName"] = GetSchemaValue (schema, "BaseServerName");
  260. row ["BaseCatalogName"] = GetSchemaValue (schema, "BaseCatalogName");
  261. row ["BaseColumnName"] = GetSchemaValue (schema, "BaseColumnName");
  262. row ["BaseSchemaName"] = GetSchemaValue (schema, "BaseSchemaName");
  263. row ["BaseTableName"] = GetSchemaValue (schema, "BaseTableName");
  264. row ["AllowDBNull"] = GetSchemaValue (schema, "AllowDBNull");
  265. row ["IsAliased"] = GetSchemaValue (schema, "IsAliased");
  266. row ["IsExpression"] = GetSchemaValue (schema, "IsExpression");
  267. row ["IsIdentity"] = GetSchemaValue (schema, "IsIdentity");
  268. row ["IsAutoIncrement"] = GetSchemaValue (schema, "IsAutoIncrement");
  269. row ["IsRowVersion"] = GetSchemaValue (schema, "IsRowVersion");
  270. row ["IsHidden"] = GetSchemaValue (schema, "IsHidden");
  271. row ["IsReadOnly"] = GetSchemaValue (schema, "IsReadOnly");
  272. // We don't always get the base column name.
  273. if (row ["BaseColumnName"] == DBNull.Value)
  274. row ["BaseColumnName"] = row ["ColumnName"];
  275. switch ((TdsColumnType) schema ["ColumnType"]) {
  276. case TdsColumnType.Image :
  277. dataTypeNames.Add ("image");
  278. row ["ProviderType"] = (int) SybaseType.Image;
  279. row ["DataType"] = typeof (byte[]);
  280. row ["IsLong"] = true;
  281. break;
  282. case TdsColumnType.Text :
  283. dataTypeNames.Add ("text");
  284. row ["ProviderType"] = (int) SybaseType.Text;
  285. row ["DataType"] = typeof (string);
  286. row ["IsLong"] = true;
  287. break;
  288. case TdsColumnType.UniqueIdentifier :
  289. dataTypeNames.Add ("uniqueidentifier");
  290. row ["ProviderType"] = (int) SybaseType.UniqueIdentifier;
  291. row ["DataType"] = typeof (Guid);
  292. row ["IsLong"] = false;
  293. break;
  294. case TdsColumnType.VarBinary :
  295. case TdsColumnType.BigVarBinary :
  296. dataTypeNames.Add ("varbinary");
  297. row ["ProviderType"] = (int) SybaseType.VarBinary;
  298. row ["DataType"] = typeof (byte[]);
  299. row ["IsLong"] = true;
  300. break;
  301. case TdsColumnType.IntN :
  302. case TdsColumnType.Int4 :
  303. dataTypeNames.Add ("int");
  304. row ["ProviderType"] = (int) SybaseType.Int;
  305. row ["DataType"] = typeof (int);
  306. row ["IsLong"] = false;
  307. break;
  308. case TdsColumnType.VarChar :
  309. case TdsColumnType.BigVarChar :
  310. dataTypeNames.Add ("varchar");
  311. row ["ProviderType"] = (int) SybaseType.VarChar;
  312. row ["DataType"] = typeof (string);
  313. row ["IsLong"] = false;
  314. break;
  315. case TdsColumnType.Binary :
  316. case TdsColumnType.BigBinary :
  317. dataTypeNames.Add ("binary");
  318. row ["ProviderType"] = (int) SybaseType.Binary;
  319. row ["DataType"] = typeof (byte[]);
  320. row ["IsLong"] = true;
  321. break;
  322. case TdsColumnType.Char :
  323. case TdsColumnType.BigChar :
  324. dataTypeNames.Add ("char");
  325. row ["ProviderType"] = (int) SybaseType.Char;
  326. row ["DataType"] = typeof (string);
  327. row ["IsLong"] = false;
  328. break;
  329. case TdsColumnType.Int1 :
  330. dataTypeNames.Add ("tinyint");
  331. row ["ProviderType"] = (int) SybaseType.TinyInt;
  332. row ["DataType"] = typeof (byte);
  333. row ["IsLong"] = false;
  334. break;
  335. case TdsColumnType.Bit :
  336. case TdsColumnType.BitN :
  337. dataTypeNames.Add ("bit");
  338. row ["ProviderType"] = (int) SybaseType.Bit;
  339. row ["DataType"] = typeof (bool);
  340. row ["IsLong"] = false;
  341. break;
  342. case TdsColumnType.Int2 :
  343. dataTypeNames.Add ("smallint");
  344. row ["ProviderType"] = (int) SybaseType.SmallInt;
  345. row ["DataType"] = typeof (short);
  346. row ["IsLong"] = false;
  347. break;
  348. case TdsColumnType.DateTime4 :
  349. case TdsColumnType.DateTime :
  350. case TdsColumnType.DateTimeN :
  351. dataTypeNames.Add ("datetime");
  352. row ["ProviderType"] = (int) SybaseType.DateTime;
  353. row ["DataType"] = typeof (DateTime);
  354. row ["IsLong"] = false;
  355. break;
  356. case TdsColumnType.Real :
  357. dataTypeNames.Add ("real");
  358. row ["ProviderType"] = (int) SybaseType.Real;
  359. row ["DataType"] = typeof (float);
  360. break;
  361. case TdsColumnType.Money :
  362. case TdsColumnType.MoneyN :
  363. case TdsColumnType.Money4 :
  364. dataTypeNames.Add ("money");
  365. row ["ProviderType"] = (int) SybaseType.Money;
  366. row ["DataType"] = typeof (decimal);
  367. row ["IsLong"] = false;
  368. break;
  369. case TdsColumnType.Float8 :
  370. case TdsColumnType.FloatN :
  371. dataTypeNames.Add ("float");
  372. row ["ProviderType"] = (int) SybaseType.Float;
  373. row ["DataType"] = typeof (double);
  374. row ["IsLong"] = false;
  375. break;
  376. case TdsColumnType.NText :
  377. dataTypeNames.Add ("ntext");
  378. row ["ProviderType"] = (int) SybaseType.NText;
  379. row ["DataType"] = typeof (string);
  380. row ["IsLong"] = true;
  381. break;
  382. case TdsColumnType.NVarChar :
  383. dataTypeNames.Add ("nvarchar");
  384. row ["ProviderType"] = (int) SybaseType.NVarChar;
  385. row ["DataType"] = typeof (string);
  386. row ["IsLong"] = false;
  387. break;
  388. case TdsColumnType.Decimal :
  389. case TdsColumnType.Numeric :
  390. dataTypeNames.Add ("decimal");
  391. row ["ProviderType"] = (int) SybaseType.Decimal;
  392. row ["DataType"] = typeof (decimal);
  393. row ["IsLong"] = false;
  394. break;
  395. case TdsColumnType.NChar :
  396. dataTypeNames.Add ("nchar");
  397. row ["ProviderType"] = (int) SybaseType.NChar;
  398. row ["DataType"] = typeof (string);
  399. row ["IsLong"] = false;
  400. break;
  401. case TdsColumnType.SmallMoney :
  402. dataTypeNames.Add ("smallmoney");
  403. row ["ProviderType"] = (int) SybaseType.SmallMoney;
  404. row ["DataType"] = typeof (decimal);
  405. row ["IsLong"] = false;
  406. break;
  407. default :
  408. dataTypeNames.Add ("variant");
  409. row ["ProviderType"] = (int) SybaseType.Variant;
  410. row ["DataType"] = typeof (object);
  411. row ["IsLong"] = false;
  412. break;
  413. }
  414. schemaTable.Rows.Add (row);
  415. fieldCount += 1;
  416. }
  417. return schemaTable;
  418. }
  419. private static object GetSchemaValue (TdsDataColumn schema, object key)
  420. {
  421. if (schema.ContainsKey (key) && schema [key] != null)
  422. return schema [key];
  423. return DBNull.Value;
  424. }
  425. public SybaseBinary GetSybaseBinary (int i)
  426. {
  427. throw new NotImplementedException ();
  428. }
  429. public SybaseBoolean GetSybaseBoolean (int i)
  430. {
  431. object value = GetSybaseValue (i);
  432. if (!(value is SybaseBoolean))
  433. throw new InvalidCastException ();
  434. return (SybaseBoolean) value;
  435. }
  436. public SybaseByte GetSybaseByte (int i)
  437. {
  438. object value = GetSybaseValue (i);
  439. if (!(value is SybaseByte))
  440. throw new InvalidCastException ();
  441. return (SybaseByte) value;
  442. }
  443. public SybaseDateTime GetSybaseDateTime (int i)
  444. {
  445. object value = GetSybaseValue (i);
  446. if (!(value is SybaseDateTime))
  447. throw new InvalidCastException ();
  448. return (SybaseDateTime) value;
  449. }
  450. public SybaseDecimal GetSybaseDecimal (int i)
  451. {
  452. object value = GetSybaseValue (i);
  453. if (!(value is SybaseDecimal))
  454. throw new InvalidCastException ();
  455. return (SybaseDecimal) value;
  456. }
  457. public SybaseDouble GetSybaseDouble (int i)
  458. {
  459. object value = GetSybaseValue (i);
  460. if (!(value is SybaseDouble))
  461. throw new InvalidCastException ();
  462. return (SybaseDouble) value;
  463. }
  464. public SybaseGuid GetSybaseGuid (int i)
  465. {
  466. object value = GetSybaseValue (i);
  467. if (!(value is SybaseGuid))
  468. throw new InvalidCastException ();
  469. return (SybaseGuid) value;
  470. }
  471. public SybaseInt16 GetSybaseInt16 (int i)
  472. {
  473. object value = GetSybaseValue (i);
  474. if (!(value is SybaseInt16))
  475. throw new InvalidCastException ();
  476. return (SybaseInt16) value;
  477. }
  478. public SybaseInt32 GetSybaseInt32 (int i)
  479. {
  480. object value = GetSybaseValue (i);
  481. if (!(value is SybaseInt32))
  482. throw new InvalidCastException ();
  483. return (SybaseInt32) value;
  484. }
  485. public SybaseInt64 GetSybaseInt64 (int i)
  486. {
  487. object value = GetSybaseValue (i);
  488. if (!(value is SybaseInt64))
  489. throw new InvalidCastException ();
  490. return (SybaseInt64) value;
  491. }
  492. public SybaseMoney GetSybaseMoney (int i)
  493. {
  494. object value = GetSybaseValue (i);
  495. if (!(value is SybaseMoney))
  496. throw new InvalidCastException ();
  497. return (SybaseMoney) value;
  498. }
  499. public SybaseSingle GetSybaseSingle (int i)
  500. {
  501. object value = GetSybaseValue (i);
  502. if (!(value is SybaseSingle))
  503. throw new InvalidCastException ();
  504. return (SybaseSingle) value;
  505. }
  506. public SybaseString GetSybaseString (int i)
  507. {
  508. object value = GetSybaseValue (i);
  509. if (!(value is SybaseString))
  510. throw new InvalidCastException ();
  511. return (SybaseString) value;
  512. }
  513. [MonoTODO ("Implement TdsBigDecimal conversion. SybaseType.Real fails tests?")]
  514. public object GetSybaseValue (int i)
  515. {
  516. SybaseType type = (SybaseType) (schemaTable.Rows [i]["ProviderType"]);
  517. object value = GetValue (i);
  518. switch (type) {
  519. case SybaseType.BigInt:
  520. if (value == null)
  521. return SybaseInt64.Null;
  522. return (SybaseInt64) ((long) value);
  523. case SybaseType.Binary:
  524. case SybaseType.Image:
  525. case SybaseType.VarBinary:
  526. case SybaseType.Timestamp:
  527. if (value == null)
  528. return SybaseBinary.Null;
  529. return (SybaseBinary) ((byte[]) value);
  530. case SybaseType.Bit:
  531. if (value == null)
  532. return SybaseBoolean.Null;
  533. return (SybaseBoolean) ((bool) value);
  534. case SybaseType.Char:
  535. case SybaseType.NChar:
  536. case SybaseType.NText:
  537. case SybaseType.NVarChar:
  538. case SybaseType.Text:
  539. case SybaseType.VarChar:
  540. if (value == null)
  541. return SybaseString.Null;
  542. return (SybaseString) ((string) value);
  543. case SybaseType.DateTime:
  544. case SybaseType.SmallDateTime:
  545. if (value == null)
  546. return SybaseDateTime.Null;
  547. return (SybaseDateTime) ((DateTime) value);
  548. case SybaseType.Decimal:
  549. if (value == null)
  550. return SybaseDecimal.Null;
  551. if (value is TdsBigDecimal)
  552. return SybaseDecimal.FromTdsBigDecimal ((TdsBigDecimal) value);
  553. return (SybaseDecimal) ((decimal) value);
  554. case SybaseType.Float:
  555. if (value == null)
  556. return SybaseDouble.Null;
  557. return (SybaseDouble) ((double) value);
  558. case SybaseType.Int:
  559. if (value == null)
  560. return SybaseInt32.Null;
  561. return (SybaseInt32) ((int) value);
  562. case SybaseType.Money:
  563. case SybaseType.SmallMoney:
  564. if (value == null)
  565. return SybaseMoney.Null;
  566. return (SybaseMoney) ((decimal) value);
  567. case SybaseType.Real:
  568. if (value == null)
  569. return SybaseSingle.Null;
  570. return (SybaseSingle) ((float) value);
  571. case SybaseType.UniqueIdentifier:
  572. if (value == null)
  573. return SybaseGuid.Null;
  574. return (SybaseGuid) ((Guid) value);
  575. case SybaseType.SmallInt:
  576. if (value == null)
  577. return SybaseInt16.Null;
  578. return (SybaseInt16) ((short) value);
  579. case SybaseType.TinyInt:
  580. if (value == null)
  581. return SybaseByte.Null;
  582. return (SybaseByte) ((byte) value);
  583. }
  584. throw new InvalidOperationException ("The type of this column is unknown.");
  585. }
  586. public int GetSybaseValues (object[] values)
  587. {
  588. int count = 0;
  589. int columnCount = schemaTable.Rows.Count;
  590. int arrayCount = values.Length;
  591. if (arrayCount > columnCount)
  592. count = columnCount;
  593. else
  594. count = arrayCount;
  595. for (int i = 0; i < count; i += 1)
  596. values [i] = GetSybaseValue (i);
  597. return count;
  598. }
  599. public string GetString (int i)
  600. {
  601. object value = GetValue (i);
  602. if (!(value is string))
  603. throw new InvalidCastException ();
  604. return (string) value;
  605. }
  606. public object GetValue (int i)
  607. {
  608. return command.Tds.ColumnValues [i];
  609. }
  610. public int GetValues (object[] values)
  611. {
  612. int len = values.Length;
  613. int bigDecimalIndex = command.Tds.ColumnValues.BigDecimalIndex;
  614. // If a four-byte decimal is stored, then we can't convert to
  615. // a native type. Throw an OverflowException.
  616. if (bigDecimalIndex >= 0 && bigDecimalIndex < len)
  617. throw new OverflowException ();
  618. command.Tds.ColumnValues.CopyTo (0, values, 0, len);
  619. return (len > FieldCount ? len : FieldCount);
  620. }
  621. void IDisposable.Dispose ()
  622. {
  623. Dispose (true);
  624. GC.SuppressFinalize (this);
  625. }
  626. IEnumerator IEnumerable.GetEnumerator ()
  627. {
  628. return new DbEnumerator (this);
  629. }
  630. public bool IsDBNull (int i)
  631. {
  632. return GetValue (i) == null;
  633. }
  634. public bool NextResult ()
  635. {
  636. if ((command.CommandBehavior & CommandBehavior.SingleResult) != 0 && resultsRead > 0)
  637. return false;
  638. if (command.Tds.DoneProc)
  639. return false;
  640. schemaTable.Rows.Clear ();
  641. moreResults = command.Tds.NextResult ();
  642. GetSchemaTable ();
  643. rowsRead = 0;
  644. resultsRead += 1;
  645. return moreResults;
  646. }
  647. public bool Read ()
  648. {
  649. if ((command.CommandBehavior & CommandBehavior.SingleRow) != 0 && rowsRead > 0)
  650. return false;
  651. if ((command.CommandBehavior & CommandBehavior.SchemaOnly) != 0)
  652. return false;
  653. if (!moreResults)
  654. return false;
  655. bool result = command.Tds.NextRow ();
  656. rowsRead += 1;
  657. return result;
  658. }
  659. #endregion // Methods
  660. }
  661. }