SqlDataReader.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. //
  2. // System.Data.SqlClient.SqlDataReader
  3. //
  4. // Authors:
  5. // Konstantin Triger <[email protected]>
  6. // Boris Kirzner <[email protected]>
  7. //
  8. // (C) 2005 Mainsoft Corporation (http://www.mainsoft.com)
  9. //
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System.Data.SqlTypes;
  31. using System.Data.ProviderBase;
  32. using java.sql;
  33. namespace System.Data.SqlClient
  34. {
  35. public class SqlDataReader : AbstractDataReader
  36. {
  37. #region Constructors
  38. internal SqlDataReader(SqlCommand command) : base(command)
  39. {
  40. }
  41. #endregion // Constructors
  42. #region Properties
  43. #endregion // Properties
  44. #region Methods
  45. protected sealed override SystemException CreateException(string message, SQLException e)
  46. {
  47. return new SqlException(message, e, (SqlConnection)_command.Connection);
  48. }
  49. protected sealed override SystemException CreateException(java.io.IOException e)
  50. {
  51. return new SqlException(e, (SqlConnection)_command.Connection);
  52. }
  53. public override String GetDataTypeName(int columnIndex)
  54. {
  55. try {
  56. string jdbcTypeName = Results.getMetaData().getColumnTypeName(columnIndex + 1);
  57. return SqlConvert.JdbcTypeNameToDbTypeName(jdbcTypeName);
  58. }
  59. catch (SQLException e) {
  60. throw CreateException(e);
  61. }
  62. }
  63. protected override int GetProviderType(int jdbcType)
  64. {
  65. return (int)SqlConvert.JdbcTypeToSqlDbType(jdbcType);
  66. }
  67. // Gets the value of the specified column as a SqlBinary.
  68. public SqlBinary GetSqlBinary(int columnIndex)
  69. {
  70. byte[] bytes = GetBytes(columnIndex);
  71. if(IsDBNull(columnIndex)) {
  72. return SqlBinary.Null;
  73. }
  74. else {
  75. return new SqlBinary(bytes);
  76. }
  77. }
  78. // Gets the value of the specified column as a SqlBoolean.
  79. public SqlBoolean GetSqlBoolean(int columnIndex)
  80. {
  81. bool boolean = GetBoolean(columnIndex);
  82. if(IsDBNull(columnIndex)) {
  83. return SqlBoolean.Null;
  84. }
  85. else {
  86. return new SqlBoolean(boolean);
  87. }
  88. }
  89. // Gets the value of the specified column as a SqlByte.
  90. public SqlByte GetSqlByte(int columnIndex)
  91. {
  92. byte byt = GetByte(columnIndex);
  93. if(IsDBNull(columnIndex)) {
  94. return SqlByte.Null;
  95. }
  96. else {
  97. return new SqlByte(byt);
  98. }
  99. }
  100. #if NET_2_0
  101. public virtual SqlBytes GetSqlBytes (int columnIndex)
  102. {
  103. byte [] bytes = GetBytes (columnIndex);
  104. if (IsDBNull (columnIndex)) {
  105. return SqlBytes.Null;
  106. }
  107. else {
  108. return new SqlBytes (bytes);
  109. }
  110. }
  111. public virtual SqlChars GetSqlChars (int columnIndex)
  112. {
  113. SqlString sqlStr = GetSqlString (columnIndex);
  114. if (sqlStr.IsNull) {
  115. return SqlChars.Null;
  116. }
  117. else {
  118. return new SqlChars (sqlStr);
  119. }
  120. }
  121. [MonoNotSupported("SqlXml is not fully implemented")]
  122. public virtual SqlXml GetSqlXml (int columnIndex)
  123. {
  124. throw new NotImplementedException ();
  125. }
  126. #endif
  127. // Gets the value of the specified column as a SqlDecimal.
  128. public SqlDecimal GetSqlDecimal(int columnIndex)
  129. {
  130. decimal dec = GetDecimal(columnIndex);
  131. if(IsDBNull(columnIndex)) {
  132. return SqlDecimal.Null;
  133. }
  134. else {
  135. return new SqlDecimal(dec);
  136. }
  137. }
  138. // Gets the value of the specified column as a SqlDateTime.
  139. public SqlDateTime GetSqlDateTime(int columnIndex)
  140. {
  141. DateTime dateTime = GetDateTime(columnIndex);
  142. if(IsDBNull(columnIndex)) {
  143. return SqlDateTime.Null;
  144. }
  145. else {
  146. return new SqlDateTime(dateTime);
  147. }
  148. }
  149. // Gets the value of the specified column as a SqlDouble.
  150. public SqlDouble GetSqlDouble(int columnIndex)
  151. {
  152. double doubl = GetDouble(columnIndex);
  153. if(IsDBNull(columnIndex)) {
  154. return SqlDouble.Null;
  155. }
  156. else {
  157. return new SqlDouble(doubl);
  158. }
  159. }
  160. // Gets the value of the specified column as a SqlInt16.
  161. public SqlInt16 GetSqlInt16(int columnIndex)
  162. {
  163. short s = GetInt16(columnIndex);
  164. if(IsDBNull(columnIndex)) {
  165. return SqlInt16.Null;
  166. }
  167. else {
  168. return new SqlInt16(s);
  169. }
  170. }
  171. // Gets the value of the specified column as a SqlInt32.
  172. public SqlInt32 GetSqlInt32(int columnIndex)
  173. {
  174. int i = GetInt32(columnIndex);
  175. if(IsDBNull(columnIndex)) {
  176. return SqlInt32.Null;
  177. }
  178. else {
  179. return new SqlInt32(i);
  180. }
  181. }
  182. // Gets the value of the specified column as a SqlInt64.
  183. public SqlInt64 GetSqlInt64(int columnIndex)
  184. {
  185. long l = GetInt64(columnIndex);
  186. if(IsDBNull(columnIndex)) {
  187. return SqlInt64.Null;
  188. }
  189. else {
  190. return new SqlInt64(l);
  191. }
  192. }
  193. // Gets the value of the specified column as a SqlMoney.
  194. public SqlMoney GetSqlMoney(int columnIndex)
  195. {
  196. decimal dec = GetDecimal(columnIndex);
  197. if(IsDBNull(columnIndex)) {
  198. return SqlMoney.Null;
  199. }
  200. else {
  201. return new SqlMoney(dec);
  202. }
  203. }
  204. // Gets the value of the specified column as a SqlSingle.
  205. public SqlSingle GetSqlSingle(int columnIndex)
  206. {
  207. float f = GetFloat(columnIndex);
  208. if(IsDBNull(columnIndex)) {
  209. return SqlSingle.Null;
  210. }
  211. else {
  212. return new SqlSingle(f);
  213. }
  214. }
  215. // Gets the value of the specified column as a SqlString.
  216. public SqlString GetSqlString(int columnIndex)
  217. {
  218. string str = GetString(columnIndex);
  219. if(IsDBNull(columnIndex)) {
  220. return SqlString.Null;
  221. }
  222. else {
  223. return new SqlString(str);
  224. }
  225. }
  226. // Gets the value of the specified column as a SqlGuid.
  227. public SqlGuid GetSqlGuid(int columnIndex)
  228. {
  229. object obj = GetValue(columnIndex);
  230. if(IsDBNull(columnIndex)) {
  231. return SqlGuid.Null;
  232. }
  233. else {
  234. if (obj is byte[]) {
  235. return new SqlGuid((byte[])obj);
  236. }
  237. else {
  238. return new SqlGuid((string)obj);
  239. }
  240. }
  241. }
  242. // Gets all the attribute columns in the current row.
  243. public int GetSqlValues(Object[] values)
  244. {
  245. int columnCount = FieldCount;
  246. int i = 0;
  247. for (; i < values.Length && i < columnCount; i++) {
  248. values[i] = GetSqlValue(i);
  249. }
  250. return i;
  251. }
  252. // Gets an Object that is a representation of the underlying SqlDbType Variant.
  253. public Object GetSqlValue(int columnIndex)
  254. {
  255. try {
  256. int jdbcType = ResultsMetaData.getColumnType(columnIndex + 1);
  257. SqlDbType sqlDbType = SqlConvert.JdbcTypeToSqlDbType(jdbcType);
  258. switch (sqlDbType) {
  259. case SqlDbType.BigInt : return GetSqlInt64(columnIndex);
  260. case SqlDbType.Binary : return GetSqlBinary(columnIndex);
  261. case SqlDbType.Bit : return GetSqlBoolean(columnIndex);
  262. case SqlDbType.Char : return GetSqlString(columnIndex);
  263. case SqlDbType.DateTime : return GetSqlDateTime(columnIndex);
  264. case SqlDbType.Decimal : return GetSqlDecimal(columnIndex);
  265. case SqlDbType.Float : return GetSqlDouble(columnIndex);
  266. case SqlDbType.Image : return GetSqlBinary(columnIndex);
  267. case SqlDbType.Int : return GetSqlInt32(columnIndex);
  268. case SqlDbType.Money : return GetSqlDecimal(columnIndex);
  269. case SqlDbType.NChar : return GetSqlString(columnIndex);
  270. case SqlDbType.NText : return GetSqlString(columnIndex);
  271. case SqlDbType.NVarChar : return GetSqlString(columnIndex);
  272. case SqlDbType.Real : return GetSqlSingle(columnIndex);
  273. case SqlDbType.UniqueIdentifier : return GetSqlGuid(columnIndex);
  274. case SqlDbType.SmallDateTime : return GetSqlDateTime(columnIndex);
  275. case SqlDbType.SmallInt : return GetSqlInt16(columnIndex);
  276. case SqlDbType.SmallMoney : return GetSqlDecimal(columnIndex);
  277. case SqlDbType.Text : return GetSqlString(columnIndex);
  278. case SqlDbType.Timestamp : return GetSqlDateTime(columnIndex);
  279. case SqlDbType.TinyInt : return GetSqlByte(columnIndex);
  280. case SqlDbType.VarBinary : return GetSqlBinary(columnIndex);
  281. case SqlDbType.VarChar : return GetSqlString(columnIndex);
  282. case SqlDbType.Variant : return GetValue(columnIndex);
  283. default : return GetValue(columnIndex);
  284. }
  285. }
  286. catch (SQLException exp) {
  287. throw new Exception(exp.Message);
  288. }
  289. }
  290. #if NET_2_0
  291. protected bool IsCommandBehavior (CommandBehavior condition)
  292. {
  293. return (_command.Behavior & condition) == condition;
  294. }
  295. #endif
  296. #endregion // Methods
  297. }
  298. }