SqlDataReader.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  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]
  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]
  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. [MonoTODO]
  194. public Guid GetGuid (int i)
  195. {
  196. throw new NotImplementedException ();
  197. }
  198. public short GetInt16 (int i)
  199. {
  200. object value = GetValue (i);
  201. if (!(value is short))
  202. throw new InvalidCastException ();
  203. return (short) value;
  204. }
  205. public int GetInt32 (int i)
  206. {
  207. object value = GetValue (i);
  208. if (!(value is int))
  209. throw new InvalidCastException ();
  210. return (int) value;
  211. }
  212. public long GetInt64 (int i)
  213. {
  214. object value = GetValue (i);
  215. if (!(value is long))
  216. throw new InvalidCastException ();
  217. return (long) value;
  218. }
  219. public string GetName (int i)
  220. {
  221. return (string) schemaTable.Rows[i]["ColumnName"];
  222. }
  223. [MonoTODO]
  224. public int GetOrdinal (string name)
  225. {
  226. foreach (DataRow schemaRow in schemaTable.Rows)
  227. if (((string) schemaRow ["ColumnName"]).Equals (name))
  228. return (int) schemaRow ["ColumnOrdinal"];
  229. foreach (DataRow schemaRow in schemaTable.Rows)
  230. if (String.Compare (((string) schemaRow ["ColumnName"]), name, true) == 0)
  231. return (int) schemaRow ["ColumnOrdinal"];
  232. throw new IndexOutOfRangeException ();
  233. }
  234. public DataTable GetSchemaTable ()
  235. {
  236. if (schemaTable.Rows != null && schemaTable.Rows.Count > 0)
  237. return schemaTable;
  238. if (!moreResults)
  239. return null;
  240. fieldCount = 0;
  241. dataTypeNames = new ArrayList ();
  242. dataTypes = new ArrayList ();
  243. foreach (TdsSchemaInfo schema in command.Tds.Schema) {
  244. DataRow row = schemaTable.NewRow ();
  245. // set default values
  246. row ["AllowDBNull"] = true;
  247. row ["BaseCatalogName"] = DBNull.Value;
  248. row ["BaseColumnName"] = DBNull.Value;
  249. row ["BaseSchemaName"] = DBNull.Value;
  250. row ["BaseTableName"] = DBNull.Value;
  251. row ["ColumnName"] = DBNull.Value;
  252. row ["IsAutoIncrement"] = false;
  253. row ["IsHidden"] = false;
  254. row ["IsLong"] = false;
  255. row ["IsRowVersion"] = false;
  256. row ["IsUnique"] = false;
  257. row ["NumericPrecision"] = DBNull.Value;
  258. row ["NumericScale"] = DBNull.Value;
  259. switch (schema.ColumnType) {
  260. case TdsColumnType.Image :
  261. dataTypeNames.Add ("image");
  262. row ["ProviderType"] = (int) SqlDbType.Image;
  263. row ["DataType"] = typeof (byte[]);
  264. row ["IsLong"] = true;
  265. break;
  266. case TdsColumnType.Text :
  267. dataTypes.Add (typeof (string));
  268. dataTypeNames.Add ("text");
  269. row ["ProviderType"] = (int) SqlDbType.Text;
  270. row ["DataType"] = typeof (string);
  271. row ["IsLong"] = true;
  272. break;
  273. case TdsColumnType.UniqueIdentifier :
  274. dataTypeNames.Add ("uniqueidentifier");
  275. row ["ProviderType"] = (int) SqlDbType.UniqueIdentifier;
  276. row ["DataType"] = typeof (Guid);
  277. break;
  278. case TdsColumnType.VarBinary :
  279. case TdsColumnType.BigVarBinary :
  280. dataTypeNames.Add ("varbinary");
  281. row ["ProviderType"] = (int) SqlDbType.VarBinary;
  282. row ["DataType"] = typeof (byte[]);
  283. row ["IsLong"] = true;
  284. break;
  285. case TdsColumnType.IntN :
  286. case TdsColumnType.Int4 :
  287. dataTypeNames.Add ("int");
  288. row ["ProviderType"] = (int) SqlDbType.Int;
  289. row ["DataType"] = typeof (int);
  290. break;
  291. case TdsColumnType.VarChar :
  292. case TdsColumnType.BigVarChar :
  293. dataTypeNames.Add ("varchar");
  294. row ["ProviderType"] = (int) SqlDbType.VarChar;
  295. row ["DataType"] = typeof (string);
  296. break;
  297. case TdsColumnType.Binary :
  298. case TdsColumnType.BigBinary :
  299. dataTypeNames.Add ("binary");
  300. row ["ProviderType"] = (int) SqlDbType.Binary;
  301. row ["DataType"] = typeof (byte[]);
  302. row ["IsLong"] = true;
  303. break;
  304. case TdsColumnType.Char :
  305. case TdsColumnType.BigChar :
  306. dataTypeNames.Add ("char");
  307. row ["ProviderType"] = (int) SqlDbType.Char;
  308. row ["DataType"] = typeof (string);
  309. break;
  310. case TdsColumnType.Int1 :
  311. dataTypeNames.Add ("tinyint");
  312. row ["ProviderType"] = (int) SqlDbType.TinyInt;
  313. row ["DataType"] = typeof (byte);
  314. break;
  315. case TdsColumnType.Bit :
  316. case TdsColumnType.BitN :
  317. dataTypeNames.Add ("bit");
  318. row ["ProviderType"] = (int) SqlDbType.Bit;
  319. row ["DataType"] = typeof (bool);
  320. break;
  321. case TdsColumnType.Int2 :
  322. dataTypeNames.Add ("smallint");
  323. row ["ProviderType"] = (int) SqlDbType.SmallInt;
  324. row ["DataType"] = typeof (short);
  325. break;
  326. case TdsColumnType.DateTime4 :
  327. case TdsColumnType.DateTime :
  328. case TdsColumnType.DateTimeN :
  329. dataTypeNames.Add ("datetime");
  330. row ["ProviderType"] = (int) SqlDbType.DateTime;
  331. row ["DataType"] = typeof (DateTime);
  332. break;
  333. case TdsColumnType.Real :
  334. dataTypeNames.Add ("real");
  335. row ["ProviderType"] = (int) SqlDbType.Real;
  336. row ["DataType"] = typeof (float);
  337. break;
  338. case TdsColumnType.Money :
  339. case TdsColumnType.MoneyN :
  340. case TdsColumnType.Money4 :
  341. dataTypeNames.Add ("money");
  342. row ["ProviderType"] = (int) SqlDbType.Money;
  343. row ["DataType"] = typeof (decimal);
  344. break;
  345. case TdsColumnType.Float8 :
  346. case TdsColumnType.FloatN :
  347. dataTypeNames.Add ("float");
  348. row ["ProviderType"] = (int) SqlDbType.Float;
  349. row ["DataType"] = typeof (double);
  350. break;
  351. case TdsColumnType.NText :
  352. dataTypeNames.Add ("ntext");
  353. row ["ProviderType"] = (int) SqlDbType.NText;
  354. row ["DataType"] = typeof (string);
  355. row ["IsLong"] = true;
  356. break;
  357. case TdsColumnType.NVarChar :
  358. dataTypeNames.Add ("nvarchar");
  359. row ["ProviderType"] = (int) SqlDbType.NVarChar;
  360. row ["DataType"] = typeof (string);
  361. break;
  362. case TdsColumnType.Decimal :
  363. case TdsColumnType.Numeric :
  364. dataTypeNames.Add ("decimal");
  365. row ["ProviderType"] = (int) SqlDbType.Decimal;
  366. row ["DataType"] = typeof (decimal);
  367. break;
  368. case TdsColumnType.NChar :
  369. dataTypeNames.Add ("nchar");
  370. row ["ProviderType"] = (int) SqlDbType.Char;
  371. row ["DataType"] = typeof (string);
  372. break;
  373. case TdsColumnType.SmallMoney :
  374. dataTypeNames.Add ("smallmoney");
  375. row ["ProviderType"] = (int) SqlDbType.SmallMoney;
  376. row ["DataType"] = typeof (decimal);
  377. break;
  378. default :
  379. dataTypeNames.Add ("variant");
  380. row ["ProviderType"] = (int) SqlDbType.Variant;
  381. row ["DataType"] = typeof (object);
  382. break;
  383. }
  384. // load schema values
  385. row ["ColumnOrdinal"] = schema.ColumnOrdinal;
  386. row ["ColumnSize"] = schema.ColumnSize;
  387. row ["AllowDBNull"] = schema.AllowDBNull;
  388. row ["IsExpression"] = schema.IsExpression;
  389. row ["IsIdentity"] = schema.IsIdentity;
  390. row ["IsReadOnly"] = schema.IsReadOnly;
  391. row ["IsKey"] = schema.IsKey;
  392. if (schema.BaseColumnName != null)
  393. row ["BaseColumnName"] = schema.BaseColumnName;
  394. if (schema.ColumnName != null)
  395. row ["ColumnName"] = schema.ColumnName;
  396. if (schema.BaseTableName != null)
  397. row ["BaseTableName"] = schema.BaseTableName;
  398. if (schema.NumericScale != 0)
  399. row ["NumericPrecision"] = schema.NumericPrecision;
  400. if (schema.NumericScale == 0)
  401. row ["NumericScale"] = schema.NumericScale;
  402. schemaTable.Rows.Add (row);
  403. fieldCount += 1;
  404. }
  405. return schemaTable;
  406. }
  407. public SqlBinary GetSqlBinary (int i)
  408. {
  409. throw new NotImplementedException ();
  410. }
  411. public SqlBoolean GetSqlBoolean (int i)
  412. {
  413. object value = GetValue (i);
  414. if (value == null)
  415. return SqlBoolean.Null;
  416. if (!(value is bool))
  417. throw new InvalidCastException ();
  418. return (bool) value;
  419. }
  420. public SqlByte GetSqlByte (int i)
  421. {
  422. object value = GetValue (i);
  423. if (value == null)
  424. return SqlByte.Null;
  425. if (!(value is byte))
  426. throw new InvalidCastException ();
  427. return (byte) value;
  428. }
  429. public SqlDateTime GetSqlDateTime (int i)
  430. {
  431. object value = GetValue (i);
  432. if (value == null)
  433. return SqlDateTime.Null;
  434. if (!(value is DateTime))
  435. throw new InvalidCastException ();
  436. return (DateTime) value;
  437. }
  438. public SqlDecimal GetSqlDecimal (int i)
  439. {
  440. object value = GetValue (i);
  441. if (value == null)
  442. return SqlDecimal.Null;
  443. if (!(value is TdsBigDecimal))
  444. throw new InvalidCastException ();
  445. return SqlDecimal.FromTdsBigDecimal ((TdsBigDecimal) value);
  446. }
  447. public SqlDouble GetSqlDouble (int i)
  448. {
  449. object value = GetValue (i);
  450. if (value == null)
  451. return SqlDouble.Null;
  452. if (!(value is double))
  453. throw new InvalidCastException ();
  454. return (double) value;
  455. }
  456. public SqlGuid GetSqlGuid (int i)
  457. {
  458. object value = GetValue (i);
  459. if (value == null)
  460. return SqlGuid.Null;
  461. if (!(value is Guid))
  462. throw new InvalidCastException ();
  463. return (Guid) value;
  464. }
  465. public SqlInt16 GetSqlInt16 (int i)
  466. {
  467. object value = GetValue (i);
  468. if (value == null)
  469. return SqlInt16.Null;
  470. if (!(value is short))
  471. throw new InvalidCastException ();
  472. return (short) value;
  473. }
  474. public SqlInt32 GetSqlInt32 (int i)
  475. {
  476. object value = GetValue (i);
  477. if (value == null)
  478. return SqlInt32.Null;
  479. if (!(value is int))
  480. throw new InvalidCastException ();
  481. return (int) value;
  482. }
  483. public SqlInt64 GetSqlInt64 (int i)
  484. {
  485. object value = GetValue (i);
  486. if (value == null)
  487. return SqlInt64.Null;
  488. if (!(value is long))
  489. throw new InvalidCastException ();
  490. return (long) value;
  491. }
  492. public SqlMoney GetSqlMoney (int i)
  493. {
  494. object value = GetValue (i);
  495. if (value == null)
  496. return SqlMoney.Null;
  497. if (!(value is TdsBigDecimal))
  498. throw new InvalidCastException ();
  499. return (SqlMoney) (SqlDecimal) value;
  500. }
  501. public SqlSingle GetSqlSingle (int i)
  502. {
  503. object value = GetValue (i);
  504. if (value == null)
  505. return SqlSingle.Null;
  506. if (!(value is float))
  507. throw new InvalidCastException ();
  508. return (float) value;
  509. }
  510. public SqlString GetSqlString (int i)
  511. {
  512. object value = GetValue (i);
  513. if (value == null)
  514. return SqlString.Null;
  515. if (!(value is string))
  516. throw new InvalidCastException ();
  517. return (string) value;
  518. }
  519. [MonoTODO]
  520. public object GetSqlValue (int i)
  521. {
  522. object value = GetValue (i);
  523. if (value == null)
  524. return DBNull.Value;
  525. throw new NotImplementedException ();
  526. }
  527. [MonoTODO]
  528. public int GetSqlValues (object[] values)
  529. {
  530. throw new NotImplementedException ();
  531. }
  532. public string GetString (int i)
  533. {
  534. object value = GetValue (i);
  535. if (!(value is string))
  536. throw new InvalidCastException ();
  537. return (string) value;
  538. }
  539. public object GetValue (int i)
  540. {
  541. return command.Tds.ColumnValues[i];
  542. }
  543. public int GetValues (object[] values)
  544. {
  545. int len = values.Length;
  546. command.Tds.ColumnValues.CopyTo (0, values, 0, len);
  547. return (len > FieldCount ? len : FieldCount);
  548. }
  549. [MonoTODO]
  550. void IDisposable.Dispose ()
  551. {
  552. throw new NotImplementedException ();
  553. }
  554. IEnumerator IEnumerable.GetEnumerator ()
  555. {
  556. return new DbEnumerator (this);
  557. }
  558. public bool IsDBNull (int i)
  559. {
  560. return GetValue (i) == null;
  561. }
  562. public bool NextResult ()
  563. {
  564. if ((command.CommandBehavior & CommandBehavior.SingleResult) != 0 && resultsRead > 0)
  565. return false;
  566. if (command.Tds.DoneProc)
  567. return false;
  568. schemaTable.Rows.Clear ();
  569. moreResults = command.Tds.NextResult ();
  570. GetSchemaTable ();
  571. rowsRead = 0;
  572. resultsRead += 1;
  573. return moreResults;
  574. }
  575. public bool Read ()
  576. {
  577. if ((command.CommandBehavior & CommandBehavior.SingleRow) != 0 && rowsRead > 0)
  578. return false;
  579. if ((command.CommandBehavior & CommandBehavior.SchemaOnly) != 0)
  580. return false;
  581. if (!moreResults)
  582. return false;
  583. bool result = command.Tds.NextRow ();
  584. rowsRead += 1;
  585. return result;
  586. }
  587. #endregion // Methods
  588. }
  589. }