OdbcParameter.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. if (value != null && !value.GetType ().IsValueType) {
  76. Type type = value.GetType ();
  77. if (type.IsArray)
  78. size = type.GetElementType () == typeof (byte) ?
  79. ((Array) value).Length : 0;
  80. else
  81. size = value.ToString ().Length;
  82. }
  83. }
  84. public OdbcParameter (string name, OdbcType dataType)
  85. : this ()
  86. {
  87. this.name = name;
  88. OdbcType = dataType;
  89. }
  90. public OdbcParameter (string name, OdbcType dataType, int size)
  91. : this (name, dataType)
  92. {
  93. this.size = size;
  94. }
  95. public OdbcParameter (string name, OdbcType dataType, int size, string srcColumn)
  96. : this (name, dataType, size)
  97. {
  98. this.sourceColumn = srcColumn;
  99. }
  100. [EditorBrowsable (EditorBrowsableState.Advanced)]
  101. public OdbcParameter(string name, OdbcType dataType, int size, ParameterDirection direction, bool isNullable, byte precision, byte scale, string srcColumn, DataRowVersion srcVersion, object value)
  102. : this (name, dataType, size, srcColumn)
  103. {
  104. this.direction = direction;
  105. this.isNullable = isNullable;
  106. this.precision = precision;
  107. this.scale = scale;
  108. this.sourceVersion = srcVersion;
  109. this.ParamValue = value;
  110. }
  111. #endregion
  112. #region Properties
  113. // Used to ensure that only one collection can contain this
  114. // parameter
  115. internal OdbcParameterCollection Container {
  116. get { return container; }
  117. set { container = value; }
  118. }
  119. [BrowsableAttribute (false)]
  120. [OdbcDescriptionAttribute ("The parameter generic type")]
  121. [RefreshPropertiesAttribute (RefreshProperties.All)]
  122. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  123. [OdbcCategory ("Data")]
  124. public DbType DbType {
  125. get { return dbType; }
  126. set {
  127. dbType = value;
  128. }
  129. }
  130. [OdbcCategory ("Data")]
  131. [OdbcDescriptionAttribute ("Input, output, or bidirectional parameter")]
  132. [DefaultValue (ParameterDirection.Input)]
  133. public ParameterDirection Direction {
  134. get { return direction; }
  135. set { direction = value; }
  136. }
  137. [BrowsableAttribute (false)]
  138. [OdbcDescriptionAttribute ("A design-time property used for strongly typed code generation")]
  139. [DesignOnlyAttribute (true)]
  140. [EditorBrowsableAttribute (EditorBrowsableState.Advanced)]
  141. [DefaultValue (false)]
  142. public bool IsNullable {
  143. get { return isNullable; }
  144. set { isNullable = value; }
  145. }
  146. [DefaultValue (OdbcType.NChar)]
  147. [OdbcDescriptionAttribute ("The parameter native type")]
  148. [RefreshPropertiesAttribute (RefreshProperties.All)]
  149. [OdbcCategory ("Data")]
  150. public OdbcType OdbcType {
  151. get { return odbcType; }
  152. set {
  153. odbcType = value;
  154. }
  155. }
  156. [OdbcDescription ("DataParameter_ParameterName")]
  157. [DefaultValue ("")]
  158. public string ParameterName {
  159. get { return name; }
  160. set { name = value; }
  161. }
  162. [OdbcDescription ("DbDataParameter_Precision")]
  163. [OdbcCategory ("DataCategory_Data")]
  164. [DefaultValue (0)]
  165. public byte Precision {
  166. get { return precision; }
  167. set { precision = value; }
  168. }
  169. [OdbcDescription ("DbDataParameter_Scale")]
  170. [OdbcCategory ("DataCategory_Data")]
  171. [DefaultValue (0)]
  172. public byte Scale {
  173. get { return scale; }
  174. set { scale = value; }
  175. }
  176. [OdbcDescription ("DbDataParameter_Size")]
  177. [OdbcCategory ("DataCategory_Data")]
  178. [DefaultValue (0)]
  179. public int Size {
  180. get { return size; }
  181. set { size = value; }
  182. }
  183. [OdbcDescription ("DataParameter_SourceColumn")]
  184. [OdbcCategory ("DataCategory_Data")]
  185. [DefaultValue ("")]
  186. public string SourceColumn {
  187. get { return sourceColumn; }
  188. set { sourceColumn = value; }
  189. }
  190. [OdbcDescription ("DataParameter_SourceVersion")]
  191. [OdbcCategory ("DataCategory_Data")]
  192. [DefaultValue (512)]
  193. public DataRowVersion SourceVersion {
  194. get { return sourceVersion; }
  195. set { sourceVersion = value; }
  196. }
  197. [TypeConverter (typeof(StringConverter))]
  198. [OdbcDescription ("DataParameter_Value")]
  199. [OdbcCategory ("DataCategory_Data")]
  200. [DefaultValue (null)]
  201. public object Value {
  202. get {
  203. return ParamValue;
  204. }
  205. set {
  206. this.ParamValue = value;
  207. bufferIsSet = false;
  208. }
  209. }
  210. #endregion // Properties
  211. #region Methods
  212. internal void Bind(IntPtr hstmt, int ParamNum) {
  213. OdbcReturn ret;
  214. // Set up the buffer if we haven't done so yet
  215. if (!bufferIsSet)
  216. setBuffer();
  217. // Convert System.Data.ParameterDirection into odbc enum
  218. OdbcInputOutputDirection paramdir = libodbc.ConvertParameterDirection(this.direction);
  219. // Bind parameter based on type
  220. if (odbcType == OdbcType.Int)
  221. ret = libodbc.SQLBindParameter(hstmt, (ushort)ParamNum, (short)paramdir,
  222. (short)odbcType, (short)odbcType, Convert.ToUInt32(size),
  223. 0, ref intbuf, 0, 0);
  224. else
  225. ret = libodbc.SQLBindParameter(hstmt, (ushort)ParamNum, (short)paramdir,
  226. (short)OdbcType.Char, (short)odbcType, Convert.ToUInt32(size),
  227. 0, buffer, 0, 0);
  228. // Check for error condition
  229. if ((ret != OdbcReturn.Success) && (ret != OdbcReturn.SuccessWithInfo))
  230. throw new OdbcException(new OdbcError("SQLBindParam", OdbcHandleType.Stmt, hstmt));
  231. }
  232. private void setBuffer() {
  233. // Load buffer with new value
  234. if (odbcType == OdbcType.Int)
  235. intbuf = ParamValue == null ? new int () : (int) ParamValue;
  236. else {
  237. string paramValueString = ParamValue.ToString();
  238. // Treat everything else as a string
  239. // Init string buffer
  240. if (ParamValue is String)
  241. paramValueString = "\'"+paramValueString+"\'";
  242. int minSize = size;
  243. minSize = size > 20 ? size : 20;
  244. if (buffer == null || buffer.Length < minSize)
  245. buffer = new byte[minSize];
  246. else
  247. buffer.Initialize();
  248. // Convert value into string and store into buffer
  249. minSize = paramValueString.Length < minSize ? paramValueString.Length : minSize;
  250. System.Text.Encoding.ASCII.GetBytes(paramValueString, 0, minSize, buffer, 0);
  251. }
  252. bufferIsSet = true;
  253. }
  254. [MonoTODO]
  255. object ICloneable.Clone ()
  256. {
  257. throw new NotImplementedException ();
  258. }
  259. public override string ToString ()
  260. {
  261. return ParameterName;
  262. }
  263. #endregion
  264. }
  265. }