OdbcParameter.cs 15 KB

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