* Mono.Data.SqlClient/SqliteCobmmand.cs, SqliteDataReader.cs,
[mono.git] / mcs / class / Mono.Data.SqliteClient / Mono.Data.SqliteClient / SqliteParameterCollection.cs
1 // -*- c-basic-offset: 8; inent-tabs-mode: nil -*-
2 //
3 //  SqliteParameterCollection.cs
4 //
5 //  Author(s): Vladimir Vukicevic  <vladimir@pobox.com>
6 //
7 //  Copyright (C) 2002  Vladimir Vukicevic
8 //
9
10 using System;
11 using System.Data;
12 using System.Collections;
13
14 namespace Mono.Data.SqliteClient
15 {
16         public class SqliteParameterCollection : IDataParameterCollection,
17                 IList
18         {
19                 ArrayList numeric_param_list = new ArrayList ();
20                 Hashtable named_param_hash = new Hashtable ();
21
22                 public IEnumerator GetEnumerator ()
23                 {
24                         throw new NotImplementedException ();
25                 }
26
27                 public void RemoveAt (string parameterName)
28                 {
29                         if (!named_param_hash.Contains (parameterName))
30                                 throw new ApplicationException ("Parameter " + parameterName + " not found");
31
32                         numeric_param_list.RemoveAt ((int) named_param_hash[parameterName]);
33                         named_param_hash.Remove (parameterName);
34
35                         RecreateNamedHash ();
36                 }
37
38                 public void RemoveAt (SqliteParameter param)
39                 {
40                         RemoveAt (param.ParameterName);
41                 }
42
43                 public void RemoveAt (int index)
44                 {
45                         RemoveAt (((SqliteParameter) numeric_param_list[index]).ParameterName);
46                 }
47
48                 int IList.IndexOf (object o)
49                 {
50                         return IndexOf ((SqliteParameter) o);
51                 }
52
53                 public int IndexOf (string parameterName)
54                 {
55                         return (int) named_param_hash[parameterName];
56                 }
57
58                 public int IndexOf (SqliteParameter param)
59                 {
60                         return IndexOf (param.ParameterName);
61                 }
62
63                 bool IList.Contains (object value)
64                 {
65                         return Contains ((SqliteParameter) value);
66                 }
67
68                 public bool Contains (string parameterName)
69                 {
70                         return named_param_hash.Contains (parameterName);
71                 }
72
73                 public bool Contains (SqliteParameter param)
74                 {
75                         return Contains (param.ParameterName);
76                 }
77
78                 object IList.this[int index] {
79                         get {
80                                 return this[index];
81                         }
82                         set {
83                                 CheckSqliteParam (value);
84                                 this[index] = (SqliteParameter) value;
85                         }
86                 }
87
88                 object IDataParameterCollection.this[string parameterName] {
89                         get {
90                                 return this[parameterName];
91                         }
92                         set {
93                                 CheckSqliteParam (value);
94                                 this[parameterName] = (SqliteParameter) value;
95                         }
96                 }
97
98                 public SqliteParameter this[string parameterName] {
99                         get {
100                                 return this[(int) named_param_hash[parameterName]];
101                         }
102                         set {
103                                 if (this.Contains (parameterName))
104                                         numeric_param_list[(int) named_param_hash[parameterName]] = value;
105                                 else          // uhm, do we add it if it doesn't exist? what does ms do?
106                                         Add (value);
107                         }
108                 }
109
110                 public SqliteParameter this[int parameterIndex] {
111                         get {
112                                 return (SqliteParameter) numeric_param_list[parameterIndex];
113                         }
114                         set {
115                                 numeric_param_list[parameterIndex] = value;
116                         }
117                 }
118
119                 public int Add (object value)
120                 {
121                         CheckSqliteParam (value);
122                         SqliteParameter sqlp = (SqliteParameter) value;
123                         if (named_param_hash.Contains (sqlp.ParameterName))
124                                 throw new DuplicateNameException ("Parameter collection already contains given value.");
125
126                         named_param_hash[value] = numeric_param_list.Add (value);
127
128                         return (int) named_param_hash[value];
129                 }
130
131                 // IList
132
133                 public SqliteParameter Add (SqliteParameter param)
134                 {
135                         Add (param);
136                         return param;
137                 }
138
139                 public SqliteParameter Add (string name, object value)
140                 {
141                         return Add (new SqliteParameter (name, value));
142                 }
143
144                 public SqliteParameter Add (string name, DbType type)
145                 {
146                         return Add (new SqliteParameter (name, type));
147                 }
148
149                 public bool IsFixedSize {
150                         get {
151                                 return false;
152                         }
153                 }
154
155                 public bool IsReadOnly {
156                         get {
157                                 return false;
158                         }
159                 }
160
161                 public void Clear ()
162                 {
163                         numeric_param_list.Clear ();
164                         named_param_hash.Clear ();
165                 }
166
167                 public void Insert (int index, object value)
168                 {
169                         CheckSqliteParam (value);
170                         if (numeric_param_list.Count == index) {
171                                 Add (value);
172                                 return;
173                         }
174
175                         numeric_param_list.Insert (index, value);
176                         RecreateNamedHash ();
177                 }
178
179                 public void Remove (object value)
180                 {
181                         CheckSqliteParam (value);
182                         RemoveAt ((SqliteParameter) value);
183                 }
184
185                 // ICollection
186
187                 public int Count {
188                         get {
189                                 return numeric_param_list.Count;
190                         }
191                 }
192
193                 public bool IsSynchronized {
194                         get {
195                                 return false;
196                         }
197                 }
198
199                 public object SyncRoot {
200                         get {
201                                 return null;
202                         }
203                 }
204
205                 public void CopyTo (Array array, int index)
206                 {
207                         throw new NotImplementedException ();
208                 }
209
210                 private void CheckSqliteParam (object value)
211                 {
212                         if (!(value is SqliteParameter))
213                                 throw new InvalidCastException ("Can only use SqliteParameter objects");
214                 }
215
216                 private void RecreateNamedHash ()
217                 {
218                         for (int i = 0; i < numeric_param_list.Count; i++) {
219                                 named_param_hash[((SqliteParameter) numeric_param_list[i]).ParameterName] = i;
220                         }
221                 }
222         }
223 }