PageRenderTime 39ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/mcs/class/System.Data.OracleClient/System.Data.OracleClient/OracleParameterCollection.cs

https://bitbucket.org/danipen/mono
C# | 588 lines | 488 code | 82 blank | 18 comment | 63 complexity | 3868e5a0f96c4000f819b0a200784e2b MD5 | raw file
Possible License(s): Unlicense, Apache-2.0, LGPL-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0
  1. //
  2. // OracleParameterCollection.cs
  3. //
  4. // Part of the Mono class libraries at
  5. // mcs/class/System.Data.OracleClient/System.Data.OracleClient
  6. //
  7. // Assembly: System.Data.OracleClient.dll
  8. // Namespace: System.Data.OracleClient
  9. //
  10. // Authors:
  11. // Tim Coleman <tim@timcoleman.com>
  12. //
  13. // Copyright (C) Tim Coleman , 2003
  14. //
  15. // Licensed under the MIT/X11 License.
  16. //
  17. using System;
  18. using System.Collections;
  19. using System.ComponentModel;
  20. using System.Data;
  21. #if NET_2_0
  22. using System.Data.Common;
  23. #endif
  24. using System.Data.OracleClient.Oci;
  25. using System.Drawing.Design;
  26. using System.Globalization;
  27. using System.Reflection;
  28. namespace System.Data.OracleClient
  29. {
  30. [ListBindable (false)]
  31. [Editor ("Microsoft.VSDesigner.Data.Design.DBParametersEditor, " + Consts.AssemblyMicrosoft_VSDesigner, typeof(UITypeEditor))]
  32. public sealed class OracleParameterCollection :
  33. #if NET_2_0
  34. DbParameterCollection
  35. #else
  36. MarshalByRefObject, IDataParameterCollection, IList, ICollection, IEnumerable
  37. #endif
  38. {
  39. #region Fields
  40. readonly ArrayList list;
  41. #endregion // Fields
  42. #region Constructors
  43. public OracleParameterCollection ()
  44. {
  45. list = new ArrayList ();
  46. }
  47. #endregion // Constructors
  48. #region Properties
  49. public
  50. #if NET_2_0
  51. override
  52. #endif
  53. int Count {
  54. get { return list.Count; }
  55. }
  56. public
  57. #if NET_2_0
  58. override
  59. #endif
  60. bool IsFixedSize {
  61. get { return list.IsFixedSize; }
  62. }
  63. public
  64. #if NET_2_0
  65. override
  66. #endif
  67. bool IsReadOnly {
  68. get { return list.IsReadOnly; }
  69. }
  70. public
  71. #if NET_2_0
  72. override
  73. #endif
  74. bool IsSynchronized {
  75. get { return list.IsSynchronized; }
  76. }
  77. public
  78. #if NET_2_0
  79. new
  80. #endif
  81. OracleParameter this [string parameterName] {
  82. get {
  83. return (OracleParameter) GetParameter (parameterName);
  84. }
  85. set {
  86. SetParameter (parameterName, value);
  87. }
  88. }
  89. public
  90. #if NET_2_0
  91. override
  92. #endif
  93. object SyncRoot {
  94. get { return this; }
  95. }
  96. public
  97. #if NET_2_0
  98. new
  99. #endif
  100. OracleParameter this [int index]
  101. {
  102. get {
  103. return (OracleParameter) GetParameter (index);
  104. }
  105. set {
  106. SetParameter (index, value);
  107. }
  108. }
  109. #if !NET_2_0
  110. object IList.this [int index] {
  111. get { return this [index]; }
  112. set { this [index] = (OracleParameter) value; }
  113. }
  114. object IDataParameterCollection.this [string index] {
  115. get { return this [index]; }
  116. set {
  117. if (!(value is OracleParameter))
  118. throw new InvalidCastException ("The parameter was not an OracleParameter.");
  119. this [index] = (OracleParameter) value;
  120. }
  121. }
  122. #endif
  123. #endregion // Properties
  124. #region Methods
  125. #if NET_2_0
  126. [EditorBrowsable (EditorBrowsableState.Never)]
  127. #endif
  128. public
  129. #if NET_2_0
  130. override
  131. #endif
  132. int Add (object value)
  133. {
  134. AssertParameterValid (value);
  135. Add ((OracleParameter) value);
  136. return IndexOf (value);
  137. }
  138. public OracleParameter Add (OracleParameter value)
  139. {
  140. #if NET_2_0
  141. if (value == null)
  142. throw CreateParameterNullException ();
  143. #endif
  144. if (value.Container != null)
  145. throw new ArgumentException ("The OracleParameter specified in the value parameter is already added to this or another OracleParameterCollection.");
  146. value.Container = this;
  147. list.Add (value);
  148. return value;
  149. }
  150. public OracleParameter Add (string parameterName, object value)
  151. {
  152. return Add (new OracleParameter (parameterName, value));
  153. }
  154. public OracleParameter Add (string parameterName, OracleType dataType)
  155. {
  156. return Add (new OracleParameter (parameterName, dataType));
  157. }
  158. public OracleParameter Add (string parameterName, OracleType dataType, int size)
  159. {
  160. return Add (new OracleParameter (parameterName, dataType, size));
  161. }
  162. public OracleParameter Add (string parameterName, OracleType dataType, int size, string srcColumn)
  163. {
  164. return Add (new OracleParameter (parameterName, dataType, size, srcColumn));
  165. }
  166. #if NET_2_0
  167. public override void AddRange (Array values)
  168. {
  169. if (values == null)
  170. throw new ArgumentNullException ("values");
  171. foreach (object param in values)
  172. AssertParameterValid (param);
  173. foreach (OracleParameter param in values)
  174. Add (param);
  175. }
  176. public void AddRange (OracleParameter [] values)
  177. {
  178. if (values == null)
  179. throw new ArgumentNullException ("values");
  180. foreach (OracleParameter param in values)
  181. if (param == null)
  182. throw CreateParameterNullException ();
  183. foreach (OracleParameter param in values)
  184. Add (param);
  185. }
  186. #endif
  187. public
  188. #if NET_2_0
  189. override
  190. #endif
  191. void Clear ()
  192. {
  193. foreach (OracleParameter param in list)
  194. param.Container = null;
  195. list.Clear ();
  196. }
  197. public
  198. #if NET_2_0
  199. override
  200. #endif
  201. bool Contains (object value)
  202. {
  203. return (IndexOf (value) != -1);
  204. }
  205. #if NET_2_0
  206. public bool Contains (OracleParameter value)
  207. {
  208. return (IndexOf (value) != -1);
  209. }
  210. #endif
  211. public
  212. #if NET_2_0
  213. override
  214. #endif
  215. bool Contains (string parameterName)
  216. {
  217. return (IndexOf (parameterName) != -1);
  218. }
  219. public
  220. #if NET_2_0
  221. override
  222. #endif
  223. void CopyTo (Array array, int index)
  224. {
  225. list.CopyTo (array, index);
  226. }
  227. public
  228. #if NET_2_0
  229. override
  230. #endif
  231. IEnumerator GetEnumerator ()
  232. {
  233. return list.GetEnumerator ();
  234. }
  235. #if NET_2_0
  236. protected override DbParameter GetParameter (int index)
  237. #else
  238. object GetParameter (int index)
  239. #endif
  240. {
  241. AssertIndex (index);
  242. return (OracleParameter) list [index];
  243. }
  244. #if NET_2_0
  245. protected override DbParameter GetParameter (string parameterName)
  246. #else
  247. object GetParameter (string parameterName)
  248. #endif
  249. {
  250. int index = IndexOf (parameterName);
  251. if (index == -1)
  252. throw ParameterNotFoundException (parameterName, index);
  253. return (OracleParameter) list [index];
  254. }
  255. #if NET_2_0
  256. protected override void SetParameter (int index, DbParameter value)
  257. #else
  258. void SetParameter (int index, IDbDataParameter value)
  259. #endif
  260. {
  261. AssertIndex (index);
  262. AssertParameterValid (value);
  263. OracleParameter new_value = (OracleParameter) value;
  264. OracleParameter old_value = (OracleParameter) list [index];
  265. #if !NET_2_0
  266. old_value.Container = null;
  267. #endif
  268. if (new_value.Container != null) {
  269. if (new_value.Container != this)
  270. throw ParameterAlreadyOwnedException ();
  271. if (IndexOf (new_value) != index)
  272. throw ParameterAlreadyOwnedException ();
  273. }
  274. list [index] = new_value;
  275. new_value.Container = this;
  276. #if NET_2_0
  277. old_value.Container = null;
  278. #endif
  279. }
  280. #if NET_2_0
  281. protected override void SetParameter (string parameterName, DbParameter value)
  282. #else
  283. void SetParameter (string parameterName, IDbDataParameter value)
  284. #endif
  285. {
  286. int index = IndexOf (parameterName);
  287. if (index == -1)
  288. throw ParameterNotFoundException (parameterName, index);
  289. AssertParameterValid (value);
  290. OracleParameter new_value = (OracleParameter) value;
  291. OracleParameter old_value = (OracleParameter) list [index];
  292. #if !NET_2_0
  293. old_value.Container = null;
  294. #endif
  295. if (new_value.Container != null) {
  296. if (new_value.Container != this)
  297. throw ParameterAlreadyOwnedException ();
  298. if (IndexOf (new_value) != index)
  299. throw ParameterAlreadyOwnedException ();
  300. }
  301. list [index] = new_value;
  302. new_value.Container = this;
  303. #if NET_2_0
  304. old_value.Container = null;
  305. #endif
  306. }
  307. public
  308. #if NET_2_0
  309. override
  310. #endif
  311. int IndexOf (object value)
  312. {
  313. if (value != null)
  314. AssertParameterValid (value);
  315. for (int i = 0; i < Count; i += 1)
  316. if (list [i] == value)
  317. return i;
  318. return -1;
  319. }
  320. #if NET_2_0
  321. public int IndexOf (OracleParameter value)
  322. {
  323. for (int i = 0; i < Count; i += 1)
  324. if (list [i] == value)
  325. return i;
  326. return -1;
  327. }
  328. #endif
  329. public
  330. #if NET_2_0
  331. override
  332. #endif
  333. int IndexOf (string parameterName)
  334. {
  335. #if NET_2_0
  336. // case-sensitive lookup
  337. for (int i = 0; i < Count; i += 1) {
  338. OracleParameter param = (OracleParameter) list [i];
  339. if (string.Compare (param.ParameterName, parameterName, false, CultureInfo.CurrentCulture) == 0)
  340. return i;
  341. }
  342. #endif
  343. // case-insensitive lookup
  344. for (int i = 0; i < Count; i += 1) {
  345. OracleParameter param = (OracleParameter) list [i];
  346. if (string.Compare (param.ParameterName, parameterName, true, CultureInfo.CurrentCulture) == 0)
  347. return i;
  348. }
  349. return -1;
  350. }
  351. public
  352. #if NET_2_0
  353. override
  354. #endif
  355. void Insert (int index, object value)
  356. {
  357. AssertParameterValid (value);
  358. OracleParameter new_value = (OracleParameter) value;
  359. if (new_value.Container != null) {
  360. if (new_value.Container != this)
  361. throw ParameterAlreadyOwnedException ();
  362. if (IndexOf (value) != -1)
  363. throw ParameterAlreadyOwnedException ();
  364. }
  365. list.Insert (index, new_value);
  366. new_value.Container = this;
  367. }
  368. #if NET_2_0
  369. public void Insert (int index, OracleParameter value)
  370. {
  371. Insert (index, (object) value);
  372. }
  373. #endif
  374. public
  375. #if NET_2_0
  376. override
  377. #endif
  378. void Remove (object value)
  379. {
  380. AssertParameterValid (value);
  381. int index = IndexOf (value);
  382. if (index == -1)
  383. throw ParameterNotOwnedException ();
  384. ((OracleParameter) value).Container = null;
  385. list.RemoveAt (index);
  386. }
  387. #if NET_2_0
  388. public void Remove (OracleParameter value)
  389. {
  390. if (value == null)
  391. throw CreateParameterNullException ();
  392. int index = IndexOf (value);
  393. if (index == -1)
  394. throw ParameterNotOwnedException ();
  395. value.Container = null;
  396. list.RemoveAt (index);
  397. }
  398. #endif
  399. public
  400. #if NET_2_0
  401. override
  402. #endif
  403. void RemoveAt (int index)
  404. {
  405. AssertIndex (index);
  406. OracleParameter param = (OracleParameter) list [index];
  407. param.Container = null;
  408. list.RemoveAt (index);
  409. }
  410. public
  411. #if NET_2_0
  412. override
  413. #endif
  414. void RemoveAt (string parameterName)
  415. {
  416. int index = IndexOf (parameterName);
  417. if (index == -1)
  418. throw ParameterNotOwnedException (parameterName);
  419. OracleParameter param = (OracleParameter) list [index];
  420. param.Container = null;
  421. list.RemoveAt (index);
  422. }
  423. static void AssertParameterValid (object value)
  424. {
  425. if (value == null)
  426. throw CreateParameterNullException ();
  427. if (value is OracleParameter)
  428. return;
  429. string msg = string.Format (CultureInfo.InvariantCulture,
  430. #if NET_2_0
  431. "Only non-null {0} instances are valid for " +
  432. "the {1}, not {2} instances.",
  433. typeof (OracleParameter).Name,
  434. typeof (OracleParameterCollection).Name,
  435. value.GetType ().Name);
  436. #else
  437. "Value is not {0}.",
  438. typeof (OracleParameter).Name);
  439. #endif
  440. throw new InvalidCastException (msg);
  441. }
  442. static Exception CreateParameterNullException ()
  443. {
  444. #if NET_2_0
  445. string msg = string.Format (CultureInfo.InvariantCulture,
  446. "Only non-null {0} instances are valid for " +
  447. "{1}.", typeof (OracleParameter).Name,
  448. typeof (OracleParameterCollection).Name);
  449. return new ArgumentNullException ("value", msg);
  450. #else
  451. return new ArgumentNullException ("value");
  452. #endif
  453. }
  454. static Exception ParameterAlreadyOwnedException ()
  455. {
  456. string msg = string.Format (CultureInfo.InvariantCulture,
  457. "The specified {0} is already owned by this " +
  458. "or another {1}.", typeof (OracleParameter).Name,
  459. typeof (OracleParameterCollection).Name);
  460. throw new ArgumentException (msg);
  461. }
  462. Exception ParameterNotFoundException (string name, int index)
  463. {
  464. string msg = string.Format (CultureInfo.InvariantCulture,
  465. #if NET_2_0
  466. "Index {0} is not valid for this {1}.",
  467. index, typeof (OracleParameterCollection).Name);
  468. #else
  469. "Parameter '{0}' not found.", name);
  470. #endif
  471. throw new IndexOutOfRangeException (msg);
  472. }
  473. Exception ParameterNotOwnedException ()
  474. {
  475. throw new ArgumentException (string.Format (
  476. CultureInfo.InvariantCulture,
  477. "An {0} instance that is not contained " +
  478. "by this {1} cannot be removed.",
  479. typeof (OracleParameter).Name,
  480. this.GetType ().Name));
  481. }
  482. Exception ParameterNotOwnedException (string name)
  483. {
  484. #if NET_2_0
  485. throw new IndexOutOfRangeException (string.Format (
  486. CultureInfo.InvariantCulture,
  487. "{0} parameter '{1}' is not contained by " +
  488. "this {2}.", typeof (OracleParameter).Name,
  489. name, this.GetType ().Name));
  490. #else
  491. throw new IndexOutOfRangeException (string.Format (
  492. CultureInfo.InvariantCulture,
  493. "Parameter '{0}' does not exist.", name));
  494. #endif
  495. }
  496. void AssertIndex (int index)
  497. {
  498. if (index < 0 || index >= Count)
  499. throw new IndexOutOfRangeException (string.Format (
  500. CultureInfo.InvariantCulture, "Index {0} " +
  501. "is not valid for this {1}.", index,
  502. typeof (OracleParameterCollection).Name));
  503. }
  504. #endregion // Methods
  505. }
  506. }