SqlDataReader.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. namespace System.Data.SqlClient {
  19. public sealed class SqlDataReader : MarshalByRefObject, IEnumerable, IDataReader, IDisposable, IDataRecord
  20. {
  21. #region Fields
  22. int fieldCount;
  23. bool hasRows;
  24. bool isClosed;
  25. int recordsAffected;
  26. SqlCommand command;
  27. DataTable schemaTable;
  28. #endregion // eields
  29. #region Constructors
  30. internal SqlDataReader (SqlCommand command)
  31. {
  32. schemaTable = ConstructSchemaTable ();
  33. this.command = command;
  34. this.fieldCount = 0;
  35. this.isClosed = false;
  36. NextResult ();
  37. }
  38. #endregion
  39. #region Properties
  40. public int Depth {
  41. get { return 0; }
  42. }
  43. public int FieldCount {
  44. get { return fieldCount; }
  45. }
  46. public bool HasRows {
  47. get { return hasRows; }
  48. }
  49. public bool IsClosed {
  50. get { return isClosed; }
  51. }
  52. public object this [int i] {
  53. get { return GetValue (i); }
  54. }
  55. public object this [string name] {
  56. get { return GetValue (GetOrdinal (name)); }
  57. }
  58. public int RecordsAffected {
  59. get { return recordsAffected; }
  60. }
  61. #endregion // Properties
  62. #region Methods
  63. public void Close()
  64. {
  65. isClosed = true;
  66. command.Connection.DataReaderOpen = false;
  67. }
  68. private static DataTable ConstructSchemaTable ()
  69. {
  70. Type booleanType = Type.GetType ("System.Boolean");
  71. Type stringType = Type.GetType ("System.String");
  72. Type intType = Type.GetType ("System.Int32");
  73. Type typeType = Type.GetType ("System.Type");
  74. DataTable schemaTable = new DataTable ("SchemaTable");
  75. schemaTable.Columns.Add ("ColumnName", stringType);
  76. schemaTable.Columns.Add ("ColumnOrdinal", intType);
  77. schemaTable.Columns.Add ("ColumnSize", intType);
  78. schemaTable.Columns.Add ("NumericPrecision", intType);
  79. schemaTable.Columns.Add ("NumericScale", intType);
  80. schemaTable.Columns.Add ("IsUnique", booleanType);
  81. schemaTable.Columns.Add ("IsKey", booleanType);
  82. schemaTable.Columns.Add ("BaseServerName", stringType);
  83. schemaTable.Columns.Add ("BaseCatalogName", stringType);
  84. schemaTable.Columns.Add ("BaseColumnName", stringType);
  85. schemaTable.Columns.Add ("BaseSchemaName", stringType);
  86. schemaTable.Columns.Add ("BaseTableName", stringType);
  87. schemaTable.Columns.Add ("DataType", typeType);
  88. schemaTable.Columns.Add ("AllowDBNull", booleanType);
  89. schemaTable.Columns.Add ("ProviderType", booleanType);
  90. schemaTable.Columns.Add ("IsAliased", booleanType);
  91. schemaTable.Columns.Add ("IsExpression", booleanType);
  92. schemaTable.Columns.Add ("IsIdentity", booleanType);
  93. schemaTable.Columns.Add ("IsAutoIncrement", booleanType);
  94. schemaTable.Columns.Add ("IsRowVersion", booleanType);
  95. schemaTable.Columns.Add ("IsHidden", booleanType);
  96. schemaTable.Columns.Add ("IsLong", booleanType);
  97. schemaTable.Columns.Add ("IsReadOnly", booleanType);
  98. return schemaTable;
  99. }
  100. [MonoTODO]
  101. public bool GetBoolean (int i)
  102. {
  103. throw new NotImplementedException ();
  104. }
  105. [MonoTODO]
  106. public byte GetByte (int i)
  107. {
  108. throw new NotImplementedException ();
  109. }
  110. [MonoTODO]
  111. public long GetBytes (int i, long dataIndex, byte[] buffer, int bufferIndex, int length)
  112. {
  113. throw new NotImplementedException ();
  114. }
  115. [MonoTODO]
  116. public char GetChar (int i)
  117. {
  118. throw new NotImplementedException ();
  119. }
  120. [MonoTODO]
  121. public long GetChars (int i, long dataIndex, char[] buffer, int bufferIndex, int length)
  122. {
  123. throw new NotImplementedException ();
  124. }
  125. [MonoTODO]
  126. public IDataReader GetData (int i)
  127. {
  128. throw new NotImplementedException ();
  129. }
  130. [MonoTODO]
  131. public string GetDataTypeName (int i)
  132. {
  133. throw new NotImplementedException ();
  134. }
  135. public DateTime GetDateTime (int i)
  136. {
  137. object value = GetValue (i);
  138. if (!(value is DateTime))
  139. throw new InvalidCastException ();
  140. return (DateTime) value;
  141. }
  142. public decimal GetDecimal (int i)
  143. {
  144. object value = GetValue (i);
  145. if (!(value is decimal))
  146. throw new InvalidCastException ();
  147. return (decimal) value;
  148. }
  149. public double GetDouble (int i)
  150. {
  151. object value = GetValue (i);
  152. if (!(value is double))
  153. throw new InvalidCastException ();
  154. return (double) value;
  155. }
  156. public Type GetFieldType (int i)
  157. {
  158. return GetValue (i).GetType ();
  159. }
  160. public float GetFloat (int i)
  161. {
  162. object value = GetValue (i);
  163. if (!(value is float))
  164. throw new InvalidCastException ();
  165. return (float) value;
  166. }
  167. [MonoTODO]
  168. public Guid GetGuid (int i)
  169. {
  170. throw new NotImplementedException ();
  171. }
  172. public short GetInt16 (int i)
  173. {
  174. object value = GetValue (i);
  175. if (!(value is short))
  176. throw new InvalidCastException ();
  177. return (short) value;
  178. }
  179. public int GetInt32 (int i)
  180. {
  181. object value = GetValue (i);
  182. if (!(value is int))
  183. throw new InvalidCastException ();
  184. return (int) value;
  185. }
  186. public long GetInt64 (int i)
  187. {
  188. object value = GetValue (i);
  189. if (!(value is long))
  190. throw new InvalidCastException ();
  191. return (long) value;
  192. }
  193. public string GetName (int i)
  194. {
  195. return (string) schemaTable.Rows[i]["ColumnName"];
  196. }
  197. [MonoTODO]
  198. public int GetOrdinal (string name)
  199. {
  200. foreach (DataRow schemaRow in schemaTable.Rows)
  201. if (((string) schemaRow ["ColumnName"]).Equals (name))
  202. return (int) schemaRow ["ColumnOrdinal"];
  203. foreach (DataRow schemaRow in schemaTable.Rows)
  204. if (String.Compare (((string) schemaRow ["ColumnName"]), name, true) == 0)
  205. return (int) schemaRow ["ColumnOrdinal"];
  206. throw new IndexOutOfRangeException ();
  207. }
  208. public DataTable GetSchemaTable ()
  209. {
  210. if (schemaTable.Rows != null && schemaTable.Rows.Count > 0)
  211. return schemaTable;
  212. fieldCount = 0;
  213. foreach (TdsColumnSchema schemaObject in command.Tds.ColumnInfo) {
  214. DataRow schemaRow = schemaTable.NewRow ();
  215. schemaRow ["ColumnName"] = schemaObject.ColumnName;
  216. schemaRow ["ColumnOrdinal"] = schemaObject.ColumnOrdinal;
  217. schemaRow ["BaseTableName"] = schemaObject.TableName;
  218. schemaRow ["AllowDBNull"] = schemaObject.Nullable;
  219. schemaRow ["IsReadOnly"] = !schemaObject.Writable;
  220. schemaTable.Rows.Add (schemaRow);
  221. fieldCount += 1;
  222. }
  223. return schemaTable;
  224. }
  225. public string GetString (int i)
  226. {
  227. object value = GetValue (i);
  228. if (!(value is string))
  229. throw new InvalidCastException ();
  230. return (string) value;
  231. }
  232. public object GetValue (int i)
  233. {
  234. return command.Tds.ColumnValues[i];
  235. }
  236. public int GetValues (object[] values)
  237. {
  238. int len = values.Length;
  239. command.Tds.ColumnValues.CopyTo (0, values, 0, len);
  240. return (len > FieldCount ? len : FieldCount);
  241. }
  242. [MonoTODO]
  243. void IDisposable.Dispose ()
  244. {
  245. throw new NotImplementedException ();
  246. }
  247. [MonoTODO]
  248. IEnumerator IEnumerable.GetEnumerator ()
  249. {
  250. throw new NotImplementedException ();
  251. }
  252. [MonoTODO]
  253. public bool IsDBNull (int i)
  254. {
  255. throw new NotImplementedException ();
  256. }
  257. public bool NextResult ()
  258. {
  259. schemaTable.Rows.Clear ();
  260. return command.Tds.NextResult ();
  261. }
  262. public bool Read ()
  263. {
  264. return command.Tds.NextRow ();
  265. }
  266. #endregion // Methods
  267. }
  268. }