SqlDataReader.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  1. //
  2. // System.Data.SqlClient.SqlDataReader.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.TdsClient.Internal;
  14. using System;
  15. using System.Collections;
  16. using System.ComponentModel;
  17. using System.Data;
  18. using System.Data.Common;
  19. using System.Data.SqlTypes;
  20. namespace System.Data.SqlClient {
  21. public sealed class SqlDataReader : MarshalByRefObject, IEnumerable, IDataReader, IDisposable, IDataRecord
  22. {
  23. #region Fields
  24. int fieldCount;
  25. bool isClosed;
  26. int recordsAffected;
  27. bool moreResults;
  28. int resultsRead;
  29. int rowsRead;
  30. SqlCommand command;
  31. DataTable schemaTable;
  32. ArrayList dataTypeNames;
  33. ArrayList dataTypes;
  34. #endregion // Fields
  35. #region Constructors
  36. internal SqlDataReader (SqlCommand command)
  37. {
  38. schemaTable = ConstructSchemaTable ();
  39. this.resultsRead = 0;
  40. this.command = command;
  41. this.fieldCount = 0;
  42. this.isClosed = false;
  43. NextResult ();
  44. }
  45. #endregion
  46. #region Properties
  47. public int Depth {
  48. get { return 0; }
  49. }
  50. public int FieldCount {
  51. get { return fieldCount; }
  52. }
  53. public bool IsClosed {
  54. get { return isClosed; }
  55. }
  56. public object this [int i] {
  57. get { return GetValue (i); }
  58. }
  59. public object this [string name] {
  60. get { return GetValue (GetOrdinal (name)); }
  61. }
  62. public int RecordsAffected {
  63. get { return recordsAffected; }
  64. }
  65. #endregion // Properties
  66. #region Methods
  67. public void Close()
  68. {
  69. isClosed = true;
  70. command.CloseDataReader (moreResults);
  71. }
  72. private static DataTable ConstructSchemaTable ()
  73. {
  74. Type booleanType = Type.GetType ("System.Boolean");
  75. Type stringType = Type.GetType ("System.String");
  76. Type intType = Type.GetType ("System.Int32");
  77. Type typeType = Type.GetType ("System.Type");
  78. Type shortType = Type.GetType ("System.Int16");
  79. DataTable schemaTable = new DataTable ("SchemaTable");
  80. schemaTable.Columns.Add ("ColumnName", stringType);
  81. schemaTable.Columns.Add ("ColumnOrdinal", intType);
  82. schemaTable.Columns.Add ("ColumnSize", intType);
  83. schemaTable.Columns.Add ("NumericPrecision", shortType);
  84. schemaTable.Columns.Add ("NumericScale", shortType);
  85. schemaTable.Columns.Add ("IsUnique", booleanType);
  86. schemaTable.Columns.Add ("IsKey", booleanType);
  87. schemaTable.Columns.Add ("BaseServerName", stringType);
  88. schemaTable.Columns.Add ("BaseCatalogName", stringType);
  89. schemaTable.Columns.Add ("BaseColumnName", stringType);
  90. schemaTable.Columns.Add ("BaseSchemaName", stringType);
  91. schemaTable.Columns.Add ("BaseTableName", stringType);
  92. schemaTable.Columns.Add ("DataType", typeType);
  93. schemaTable.Columns.Add ("AllowDBNull", booleanType);
  94. schemaTable.Columns.Add ("ProviderType", intType);
  95. schemaTable.Columns.Add ("IsAliased", booleanType);
  96. schemaTable.Columns.Add ("IsExpression", booleanType);
  97. schemaTable.Columns.Add ("IsIdentity", booleanType);
  98. schemaTable.Columns.Add ("IsAutoIncrement", booleanType);
  99. schemaTable.Columns.Add ("IsRowVersion", booleanType);
  100. schemaTable.Columns.Add ("IsHidden", booleanType);
  101. schemaTable.Columns.Add ("IsLong", booleanType);
  102. schemaTable.Columns.Add ("IsReadOnly", booleanType);
  103. return schemaTable;
  104. }
  105. public bool GetBoolean (int i)
  106. {
  107. object value = GetValue (i);
  108. if (!(value is bool))
  109. throw new InvalidCastException ();
  110. return (bool) value;
  111. }
  112. public byte GetByte (int i)
  113. {
  114. object value = GetValue (i);
  115. if (!(value is byte))
  116. throw new InvalidCastException ();
  117. return (byte) value;
  118. }
  119. public long GetBytes (int i, long dataIndex, byte[] buffer, int bufferIndex, int length)
  120. {
  121. object value = GetValue (i);
  122. if (!(value is byte []))
  123. throw new InvalidCastException ();
  124. Array.Copy ((byte []) value, (int) dataIndex, buffer, bufferIndex, length);
  125. return ((byte []) value).Length - dataIndex;
  126. }
  127. [MonoTODO ("Implement GetChar")]
  128. public char GetChar (int i)
  129. {
  130. throw new NotImplementedException ();
  131. }
  132. public long GetChars (int i, long dataIndex, char[] buffer, int bufferIndex, int length)
  133. {
  134. object value = GetValue (i);
  135. if (!(value is char []))
  136. throw new InvalidCastException ();
  137. Array.Copy ((char []) value, (int) dataIndex, buffer, bufferIndex, length);
  138. return ((char []) value).Length - dataIndex;
  139. }
  140. [MonoTODO ("Implement GetData")]
  141. public IDataReader GetData (int i)
  142. {
  143. throw new NotImplementedException ();
  144. }
  145. public string GetDataTypeName (int i)
  146. {
  147. return (string) dataTypeNames [i];
  148. }
  149. public DateTime GetDateTime (int i)
  150. {
  151. object value = GetValue (i);
  152. if (!(value is DateTime))
  153. throw new InvalidCastException ();
  154. return (DateTime) value;
  155. }
  156. public decimal GetDecimal (int i)
  157. {
  158. object value = GetValue (i);
  159. if (!(value is TdsBigDecimal))
  160. throw new InvalidCastException ();
  161. int[] bits = ((TdsBigDecimal) value).Data;
  162. if (bits[3] != 0)
  163. throw new OverflowException ();
  164. byte scale = ((TdsBigDecimal) value).Scale;
  165. bool isNegative = ((TdsBigDecimal) value).IsNegative;
  166. return new Decimal (bits[0], bits[1], bits[2], isNegative, scale);
  167. }
  168. private TdsBigDecimal GetDecimalImpl (int i)
  169. {
  170. object value = GetValue (i);
  171. if (!(value is TdsBigDecimal))
  172. throw new InvalidCastException ();
  173. return (TdsBigDecimal) value;
  174. }
  175. public double GetDouble (int i)
  176. {
  177. object value = GetValue (i);
  178. if (!(value is double))
  179. throw new InvalidCastException ();
  180. return (double) value;
  181. }
  182. public Type GetFieldType (int i)
  183. {
  184. return (Type) schemaTable.Rows[i]["DataType"];
  185. }
  186. public float GetFloat (int i)
  187. {
  188. object value = GetValue (i);
  189. if (!(value is float))
  190. throw new InvalidCastException ();
  191. return (float) value;
  192. }
  193. public Guid GetGuid (int i)
  194. {
  195. object value = GetValue (i);
  196. if (!(value is Guid))
  197. throw new InvalidCastException ();
  198. return (Guid) value;
  199. }
  200. public short GetInt16 (int i)
  201. {
  202. object value = GetValue (i);
  203. if (!(value is short))
  204. throw new InvalidCastException ();
  205. return (short) value;
  206. }
  207. public int GetInt32 (int i)
  208. {
  209. object value = GetValue (i);
  210. if (!(value is int))
  211. throw new InvalidCastException ();
  212. return (int) value;
  213. }
  214. public long GetInt64 (int i)
  215. {
  216. object value = GetValue (i);
  217. if (!(value is long))
  218. throw new InvalidCastException ();
  219. return (long) value;
  220. }
  221. public string GetName (int i)
  222. {
  223. return (string) schemaTable.Rows[i]["ColumnName"];
  224. }
  225. [MonoTODO ("Make sure that ordinal is in fact zero-based.")]
  226. public int GetOrdinal (string name)
  227. {
  228. foreach (DataRow schemaRow in schemaTable.Rows)
  229. if (((string) schemaRow ["ColumnName"]).Equals (name))
  230. return (int) schemaRow ["ColumnOrdinal"];
  231. foreach (DataRow schemaRow in schemaTable.Rows)
  232. if (String.Compare (((string) schemaRow ["ColumnName"]), name, true) == 0)
  233. return (int) schemaRow ["ColumnOrdinal"];
  234. throw new IndexOutOfRangeException ();
  235. }
  236. public DataTable GetSchemaTable ()
  237. {
  238. if (schemaTable.Rows != null && schemaTable.Rows.Count > 0)
  239. return schemaTable;
  240. if (!moreResults)
  241. return null;
  242. fieldCount = 0;
  243. dataTypeNames = new ArrayList ();
  244. dataTypes = new ArrayList ();
  245. foreach (TdsSchemaInfo schema in command.Tds.Schema) {
  246. DataRow row = schemaTable.NewRow ();
  247. // set default values
  248. row ["AllowDBNull"] = true;
  249. row ["BaseCatalogName"] = DBNull.Value;
  250. row ["BaseColumnName"] = DBNull.Value;
  251. row ["BaseSchemaName"] = DBNull.Value;
  252. row ["BaseTableName"] = DBNull.Value;
  253. row ["ColumnName"] = DBNull.Value;
  254. row ["IsAutoIncrement"] = false;
  255. row ["IsHidden"] = false;
  256. row ["IsLong"] = false;
  257. row ["IsRowVersion"] = false;
  258. row ["IsUnique"] = false;
  259. row ["NumericPrecision"] = DBNull.Value;
  260. row ["NumericScale"] = DBNull.Value;
  261. switch (schema.ColumnType) {
  262. case TdsColumnType.Image :
  263. dataTypeNames.Add ("image");
  264. row ["ProviderType"] = (int) SqlDbType.Image;
  265. row ["DataType"] = typeof (byte[]);
  266. row ["IsLong"] = true;
  267. break;
  268. case TdsColumnType.Text :
  269. dataTypes.Add (typeof (string));
  270. dataTypeNames.Add ("text");
  271. row ["ProviderType"] = (int) SqlDbType.Text;
  272. row ["DataType"] = typeof (string);
  273. row ["IsLong"] = true;
  274. break;
  275. case TdsColumnType.UniqueIdentifier :
  276. dataTypeNames.Add ("uniqueidentifier");
  277. row ["ProviderType"] = (int) SqlDbType.UniqueIdentifier;
  278. row ["DataType"] = typeof (Guid);
  279. break;
  280. case TdsColumnType.VarBinary :
  281. case TdsColumnType.BigVarBinary :
  282. dataTypeNames.Add ("varbinary");
  283. row ["ProviderType"] = (int) SqlDbType.VarBinary;
  284. row ["DataType"] = typeof (byte[]);
  285. row ["IsLong"] = true;
  286. break;
  287. case TdsColumnType.IntN :
  288. case TdsColumnType.Int4 :
  289. dataTypeNames.Add ("int");
  290. row ["ProviderType"] = (int) SqlDbType.Int;
  291. row ["DataType"] = typeof (int);
  292. break;
  293. case TdsColumnType.VarChar :
  294. case TdsColumnType.BigVarChar :
  295. dataTypeNames.Add ("varchar");
  296. row ["ProviderType"] = (int) SqlDbType.VarChar;
  297. row ["DataType"] = typeof (string);
  298. break;
  299. case TdsColumnType.Binary :
  300. case TdsColumnType.BigBinary :
  301. dataTypeNames.Add ("binary");
  302. row ["ProviderType"] = (int) SqlDbType.Binary;
  303. row ["DataType"] = typeof (byte[]);
  304. row ["IsLong"] = true;
  305. break;
  306. case TdsColumnType.Char :
  307. case TdsColumnType.BigChar :
  308. dataTypeNames.Add ("char");
  309. row ["ProviderType"] = (int) SqlDbType.Char;
  310. row ["DataType"] = typeof (string);
  311. break;
  312. case TdsColumnType.Int1 :
  313. dataTypeNames.Add ("tinyint");
  314. row ["ProviderType"] = (int) SqlDbType.TinyInt;
  315. row ["DataType"] = typeof (byte);
  316. break;
  317. case TdsColumnType.Bit :
  318. case TdsColumnType.BitN :
  319. dataTypeNames.Add ("bit");
  320. row ["ProviderType"] = (int) SqlDbType.Bit;
  321. row ["DataType"] = typeof (bool);
  322. break;
  323. case TdsColumnType.Int2 :
  324. dataTypeNames.Add ("smallint");
  325. row ["ProviderType"] = (int) SqlDbType.SmallInt;
  326. row ["DataType"] = typeof (short);
  327. break;
  328. case TdsColumnType.DateTime4 :
  329. case TdsColumnType.DateTime :
  330. case TdsColumnType.DateTimeN :
  331. dataTypeNames.Add ("datetime");
  332. row ["ProviderType"] = (int) SqlDbType.DateTime;
  333. row ["DataType"] = typeof (DateTime);
  334. break;
  335. case TdsColumnType.Real :
  336. dataTypeNames.Add ("real");
  337. row ["ProviderType"] = (int) SqlDbType.Real;
  338. row ["DataType"] = typeof (float);
  339. break;
  340. case TdsColumnType.Money :
  341. case TdsColumnType.MoneyN :
  342. case TdsColumnType.Money4 :
  343. dataTypeNames.Add ("money");
  344. row ["ProviderType"] = (int) SqlDbType.Money;
  345. row ["DataType"] = typeof (decimal);
  346. break;
  347. case TdsColumnType.Float8 :
  348. case TdsColumnType.FloatN :
  349. dataTypeNames.Add ("float");
  350. row ["ProviderType"] = (int) SqlDbType.Float;
  351. row ["DataType"] = typeof (double);
  352. break;
  353. case TdsColumnType.NText :
  354. dataTypeNames.Add ("ntext");
  355. row ["ProviderType"] = (int) SqlDbType.NText;
  356. row ["DataType"] = typeof (string);
  357. row ["IsLong"] = true;
  358. break;
  359. case TdsColumnType.NVarChar :
  360. dataTypeNames.Add ("nvarchar");
  361. row ["ProviderType"] = (int) SqlDbType.NVarChar;
  362. row ["DataType"] = typeof (string);
  363. break;
  364. case TdsColumnType.Decimal :
  365. case TdsColumnType.Numeric :
  366. dataTypeNames.Add ("decimal");
  367. row ["ProviderType"] = (int) SqlDbType.Decimal;
  368. row ["DataType"] = typeof (decimal);
  369. break;
  370. case TdsColumnType.NChar :
  371. dataTypeNames.Add ("nchar");
  372. row ["ProviderType"] = (int) SqlDbType.Char;
  373. row ["DataType"] = typeof (string);
  374. break;
  375. case TdsColumnType.SmallMoney :
  376. dataTypeNames.Add ("smallmoney");
  377. row ["ProviderType"] = (int) SqlDbType.SmallMoney;
  378. row ["DataType"] = typeof (decimal);
  379. break;
  380. default :
  381. dataTypeNames.Add ("variant");
  382. row ["ProviderType"] = (int) SqlDbType.Variant;
  383. row ["DataType"] = typeof (object);
  384. break;
  385. }
  386. // load schema values
  387. row ["ColumnOrdinal"] = schema.ColumnOrdinal;
  388. row ["ColumnSize"] = schema.ColumnSize;
  389. row ["AllowDBNull"] = schema.AllowDBNull;
  390. row ["IsExpression"] = schema.IsExpression;
  391. row ["IsIdentity"] = schema.IsIdentity;
  392. row ["IsReadOnly"] = schema.IsReadOnly;
  393. row ["IsKey"] = schema.IsKey;
  394. if (schema.BaseColumnName != null)
  395. row ["BaseColumnName"] = schema.BaseColumnName;
  396. if (schema.ColumnName != null)
  397. row ["ColumnName"] = schema.ColumnName;
  398. if (schema.BaseTableName != null)
  399. row ["BaseTableName"] = schema.BaseTableName;
  400. if (schema.NumericScale != 0)
  401. row ["NumericPrecision"] = schema.NumericPrecision;
  402. if (schema.NumericScale == 0)
  403. row ["NumericScale"] = schema.NumericScale;
  404. schemaTable.Rows.Add (row);
  405. fieldCount += 1;
  406. }
  407. return schemaTable;
  408. }
  409. public SqlBinary GetSqlBinary (int i)
  410. {
  411. throw new NotImplementedException ();
  412. }
  413. public SqlBoolean GetSqlBoolean (int i)
  414. {
  415. object value = GetValue (i);
  416. if (value == null)
  417. return SqlBoolean.Null;
  418. if (!(value is bool))
  419. throw new InvalidCastException ();
  420. return (bool) value;
  421. }
  422. public SqlByte GetSqlByte (int i)
  423. {
  424. object value = GetValue (i);
  425. if (value == null)
  426. return SqlByte.Null;
  427. if (!(value is byte))
  428. throw new InvalidCastException ();
  429. return (byte) value;
  430. }
  431. public SqlDateTime GetSqlDateTime (int i)
  432. {
  433. object value = GetValue (i);
  434. if (value == null)
  435. return SqlDateTime.Null;
  436. if (!(value is DateTime))
  437. throw new InvalidCastException ();
  438. return (DateTime) value;
  439. }
  440. public SqlDecimal GetSqlDecimal (int i)
  441. {
  442. object value = GetValue (i);
  443. if (value == null)
  444. return SqlDecimal.Null;
  445. if (!(value is TdsBigDecimal))
  446. throw new InvalidCastException ();
  447. return SqlDecimal.FromTdsBigDecimal ((TdsBigDecimal) value);
  448. }
  449. public SqlDouble GetSqlDouble (int i)
  450. {
  451. object value = GetValue (i);
  452. if (value == null)
  453. return SqlDouble.Null;
  454. if (!(value is double))
  455. throw new InvalidCastException ();
  456. return (double) value;
  457. }
  458. public SqlGuid GetSqlGuid (int i)
  459. {
  460. object value = GetValue (i);
  461. if (value == null)
  462. return SqlGuid.Null;
  463. if (!(value is Guid))
  464. throw new InvalidCastException ();
  465. return (Guid) value;
  466. }
  467. public SqlInt16 GetSqlInt16 (int i)
  468. {
  469. object value = GetValue (i);
  470. if (value == null)
  471. return SqlInt16.Null;
  472. if (!(value is short))
  473. throw new InvalidCastException ();
  474. return (short) value;
  475. }
  476. public SqlInt32 GetSqlInt32 (int i)
  477. {
  478. object value = GetValue (i);
  479. if (value == null)
  480. return SqlInt32.Null;
  481. if (!(value is int))
  482. throw new InvalidCastException ();
  483. return (int) value;
  484. }
  485. public SqlInt64 GetSqlInt64 (int i)
  486. {
  487. object value = GetValue (i);
  488. if (value == null)
  489. return SqlInt64.Null;
  490. if (!(value is long))
  491. throw new InvalidCastException ();
  492. return (long) value;
  493. }
  494. public SqlMoney GetSqlMoney (int i)
  495. {
  496. object value = GetValue (i);
  497. if (value == null)
  498. return SqlMoney.Null;
  499. if (!(value is TdsBigDecimal))
  500. throw new InvalidCastException ();
  501. return (SqlMoney) (SqlDecimal) value;
  502. }
  503. public SqlSingle GetSqlSingle (int i)
  504. {
  505. object value = GetValue (i);
  506. if (value == null)
  507. return SqlSingle.Null;
  508. if (!(value is float))
  509. throw new InvalidCastException ();
  510. return (float) value;
  511. }
  512. public SqlString GetSqlString (int i)
  513. {
  514. object value = GetValue (i);
  515. if (value == null)
  516. return SqlString.Null;
  517. if (!(value is string))
  518. throw new InvalidCastException ();
  519. return (string) value;
  520. }
  521. [MonoTODO ("Implement GetSqlValue")]
  522. public object GetSqlValue (int i)
  523. {
  524. object value = GetValue (i);
  525. if (value == null)
  526. return DBNull.Value;
  527. throw new NotImplementedException ();
  528. }
  529. [MonoTODO ("Implement GetSqlValues")]
  530. public int GetSqlValues (object[] values)
  531. {
  532. throw new NotImplementedException ();
  533. }
  534. public string GetString (int i)
  535. {
  536. object value = GetValue (i);
  537. if (!(value is string))
  538. throw new InvalidCastException ();
  539. return (string) value;
  540. }
  541. public object GetValue (int i)
  542. {
  543. return command.Tds.ColumnValues[i];
  544. }
  545. public int GetValues (object[] values)
  546. {
  547. int len = values.Length;
  548. command.Tds.ColumnValues.CopyTo (0, values, 0, len);
  549. return (len > FieldCount ? len : FieldCount);
  550. }
  551. [MonoTODO ("Implement Dispose(). Determine what must be disposed of.")]
  552. void IDisposable.Dispose ()
  553. {
  554. throw new NotImplementedException ();
  555. }
  556. IEnumerator IEnumerable.GetEnumerator ()
  557. {
  558. return new DbEnumerator (this);
  559. }
  560. public bool IsDBNull (int i)
  561. {
  562. return GetValue (i) == null;
  563. }
  564. public bool NextResult ()
  565. {
  566. if ((command.CommandBehavior & CommandBehavior.SingleResult) != 0 && resultsRead > 0)
  567. return false;
  568. if (command.Tds.DoneProc)
  569. return false;
  570. schemaTable.Rows.Clear ();
  571. moreResults = command.Tds.NextResult ();
  572. GetSchemaTable ();
  573. rowsRead = 0;
  574. resultsRead += 1;
  575. return moreResults;
  576. }
  577. public bool Read ()
  578. {
  579. if ((command.CommandBehavior & CommandBehavior.SingleRow) != 0 && rowsRead > 0)
  580. return false;
  581. if ((command.CommandBehavior & CommandBehavior.SchemaOnly) != 0)
  582. return false;
  583. if (!moreResults)
  584. return false;
  585. bool result = command.Tds.NextRow ();
  586. rowsRead += 1;
  587. return result;
  588. }
  589. #endregion // Methods
  590. }
  591. }