OdbcParameter.cs 12 KB

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