OleDbParameter.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. //
  2. // System.Data.OleDb.OleDbParameter
  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;
  31. using System.Text;
  32. using System.Data;
  33. using System.Data.Common;
  34. using System.Data.ProviderBase;
  35. using java.sql;
  36. using java.lang;
  37. using System.Globalization;
  38. namespace System.Data.OleDb
  39. {
  40. public sealed class OleDbParameter : AbstractDbParameter
  41. {
  42. #region Fields
  43. private OleDbType _oleDbType = OleDbType.VarWChar;
  44. private bool _isOracleRefCursor = false;
  45. #endregion // Fields
  46. #region Constructors
  47. public OleDbParameter()
  48. {
  49. }
  50. public OleDbParameter(String parameterName, Object value)
  51. : this (parameterName, OleDbType.VarWChar, 0, ParameterDirection.Input,
  52. false, 0, 0, String.Empty, DataRowVersion.Current, value)
  53. {
  54. IsDbTypeSet = false;
  55. }
  56. public OleDbParameter(String parameterName, OleDbType dbType)
  57. : this (parameterName, dbType, 0, ParameterDirection.Input,
  58. false, 0, 0, String.Empty, DataRowVersion.Current, null)
  59. {
  60. }
  61. public OleDbParameter(String parameterName, OleDbType dbType, int size)
  62. : this (parameterName, dbType, size, ParameterDirection.Input,
  63. false, 0, 0, String.Empty, DataRowVersion.Current, null)
  64. {
  65. }
  66. public OleDbParameter(String parameterName, OleDbType dbType, int size,
  67. String sourceColumn)
  68. : this (parameterName, dbType, size, ParameterDirection.Input,
  69. false, 0, 0, sourceColumn, DataRowVersion.Current, null)
  70. {
  71. }
  72. public OleDbParameter(String parameterName,
  73. OleDbType dbType,
  74. int size,
  75. ParameterDirection direction,
  76. bool isNullable,
  77. byte precision,
  78. byte scale,
  79. String sourceColumn,
  80. DataRowVersion sourceVersion,
  81. Object value)
  82. {
  83. ParameterName = parameterName;
  84. OleDbType = dbType;
  85. Size = size;
  86. Direction = direction;
  87. IsNullable = isNullable;
  88. Precision = precision;
  89. Scale = scale;
  90. SourceColumn = sourceColumn;
  91. SourceVersion = sourceVersion;
  92. Value = value;
  93. }
  94. #endregion // Constructors
  95. #region Properties
  96. public override DbType DbType
  97. {
  98. get { return OleDbConvert.OleDbTypeToDbType(_oleDbType); }
  99. set { OleDbType = OleDbConvert.DbTypeToOleDbType(value); }
  100. }
  101. public OleDbType OleDbType
  102. {
  103. get { return _oleDbType; }
  104. set {
  105. _oleDbType = value;
  106. IsDbTypeSet = true;
  107. }
  108. }
  109. #if NET_2_0
  110. public new byte Precision
  111. {
  112. get { return base.Precision; }
  113. set { base.Precision = value; }
  114. }
  115. public new byte Scale
  116. {
  117. get { return base.Scale; }
  118. set { base.Scale = value; }
  119. }
  120. #endif
  121. public new Object Value
  122. {
  123. get { return base.Value; }
  124. set {
  125. if (!IsDbTypeSet && (value != null) && (value != DBNull.Value)) {
  126. _oleDbType = OleDbConvert.ValueTypeToOleDbType(value.GetType());
  127. }
  128. base.Value = value;
  129. }
  130. }
  131. protected internal sealed override bool IsSpecial {
  132. get {
  133. return (Direction == ParameterDirection.Output) && IsOracleRefCursor;
  134. }
  135. }
  136. internal bool IsOracleRefCursor
  137. {
  138. get { return _isOracleRefCursor; }
  139. set { _isOracleRefCursor = value; }
  140. }
  141. #endregion // Properties
  142. #region Methods
  143. protected internal sealed override object ConvertValue(object value)
  144. {
  145. // can not convert null or DbNull to other types
  146. if (value == null || value == DBNull.Value) {
  147. return value;
  148. }
  149. // FIXME : some other way to do this?
  150. if (OleDbType == OleDbType.Binary) {
  151. return value;
  152. }
  153. // .NET throws an exception to the user.
  154. object convertedValue = value;
  155. // note : if we set user parameter jdbc type inside prepare interbal, the db type is not set
  156. if (value is IConvertible && (IsDbTypeSet || IsJdbcTypeSet)) {
  157. OleDbType oleDbType = (IsDbTypeSet) ? OleDbType : OleDbConvert.JdbcTypeToOleDbType((int)JdbcType);
  158. Type to = OleDbConvert.OleDbTypeToValueType(oleDbType);
  159. if (!(value is DateTime && to == DbTypes.TypeOfTimespan)) //anyway will go by jdbc type
  160. convertedValue = Convert.ChangeType(value,to);
  161. }
  162. return convertedValue;
  163. }
  164. protected internal sealed override void SetParameterName(ResultSet res)
  165. {
  166. ParameterName = res.getString("COLUMN_NAME");
  167. if (ParameterName.StartsWith("@")) {
  168. ParameterName = ParameterName.Remove(0,1);
  169. }
  170. }
  171. protected internal sealed override void SetParameterDbType(ResultSet res)
  172. {
  173. int jdbcType = res.getInt("DATA_TYPE");
  174. // FIXME : is that correct?
  175. if (jdbcType == Types.OTHER) {
  176. string typeName = res.getString("TYPE_NAME");
  177. if (String.Compare ("BLOB", typeName, true, CultureInfo.InvariantCulture) == 0) {
  178. jdbcType = Types.BLOB;
  179. }
  180. else if (String.Compare ("CLOB", typeName, true, CultureInfo.InvariantCulture) == 0) {
  181. jdbcType = Types.CLOB;
  182. }
  183. else if (String.Compare ("FLOAT", typeName, true, CultureInfo.InvariantCulture) == 0) {
  184. jdbcType = Types.FLOAT;
  185. }
  186. else if (String.Compare ("NVARCHAR2", typeName, true, CultureInfo.InvariantCulture) == 0) {
  187. jdbcType = Types.VARCHAR;
  188. }
  189. else if (String.Compare ("NCHAR", typeName, true, CultureInfo.InvariantCulture) == 0) {
  190. jdbcType = Types.VARCHAR;
  191. }
  192. }
  193. OleDbType = OleDbConvert.JdbcTypeToOleDbType(jdbcType);
  194. JdbcType = jdbcType;
  195. }
  196. #if NET_2_0
  197. public void ResetOleDbType ()
  198. {
  199. IsDbTypeSet = false;
  200. }
  201. public override void ResetDbType ()
  202. {
  203. ResetOleDbType ();
  204. }
  205. #endif
  206. protected internal sealed override void SetSpecialFeatures(ResultSet res)
  207. {
  208. IsOracleRefCursor = (res.getString("TYPE_NAME") == "REF CURSOR");
  209. }
  210. protected internal sealed override int JdbcTypeFromProviderType()
  211. {
  212. return OleDbConvert.OleDbTypeToJdbcType(OleDbType);
  213. }
  214. #endregion // Methods
  215. }
  216. }