**** Merged from MCS ****
[mono-project.git] / mcs / class / Mono.Data.SqliteClient / Mono.Data.SqliteClient / SqliteParameterCollection.cs
blobe778c1e5116db6ecaff995d2e6d069e310963a8a
1 //
2 // Mono.Data.SqliteClient.SqliteParameterCollection.cs
3 //
4 // Represents a collection of parameters relevant to a SqliteCommand as well as
5 // their respective mappings to columns in a DataSet.
6 //
7 // Author(s): Vladimir Vukicevic <vladimir@pobox.com>
8 // Everaldo Canuto <everaldo_canuto@yahoo.com.br>
9 //
10 // Copyright (C) 2002 Vladimir Vukicevic
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 //
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 //
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System;
33 using System.Data;
34 using System.Collections;
36 namespace Mono.Data.SqliteClient
38 public class SqliteParameterCollection : IDataParameterCollection, IList
41 #region Fields
43 ArrayList numeric_param_list = new ArrayList();
44 Hashtable named_param_hash = new Hashtable();
46 #endregion
48 #region Private Methods
50 private void CheckSqliteParam (object value)
52 if (!(value is SqliteParameter))
53 throw new InvalidCastException("Can only use SqliteParameter objects");
56 private void RecreateNamedHash ()
58 for (int i = 0; i < numeric_param_list.Count; i++) {
59 named_param_hash[((SqliteParameter) numeric_param_list[i]).ParameterName] = i;
63 #endregion
65 #region Properties
67 object IList.this[int index] {
68 get {
69 return this[index];
71 set {
72 CheckSqliteParam (value);
73 this[index] = (SqliteParameter) value;
77 object IDataParameterCollection.this[string parameterName] {
78 get {
79 return this[parameterName];
81 set {
82 CheckSqliteParam (value);
83 this[parameterName] = (SqliteParameter) value;
87 public SqliteParameter this[string parameterName] {
88 get {
89 return this[(int) named_param_hash[parameterName]];
91 set {
92 if (this.Contains (parameterName))
93 numeric_param_list[(int) named_param_hash[parameterName]] = value;
94 else // uhm, do we add it if it doesn't exist? what does ms do?
95 Add (value);
99 public SqliteParameter this[int parameterIndex] {
100 get {
101 return (SqliteParameter) numeric_param_list[parameterIndex];
103 set {
104 numeric_param_list[parameterIndex] = value;
108 public int Count {
109 get { return numeric_param_list.Count; }
112 public bool IsFixedSize {
113 get { return false; }
116 public bool IsReadOnly {
117 get { return false; }
120 public bool IsSynchronized {
121 get { return false; }
124 public object SyncRoot {
125 get { return null; }
128 #endregion
130 #region Public Methods
132 public int Add (object value)
134 CheckSqliteParam (value);
135 SqliteParameter sqlp = (SqliteParameter) value;
136 if (named_param_hash.Contains (sqlp.ParameterName))
137 throw new DuplicateNameException ("Parameter collection already contains given value.");
139 named_param_hash[value] = numeric_param_list.Add (value);
141 return (int) named_param_hash[value];
144 public SqliteParameter Add (SqliteParameter param)
146 Add ((object)param);
147 return param;
150 public SqliteParameter Add (string name, object value)
152 return Add (new SqliteParameter (name, value));
155 public SqliteParameter Add (string name, DbType type)
157 return Add (new SqliteParameter (name, type));
160 public void Clear ()
162 numeric_param_list.Clear ();
163 named_param_hash.Clear ();
166 public void CopyTo (Array array, int index)
168 throw new NotImplementedException ();
171 bool IList.Contains (object value)
173 return Contains ((SqliteParameter) value);
176 public bool Contains (string parameterName)
178 return named_param_hash.Contains (parameterName);
181 public bool Contains (SqliteParameter param)
183 return Contains (param.ParameterName);
186 public IEnumerator GetEnumerator ()
188 throw new NotImplementedException ();
191 int IList.IndexOf (object param)
193 return IndexOf ((SqliteParameter) param);
196 public int IndexOf (string parameterName)
198 return (int) named_param_hash[parameterName];
201 public int IndexOf (SqliteParameter param)
203 return IndexOf (param.ParameterName);
206 public void Insert (int index, object value)
208 CheckSqliteParam (value);
209 if (numeric_param_list.Count == index) {
210 Add (value);
211 return;
214 numeric_param_list.Insert (index, value);
215 RecreateNamedHash ();
218 public void Remove (object value)
220 CheckSqliteParam (value);
221 RemoveAt ((SqliteParameter) value);
224 public void RemoveAt (int index)
226 RemoveAt (((SqliteParameter) numeric_param_list[index]).ParameterName);
229 public void RemoveAt (string parameterName)
231 if (!named_param_hash.Contains (parameterName))
232 throw new ApplicationException ("Parameter " + parameterName + " not found");
234 numeric_param_list.RemoveAt ((int) named_param_hash[parameterName]);
235 named_param_hash.Remove (parameterName);
237 RecreateNamedHash ();
240 public void RemoveAt (SqliteParameter param)
242 RemoveAt (param.ParameterName);
245 #endregion