AbstractDBParameter.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 : DbParameterBase
  37. {
  38. #region Fields
  39. protected byte _precision;
  40. protected byte _scale;
  41. protected DataRowVersion _sourceVersion;
  42. private DbTypes.JavaSqlTypes _jdbcType;
  43. protected bool _isDbTypeSet = false;
  44. protected bool _isJdbcTypeSet = false;
  45. object _convertedValue;
  46. #endregion // Fields
  47. #region Constructors
  48. [MonoTODO]
  49. protected AbstractDbParameter ()
  50. {
  51. }
  52. #endregion // Constructors
  53. #region Properties
  54. public override byte Precision
  55. {
  56. get { return _precision; }
  57. set { _precision = value; }
  58. }
  59. public override byte Scale
  60. {
  61. get { return _scale; }
  62. set { _scale = value; }
  63. }
  64. public override DataRowVersion SourceVersion
  65. {
  66. get { return _sourceVersion; }
  67. set { _sourceVersion = value; }
  68. }
  69. protected internal DbTypes.JavaSqlTypes JdbcType
  70. {
  71. get {
  72. if (!_isJdbcTypeSet) {
  73. return JdbcTypeFromProviderType();
  74. }
  75. return _jdbcType;
  76. }
  77. set {
  78. _jdbcType = value;
  79. _isJdbcTypeSet = true;
  80. }
  81. }
  82. protected internal bool IsJdbcTypeSet
  83. {
  84. get {
  85. return _isJdbcTypeSet;
  86. }
  87. set {
  88. _isJdbcTypeSet = value;
  89. }
  90. }
  91. internal bool IsDbTypeSet
  92. {
  93. get { return _isDbTypeSet; }
  94. }
  95. protected internal virtual bool IsSpecial {
  96. get {
  97. return false;
  98. }
  99. }
  100. private bool IsFixedLength
  101. {
  102. get {
  103. return ((DbType != DbType.AnsiString) && (DbType != DbType.Binary) &&
  104. (DbType != DbType.String) && (DbType != DbType.VarNumeric));
  105. }
  106. }
  107. protected internal virtual string Placeholder {
  108. get {
  109. return "?";
  110. }
  111. }
  112. internal object ConvertedValue
  113. {
  114. get {
  115. if (_convertedValue == null) {
  116. object value = Value;
  117. _convertedValue = ((value != null) && (value != DBNull.Value)) ? ConvertValue(value) : value;
  118. }
  119. return _convertedValue;
  120. }
  121. }
  122. public override object Value {
  123. get { return base.Value; }
  124. set {
  125. _convertedValue = null;
  126. base.Value = value;
  127. }
  128. }
  129. #endregion // Properties
  130. #region Methods
  131. protected internal abstract void SetParameterName(ResultSet res);
  132. protected internal abstract void SetParameterDbType(ResultSet res);
  133. protected internal abstract void SetSpecialFeatures(ResultSet res);
  134. public abstract object Clone();
  135. protected internal abstract DbTypes.JavaSqlTypes JdbcTypeFromProviderType();
  136. protected internal abstract object ConvertValue(object value);
  137. internal void SetParameterPrecisionAndScale(ResultSet res)
  138. {
  139. int jdbcType = res.getInt("DATA_TYPE");
  140. if(jdbcType == java.sql.Types.DECIMAL || jdbcType == java.sql.Types.NUMERIC) {
  141. Precision = (byte)res.getInt("PRECISION");
  142. Scale = (byte)res.getInt("SCALE");
  143. }
  144. }
  145. internal void SetParameterSize(ResultSet res)
  146. {
  147. Size = res.getInt("LENGTH");
  148. }
  149. internal void SetParameterIsNullable(ResultSet res)
  150. {
  151. IsNullable = (res.getInt("NULLABLE") == 1);
  152. }
  153. internal void Validate()
  154. {
  155. if (!IsFixedLength && ((Direction & ParameterDirection.Output) != 0) && (Size == 0)) {
  156. throw ExceptionHelper.ParameterSizeNotInitialized(Offset,ParameterName,DbType.ToString(),Size);
  157. }
  158. }
  159. public override void CopyTo (DbParameter target)
  160. {
  161. base.CopyTo(target);
  162. AbstractDbParameter t = (AbstractDbParameter) target;
  163. t._precision = _precision;
  164. t._scale = _scale;
  165. t._sourceVersion = _sourceVersion;
  166. t._jdbcType = _jdbcType;
  167. }
  168. #endregion // Methods
  169. }
  170. }