SqlDataReader.cs 17 KB

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