OdbcParameter.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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.Text;
  34. using System.Data;
  35. using System.Data.Common;
  36. #if NET_2_0
  37. using System.Data.ProviderBase;
  38. #endif // NET_2_0
  39. using System.ComponentModel;
  40. namespace System.Data.Odbc
  41. {
  42. [TypeConverterAttribute (typeof (OdbcParameterConverter))]
  43. #if NET_2_0
  44. public sealed class OdbcParameter : DbParameterBase, ICloneable
  45. #else
  46. public sealed class OdbcParameter : MarshalByRefObject, IDbDataParameter, IDataParameter, ICloneable
  47. #endif // NET_2_0
  48. {
  49. #region Fields
  50. #if ONLY_1_1
  51. string name;
  52. ParameterDirection direction;
  53. bool isNullable;
  54. int size;
  55. byte precision;
  56. byte scale;
  57. object paramValue;
  58. DataRowVersion sourceVersion;
  59. string sourceColumn;
  60. #endif // ONLY_1_1
  61. OdbcType odbcType = OdbcType.NVarChar;
  62. DbType dbType = DbType.String;
  63. OdbcParameterCollection container = null;
  64. // Buffers for parameter value based on type. Currently I've only optimized
  65. // for int parameters and everything else is just converted to a string.
  66. private bool bufferIsSet;
  67. int intbuf;
  68. byte[] buffer;
  69. #endregion
  70. #region Constructors
  71. public OdbcParameter ()
  72. {
  73. ParameterName = String.Empty;
  74. Value = null;
  75. Size = 0;
  76. IsNullable = true;
  77. Precision = 0;
  78. Scale = 0;
  79. SourceColumn = String.Empty;
  80. }
  81. public OdbcParameter (string name, object value)
  82. : this ()
  83. {
  84. this.ParameterName = name;
  85. this.Value = value;
  86. if (value != null && !value.GetType ().IsValueType) {
  87. Type type = value.GetType ();
  88. if (type.IsArray)
  89. Size = type.GetElementType () == typeof (byte) ?
  90. ((Array) value).Length : 0;
  91. else
  92. Size = value.ToString ().Length;
  93. }
  94. }
  95. public OdbcParameter (string name, OdbcType dataType)
  96. : this ()
  97. {
  98. this.ParameterName = name;
  99. OdbcType = dataType;
  100. }
  101. public OdbcParameter (string name, OdbcType dataType, int size)
  102. : this (name, dataType)
  103. {
  104. this.Size = size;
  105. }
  106. public OdbcParameter (string name, OdbcType dataType, int size, string srcColumn)
  107. : this (name, dataType, size)
  108. {
  109. this.SourceColumn = srcColumn;
  110. }
  111. [EditorBrowsable (EditorBrowsableState.Advanced)]
  112. public OdbcParameter(string name, OdbcType dataType, int size,
  113. ParameterDirection direction, bool isNullable,
  114. byte precision, byte scale, string srcColumn,
  115. DataRowVersion srcVersion, object value)
  116. : this (name, dataType, size, srcColumn)
  117. {
  118. this.Direction = direction;
  119. this.IsNullable = isNullable;
  120. this.Precision = precision;
  121. this.Scale = scale;
  122. this.SourceVersion = srcVersion;
  123. this.Value = value;
  124. }
  125. #endregion
  126. #region Properties
  127. // Used to ensure that only one collection can contain this
  128. // parameter
  129. internal OdbcParameterCollection Container {
  130. get { return container; }
  131. set { container = value; }
  132. }
  133. [BrowsableAttribute (false)]
  134. [OdbcDescriptionAttribute ("The parameter generic type")]
  135. [RefreshPropertiesAttribute (RefreshProperties.All)]
  136. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  137. [OdbcCategory ("Data")]
  138. public
  139. #if NET_2_0
  140. override
  141. #endif // NET_2_0
  142. DbType DbType {
  143. get { return dbType; }
  144. set {
  145. dbType = value;
  146. }
  147. }
  148. #if ONLY_1_1
  149. [OdbcCategory ("Data")]
  150. [OdbcDescriptionAttribute ("Input, output, or bidirectional parameter")]
  151. [DefaultValue (ParameterDirection.Input)]
  152. public ParameterDirection Direction {
  153. get { return direction; }
  154. set { direction = value; }
  155. }
  156. [BrowsableAttribute (false)]
  157. [OdbcDescriptionAttribute ("A design-time property used for strongly typed code generation")]
  158. [DesignOnlyAttribute (true)]
  159. [EditorBrowsableAttribute (EditorBrowsableState.Advanced)]
  160. [DefaultValue (false)]
  161. public bool IsNullable {
  162. get { return isNullable; }
  163. set { isNullable = value; }
  164. }
  165. #endif // ONLY_1_1
  166. [DefaultValue (OdbcType.NChar)]
  167. [OdbcDescriptionAttribute ("The parameter native type")]
  168. [RefreshPropertiesAttribute (RefreshProperties.All)]
  169. [OdbcCategory ("Data")]
  170. public OdbcType OdbcType {
  171. get { return odbcType; }
  172. set {
  173. odbcType = value;
  174. }
  175. }
  176. #if ONLY_1_1
  177. [OdbcDescription ("DataParameter_ParameterName")]
  178. [DefaultValue ("")]
  179. public string ParameterName {
  180. get { return name; }
  181. set { name = value; }
  182. }
  183. [OdbcDescription ("DbDataParameter_Precision")]
  184. [OdbcCategory ("DataCategory_Data")]
  185. [DefaultValue (0)]
  186. public byte Precision {
  187. get { return precision; }
  188. set { precision = value; }
  189. }
  190. [OdbcDescription ("DbDataParameter_Scale")]
  191. [OdbcCategory ("DataCategory_Data")]
  192. [DefaultValue (0)]
  193. public byte Scale {
  194. get { return scale; }
  195. set { scale = value; }
  196. }
  197. [OdbcDescription ("DbDataParameter_Size")]
  198. [OdbcCategory ("DataCategory_Data")]
  199. [DefaultValue (0)]
  200. public int Size {
  201. get { return size; }
  202. set { size = value; }
  203. }
  204. [OdbcDescription ("DataParameter_SourceColumn")]
  205. [OdbcCategory ("DataCategory_Data")]
  206. [DefaultValue ("")]
  207. public string SourceColumn {
  208. get { return sourceColumn; }
  209. set { sourceColumn = value; }
  210. }
  211. [OdbcDescription ("DataParameter_SourceVersion")]
  212. [OdbcCategory ("DataCategory_Data")]
  213. [DefaultValue (512)]
  214. public DataRowVersion SourceVersion {
  215. get { return sourceVersion; }
  216. set { sourceVersion = value; }
  217. }
  218. [TypeConverter (typeof(StringConverter))]
  219. [OdbcDescription ("DataParameter_Value")]
  220. [OdbcCategory ("DataCategory_Data")]
  221. [DefaultValue (null)]
  222. public object Value {
  223. get {
  224. return paramValue;
  225. }
  226. set {
  227. paramValue = value;
  228. bufferIsSet = false;
  229. }
  230. }
  231. #endif // ONLY_1_1
  232. #if NET_2_0
  233. [TypeConverter (typeof(StringConverter))]
  234. [OdbcDescription ("DataParameter_Value")]
  235. [OdbcCategory ("DataCategory_Data")]
  236. [DefaultValue (null)]
  237. public override object Value {
  238. get {
  239. return base.Value;
  240. }
  241. set {
  242. base.Value = value;
  243. bufferIsSet = false;
  244. }
  245. }
  246. #endif // NET_2_0
  247. #endregion // Properties
  248. #region Methods
  249. internal void Bind(IntPtr hstmt, int ParamNum) {
  250. OdbcReturn ret;
  251. // Set up the buffer if we haven't done so yet
  252. if (!bufferIsSet)
  253. setBuffer();
  254. // Convert System.Data.ParameterDirection into odbc enum
  255. OdbcInputOutputDirection paramdir = libodbc.ConvertParameterDirection(this.Direction);
  256. SQL_C_TYPE ctype = OdbcTypeConverter.ConvertToSqlCType (odbcType);
  257. SQL_TYPE sqltype = OdbcTypeConverter.ConvertToSqlType (odbcType);
  258. // Bind parameter based on type
  259. int ind = -3;
  260. if (odbcType == OdbcType.Int)
  261. ret = libodbc.SQLBindParameter(hstmt, (ushort)ParamNum, (short)paramdir,
  262. ctype, sqltype, Convert.ToUInt32(Size),
  263. 0, ref intbuf, 0, ref ind);
  264. else
  265. ret = libodbc.SQLBindParameter(hstmt, (ushort)ParamNum, (short)paramdir,
  266. ctype, sqltype, Convert.ToUInt32(Size),
  267. 0, buffer, buffer.Length, ref ind);
  268. // Check for error condition
  269. if ((ret != OdbcReturn.Success) && (ret != OdbcReturn.SuccessWithInfo))
  270. throw new OdbcException(new OdbcError("SQLBindParam", OdbcHandleType.Stmt, hstmt));
  271. }
  272. private void setBuffer() {
  273. // Load buffer with new value
  274. if (odbcType == OdbcType.Int)
  275. intbuf = Value == null ? new int () : (int) Value;
  276. else if (odbcType == OdbcType.Numeric
  277. || odbcType == OdbcType.Decimal) {
  278. // for numeric, the buffer is a packed decimal struct.
  279. // ref http://www.it-faq.pl/mskb/181/254.HTM
  280. if (Value == null)
  281. Value = (decimal) 0;
  282. int [] bits = Decimal.GetBits (Convert.ToDecimal (Value));
  283. buffer = new byte [19]; // ref sqltypes.h
  284. buffer [0] = Precision;
  285. buffer [1] = (byte) ((bits [3] & 0x00FF0000) >> 16); // scale
  286. buffer [2] = (byte) ((bits [3] & 0x80000000) > 0 ? 2 : 1); //sign
  287. Buffer.BlockCopy (bits, 0, buffer, 3, 12); // copy data
  288. for (int j = 16; j < 19; j++) // pad with 0
  289. buffer [j] = 0;
  290. } else {
  291. string paramValueString = Value.ToString();
  292. // Treat everything else as a string
  293. // Init string buffer
  294. int minSize = Size;
  295. minSize = Size > 20 ? Size : 20;
  296. if (buffer == null || buffer.Length < minSize)
  297. buffer = new byte[minSize];
  298. else
  299. buffer.Initialize();
  300. // Convert value into string and store into buffer
  301. minSize = paramValueString.Length < minSize ? paramValueString.Length : minSize;
  302. Encoding.ASCII.GetBytes(paramValueString, 0, minSize, buffer, 0);
  303. }
  304. bufferIsSet = true;
  305. }
  306. [MonoTODO]
  307. object ICloneable.Clone ()
  308. {
  309. throw new NotImplementedException ();
  310. }
  311. public override string ToString ()
  312. {
  313. return ParameterName;
  314. }
  315. #if NET_2_0
  316. [MonoTODO]
  317. public override void PropertyChanging ()
  318. {
  319. }
  320. [MonoTODO]
  321. protected override byte ValuePrecision (object value)
  322. {
  323. throw new NotImplementedException ();
  324. }
  325. [MonoTODO]
  326. protected override byte ValueScale (object value)
  327. {
  328. throw new NotImplementedException ();
  329. }
  330. [MonoTODO]
  331. protected override int ValueSize (object value)
  332. {
  333. throw new NotImplementedException ();
  334. }
  335. [MonoTODO]
  336. public override void ResetDbType ()
  337. {
  338. throw new NotImplementedException ();
  339. }
  340. #endif // NET_2_0
  341. #endregion
  342. }
  343. }