SqlDataReader.cs 13 KB

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