AbstractDBParameter.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. //
  2. // System.Data.ProviderBase.AbstractDbParameter
  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.Data;
  32. using System.Data.Common;
  33. using java.sql;
  34. namespace System.Data.ProviderBase
  35. {
  36. public abstract class AbstractDbParameter : DbParameter, IDbDataParameter, ICloneable
  37. {
  38. #region Fields
  39. byte _precision;
  40. byte _scale;
  41. protected DataRowVersion _sourceVersion;
  42. private int _jdbcType;
  43. bool _isDbTypeSet;
  44. bool _isJdbcTypeSet;
  45. object _convertedValue;
  46. string _parameterName;
  47. ParameterDirection _direction = ParameterDirection.Input;
  48. int _size;
  49. object _value;
  50. bool _isNullable;
  51. int _offset;
  52. string _sourceColumn;
  53. DbParameterCollection _parent = null;
  54. #endregion // Fields
  55. #region Constructors
  56. [MonoTODO]
  57. protected AbstractDbParameter ()
  58. {
  59. }
  60. #endregion // Constructors
  61. #region Properties
  62. public override ParameterDirection Direction {
  63. get { return _direction; }
  64. set {
  65. if (_direction != value) {
  66. switch (value) {
  67. case ParameterDirection.Input:
  68. case ParameterDirection.Output:
  69. case ParameterDirection.InputOutput:
  70. case ParameterDirection.ReturnValue:
  71. {
  72. _direction = value;
  73. return;
  74. }
  75. }
  76. throw ExceptionHelper.InvalidParameterDirection (value);
  77. }
  78. }
  79. }
  80. public override bool IsNullable {
  81. get { return _isNullable; }
  82. set { _isNullable = value; }
  83. }
  84. public virtual int Offset {
  85. get { return _offset; }
  86. set { _offset = value; }
  87. }
  88. public override string ParameterName {
  89. get {
  90. if (_parameterName == null)
  91. return String.Empty;
  92. return _parameterName;
  93. }
  94. set {
  95. if (_parameterName != value) {
  96. _parameterName = value;
  97. }
  98. }
  99. }
  100. public override int Size {
  101. get { return _size; }
  102. set {
  103. if (_size != value) {
  104. if (value < -1)
  105. throw ExceptionHelper.InvalidSizeValue (value);
  106. _size = value;
  107. }
  108. }
  109. }
  110. public override string SourceColumn {
  111. get {
  112. if (_sourceColumn == null)
  113. return String.Empty;
  114. return _sourceColumn;
  115. }
  116. set { _sourceColumn = value; }
  117. }
  118. internal DbParameterCollection Parent
  119. {
  120. get { return _parent; }
  121. set { _parent = value; }
  122. }
  123. public byte Precision
  124. {
  125. get { return _precision; }
  126. set { _precision = value; }
  127. }
  128. public byte Scale
  129. {
  130. get { return _scale; }
  131. set { _scale = value; }
  132. }
  133. public override DataRowVersion SourceVersion
  134. {
  135. get { return _sourceVersion; }
  136. set { _sourceVersion = value; }
  137. }
  138. protected internal int JdbcType
  139. {
  140. get {
  141. if (!IsJdbcTypeSet) {
  142. return JdbcTypeFromProviderType();
  143. }
  144. return _jdbcType;
  145. }
  146. set {
  147. _jdbcType = value;
  148. IsJdbcTypeSet = true;
  149. }
  150. }
  151. protected internal bool IsJdbcTypeSet
  152. {
  153. get {
  154. return _isJdbcTypeSet;
  155. }
  156. set {
  157. _isJdbcTypeSet = value;
  158. }
  159. }
  160. protected internal bool IsDbTypeSet
  161. {
  162. get { return _isDbTypeSet; }
  163. set { _isDbTypeSet = value; }
  164. }
  165. protected internal virtual bool IsSpecial {
  166. get {
  167. return false;
  168. }
  169. }
  170. private bool IsFixedLength
  171. {
  172. get {
  173. return ((DbType != DbType.AnsiString) && (DbType != DbType.Binary) &&
  174. (DbType != DbType.String) && (DbType != DbType.VarNumeric));
  175. }
  176. }
  177. protected internal virtual string Placeholder {
  178. get {
  179. return "?";
  180. }
  181. }
  182. internal object ConvertedValue
  183. {
  184. get {
  185. if (_convertedValue == null) {
  186. object value = Value;
  187. _convertedValue = ((value != null) && (value != DBNull.Value)) ? ConvertValue(value) : value;
  188. }
  189. return _convertedValue;
  190. }
  191. }
  192. public override object Value {
  193. get { return _value; }
  194. set {
  195. _convertedValue = null;
  196. _value = value;
  197. }
  198. }
  199. //DbParameter overrides
  200. public override bool SourceColumnNullMapping {
  201. get {
  202. throw new NotImplementedException();
  203. }
  204. set {
  205. throw new NotImplementedException();
  206. }
  207. }
  208. #endregion // Properties
  209. #region Methods
  210. public override String ToString()
  211. {
  212. return ParameterName;
  213. }
  214. protected internal abstract void SetParameterName(ResultSet res);
  215. protected internal abstract void SetParameterDbType(ResultSet res);
  216. protected internal abstract void SetSpecialFeatures(ResultSet res);
  217. public virtual object Clone()
  218. {
  219. return MemberwiseClone();
  220. }
  221. protected internal abstract int JdbcTypeFromProviderType();
  222. protected internal abstract object ConvertValue(object value);
  223. internal void SetParameterPrecisionAndScale(ResultSet res)
  224. {
  225. int jdbcType = res.getInt("DATA_TYPE");
  226. if(jdbcType == java.sql.Types.DECIMAL || jdbcType == java.sql.Types.NUMERIC) {
  227. Precision = (byte)res.getInt("PRECISION");
  228. Scale = (byte)res.getInt("SCALE");
  229. }
  230. }
  231. internal void SetParameterSize(ResultSet res)
  232. {
  233. Size = res.getInt("LENGTH");
  234. }
  235. internal void SetParameterIsNullable(ResultSet res)
  236. {
  237. IsNullable = (res.getInt("NULLABLE") == 1);
  238. }
  239. internal void Validate()
  240. {
  241. if (!IsFixedLength && ((Direction & ParameterDirection.Output) != 0) && (Size == 0)) {
  242. throw ExceptionHelper.ParameterSizeNotInitialized(Offset,ParameterName,DbType.ToString(),Size);
  243. }
  244. }
  245. //DbParameter overrides
  246. public override void ResetDbType() {
  247. throw new NotImplementedException();
  248. }
  249. #endregion // Methods
  250. }
  251. }