001: /**
002: * Sequoia: Database clustering technology.
003: * Copyright (C) 2002-2004 French National Institute For Research In Computer
004: * Science And Control (INRIA).
005: * Copyright (C) 2005 AmicoSoft, Inc. dba Emic Networks
006: * Copyright (C) 2005 Continuent, Inc.
007: * Contact: sequoia@continuent.org
008: *
009: * Licensed under the Apache License, Version 2.0 (the "License");
010: * you may not use this file except in compliance with the License.
011: * You may obtain a copy of the License at
012: *
013: * http://www.apache.org/licenses/LICENSE-2.0
014: *
015: * Unless required by applicable law or agreed to in writing, software
016: * distributed under the License is distributed on an "AS IS" BASIS,
017: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018: * See the License for the specific language governing permissions and
019: * limitations under the License.
020: *
021: * Initial developer(s): Nicolas Modrzyk
022: * Contributor(s): Emmanuel Cecchet, Edward Archibald
023: */package org.continuent.sequoia.common.sql.schema;
024:
025: import java.util.ArrayList;
026:
027: import org.continuent.sequoia.common.xml.DatabasesXmlTags;
028:
029: /**
030: * Represents a database stored procedure and its metadata.
031: *
032: * @author <a href="mailto:ed.archibald@continuent.com">Edward Archibald</a>
033: * @author <a href="mailto:emmanuel.cecchet@continuent.com">Emmanuel Cecchet
034: * </a>
035: * @author <a href="mailto:Nicolas.Modrzyk@inrialpes.fr">Nicolas Modrzyk </a>
036: */
037: public class DatabaseProcedure {
038: /** May return a result */
039: public static final int ProcedureResultUnknown = 0;
040: /** Does not return a result */
041: public static final int ProcedureNoResult = 1;
042: /** Returns a result */
043: public static final int ProcedureReturnsResult = 2;
044:
045: ArrayList parameters;
046: private String name;
047: private String remarks;
048: private int procedureType;
049: private DatabaseProcedureSemantic semantic;
050:
051: /**
052: * @param name of the procedure
053: * @param remarks of the procedure
054: * @param procedureType see above types
055: */
056: public DatabaseProcedure(String name, String remarks,
057: int procedureType) {
058: this .name = name;
059: this .remarks = remarks;
060: this .procedureType = procedureType;
061: this .parameters = new ArrayList();
062: }
063:
064: /**
065: * Add a parameter to this procedure
066: *
067: * @param param to add
068: */
069: public void addParameter(DatabaseProcedureParameter param) {
070: parameters.add(param);
071: }
072:
073: /**
074: * Build a unique key based on a stored procedure name and its number of
075: * parameters. Please note that this is enough for some DBMS: Postgresql 8 for
076: * instance allows full function overloading.
077: *
078: * @param storedProcedureName stored procedure name
079: * @param nbOfParameters number of parameters
080: * @return a String representation of the key (used by HashMap in schema)
081: */
082: public static String buildKey(String storedProcedureName,
083: int nbOfParameters) {
084: return storedProcedureName + "(" + nbOfParameters + ")";
085: }
086:
087: /**
088: * Returns a unique key identifying this stored procedure.
089: *
090: * @return the unique key
091: */
092: public String getKey() {
093: if (procedureType == ProcedureReturnsResult)
094: // Strip return value from the parameter list
095: return buildKey(name, parameters.size() - 1);
096: else
097: return buildKey(name, parameters.size());
098: }
099:
100: /**
101: * @return Returns the name.
102: */
103: public String getName() {
104: return name;
105: }
106:
107: /**
108: * @param name The name to set.
109: */
110: public void setName(String name) {
111: this .name = name;
112: }
113:
114: /**
115: * @return Returns the parameters.
116: */
117: public ArrayList getParameters() {
118: return parameters;
119: }
120:
121: /**
122: * @param parameters The parameters to set.
123: */
124: public void setParameters(ArrayList parameters) {
125: this .parameters = parameters;
126: }
127:
128: /**
129: * @return Returns the procedureType.
130: */
131: public int getProcedureType() {
132: return procedureType;
133: }
134:
135: /**
136: * @param procedureType The procedureType to set.
137: */
138: public void setProcedureType(int procedureType) {
139: this .procedureType = procedureType;
140: }
141:
142: /**
143: * @return Returns the remarks.
144: */
145: public String getRemarks() {
146: return remarks;
147: }
148:
149: /**
150: * @param remarks The remarks to set.
151: */
152: public void setRemarks(String remarks) {
153: this .remarks = remarks;
154: }
155:
156: /**
157: * Returns the procedureNoResult value.
158: *
159: * @return Returns the procedureNoResult.
160: */
161: public static int getProcedureNoResult() {
162: return ProcedureNoResult;
163: }
164:
165: /**
166: * Returns the stored procedure semantic.
167: *
168: * @return Returns the semantic.
169: */
170: public DatabaseProcedureSemantic getSemantic() {
171: return semantic;
172: }
173:
174: /**
175: * Sets the stored procedure semantic.
176: *
177: * @param semantic The semantic to set.
178: */
179: public void setSemantic(DatabaseProcedureSemantic semantic) {
180: this .semantic = semantic;
181: }
182:
183: /**
184: * Convert type from string to integer
185: *
186: * @param type as a string
187: * @return ProcedureNoResult or ProcedureReturnsResult or
188: * ProcedureResultUnknown if not found
189: */
190: public static int getTypeFromString(String type) {
191: if (type.equals(DatabasesXmlTags.VAL_noResult))
192: return ProcedureNoResult;
193: if (type.equals(DatabasesXmlTags.VAL_returnsResult))
194: return ProcedureReturnsResult;
195: else
196: return ProcedureResultUnknown;
197: }
198:
199: /**
200: * Convert type from integer to string
201: *
202: * @param type as an int
203: * @return string value conforms to xml tags.
204: */
205: public static String getTypeFromInt(int type) {
206: switch (type) {
207: case ProcedureNoResult:
208: return DatabasesXmlTags.VAL_noResult;
209: case ProcedureReturnsResult:
210: return DatabasesXmlTags.VAL_returnsResult;
211: default:
212: return DatabasesXmlTags.VAL_resultUnknown;
213: }
214: }
215:
216: /**
217: * Get xml information about this procedure.
218: *
219: * @return xml formatted information on this database procedure.
220: */
221: public String getXml() {
222: StringBuffer info = new StringBuffer();
223: info.append("<" + DatabasesXmlTags.ELT_DatabaseProcedure + " "
224: + DatabasesXmlTags.ATT_name + "=\"" + name + "\" "
225: + DatabasesXmlTags.ATT_returnType + "=\""
226: + getTypeFromInt(procedureType) + "\">");
227: for (int i = 0; i < parameters.size(); i++)
228: info
229: .append(((DatabaseProcedureParameter) parameters
230: .get(i)).getXml());
231: info
232: .append("</" + DatabasesXmlTags.ELT_DatabaseProcedure
233: + ">");
234: return info.toString();
235: }
236:
237: /**
238: * Two <code>DatabaseProcedure</code> are considered equal if they have the
239: * same name and the same parameters.
240: *
241: * @param other the object to compare with
242: * @return <code>true</code> if the DatabaseProcedures are equal
243: */
244: public boolean equals(Object other) {
245: if ((other == null) || !(other instanceof DatabaseProcedure))
246: return false;
247:
248: DatabaseProcedure p = (DatabaseProcedure) other;
249: return getKey().equals(p.getKey());
250: }
251:
252: /**
253: * @see java.lang.Object#toString()
254: */
255: public String toString() {
256: return getKey() + " " + getSemantic();
257: }
258:
259: }
|