SqlDataReader.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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. namespace System.Data.SqlClient {
  20. public sealed class SqlDataReader : MarshalByRefObject, IEnumerable, IDataReader, IDisposable, IDataRecord
  21. {
  22. #region Fields
  23. int fieldCount;
  24. bool hasRows;
  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 HasRows {
  54. get { return hasRows; }
  55. }
  56. public bool IsClosed {
  57. get { return isClosed; }
  58. }
  59. public object this [int i] {
  60. get { return GetValue (i); }
  61. }
  62. public object this [string name] {
  63. get { return GetValue (GetOrdinal (name)); }
  64. }
  65. public int RecordsAffected {
  66. get { return recordsAffected; }
  67. }
  68. #endregion // Properties
  69. #region Methods
  70. public void Close()
  71. {
  72. isClosed = true;
  73. command.CloseDataReader (moreResults);
  74. }
  75. private static DataTable ConstructSchemaTable ()
  76. {
  77. Type booleanType = Type.GetType ("System.Boolean");
  78. Type stringType = Type.GetType ("System.String");
  79. Type intType = Type.GetType ("System.Int32");
  80. Type typeType = Type.GetType ("System.Type");
  81. Type shortType = Type.GetType ("System.Int16");
  82. DataTable schemaTable = new DataTable ("SchemaTable");
  83. schemaTable.Columns.Add ("ColumnName", stringType);
  84. schemaTable.Columns.Add ("ColumnOrdinal", intType);
  85. schemaTable.Columns.Add ("ColumnSize", intType);
  86. schemaTable.Columns.Add ("NumericPrecision", shortType);
  87. schemaTable.Columns.Add ("NumericScale", shortType);
  88. schemaTable.Columns.Add ("IsUnique", booleanType);
  89. schemaTable.Columns.Add ("IsKey", booleanType);
  90. schemaTable.Columns.Add ("BaseServerName", stringType);
  91. schemaTable.Columns.Add ("BaseCatalogName", stringType);
  92. schemaTable.Columns.Add ("BaseColumnName", stringType);
  93. schemaTable.Columns.Add ("BaseSchemaName", stringType);
  94. schemaTable.Columns.Add ("BaseTableName", stringType);
  95. schemaTable.Columns.Add ("DataType", typeType);
  96. schemaTable.Columns.Add ("AllowDBNull", booleanType);
  97. schemaTable.Columns.Add ("ProviderType", intType);
  98. schemaTable.Columns.Add ("IsAliased", booleanType);
  99. schemaTable.Columns.Add ("IsExpression", booleanType);
  100. schemaTable.Columns.Add ("IsIdentity", booleanType);
  101. schemaTable.Columns.Add ("IsAutoIncrement", booleanType);
  102. schemaTable.Columns.Add ("IsRowVersion", booleanType);
  103. schemaTable.Columns.Add ("IsHidden", booleanType);
  104. schemaTable.Columns.Add ("IsLong", booleanType);
  105. schemaTable.Columns.Add ("IsReadOnly", booleanType);
  106. return schemaTable;
  107. }
  108. public bool GetBoolean (int i)
  109. {
  110. object value = GetValue (i);
  111. if (!(value is bool))
  112. throw new InvalidCastException ();
  113. return (bool) value;
  114. }
  115. public byte GetByte (int i)
  116. {
  117. object value = GetValue (i);
  118. if (!(value is byte))
  119. throw new InvalidCastException ();
  120. return (byte) value;
  121. }
  122. [MonoTODO]
  123. public long GetBytes (int i, long dataIndex, byte[] buffer, int bufferIndex, int length)
  124. {
  125. object value = GetValue (i);
  126. if (!(value is byte []))
  127. throw new InvalidCastException ();
  128. Array.Copy ((byte []) value, (int) dataIndex, buffer, bufferIndex, length);
  129. return ((byte []) value).Length - dataIndex;
  130. }
  131. [MonoTODO]
  132. public char GetChar (int i)
  133. {
  134. throw new NotImplementedException ();
  135. }
  136. [MonoTODO]
  137. public long GetChars (int i, long dataIndex, char[] buffer, int bufferIndex, int length)
  138. {
  139. object value = GetValue (i);
  140. if (!(value is char []))
  141. throw new InvalidCastException ();
  142. Array.Copy ((char []) value, (int) dataIndex, buffer, bufferIndex, length);
  143. return ((char []) value).Length - dataIndex;
  144. }
  145. [MonoTODO]
  146. public IDataReader GetData (int i)
  147. {
  148. throw new NotImplementedException ();
  149. }
  150. public string GetDataTypeName (int i)
  151. {
  152. return (string) dataTypeNames [i];
  153. }
  154. public DateTime GetDateTime (int i)
  155. {
  156. object value = GetValue (i);
  157. if (!(value is DateTime))
  158. throw new InvalidCastException ();
  159. return (DateTime) value;
  160. }
  161. public decimal GetDecimal (int i)
  162. {
  163. object value = GetValue (i);
  164. if (!(value is decimal))
  165. throw new InvalidCastException ();
  166. return (decimal) value;
  167. }
  168. public double GetDouble (int i)
  169. {
  170. object value = GetValue (i);
  171. if (!(value is double))
  172. throw new InvalidCastException ();
  173. return (double) value;
  174. }
  175. public Type GetFieldType (int i)
  176. {
  177. return (Type) schemaTable.Rows[i]["DataType"];
  178. }
  179. public float GetFloat (int i)
  180. {
  181. object value = GetValue (i);
  182. if (!(value is float))
  183. throw new InvalidCastException ();
  184. return (float) value;
  185. }
  186. [MonoTODO]
  187. public Guid GetGuid (int i)
  188. {
  189. throw new NotImplementedException ();
  190. }
  191. public short GetInt16 (int i)
  192. {
  193. object value = GetValue (i);
  194. if (!(value is short))
  195. throw new InvalidCastException ();
  196. return (short) value;
  197. }
  198. public int GetInt32 (int i)
  199. {
  200. object value = GetValue (i);
  201. if (!(value is int))
  202. throw new InvalidCastException ();
  203. return (int) value;
  204. }
  205. public long GetInt64 (int i)
  206. {
  207. object value = GetValue (i);
  208. if (!(value is long))
  209. throw new InvalidCastException ();
  210. return (long) value;
  211. }
  212. public string GetName (int i)
  213. {
  214. return (string) schemaTable.Rows[i]["ColumnName"];
  215. }
  216. [MonoTODO]
  217. public int GetOrdinal (string name)
  218. {
  219. foreach (DataRow schemaRow in schemaTable.Rows)
  220. if (((string) schemaRow ["ColumnName"]).Equals (name))
  221. return (int) schemaRow ["ColumnOrdinal"];
  222. foreach (DataRow schemaRow in schemaTable.Rows)
  223. if (String.Compare (((string) schemaRow ["ColumnName"]), name, true) == 0)
  224. return (int) schemaRow ["ColumnOrdinal"];
  225. throw new IndexOutOfRangeException ();
  226. }
  227. public DataTable GetSchemaTable ()
  228. {
  229. if (schemaTable.Rows != null && schemaTable.Rows.Count > 0)
  230. return schemaTable;
  231. if (!moreResults)
  232. return null;
  233. fieldCount = 0;
  234. dataTypeNames = new ArrayList ();
  235. dataTypes = new ArrayList ();
  236. foreach (TdsSchemaInfo schema in command.Tds.Schema) {
  237. DataRow row = schemaTable.NewRow ();
  238. switch (schema.ColumnType) {
  239. case TdsColumnType.Image :
  240. dataTypeNames.Add ("image");
  241. row ["ProviderType"] = (int) SqlDbType.Image;
  242. row ["DataType"] = typeof (byte[]);
  243. break;
  244. case TdsColumnType.Text :
  245. dataTypes.Add (typeof (string));
  246. dataTypeNames.Add ("text");
  247. row ["ProviderType"] = (int) SqlDbType.Text;
  248. row ["DataType"] = typeof (string);
  249. break;
  250. case TdsColumnType.UniqueIdentifier :
  251. dataTypeNames.Add ("uniqueidentifier");
  252. row ["ProviderType"] = (int) SqlDbType.UniqueIdentifier;
  253. row ["DataType"] = typeof (Guid);
  254. break;
  255. case TdsColumnType.VarBinary :
  256. case TdsColumnType.BigVarBinary :
  257. dataTypeNames.Add ("varbinary");
  258. row ["ProviderType"] = (int) SqlDbType.VarBinary;
  259. row ["DataType"] = typeof (byte[]);
  260. break;
  261. case TdsColumnType.IntN :
  262. case TdsColumnType.Int4 :
  263. dataTypeNames.Add ("int");
  264. row ["ProviderType"] = (int) SqlDbType.Int;
  265. row ["DataType"] = typeof (int);
  266. break;
  267. case TdsColumnType.VarChar :
  268. case TdsColumnType.BigVarChar :
  269. dataTypeNames.Add ("varchar");
  270. row ["ProviderType"] = (int) SqlDbType.VarChar;
  271. row ["DataType"] = typeof (string);
  272. break;
  273. case TdsColumnType.Binary :
  274. case TdsColumnType.BigBinary :
  275. dataTypeNames.Add ("binary");
  276. row ["ProviderType"] = (int) SqlDbType.Binary;
  277. row ["DataType"] = typeof (byte[]);
  278. break;
  279. case TdsColumnType.Char :
  280. case TdsColumnType.BigChar :
  281. dataTypeNames.Add ("char");
  282. row ["ProviderType"] = (int) SqlDbType.Char;
  283. row ["DataType"] = typeof (string);
  284. break;
  285. case TdsColumnType.Int1 :
  286. dataTypeNames.Add ("tinyint");
  287. row ["ProviderType"] = (int) SqlDbType.TinyInt;
  288. row ["DataType"] = typeof (byte);
  289. break;
  290. case TdsColumnType.Bit :
  291. case TdsColumnType.BitN :
  292. dataTypeNames.Add ("bit");
  293. row ["ProviderType"] = (int) SqlDbType.Bit;
  294. row ["DataType"] = typeof (bool);
  295. break;
  296. case TdsColumnType.Int2 :
  297. dataTypeNames.Add ("smallint");
  298. row ["ProviderType"] = (int) SqlDbType.SmallInt;
  299. row ["DataType"] = typeof (short);
  300. break;
  301. case TdsColumnType.DateTime4 :
  302. case TdsColumnType.DateTime :
  303. case TdsColumnType.DateTimeN :
  304. dataTypeNames.Add ("datetime");
  305. row ["ProviderType"] = (int) SqlDbType.DateTime;
  306. row ["DataType"] = typeof (DateTime);
  307. break;
  308. case TdsColumnType.Real :
  309. dataTypeNames.Add ("real");
  310. row ["ProviderType"] = (int) SqlDbType.Real;
  311. row ["DataType"] = typeof (float);
  312. break;
  313. case TdsColumnType.Money :
  314. case TdsColumnType.MoneyN :
  315. case TdsColumnType.Money4 :
  316. dataTypeNames.Add ("money");
  317. row ["ProviderType"] = (int) SqlDbType.Money;
  318. row ["DataType"] = typeof (decimal);
  319. break;
  320. case TdsColumnType.Float8 :
  321. case TdsColumnType.FloatN :
  322. dataTypeNames.Add ("float");
  323. row ["ProviderType"] = (int) SqlDbType.Float;
  324. row ["DataType"] = typeof (double);
  325. break;
  326. case TdsColumnType.NText :
  327. dataTypeNames.Add ("ntext");
  328. row ["ProviderType"] = (int) SqlDbType.NText;
  329. row ["DataType"] = typeof (string);
  330. break;
  331. case TdsColumnType.NVarChar :
  332. dataTypeNames.Add ("nvarchar");
  333. row ["ProviderType"] = (int) SqlDbType.NVarChar;
  334. row ["DataType"] = typeof (string);
  335. break;
  336. case TdsColumnType.Decimal :
  337. case TdsColumnType.Numeric :
  338. dataTypeNames.Add ("decimal");
  339. row ["ProviderType"] = (int) SqlDbType.Decimal;
  340. row ["DataType"] = typeof (decimal);
  341. break;
  342. case TdsColumnType.NChar :
  343. dataTypeNames.Add ("nchar");
  344. row ["ProviderType"] = (int) SqlDbType.Char;
  345. row ["DataType"] = typeof (string);
  346. break;
  347. case TdsColumnType.SmallMoney :
  348. dataTypeNames.Add ("smallmoney");
  349. row ["ProviderType"] = (int) SqlDbType.SmallMoney;
  350. row ["DataType"] = typeof (decimal);
  351. break;
  352. default :
  353. dataTypeNames.Add ("variant");
  354. row ["ProviderType"] = (int) SqlDbType.Variant;
  355. row ["DataType"] = typeof (object);
  356. break;
  357. }
  358. // set default values
  359. row ["AllowDBNull"] = true;
  360. row ["BaseCatalogName"] = DBNull.Value;
  361. row ["BaseColumnName"] = DBNull.Value;
  362. row ["BaseSchemaName"] = DBNull.Value;
  363. row ["BaseTableName"] = DBNull.Value;
  364. row ["ColumnName"] = DBNull.Value;
  365. row ["IsUnique"] = false;
  366. row ["NumericPrecision"] = DBNull.Value;
  367. row ["NumericScale"] = DBNull.Value;
  368. // load schema values
  369. row ["ColumnOrdinal"] = schema.ColumnOrdinal;
  370. row ["ColumnSize"] = schema.ColumnSize;
  371. row ["AllowDBNull"] = schema.AllowDBNull;
  372. row ["IsExpression"] = schema.IsExpression;
  373. row ["IsIdentity"] = schema.IsIdentity;
  374. row ["IsReadOnly"] = schema.IsReadOnly;
  375. row ["IsKey"] = schema.IsKey;
  376. if (schema.BaseColumnName != null)
  377. row ["BaseColumnName"] = schema.BaseColumnName;
  378. if (schema.ColumnName != null)
  379. row ["ColumnName"] = schema.ColumnName;
  380. if (schema.BaseTableName != null)
  381. row ["BaseTableName"] = schema.BaseTableName;
  382. if (schema.NumericScale != 0)
  383. row ["NumericPrecision"] = schema.NumericPrecision;
  384. if (schema.NumericScale == 0)
  385. row ["NumericScale"] = schema.NumericScale;
  386. schemaTable.Rows.Add (row);
  387. fieldCount += 1;
  388. }
  389. return schemaTable;
  390. }
  391. public string GetString (int i)
  392. {
  393. object value = GetValue (i);
  394. if (!(value is string))
  395. throw new InvalidCastException ();
  396. return (string) value;
  397. }
  398. public object GetValue (int i)
  399. {
  400. return command.Tds.ColumnValues[i];
  401. }
  402. public int GetValues (object[] values)
  403. {
  404. int len = values.Length;
  405. command.Tds.ColumnValues.CopyTo (0, values, 0, len);
  406. return (len > FieldCount ? len : FieldCount);
  407. }
  408. [MonoTODO]
  409. void IDisposable.Dispose ()
  410. {
  411. throw new NotImplementedException ();
  412. }
  413. IEnumerator IEnumerable.GetEnumerator ()
  414. {
  415. return new DbEnumerator (this);
  416. }
  417. public bool IsDBNull (int i)
  418. {
  419. return GetValue (i) == null;
  420. }
  421. public bool NextResult ()
  422. {
  423. if ((command.CommandBehavior & CommandBehavior.SingleResult) != 0 && resultsRead > 0)
  424. return false;
  425. if (command.Tds.DoneProc)
  426. return false;
  427. schemaTable.Rows.Clear ();
  428. moreResults = command.Tds.NextResult ();
  429. command.Connection.CheckForErrors ();
  430. rowsRead = 0;
  431. resultsRead += 1;
  432. return moreResults;
  433. }
  434. public bool Read ()
  435. {
  436. if ((command.CommandBehavior & CommandBehavior.SingleRow) != 0 && rowsRead > 0)
  437. return false;
  438. if ((command.CommandBehavior & CommandBehavior.SchemaOnly) != 0)
  439. return false;
  440. if (!moreResults)
  441. return false;
  442. bool result = command.Tds.NextRow ();
  443. command.Connection.CheckForErrors ();
  444. rowsRead += 1;
  445. return result;
  446. }
  447. #endregion // Methods
  448. }
  449. }