OdbcParameter.cs 12 KB

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