OdbcParameter.cs 15 KB

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