OdbcParameter.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  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 = null;
  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 odbcType)
  96. : this ()
  97. {
  98. this.ParameterName = name;
  99. _typeMap = (OdbcTypeMap) OdbcTypeConverter.GetTypeMap (odbcType);
  100. }
  101. public OdbcParameter (string name, OdbcType odbcType, int size)
  102. : this (name, odbcType)
  103. {
  104. this.Size = size;
  105. }
  106. public OdbcParameter (string name, OdbcType odbcType, int size, string srcColumn)
  107. : this (name, odbcType, size)
  108. {
  109. this.SourceColumn = srcColumn;
  110. }
  111. [EditorBrowsable (EditorBrowsableState.Advanced)]
  112. public OdbcParameter(string name, OdbcType odbcType, int size,
  113. ParameterDirection direction, bool isNullable,
  114. byte precision, byte scale, string srcColumn,
  115. DataRowVersion srcVersion, object value)
  116. : this (name, odbcType, size, srcColumn)
  117. {
  118. this.Direction = direction;
  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 {
  273. return _value;
  274. }
  275. set {
  276. _value = value;
  277. }
  278. }
  279. #endregion // Properties
  280. #region Methods
  281. internal void Bind(IntPtr hstmt, int ParamNum) {
  282. OdbcReturn ret;
  283. // Convert System.Data.ParameterDirection into odbc enum
  284. OdbcInputOutputDirection paramdir = libodbc.ConvertParameterDirection(this.Direction);
  285. _cbLengthInd.EnsureAlloc (Marshal.SizeOf (typeof (int)));
  286. Marshal.WriteInt32 (_cbLengthInd, GetNativeSize ());
  287. AllocateBuffer ();
  288. ret = libodbc.SQLBindParameter(hstmt, (ushort) ParamNum, (short) paramdir,
  289. _typeMap.NativeType, _typeMap.SqlType, Convert.ToUInt32(Size),
  290. 0, (IntPtr) _nativeBuffer, 0, _cbLengthInd);
  291. // Check for error condition
  292. if ((ret != OdbcReturn.Success) && (ret != OdbcReturn.SuccessWithInfo))
  293. throw new OdbcException(new OdbcError("SQLBindParam", OdbcHandleType.Stmt, hstmt));
  294. }
  295. [MonoTODO]
  296. object ICloneable.Clone ()
  297. {
  298. throw new NotImplementedException ();
  299. }
  300. public override string ToString ()
  301. {
  302. return ParameterName;
  303. }
  304. private int GetNativeSize ()
  305. {
  306. TextInfo ti = CultureInfo.InvariantCulture.TextInfo;
  307. Encoding enc = Encoding.GetEncoding (ti.ANSICodePage);
  308. switch (_typeMap.OdbcType) {
  309. case OdbcType.Binary:
  310. if (Value.GetType ().IsArray &&
  311. Value.GetType ().GetElementType () == typeof (byte))
  312. return ( (Array) Value).Length;
  313. else
  314. return Value.ToString ().Length;
  315. case OdbcType.Bit:
  316. return Marshal.SizeOf (typeof (byte));
  317. case OdbcType.Double:
  318. return Marshal.SizeOf (typeof (double));
  319. case OdbcType.Real:
  320. return Marshal.SizeOf (typeof (float));
  321. case OdbcType.Int:
  322. return Marshal.SizeOf (typeof (int));
  323. case OdbcType.BigInt:
  324. return Marshal.SizeOf (typeof (long));
  325. case OdbcType.Decimal:
  326. case OdbcType.Numeric:
  327. return 19;
  328. case OdbcType.SmallInt:
  329. return Marshal.SizeOf (typeof (Int16));
  330. case OdbcType.TinyInt:
  331. return Marshal.SizeOf (typeof (byte));
  332. case OdbcType.Char:
  333. case OdbcType.Text:
  334. case OdbcType.VarChar:
  335. return enc.GetByteCount (Convert.ToString (Value)) + 1;
  336. case OdbcType.NChar:
  337. case OdbcType.NText:
  338. case OdbcType.NVarChar:
  339. // FIXME: Change to unicode
  340. return enc.GetByteCount (Convert.ToString (Value)) + 1;
  341. case OdbcType.VarBinary:
  342. case OdbcType.Image:
  343. if (Value.GetType ().IsArray &&
  344. Value.GetType ().GetElementType () == typeof (byte))
  345. return ( (Array) Value).Length;
  346. throw new ArgumentException ("Unsupported Native Type!");
  347. case OdbcType.Date:
  348. case OdbcType.DateTime:
  349. case OdbcType.SmallDateTime:
  350. case OdbcType.Time:
  351. case OdbcType.Timestamp:
  352. return 18;
  353. case OdbcType.UniqueIdentifier:
  354. return Marshal.SizeOf (typeof (Guid));
  355. }
  356. if (Value.GetType ().IsArray &&
  357. Value.GetType ().GetElementType () == typeof (byte))
  358. return ( (Array) Value).Length;
  359. return Value.ToString ().Length;
  360. }
  361. private void AllocateBuffer ()
  362. {
  363. int size = GetNativeSize ();
  364. if (_nativeBuffer.Size == size)
  365. return;
  366. _nativeBuffer.AllocBuffer (size);
  367. }
  368. internal void CopyValue ()
  369. {
  370. if (_nativeBuffer.Handle == IntPtr.Zero)
  371. return;
  372. DateTime dt;
  373. TextInfo ti = CultureInfo.InvariantCulture.TextInfo;
  374. Encoding enc = Encoding.GetEncoding (ti.ANSICodePage);
  375. byte [] nativeBytes, buffer;
  376. switch (_typeMap.OdbcType) {
  377. case OdbcType.Binary:
  378. throw new NotImplementedException ();
  379. case OdbcType.Bit:
  380. Marshal.WriteByte (_nativeBuffer, Convert.ToByte (Value));
  381. return;
  382. case OdbcType.Double:
  383. Marshal.StructureToPtr (Convert.ToDouble (Value), _nativeBuffer, false);
  384. return;
  385. case OdbcType.Real:
  386. Marshal.StructureToPtr (Convert.ToSingle (Value), _nativeBuffer, false);
  387. return;
  388. case OdbcType.Int:
  389. Marshal.WriteInt32 (_nativeBuffer, Convert.ToInt32 (Value));
  390. return;
  391. case OdbcType.BigInt:
  392. Marshal.WriteInt64 (_nativeBuffer, Convert.ToInt64 (Value));
  393. return;
  394. case OdbcType.Decimal:
  395. case OdbcType.Numeric:
  396. // for numeric, the buffer is a packed decimal struct.
  397. // ref http://www.it-faq.pl/mskb/181/254.HTM
  398. int [] bits = Decimal.GetBits (Convert.ToDecimal (Value));
  399. buffer = new byte [19]; // ref sqltypes.h
  400. buffer [0] = Precision;
  401. buffer [1] = (byte) ((bits [3] & 0x00FF0000) >> 16); // scale
  402. buffer [2] = (byte) ((bits [3] & 0x80000000) > 0 ? 2 : 1); //sign
  403. Buffer.BlockCopy (bits, 0, buffer, 3, 12); // copy data
  404. for (int j = 16; j < 19; j++) // pad with 0
  405. buffer [j] = 0;
  406. Marshal.Copy (buffer, 0, _nativeBuffer, 19);
  407. return;
  408. case OdbcType.SmallInt:
  409. Marshal.WriteInt16 (_nativeBuffer, Convert.ToInt16 (Value));
  410. return;
  411. case OdbcType.TinyInt:
  412. Marshal.WriteByte (_nativeBuffer, Convert.ToByte (Value));
  413. return;
  414. case OdbcType.Char:
  415. case OdbcType.Text:
  416. case OdbcType.VarChar:
  417. buffer = new byte [GetNativeSize ()];
  418. nativeBytes = enc.GetBytes (Convert.ToString (Value));
  419. Array.Copy (nativeBytes, 0, buffer, 0, nativeBytes.Length);
  420. buffer [buffer.Length-1] = (byte) 0;
  421. Marshal.Copy (buffer, 0, _nativeBuffer, buffer.Length);
  422. Marshal.WriteInt32 (_cbLengthInd, -3);
  423. return;
  424. case OdbcType.NChar:
  425. case OdbcType.NText:
  426. case OdbcType.NVarChar:
  427. // FIXME : change to unicode
  428. buffer = new byte [GetNativeSize ()];
  429. nativeBytes = enc.GetBytes (Convert.ToString (Value));
  430. Array.Copy (nativeBytes, 0, buffer, 0, nativeBytes.Length);
  431. buffer [buffer.Length-1] = (byte) 0;
  432. Marshal.Copy (buffer, 0, _nativeBuffer, buffer.Length);
  433. Marshal.WriteInt32 (_cbLengthInd, -3);
  434. return;
  435. case OdbcType.VarBinary:
  436. case OdbcType.Image:
  437. if (Value.GetType ().IsArray &&
  438. Value.GetType ().GetElementType () == typeof (byte)) {
  439. Marshal.Copy ( (byte []) Value, 0, _nativeBuffer, ((byte []) Value).Length);
  440. }else
  441. throw new ArgumentException ("Unsupported Native Type!");
  442. return;
  443. case OdbcType.Date:
  444. dt = (DateTime) Value;
  445. Marshal.WriteInt16 (_nativeBuffer, 0, (short) dt.Year);
  446. Marshal.WriteInt16 (_nativeBuffer, 2, (short) dt.Month);
  447. Marshal.WriteInt16 (_nativeBuffer, 4, (short) dt.Day);
  448. return;
  449. case OdbcType.Time:
  450. dt = (DateTime) Value;
  451. Marshal.WriteInt16 (_nativeBuffer, 0, (short) dt.Hour);
  452. Marshal.WriteInt16 (_nativeBuffer, 2, (short) dt.Minute);
  453. Marshal.WriteInt16 (_nativeBuffer, 4, (short) dt.Second);
  454. return;
  455. case OdbcType.SmallDateTime:
  456. case OdbcType.Timestamp:
  457. case OdbcType.DateTime:
  458. dt = (DateTime) Value;
  459. Marshal.WriteInt16 (_nativeBuffer, 0, (short) dt.Year);
  460. Marshal.WriteInt16 (_nativeBuffer, 2, (short) dt.Month);
  461. Marshal.WriteInt16 (_nativeBuffer, 4, (short) dt.Day);
  462. Marshal.WriteInt16 (_nativeBuffer, 6, (short) dt.Hour);
  463. Marshal.WriteInt16 (_nativeBuffer, 8, (short) dt.Minute);
  464. Marshal.WriteInt16 (_nativeBuffer, 10, (short) dt.Second);
  465. Marshal.WriteInt32 (_nativeBuffer, 12, (int) (dt.Ticks % 10000000) * 100);
  466. return;
  467. case OdbcType.UniqueIdentifier:
  468. throw new NotImplementedException ();
  469. }
  470. if (Value.GetType ().IsArray &&
  471. Value.GetType ().GetElementType () == typeof (byte)) {
  472. Marshal.Copy ( (byte []) Value, 0, _nativeBuffer, ((byte []) Value).Length);
  473. }else
  474. throw new ArgumentException ("Unsupported Native Type!");
  475. }
  476. #if NET_2_0
  477. public override bool SourceColumnNullMapping {
  478. get {return false;}
  479. set {}
  480. }
  481. public override void ResetDbType ()
  482. {
  483. _typeMap = OdbcTypeConverter.GetTypeMap (OdbcType.NVarChar);
  484. }
  485. public void ResetOdbcType ()
  486. {
  487. _typeMap = OdbcTypeConverter.GetTypeMap (OdbcType.NVarChar);
  488. }
  489. #endif
  490. #endregion
  491. }
  492. }