OdbcParameter.cs 16 KB

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