OdbcParameter.cs 15 KB

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