KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > common > sql > schema > DatabaseProcedure


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2005 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Nicolas Modrzyk
22  * Contributor(s):
23  */

24
25 package org.objectweb.cjdbc.common.sql.schema;
26
27 import java.sql.SQLException JavaDoc;
28 import java.util.ArrayList JavaDoc;
29
30 import org.objectweb.cjdbc.common.xml.DatabasesXmlTags;
31
32 /**
33  * Represents a procedure
34  *
35  * @author <a HREF="mailto:Nicolas.Modrzyk@inrialpes.fr">Nicolas Modrzyk </a>
36  */

37 public class DatabaseProcedure
38 {
39   /** May return a result */
40   public static final int ProcedureResultUnknown = 0;
41   /** Does not return a result */
42   public static final int ProcedureNoResult = 1;
43   /** Returns a result */
44   public static final int ProcedureReturnsResult = 2;
45
46   ArrayList JavaDoc parameters;
47   private String JavaDoc name;
48   private String JavaDoc remarks;
49   private int procedureType;
50
51   /**
52    * Convert type from string to integer
53    *
54    * @param type as a string
55    * @return ProcedureNoResult or ProcedureReturnsResult or
56    * ProcedureResultUnknown if not found
57    */

58   public static int getTypeFromString(String JavaDoc type)
59   {
60     if (type.equals(DatabasesXmlTags.VAL_noResult))
61       return ProcedureNoResult;
62     if (type.equals(DatabasesXmlTags.VAL_returnsResult))
63       return ProcedureReturnsResult;
64     else
65       return ProcedureResultUnknown;
66   }
67
68   /**
69    * Convert type from integer to string
70    *
71    * @param type as an int
72    * @return string value conforms to xml tags.
73    */

74   public static String JavaDoc getTypeFromInt(int type)
75   {
76     switch (type)
77     {
78       case ProcedureNoResult :
79         return DatabasesXmlTags.VAL_noResult;
80       case ProcedureReturnsResult :
81         return DatabasesXmlTags.VAL_returnsResult;
82       default :
83         return DatabasesXmlTags.VAL_resultUnknown;
84     }
85   }
86
87   /**
88    * @param name of the procedure
89    * @param remarks of the procedure
90    * @param procedureType see above types
91    */

92   public DatabaseProcedure(String JavaDoc name, String JavaDoc remarks, int procedureType)
93   {
94     this.name = name;
95     this.remarks = remarks;
96     this.procedureType = procedureType;
97     this.parameters = new ArrayList JavaDoc();
98   }
99
100   /**
101    * Add a parameter to this procedure
102    *
103    * @param param to add
104    */

105   public void addParameter(DatabaseProcedureParameter param)
106   {
107     parameters.add(param);
108   }
109
110   /**
111    * @return Returns the name.
112    */

113   public String JavaDoc getName()
114   {
115     return name;
116   }
117
118   /**
119    * @param name The name to set.
120    */

121   public void setName(String JavaDoc name)
122   {
123     this.name = name;
124   }
125
126   /**
127    * @return Returns the parameters.
128    */

129   public ArrayList JavaDoc getParameters()
130   {
131     return parameters;
132   }
133
134   /**
135    * @param parameters The parameters to set.
136    */

137   public void setParameters(ArrayList JavaDoc parameters)
138   {
139     this.parameters = parameters;
140   }
141
142   /**
143    * @return Returns the procedureType.
144    */

145   public int getProcedureType()
146   {
147     return procedureType;
148   }
149
150   /**
151    * @param procedureType The procedureType to set.
152    */

153   public void setProcedureType(int procedureType)
154   {
155     this.procedureType = procedureType;
156   }
157
158   /**
159    * @return Returns the remarks.
160    */

161   public String JavaDoc getRemarks()
162   {
163     return remarks;
164   }
165
166   /**
167    * @param remarks The remarks to set.
168    */

169   public void setRemarks(String JavaDoc remarks)
170   {
171     this.remarks = remarks;
172   }
173
174   /**
175    * Merges this procedure parameters with the given procedure's parameters. An
176    * exception is thrown if the given procedure parameters conflict with this
177    * one. If any parameter is different the exception is thrown.
178    *
179    * @param procedure the procedure to merge
180    * @throws SQLException if the schemas conflict
181    */

182   public void mergeParameters(DatabaseProcedure procedure) throws SQLException JavaDoc
183   {
184     if (procedure == null)
185       return;
186
187     ArrayList JavaDoc otherParameters = procedure.getParameters();
188     if (otherParameters == null && parameters == null)
189       return;
190
191     if (this.equals(procedure))
192     {
193       // Procedures are the same, no conflict
194
return;
195     }
196     else
197     {
198       throw new SQLException JavaDoc("Unable to merge procedure " + getName()
199           + ": parameters are differents ");
200     }
201   }
202
203   /**
204    * Two <code>DatabaseProcedure</code> are considered equal if they have the
205    * same name and the same parameters.
206    *
207    * @param other the object to compare with
208    * @return <code>true</code> if the DatabaseProcedures are equal
209    */

210   public boolean equals(Object JavaDoc other)
211   {
212     if ((other == null) || !(other instanceof DatabaseProcedure))
213       return false;
214
215     DatabaseProcedure p = (DatabaseProcedure) other;
216     return (p.getName().equals(name)) && (p.getParameters().equals(parameters));
217   }
218
219   /**
220    * Get xml information about this procedure.
221    *
222    * @return xml formatted information on this database procedure.
223    */

224   public String JavaDoc getXml()
225   {
226     StringBuffer JavaDoc info = new StringBuffer JavaDoc();
227     info.append("<" + DatabasesXmlTags.ELT_DatabaseProcedure + " "
228         + DatabasesXmlTags.ATT_name + "=\"" + name + "\" "
229         + DatabasesXmlTags.ATT_returnType + "=\""
230         + getTypeFromInt(procedureType) + "\">");
231     for (int i = 0; i < parameters.size(); i++)
232       info.append(((DatabaseProcedureParameter) parameters.get(i)).getXml());
233     info.append("</" + DatabasesXmlTags.ELT_DatabaseProcedure + ">");
234     return info.toString();
235   }
236
237 }
238
Popular Tags