2003-06-11 Francisco Figueiredo Jr. <fxjrlists@yahoo.com.br>
[mono.git] / mcs / class / Npgsql / Npgsql / NpgsqlParameterCollection.cs
1 // created on 18/5/2002 at 00:59
2
3 // Npgsql.NpgsqlParameterCollection.cs
4 // 
5 // Author:
6 //      Francisco Jr. (fxjrlists@yahoo.com.br)
7 //
8 //      Copyright (C) 2002 The Npgsql Development Team
9 //      npgsql-general@gborg.postgresql.org
10 //      http://gborg.postgresql.org/project/npgsql/projdisplay.php
11 //
12 //
13 // This library is free software; you can redistribute it and/or
14 // modify it under the terms of the GNU Lesser General Public
15 // License as published by the Free Software Foundation; either
16 // version 2.1 of the License, or (at your option) any later version.
17 // 
18 // This library is distributed in the hope that it will be useful,
19 // but WITHOUT ANY WARRANTY; without even the implied warranty of
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21 // Lesser General Public License for more details.
22 // 
23 // You should have received a copy of the GNU Lesser General Public
24 // License along with this library; if not, write to the Free Software
25 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26
27
28
29 using System;
30 using System.Data;
31 using System.Collections;
32
33
34 namespace Npgsql
35 {
36         
37         // Use ArrayList as base class so that we can get a lot of required methods implemented.
38         
39         // [TODO] Implement more Add methods that construct the Parameter object.
40         // [TODO] Remove dependency on ArrayList. Implement the interfaces by hand.
41         
42         public sealed class NpgsqlParameterCollection : ArrayList, IDataParameterCollection
43         {
44                 
45     // Logging related values
46     private static readonly String CLASSNAME = "NpgsqlParameterCollection";
47     
48                 public override Int32 Add(Object parameter)
49                 {
50                   NpgsqlEventLog.LogMsg("Entering " + CLASSNAME + ".Add()", LogLevel.Debug);
51                   
52                         // Call the add version that receives a NpgsqlParameter as parameter
53                         try
54                         {
55                                 Add((NpgsqlParameter) parameter);
56                                 return IndexOf(((NpgsqlParameter) parameter).ParameterName);
57                         }
58                         catch(InvalidCastException e)
59                         {
60                                 throw new NpgsqlException("Only NpgsqlParameter objects can be added to collection.", e);
61                         }
62                 }
63                 
64                 public NpgsqlParameter Add(NpgsqlParameter parameter)
65                 {
66                   NpgsqlEventLog.LogMsg("Entering " + CLASSNAME + ".Add()", LogLevel.Debug);
67                   
68                         // Check if the parameter has at least a name.
69                         if (parameter.ParameterName != null)
70                         {
71                                 // Add the parameter
72                                 base.Add(parameter);
73                                 // Return the parameter added.
74                                 return parameter;
75                         }
76                         else
77                                 throw new NpgsqlException("A parameter must have a name when added to collection");
78                         
79                 }
80                 
81                 public Boolean Contains(String parameterName)
82                 {
83                   NpgsqlEventLog.LogMsg("Entering " + CLASSNAME + ".Contains(" + parameterName + ")", LogLevel.Debug);
84                   
85                         // Check if parameterName is in the collection.
86                         return (IndexOf(parameterName) != -1);
87                 }
88                                 
89                 public Int32 IndexOf(String parameterName)
90                 {
91                   NpgsqlEventLog.LogMsg("Entering " + CLASSNAME + ".IndexOf(" + parameterName + ")", LogLevel.Debug);
92                   
93                         // Iterate values to see what is the index of parameter.
94                         Int32 index = 0;
95                         
96                         if (parameterName[0] != ':')
97                           parameterName = ':' + parameterName;
98                         foreach(NpgsqlParameter parameter in this)
99                         {
100                                 if (parameter.ParameterName == parameterName)
101                                         return index;
102                                 index++;
103                                         
104                         }
105                         return -1;
106                 }
107                 
108                 public void RemoveAt(String parameterName)
109                 {
110                   NpgsqlEventLog.LogMsg("Entering " + CLASSNAME + ".RemoveAt(" + parameterName + ")", LogLevel.Debug);
111                   
112                         base.RemoveAt(IndexOf(parameterName));
113                 }
114                 
115                 public NpgsqlParameter this[String parameterName]
116                 {
117                         get
118                         {
119                                 // return base[IndexOf(parameterName)];
120                                 NpgsqlEventLog.LogMsg("Get " + CLASSNAME + ".this[]", LogLevel.Normal);
121                                 return (NpgsqlParameter) base[IndexOf(parameterName)];
122                         }
123                         set
124                         {
125                                 // base[IndexOf(parameterName)] = value;
126                                 base[IndexOf(parameterName)] = value;
127                                 NpgsqlEventLog.LogMsg("Set " + CLASSNAME + ".Value", LogLevel.Normal);
128                         }
129                 }
130                 
131                 public new NpgsqlParameter this[Int32 i]
132                 {
133                         get
134                         {
135                                 // return base[IndexOf(parameterName)];
136                                 NpgsqlEventLog.LogMsg("Get " + CLASSNAME + ".this[]", LogLevel.Normal);
137                                 return (NpgsqlParameter) base[i];
138                         }
139                         set
140                         {
141                                 // base[IndexOf(parameterName)] = value;
142                                 base[i] = value;
143                                 NpgsqlEventLog.LogMsg("Set " + CLASSNAME + ".Value", LogLevel.Normal);
144                         }
145                 }
146                 
147                 Object IDataParameterCollection.this[String parameterName]
148                 {
149                   get
150                   {
151                     return this[parameterName];
152                   }
153                   
154                   set
155                   {
156                     this[parameterName] = (NpgsqlParameter)value;
157                   }
158                 }
159                 
160         }
161 }