OdbcParameter.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. //
  2. // System.Data.Odbc.OdbcParameter
  3. //
  4. // Authors:
  5. // Brian Ritchie ([email protected])
  6. //
  7. // Copyright (C) Brian Ritchie, 2002
  8. //
  9. //
  10. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System;
  32. using System.Data;
  33. using System.Data.Common;
  34. using System.ComponentModel;
  35. namespace System.Data.Odbc
  36. {
  37. [TypeConverterAttribute (typeof (OdbcParameterConverter))]
  38. public sealed class OdbcParameter : MarshalByRefObject, IDbDataParameter, IDataParameter, ICloneable
  39. {
  40. #region Fields
  41. string name;
  42. object ParamValue;
  43. int size;
  44. bool isNullable;
  45. byte precision;
  46. byte scale;
  47. DataRowVersion sourceVersion;
  48. string sourceColumn;
  49. ParameterDirection direction;
  50. OdbcType odbcType;
  51. DbType dbType;
  52. OdbcParameterCollection container = null;
  53. // Buffers for parameter value based on type. Currently I've only optimized
  54. // for int parameters and everything else is just converted to a string.
  55. private bool bufferIsSet;
  56. int intbuf;
  57. byte[] buffer;
  58. #endregion
  59. #region Constructors
  60. public OdbcParameter ()
  61. {
  62. name = String.Empty;
  63. ParamValue = null;
  64. size = 0;
  65. isNullable = true;
  66. precision = 0;
  67. scale = 0;
  68. sourceColumn = String.Empty;
  69. }
  70. public OdbcParameter (string name, object value)
  71. : this ()
  72. {
  73. this.name = name;
  74. this.ParamValue = value;
  75. }
  76. public OdbcParameter (string name, OdbcType dataType)
  77. : this ()
  78. {
  79. this.name = name;
  80. OdbcType = dataType;
  81. }
  82. public OdbcParameter (string name, OdbcType dataType, int size)
  83. : this (name, dataType)
  84. {
  85. this.size = size;
  86. }
  87. public OdbcParameter (string name, OdbcType dataType, int size, string srcColumn)
  88. : this (name, dataType, size)
  89. {
  90. this.sourceColumn = srcColumn;
  91. }
  92. [EditorBrowsable (EditorBrowsableState.Advanced)]
  93. public OdbcParameter(string name, OdbcType dataType, int size, ParameterDirection direction, bool isNullable, byte precision, byte scale, string srcColumn, DataRowVersion srcVersion, object value)
  94. : this (name, dataType, size, srcColumn)
  95. {
  96. this.direction = direction;
  97. this.isNullable = isNullable;
  98. this.precision = precision;
  99. this.scale = scale;
  100. this.sourceVersion = srcVersion;
  101. this.ParamValue = value;
  102. }
  103. #endregion
  104. #region Properties
  105. // Used to ensure that only one collection can contain this
  106. // parameter
  107. internal OdbcParameterCollection Container {
  108. get { return container; }
  109. set { container = value; }
  110. }
  111. [BrowsableAttribute (false)]
  112. [OdbcDescriptionAttribute ("The parameter generic type")]
  113. [RefreshPropertiesAttribute (RefreshProperties.All)]
  114. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  115. [OdbcCategory ("Data")]
  116. public DbType DbType {
  117. get { return dbType; }
  118. set {
  119. dbType = value;
  120. }
  121. }
  122. [OdbcCategory ("Data")]
  123. [OdbcDescriptionAttribute ("Input, output, or bidirectional parameter")]
  124. [DefaultValue (ParameterDirection.Input)]
  125. public ParameterDirection Direction {
  126. get { return direction; }
  127. set { direction = value; }
  128. }
  129. [BrowsableAttribute (false)]
  130. [OdbcDescriptionAttribute ("A design-time property used for strongly typed code generation")]
  131. [DesignOnlyAttribute (true)]
  132. [EditorBrowsableAttribute (EditorBrowsableState.Advanced)]
  133. [DefaultValue (false)]
  134. public bool IsNullable {
  135. get { return isNullable; }
  136. set { isNullable = value; }
  137. }
  138. [DefaultValue (OdbcType.NChar)]
  139. [OdbcDescriptionAttribute ("The parameter native type")]
  140. [RefreshPropertiesAttribute (RefreshProperties.All)]
  141. [OdbcCategory ("Data")]
  142. public OdbcType OdbcType {
  143. get { return odbcType; }
  144. set {
  145. odbcType = value;
  146. }
  147. }
  148. [OdbcDescription ("DataParameter_ParameterName")]
  149. [DefaultValue ("")]
  150. public string ParameterName {
  151. get { return name; }
  152. set { name = value; }
  153. }
  154. [OdbcDescription ("DbDataParameter_Precision")]
  155. [OdbcCategory ("DataCategory_Data")]
  156. [DefaultValue (0)]
  157. public byte Precision {
  158. get { return precision; }
  159. set { precision = value; }
  160. }
  161. [OdbcDescription ("DbDataParameter_Scale")]
  162. [OdbcCategory ("DataCategory_Data")]
  163. [DefaultValue (0)]
  164. public byte Scale {
  165. get { return scale; }
  166. set { scale = value; }
  167. }
  168. [OdbcDescription ("DbDataParameter_Size")]
  169. [OdbcCategory ("DataCategory_Data")]
  170. [DefaultValue (0)]
  171. public int Size {
  172. get { return size; }
  173. set { size = value; }
  174. }
  175. [OdbcDescription ("DataParameter_SourceColumn")]
  176. [OdbcCategory ("DataCategory_Data")]
  177. [DefaultValue ("")]
  178. public string SourceColumn {
  179. get { return sourceColumn; }
  180. set { sourceColumn = value; }
  181. }
  182. [OdbcDescription ("DataParameter_SourceVersion")]
  183. [OdbcCategory ("DataCategory_Data")]
  184. [DefaultValue (512)]
  185. public DataRowVersion SourceVersion {
  186. get { return sourceVersion; }
  187. set { sourceVersion = value; }
  188. }
  189. [TypeConverter (typeof(StringConverter))]
  190. [OdbcDescription ("DataParameter_Value")]
  191. [OdbcCategory ("DataCategory_Data")]
  192. [DefaultValue (null)]
  193. public object Value {
  194. get {
  195. return ParamValue;
  196. }
  197. set {
  198. this.ParamValue = value;
  199. bufferIsSet = false;
  200. }
  201. }
  202. #endregion // Properties
  203. #region Methods
  204. internal void Bind(IntPtr hstmt, int ParamNum) {
  205. OdbcReturn ret;
  206. // Set up the buffer if we haven't done so yet
  207. if (!bufferIsSet)
  208. setBuffer();
  209. // Convert System.Data.ParameterDirection into odbc enum
  210. OdbcInputOutputDirection paramdir = libodbc.ConvertParameterDirection(this.direction);
  211. // Bind parameter based on type
  212. if (odbcType == OdbcType.Int)
  213. ret = libodbc.SQLBindParameter(hstmt, (ushort)ParamNum, (short)paramdir,
  214. (short)odbcType, (short)odbcType, Convert.ToUInt32(size),
  215. 0, ref intbuf, 0, 0);
  216. else
  217. ret = libodbc.SQLBindParameter(hstmt, (ushort)ParamNum, (short)paramdir,
  218. (short)OdbcType.Char, (short)odbcType, Convert.ToUInt32(size),
  219. 0, buffer, 0, 0);
  220. // Check for error condition
  221. if ((ret != OdbcReturn.Success) && (ret != OdbcReturn.SuccessWithInfo))
  222. throw new OdbcException(new OdbcError("SQLBindParam", OdbcHandleType.Stmt, hstmt));
  223. }
  224. private void setBuffer() {
  225. // Load buffer with new value
  226. if (odbcType == OdbcType.Int)
  227. intbuf = (int)ParamValue;
  228. else {
  229. string paramValueString = ParamValue.ToString();
  230. // Treat everything else as a string
  231. // Init string buffer
  232. if (ParamValue is String)
  233. paramValueString = "\'"+paramValueString+"\'";
  234. if (buffer == null || buffer.Length < ((size > 20) ? size : 20))
  235. buffer = new byte[(size > 20) ? size : 20];
  236. else
  237. buffer.Initialize();
  238. // Convert value into string and store into buffer
  239. System.Text.Encoding.ASCII.GetBytes(paramValueString, 0, paramValueString.Length, buffer, 0);
  240. }
  241. bufferIsSet = true;
  242. }
  243. [MonoTODO]
  244. object ICloneable.Clone ()
  245. {
  246. throw new NotImplementedException ();
  247. }
  248. public override string ToString ()
  249. {
  250. return ParameterName;
  251. }
  252. #endregion
  253. }
  254. }