OdbcParameter.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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. using System.Runtime.InteropServices;
  37. using System.Globalization;
  38. using System.ComponentModel;
  39. namespace System.Data.Odbc
  40. {
  41. [TypeConverterAttribute (typeof (OdbcParameterConverter))]
  42. public sealed class OdbcParameter :
  43. #if NET_2_0
  44. DbParameter,
  45. #else
  46. MarshalByRefObject,
  47. #endif // NET_2_0
  48. ICloneable, IDbDataParameter, IDataParameter
  49. {
  50. #region Fields
  51. string name;
  52. ParameterDirection direction;
  53. bool isNullable;
  54. int size;
  55. DataRowVersion sourceVersion;
  56. string sourceColumn;
  57. byte _precision;
  58. byte _scale;
  59. object _value;
  60. private OdbcTypeMap _typeMap;
  61. private NativeBuffer _nativeBuffer = new NativeBuffer ();
  62. private NativeBuffer _cbLengthInd;
  63. private OdbcParameterCollection container = null;
  64. #endregion
  65. #region Constructors
  66. public OdbcParameter ()
  67. {
  68. _cbLengthInd = new NativeBuffer ();
  69. ParameterName = String.Empty;
  70. IsNullable = false;
  71. SourceColumn = String.Empty;
  72. Direction = ParameterDirection.Input;
  73. _typeMap = OdbcTypeConverter.GetTypeMap (OdbcType.NVarChar);
  74. }
  75. public OdbcParameter (string name, object value)
  76. : this ()
  77. {
  78. this.ParameterName = name;
  79. Value = value;
  80. //FIXME: MS.net does not infer OdbcType from value unless a type is provided
  81. _typeMap = OdbcTypeConverter.InferFromValue (value);
  82. if (value != null && !value.GetType ().IsValueType) {
  83. Type type = value.GetType ();
  84. if (type.IsArray)
  85. Size = type.GetElementType () == typeof (byte) ?
  86. ((Array) value).Length : 0;
  87. else
  88. Size = value.ToString ().Length;
  89. }
  90. }
  91. public OdbcParameter (string name, OdbcType odbcType)
  92. : this ()
  93. {
  94. this.ParameterName = name;
  95. _typeMap = (OdbcTypeMap) OdbcTypeConverter.GetTypeMap (odbcType);
  96. }
  97. public OdbcParameter (string name, OdbcType odbcType, int size)
  98. : this (name, odbcType)
  99. {
  100. this.Size = size;
  101. }
  102. public OdbcParameter (string name, OdbcType odbcType, int size, string srcColumn)
  103. : this (name, odbcType, size)
  104. {
  105. this.SourceColumn = srcColumn;
  106. }
  107. [EditorBrowsable (EditorBrowsableState.Advanced)]
  108. public OdbcParameter(string name, OdbcType odbcType, int size,
  109. ParameterDirection direction, bool isNullable,
  110. byte precision, byte scale, string srcColumn,
  111. DataRowVersion srcVersion, object value)
  112. : this (name, odbcType, size, srcColumn)
  113. {
  114. this.Direction = direction;
  115. this.IsNullable = isNullable;
  116. this.SourceVersion = srcVersion;
  117. }
  118. #endregion
  119. #region Properties
  120. // Used to ensure that only one collection can contain this
  121. // parameter
  122. internal OdbcParameterCollection Container {
  123. get { return container; }
  124. set { container = value; }
  125. }
  126. #if NET_1_0
  127. [BrowsableAttribute (false)]
  128. [RefreshPropertiesAttribute (RefreshProperties.All)]
  129. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  130. #endif
  131. [OdbcCategory ("Data")]
  132. [OdbcDescriptionAttribute ("The parameter generic type")]
  133. public
  134. #if NET_2_0
  135. override
  136. #endif
  137. DbType DbType {
  138. get { return _typeMap.DbType; }
  139. set {
  140. if (value == _typeMap.DbType)
  141. return;
  142. _typeMap = OdbcTypeConverter.GetTypeMap (value);
  143. }
  144. }
  145. [OdbcCategory ("Data")]
  146. [OdbcDescriptionAttribute ("Input, output, or bidirectional parameter")]
  147. #if NET_2_0
  148. [RefreshPropertiesAttribute (RefreshProperties.All)]
  149. #else
  150. [DefaultValue (ParameterDirection.Input)]
  151. #endif
  152. public
  153. #if NET_2_0
  154. override
  155. #endif
  156. ParameterDirection Direction {
  157. get { return direction; }
  158. set { direction = value; }
  159. }
  160. #if NET_1_0
  161. [BrowsableAttribute (false)]
  162. [DesignOnlyAttribute (true)]
  163. [EditorBrowsableAttribute (EditorBrowsableState.Advanced)]
  164. [DefaultValue (false)]
  165. #endif
  166. [OdbcDescriptionAttribute ("A design-time property used for strongly typed code generation")]
  167. public
  168. #if NET_2_0
  169. override
  170. #endif
  171. bool IsNullable {
  172. get { return isNullable; }
  173. set { isNullable = value; }
  174. }
  175. [DefaultValue (OdbcType.NChar)]
  176. [OdbcDescriptionAttribute ("The parameter native type")]
  177. [RefreshPropertiesAttribute (RefreshProperties.All)]
  178. [OdbcCategory ("Data")]
  179. #if NET_2_0
  180. [DbProviderSpecificTypeProperty (false)]
  181. #endif
  182. public OdbcType OdbcType {
  183. get { return _typeMap.OdbcType; }
  184. set {
  185. if (value == OdbcType)
  186. return;
  187. _typeMap = OdbcTypeConverter.GetTypeMap (value);
  188. }
  189. }
  190. [OdbcDescription ("DataParameter_ParameterName")]
  191. #if NET_1_0
  192. [DefaultValue ("")]
  193. #endif
  194. public
  195. #if NET_2_0
  196. override
  197. #endif
  198. string ParameterName {
  199. get { return name; }
  200. set { name = value; }
  201. }
  202. [OdbcDescription ("DbDataParameter_Precision")]
  203. [OdbcCategory ("DataCategory_Data")]
  204. [DefaultValue (0)]
  205. public byte Precision {
  206. get { return _precision; }
  207. set { _precision = value; }
  208. }
  209. [OdbcDescription ("DbDataParameter_Scale")]
  210. [OdbcCategory ("DataCategory_Data")]
  211. [DefaultValue (0)]
  212. public byte Scale {
  213. get { return _scale; }
  214. set { _scale = value; }
  215. }
  216. [OdbcDescription ("DbDataParameter_Size")]
  217. [OdbcCategory ("DataCategory_Data")]
  218. #if NET_1_0
  219. [DefaultValue (0)]
  220. #endif
  221. public
  222. #if NET_2_0
  223. override
  224. #endif
  225. int Size {
  226. get { return size; }
  227. set { size = value; }
  228. }
  229. [OdbcDescription ("DataParameter_SourceColumn")]
  230. [OdbcCategory ("DataCategory_Data")]
  231. #if NET_1_0
  232. [DefaultValue ("")]
  233. #endif
  234. public
  235. #if NET_2_0
  236. override
  237. #endif
  238. string SourceColumn {
  239. get { return sourceColumn; }
  240. set { sourceColumn = value; }
  241. }
  242. [OdbcDescription ("DataParameter_SourceVersion")]
  243. [OdbcCategory ("DataCategory_Data")]
  244. #if NET_1_0
  245. [DefaultValue ("Current")]
  246. #endif
  247. public
  248. #if NET_2_0
  249. override
  250. #endif
  251. DataRowVersion SourceVersion {
  252. get { return sourceVersion; }
  253. set { sourceVersion = value; }
  254. }
  255. [TypeConverter (typeof(StringConverter))]
  256. [OdbcDescription ("DataParameter_Value")]
  257. [OdbcCategory ("DataCategory_Data")]
  258. #if NET_1_0
  259. [DefaultValue (null)]
  260. #else
  261. [RefreshPropertiesAttribute (RefreshProperties.All)]
  262. #endif
  263. public
  264. #if NET_2_0
  265. override
  266. #endif
  267. object Value {
  268. get {
  269. return _value;
  270. }
  271. set {
  272. _value = value;
  273. }
  274. }
  275. #endregion // Properties
  276. #region Methods
  277. internal void Bind(IntPtr hstmt, int ParamNum) {
  278. OdbcReturn ret;
  279. // Convert System.Data.ParameterDirection into odbc enum
  280. OdbcInputOutputDirection paramdir = libodbc.ConvertParameterDirection(this.Direction);
  281. _cbLengthInd.EnsureAlloc (Marshal.SizeOf (typeof (int)));
  282. Marshal.WriteInt32 (_cbLengthInd, GetNativeSize ());
  283. AllocateBuffer ();
  284. ret = libodbc.SQLBindParameter(hstmt, (ushort) ParamNum, (short) paramdir,
  285. _typeMap.NativeType, _typeMap.SqlType, Convert.ToUInt32(Size),
  286. 0, (IntPtr) _nativeBuffer, 0, _cbLengthInd);
  287. // Check for error condition
  288. if ((ret != OdbcReturn.Success) && (ret != OdbcReturn.SuccessWithInfo))
  289. throw new OdbcException(new OdbcError("SQLBindParam", OdbcHandleType.Stmt, hstmt));
  290. }
  291. [MonoTODO]
  292. object ICloneable.Clone ()
  293. {
  294. throw new NotImplementedException ();
  295. }
  296. public override string ToString ()
  297. {
  298. return ParameterName;
  299. }
  300. private int GetNativeSize ()
  301. {
  302. TextInfo ti = CultureInfo.InvariantCulture.TextInfo;
  303. Encoding enc = Encoding.GetEncoding (ti.ANSICodePage);
  304. switch (_typeMap.OdbcType) {
  305. case OdbcType.Binary:
  306. if (Value.GetType ().IsArray &&
  307. Value.GetType ().GetElementType () == typeof (byte))
  308. return ( (Array) Value).Length;
  309. else
  310. return Value.ToString ().Length;
  311. case OdbcType.Bit:
  312. return Marshal.SizeOf (typeof (byte));
  313. case OdbcType.Double:
  314. return Marshal.SizeOf (typeof (double));
  315. case OdbcType.Real:
  316. return Marshal.SizeOf (typeof (float));
  317. case OdbcType.Int:
  318. return Marshal.SizeOf (typeof (int));
  319. case OdbcType.BigInt:
  320. return Marshal.SizeOf (typeof (long));
  321. case OdbcType.Decimal:
  322. case OdbcType.Numeric:
  323. return 19;
  324. case OdbcType.SmallInt:
  325. return Marshal.SizeOf (typeof (Int16));
  326. case OdbcType.TinyInt:
  327. return Marshal.SizeOf (typeof (byte));
  328. case OdbcType.Char:
  329. case OdbcType.Text:
  330. case OdbcType.VarChar:
  331. return enc.GetByteCount (Convert.ToString (Value)) + 1;
  332. case OdbcType.NChar:
  333. case OdbcType.NText:
  334. case OdbcType.NVarChar:
  335. // FIXME: Change to unicode
  336. return enc.GetByteCount (Convert.ToString (Value)) + 1;
  337. case OdbcType.VarBinary:
  338. case OdbcType.Image:
  339. if (Value.GetType ().IsArray &&
  340. Value.GetType ().GetElementType () == typeof (byte))
  341. return ( (Array) Value).Length;
  342. throw new ArgumentException ("Unsupported Native Type!");
  343. case OdbcType.Date:
  344. case OdbcType.DateTime:
  345. case OdbcType.SmallDateTime:
  346. case OdbcType.Time:
  347. case OdbcType.Timestamp:
  348. return 18;
  349. case OdbcType.UniqueIdentifier:
  350. return Marshal.SizeOf (typeof (Guid));
  351. }
  352. if (Value.GetType ().IsArray &&
  353. Value.GetType ().GetElementType () == typeof (byte))
  354. return ( (Array) Value).Length;
  355. return Value.ToString ().Length;
  356. }
  357. private void AllocateBuffer ()
  358. {
  359. int size = GetNativeSize ();
  360. if (_nativeBuffer.Size == size)
  361. return;
  362. _nativeBuffer.AllocBuffer (size);
  363. }
  364. internal void CopyValue ()
  365. {
  366. if (_nativeBuffer.Handle == IntPtr.Zero)
  367. return;
  368. DateTime dt;
  369. TextInfo ti = CultureInfo.InvariantCulture.TextInfo;
  370. Encoding enc = Encoding.GetEncoding (ti.ANSICodePage);
  371. byte [] nativeBytes, buffer;
  372. switch (_typeMap.OdbcType) {
  373. case OdbcType.Binary:
  374. throw new NotImplementedException ();
  375. case OdbcType.Bit:
  376. Marshal.WriteByte (_nativeBuffer, Convert.ToByte (Value));
  377. return;
  378. case OdbcType.Double:
  379. Marshal.StructureToPtr (Convert.ToDouble (Value), _nativeBuffer, false);
  380. return;
  381. case OdbcType.Real:
  382. Marshal.StructureToPtr (Convert.ToSingle (Value), _nativeBuffer, false);
  383. return;
  384. case OdbcType.Int:
  385. Marshal.WriteInt32 (_nativeBuffer, Convert.ToInt32 (Value));
  386. return;
  387. case OdbcType.BigInt:
  388. Marshal.WriteInt64 (_nativeBuffer, Convert.ToInt64 (Value));
  389. return;
  390. case OdbcType.Decimal:
  391. case OdbcType.Numeric:
  392. // for numeric, the buffer is a packed decimal struct.
  393. // ref http://www.it-faq.pl/mskb/181/254.HTM
  394. int [] bits = Decimal.GetBits (Convert.ToDecimal (Value));
  395. buffer = new byte [19]; // ref sqltypes.h
  396. buffer [0] = Precision;
  397. buffer [1] = (byte) ((bits [3] & 0x00FF0000) >> 16); // scale
  398. buffer [2] = (byte) ((bits [3] & 0x80000000) > 0 ? 2 : 1); //sign
  399. Buffer.BlockCopy (bits, 0, buffer, 3, 12); // copy data
  400. for (int j = 16; j < 19; j++) // pad with 0
  401. buffer [j] = 0;
  402. Marshal.Copy (buffer, 0, _nativeBuffer, 19);
  403. return;
  404. case OdbcType.SmallInt:
  405. Marshal.WriteInt16 (_nativeBuffer, Convert.ToInt16 (Value));
  406. return;
  407. case OdbcType.TinyInt:
  408. Marshal.WriteByte (_nativeBuffer, Convert.ToByte (Value));
  409. return;
  410. case OdbcType.Char:
  411. case OdbcType.Text:
  412. case OdbcType.VarChar:
  413. buffer = new byte [GetNativeSize ()];
  414. nativeBytes = enc.GetBytes (Convert.ToString (Value));
  415. Array.Copy (nativeBytes, 0, buffer, 0, nativeBytes.Length);
  416. buffer [buffer.Length-1] = (byte) 0;
  417. Marshal.Copy (buffer, 0, _nativeBuffer, buffer.Length);
  418. Marshal.WriteInt32 (_cbLengthInd, -3);
  419. return;
  420. case OdbcType.NChar:
  421. case OdbcType.NText:
  422. case OdbcType.NVarChar:
  423. // FIXME : change to unicode
  424. buffer = new byte [GetNativeSize ()];
  425. nativeBytes = enc.GetBytes (Convert.ToString (Value));
  426. Array.Copy (nativeBytes, 0, buffer, 0, nativeBytes.Length);
  427. buffer [buffer.Length-1] = (byte) 0;
  428. Marshal.Copy (buffer, 0, _nativeBuffer, buffer.Length);
  429. Marshal.WriteInt32 (_cbLengthInd, -3);
  430. return;
  431. case OdbcType.VarBinary:
  432. case OdbcType.Image:
  433. if (Value.GetType ().IsArray &&
  434. Value.GetType ().GetElementType () == typeof (byte)) {
  435. Marshal.Copy ( (byte []) Value, 0, _nativeBuffer, ((byte []) Value).Length);
  436. }else
  437. throw new ArgumentException ("Unsupported Native Type!");
  438. return;
  439. case OdbcType.Date:
  440. dt = (DateTime) Value;
  441. Marshal.WriteInt16 (_nativeBuffer, 0, (short) dt.Year);
  442. Marshal.WriteInt16 (_nativeBuffer, 2, (short) dt.Month);
  443. Marshal.WriteInt16 (_nativeBuffer, 4, (short) dt.Day);
  444. return;
  445. case OdbcType.Time:
  446. dt = (DateTime) Value;
  447. Marshal.WriteInt16 (_nativeBuffer, 0, (short) dt.Hour);
  448. Marshal.WriteInt16 (_nativeBuffer, 2, (short) dt.Minute);
  449. Marshal.WriteInt16 (_nativeBuffer, 4, (short) dt.Second);
  450. return;
  451. case OdbcType.SmallDateTime:
  452. case OdbcType.Timestamp:
  453. case OdbcType.DateTime:
  454. dt = (DateTime) Value;
  455. Marshal.WriteInt16 (_nativeBuffer, 0, (short) dt.Year);
  456. Marshal.WriteInt16 (_nativeBuffer, 2, (short) dt.Month);
  457. Marshal.WriteInt16 (_nativeBuffer, 4, (short) dt.Day);
  458. Marshal.WriteInt16 (_nativeBuffer, 6, (short) dt.Hour);
  459. Marshal.WriteInt16 (_nativeBuffer, 8, (short) dt.Minute);
  460. Marshal.WriteInt16 (_nativeBuffer, 10, (short) dt.Second);
  461. Marshal.WriteInt32 (_nativeBuffer, 12, (int) (dt.Ticks % 10000000) * 100);
  462. return;
  463. case OdbcType.UniqueIdentifier:
  464. throw new NotImplementedException ();
  465. }
  466. if (Value.GetType ().IsArray &&
  467. Value.GetType ().GetElementType () == typeof (byte)) {
  468. Marshal.Copy ( (byte []) Value, 0, _nativeBuffer, ((byte []) Value).Length);
  469. }else
  470. throw new ArgumentException ("Unsupported Native Type!");
  471. }
  472. #if NET_2_0
  473. public override bool SourceColumnNullMapping {
  474. get {return false;}
  475. set {}
  476. }
  477. public override void ResetDbType ()
  478. {
  479. throw new NotImplementedException ();
  480. }
  481. #endif
  482. #endregion
  483. }
  484. }