2004-09-14 Everaldo Canut <everaldo_canuto@yahoo.com.br>
[mono.git] / mcs / class / Mono.Data.SqliteClient / Mono.Data.SqliteClient / SqliteParameterCollection.cs
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
11 //
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.
30 //
31
32 using System;
33 using System.Data;
34 using System.Collections;
35
36 namespace Mono.Data.SqliteClient
37 {
38         public class SqliteParameterCollection : IDataParameterCollection, IList
39         {
40         
41                 #region Fields
42                 
43                 ArrayList numeric_param_list = new ArrayList();
44                 Hashtable named_param_hash = new Hashtable();
45                 
46                 #endregion
47
48                 #region Private Methods
49                 
50                 private void CheckSqliteParam (object value)
51                 {
52                         if (!(value is SqliteParameter))
53                                 throw new InvalidCastException("Can only use SqliteParameter objects");
54                 }
55
56                 private void RecreateNamedHash ()
57                 {
58                         for (int i = 0; i < numeric_param_list.Count; i++) {
59                                 named_param_hash[((SqliteParameter) numeric_param_list[i]).ParameterName] = i;
60                         }
61                 }
62                 
63                 #endregion
64
65                 #region Properties
66                 
67                 object IList.this[int index] {
68                         get {
69                                 return this[index];
70                         }
71                         set {
72                                 CheckSqliteParam (value);
73                                 this[index] = (SqliteParameter) value;
74                         }
75                 }
76                 
77                 object IDataParameterCollection.this[string parameterName] {
78                         get {
79                                 return this[parameterName];
80                         }
81                         set {
82                                 CheckSqliteParam (value);
83                                 this[parameterName] = (SqliteParameter) value;
84                         }
85                 }
86                 
87                 public SqliteParameter this[string parameterName] {
88                         get {
89                                 return this[(int) named_param_hash[parameterName]];
90                         }
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);
96                         }
97                 }
98                 
99                 public SqliteParameter this[int parameterIndex] {
100                         get {
101                                 return (SqliteParameter) numeric_param_list[parameterIndex];
102                         }
103                         set {
104                                 numeric_param_list[parameterIndex] = value;
105                         }
106                 }
107                 
108                 public int Count {
109                         get { return numeric_param_list.Count; }
110                 }
111                 
112                 public bool IsFixedSize {
113                         get { return false; }
114                 }
115                 
116                 public bool IsReadOnly {
117                         get { return false; }
118                 }
119                 
120                 public bool IsSynchronized {
121                         get { return false; }
122                 }
123                 
124                 public object SyncRoot {
125                         get { return null; }
126                 }
127                 
128                 #endregion
129
130                 #region Public Methods
131                 
132                 public int Add (object value)
133                 {
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.");
138                         
139                         named_param_hash[value] = numeric_param_list.Add (value);
140                         
141                         return (int) named_param_hash[value];
142                 }
143                 
144                 public SqliteParameter Add (SqliteParameter param)
145                 {
146                         Add ((object)param);
147                         return param;
148                 }
149                 
150                 public SqliteParameter Add (string name, object value)
151                 {
152                         return Add (new SqliteParameter (name, value));
153                 }
154                 
155                 public SqliteParameter Add (string name, DbType type)
156                 {
157                         return Add (new SqliteParameter (name, type));
158                 }
159                 
160                 public void Clear ()
161                 {
162                         numeric_param_list.Clear ();
163                         named_param_hash.Clear ();
164                 }
165                 
166                 public void CopyTo (Array array, int index)
167                 {
168                         throw new NotImplementedException ();
169                 }
170                 
171                 bool IList.Contains (object value)
172                 {
173                         return Contains ((SqliteParameter) value);
174                 }
175                 
176                 public bool Contains (string parameterName)
177                 {
178                         return named_param_hash.Contains (parameterName);
179                 }
180                 
181                 public bool Contains (SqliteParameter param)
182                 {
183                         return Contains (param.ParameterName);
184                 }
185                 
186                 public IEnumerator GetEnumerator ()
187                 {
188                         throw new NotImplementedException ();
189                 }
190                 
191                 int IList.IndexOf (object param)
192                 {
193                         return IndexOf ((SqliteParameter) param);
194                 }
195                 
196                 public int IndexOf (string parameterName)
197                 {
198                         return (int) named_param_hash[parameterName];
199                 }
200                 
201                 public int IndexOf (SqliteParameter param)
202                 {
203                         return IndexOf (param.ParameterName);
204                 }
205                 
206                 public void Insert (int index, object value)
207                 {
208                         CheckSqliteParam (value);
209                         if (numeric_param_list.Count == index) {
210                                 Add (value);
211                                 return;
212                         }
213                         
214                         numeric_param_list.Insert (index, value);
215                         RecreateNamedHash ();
216                 }
217                 
218                 public void Remove (object value)
219                 {
220                         CheckSqliteParam (value);
221                         RemoveAt ((SqliteParameter) value);
222                 }
223                 
224                 public void RemoveAt (int index)
225                 {
226                         RemoveAt (((SqliteParameter) numeric_param_list[index]).ParameterName);
227                 }
228                 
229                 public void RemoveAt (string parameterName)
230                 {
231                         if (!named_param_hash.Contains (parameterName))
232                                 throw new ApplicationException ("Parameter " + parameterName + " not found");
233                         
234                         numeric_param_list.RemoveAt ((int) named_param_hash[parameterName]);
235                         named_param_hash.Remove (parameterName);
236                         
237                         RecreateNamedHash ();
238                 }
239                 
240                 public void RemoveAt (SqliteParameter param)
241                 {
242                         RemoveAt (param.ParameterName);
243                 }
244                 
245                 #endregion
246         }
247 }
248