OdbcParameter.cs 16 KB

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