Main Page | Packages | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

DatabaseProcedure.java

00001 /**
00002  * C-JDBC: Clustered JDBC.
00003  * Copyright (C) 2002-2004 French National Institute For Research In Computer
00004  * Science And Control (INRIA).
00005  * Contact: c-jdbc@objectweb.org
00006  * 
00007  * This library is free software; you can redistribute it and/or modify it
00008  * under the terms of the GNU Lesser General Public License as published by the
00009  * Free Software Foundation; either version 2.1 of the License, or any later
00010  * version.
00011  * 
00012  * This library is distributed in the hope that it will be useful, but WITHOUT
00013  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
00015  * for more details.
00016  * 
00017  * You should have received a copy of the GNU Lesser General Public License
00018  * along with this library; if not, write to the Free Software Foundation,
00019  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
00020  *
00021  * Initial developer(s): Nicolas Modrzyk
00022  * Contributor(s): 
00023  */
00024 
00025 package org.objectweb.cjdbc.common.sql.schema;
00026 
00027 import java.io.Serializable;
00028 import java.sql.SQLException;
00029 import java.util.ArrayList;
00030 
00031 import org.objectweb.cjdbc.common.xml.DatabasesXmlTags;
00032 
00033 /**
00034  * Represents a procedure
00035  * 
00036  * @author <a href="mailto:Nicolas.Modrzyk@inrialpes.fr">Nicolas Modrzyk </a>
00037  */
00038 public class DatabaseProcedure implements Serializable
00039 {
00040   /** May return a result */
00041   public static final int ProcedureResultUnknown = 0;
00042   /** Does not return a result */
00043   public static final int ProcedureNoResult      = 1;
00044   /** Returns a result */
00045   public static final int ProcedureReturnsResult = 2;
00046 
00047   ArrayList               parameters;
00048   private String          name;
00049   private String          remarks;
00050   private int             procedureType;
00051 
00052   /**
00053    * Convert type from string to integer
00054    * 
00055    * @param type as a string
00056    * @return ProcedureNoResult or ProcedureReturnsResult or
00057    *         ProcedureResultUnknown if not found
00058    */
00059   public static int getTypeFromString(String type)
00060   {
00061     if (type.equals(DatabasesXmlTags.VAL_noResult))
00062       return ProcedureNoResult;
00063     if (type.equals(DatabasesXmlTags.VAL_returnsResult))
00064       return ProcedureReturnsResult;
00065     else
00066       return ProcedureResultUnknown;
00067   }
00068 
00069   /**
00070    * Convert type from integer to string
00071    * 
00072    * @param type as an int
00073    * @return string value conforms to xml tags.
00074    */
00075   public static String getTypeFromInt(int type)
00076   {
00077     switch (type)
00078     {
00079       case ProcedureNoResult :
00080         return DatabasesXmlTags.VAL_noResult;
00081       case ProcedureReturnsResult :
00082         return DatabasesXmlTags.VAL_returnsResult;
00083       default :
00084         return DatabasesXmlTags.VAL_resultUnknown;
00085     }
00086   }
00087 
00088   /**
00089    * @param name of the procedure
00090    * @param remarks of the procedure
00091    * @param procedureType see above types
00092    */
00093   public DatabaseProcedure(String name, String remarks, int procedureType)
00094   {
00095     this.name = name;
00096     this.remarks = remarks;
00097     this.procedureType = procedureType;
00098     this.parameters = new ArrayList();
00099   }
00100 
00101   /**
00102    * Add a parameter to this procedure
00103    * 
00104    * @param param to add
00105    */
00106   public void addParameter(DatabaseProcedureParameter param)
00107   {
00108     parameters.add(param);
00109   }
00110 
00111   /**
00112    * @return Returns the name.
00113    */
00114   public String getName()
00115   {
00116     return name;
00117   }
00118 
00119   /**
00120    * @param name The name to set.
00121    */
00122   public void setName(String name)
00123   {
00124     this.name = name;
00125   }
00126 
00127   /**
00128    * @return Returns the parameters.
00129    */
00130   public ArrayList getParameters()
00131   {
00132     return parameters;
00133   }
00134 
00135   /**
00136    * @param parameters The parameters to set.
00137    */
00138   public void setParameters(ArrayList parameters)
00139   {
00140     this.parameters = parameters;
00141   }
00142 
00143   /**
00144    * @return Returns the procedureType.
00145    */
00146   public int getProcedureType()
00147   {
00148     return procedureType;
00149   }
00150 
00151   /**
00152    * @param procedureType The procedureType to set.
00153    */
00154   public void setProcedureType(int procedureType)
00155   {
00156     this.procedureType = procedureType;
00157   }
00158 
00159   /**
00160    * @return Returns the remarks.
00161    */
00162   public String getRemarks()
00163   {
00164     return remarks;
00165   }
00166 
00167   /**
00168    * @param remarks The remarks to set.
00169    */
00170   public void setRemarks(String remarks)
00171   {
00172     this.remarks = remarks;
00173   }
00174 
00175   /**
00176    * Merges this procedure parameters with the given procedure's parameters. An
00177    * exception is thrown if the given procedure parameters conflict with this
00178    * one. If any parameter is different the exception is thrown.
00179    * 
00180    * @param procedure the procedure to merge
00181    * @throws SQLException if the schemas conflict
00182    */
00183   public void mergeParameters(DatabaseProcedure procedure) throws SQLException
00184   {
00185     if (procedure == null)
00186       return;
00187 
00188     ArrayList otherParameters = procedure.getParameters();
00189     if (otherParameters == null && parameters == null)
00190       return;
00191 
00192     if (this.equals(procedure))
00193     {
00194       // Procedures are the same, no conflict
00195       return;
00196     }
00197     else
00198     {
00199       throw new SQLException("Unable to merge procedure " + getName()
00200           + ": parameters are differents ");
00201     }
00202   }
00203 
00204   /**
00205    * Two <code>DatabaseProcedure</code> are considered equal if they have the
00206    * same name and the same parameters.
00207    * 
00208    * @param other the object to compare with
00209    * @return <code>true</code> if the DatabaseProcedures are equal
00210    */
00211   public boolean equals(Object other)
00212   {
00213     if ((other == null) || !(other instanceof DatabaseProcedure))
00214       return false;
00215 
00216     DatabaseProcedure p = (DatabaseProcedure) other;
00217     return (p.getName().equals(name)) && (p.getParameters().equals(parameters));
00218   }
00219 
00220   /**
00221    * Get xml information about this procedure.
00222    * 
00223    * @return xml formatted information on this database procedure.
00224    */
00225   public String getXml()
00226   {
00227     StringBuffer info = new StringBuffer();
00228     info.append("<" + DatabasesXmlTags.ELT_DatabaseProcedure + " "
00229         + DatabasesXmlTags.ATT_name + "=\"" + name + "\" "
00230         + DatabasesXmlTags.ATT_returnType + "=\""
00231         + getTypeFromInt(procedureType) + "\">");
00232     for (int i = 0; i < parameters.size(); i++)
00233       info.append(((DatabaseProcedureParameter) parameters.get(i)).getXml());
00234     info.append("</" + DatabasesXmlTags.ELT_DatabaseProcedure + ">");
00235     return info.toString();
00236   }
00237 
00238 }

Generated on Mon Apr 11 22:01:31 2005 for C-JDBC by  doxygen 1.3.9.1