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

AlterRequest.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;
00026 
00027 import java.sql.SQLException;
00028 import java.util.ArrayList;
00029 
00030 import org.objectweb.cjdbc.common.sql.schema.DatabaseColumn;
00031 import org.objectweb.cjdbc.common.sql.schema.DatabaseSchema;
00032 import org.objectweb.cjdbc.common.sql.schema.DatabaseTable;
00033 import org.objectweb.cjdbc.common.sql.schema.TableColumn;
00034 
00035 /**
00036  * This class defines a AlterRequest
00037  * 
00038  * @author <a href="mailto:Nicolas.Modrzyk@inrialpes.fr">Nicolas Modrzyk </a>
00039  * @version 1.0
00040  */
00041 public class AlterRequest extends AbstractWriteRequest
00042 {
00043 
00044   /** The table to alter. */
00045   private transient DatabaseTable  table  = null;
00046 
00047   /** The column altered */
00048   private transient DatabaseColumn column = null;
00049 
00050   private transient boolean        isDrop = false;
00051   private transient boolean        isAdd  = false;
00052 
00053   /**
00054    * Creates a new <code>AlterRequest</code> instance. The caller must give
00055    * an SQL request, without any leading or trailing spaces and beginning with
00056    * 'alter table '
00057    * <p>
00058    * The request is not parsed but it can be done later by a call to
00059    * {@link #parse(DatabaseSchema, int, boolean)}.
00060    * 
00061    * @param sqlQuery the SQL request
00062    * @param escapeProcessing should the driver to escape processing before
00063    *          sending to the database ?
00064    * @param timeout an <code>int</code> value
00065    * @param lineSeparator the line separator used in the query
00066    * @see #parse
00067    */
00068   public AlterRequest(String sqlQuery, boolean escapeProcessing, int timeout,
00069       String lineSeparator)
00070   {
00071     super(sqlQuery, escapeProcessing, timeout, lineSeparator);
00072   }
00073 
00074   /**
00075    * Creates a new <code>AlterRequest</code> instance. The caller must give an
00076    * SQL request, without any leading or trailing spaces and beginning with
00077    * 'alter table '
00078    * <p>
00079    * If the syntax is incorrect an exception is thrown.
00080    * 
00081    * @param sqlQuery the SQL request
00082    * @param escapeProcessing should the driver to escape processing before
00083    *                    sending to the database?
00084    * @param timeout an <code>int</code> value
00085    * @param lineSeparator the line separator used in the query
00086    * @param schema a <code>DatabaseSchema</code> value
00087    * @param granularity parsing granularity as defined in
00088    *                    <code>ParsingGranularities</code>
00089    * @param isCaseSensitive true if parsing is case sensitive
00090    * @exception SQLException if an error occurs
00091    */
00092   public AlterRequest(String sqlQuery, boolean escapeProcessing, int timeout,
00093       String lineSeparator, DatabaseSchema schema, int granularity,
00094       boolean isCaseSensitive) throws SQLException
00095   {
00096     this(sqlQuery, escapeProcessing, timeout, lineSeparator);
00097     parse(schema, granularity, isCaseSensitive);
00098   }
00099 
00100   /**
00101    * @see org.objectweb.cjdbc.common.sql.AbstractRequest#isReadRequest()
00102    */
00103   public boolean isReadRequest()
00104   {
00105     return false;
00106   }
00107 
00108   /**
00109    * @see org.objectweb.cjdbc.common.sql.AbstractRequest#isWriteRequest()
00110    */
00111   public boolean isWriteRequest()
00112   {
00113     return true;
00114   }
00115 
00116   /**
00117    * @see org.objectweb.cjdbc.common.sql.AbstractRequest#isUnknownRequest()
00118    */
00119   public boolean isUnknownRequest()
00120   {
00121     return false;
00122   }
00123 
00124   /**
00125    * @see org.objectweb.cjdbc.common.sql.AbstractRequest#parse(org.objectweb.cjdbc.common.sql.schema.DatabaseSchema,
00126    *             int, boolean)
00127    */
00128   public void parse(DatabaseSchema schema, int granularity,
00129       boolean isCaseSensitive) throws SQLException
00130   {
00131     /*
00132      * Example Alter statement: ALTER TABLE table_name ADD column_name datatype
00133      * ALTER TABLE table_name DROP COLUMN column_name
00134      */
00135 
00136     if (granularity == ParsingGranularities.NO_PARSING)
00137     {
00138       isParsed = true;
00139       return;
00140     }
00141 
00142     String originalSQL = this.trimCarriageReturn();
00143     String sql = originalSQL.toLowerCase();
00144 
00145     // Strip 'alter table '
00146     int tableIdx = sql.indexOf("table");
00147     if (tableIdx == -1)
00148       throw new SQLException(
00149           "Malformed Alter Request. Should start with [ALTER TABLE]");
00150     sql = sql.substring(tableIdx + 5).trim();
00151 
00152     // Does the query contain a add?
00153     int addIdx = sql.indexOf(" add ");
00154 
00155     // Does the query contain a drop?
00156     int dropIdx = sql.indexOf(" drop ");
00157 
00158     if (addIdx != -1)
00159       isAdd = true;
00160     if (dropIdx != -1)
00161       isDrop = true;
00162 
00163     if (!isAdd && !isDrop)
00164       throw new SQLException(
00165           "Malformed Alter Request. No drop or add condition");
00166 
00167     if (isCaseSensitive) // Reverse to the original case
00168       sql = originalSQL.substring(originalSQL.length() - sql.length());
00169 
00170     int index = (isAdd) ? addIdx : dropIdx;
00171 
00172     tableName = sql.substring(0, index).trim();
00173     table = new DatabaseTable(tableName);
00174 
00175 
00176     if (granularity > ParsingGranularities.TABLE)
00177     {
00178       
00179 
00180       int subsIndex = index + 6 + 2; // index +
00181                                                                // column.length()
00182                                                                // + space
00183       if (isAdd)
00184         subsIndex += 3;
00185       else
00186         // Drop
00187         subsIndex += 4;
00188       
00189       columns = new ArrayList();
00190       sql = sql.substring(subsIndex).trim();
00191 
00192       if (isAdd)
00193       {
00194         int colIndex = sql.indexOf(' ');
00195         String colName = sql.substring(0, colIndex);
00196 
00197         int uniqueIndex = sql.toLowerCase().indexOf("unique");
00198         int primary = sql.toLowerCase().indexOf("primary");
00199         if (uniqueIndex != -1 || primary != -1)
00200           column = new DatabaseColumn(colName, true);
00201         else
00202           column = new DatabaseColumn(colName, false);
00203         columns.add(new TableColumn(tableName, colName));
00204       }
00205       else if (isDrop)
00206       {
00207         String colName = sql.trim();
00208         column = schema.getTable(tableName).getColumn(colName);
00209         columns.add(new TableColumn(tableName, colName));
00210       }
00211     }
00212     isParsed  = true;
00213   }
00214 
00215   /**
00216    * @see org.objectweb.cjdbc.common.sql.AbstractRequest#cloneParsing(org.objectweb.cjdbc.common.sql.AbstractRequest)
00217    */
00218   public void cloneParsing(AbstractRequest request)
00219   {
00220     if (!request.isParsed())
00221       return;
00222     AlterRequest alterRequest = (AlterRequest) request;
00223     cloneTableNameAndColumns((AbstractWriteRequest) request);
00224     table = alterRequest.getDatabaseTable();
00225     column = alterRequest.getColumn();
00226     isParsed = true;
00227   }
00228 
00229   /**
00230    * @see org.objectweb.cjdbc.common.sql.AbstractWriteRequest#isAlter()
00231    */
00232   public boolean isAlter()
00233   {
00234     return true;
00235   }
00236 
00237   /**
00238    * @see org.objectweb.cjdbc.common.sql.AbstractWriteRequest#isCreate()
00239    */
00240   public boolean isCreate()
00241   {
00242     return false;
00243   }
00244 
00245   /**
00246    * @see org.objectweb.cjdbc.common.sql.AbstractWriteRequest#isDelete()
00247    */
00248   public boolean isDelete()
00249   {
00250     return false;
00251   }
00252 
00253   /**
00254    * @see org.objectweb.cjdbc.common.sql.AbstractWriteRequest#isDrop()
00255    */
00256   public boolean isDrop()
00257   {
00258     return false;
00259   }
00260 
00261   /**
00262    * @see org.objectweb.cjdbc.common.sql.AbstractWriteRequest#isInsert()
00263    */
00264   public boolean isInsert()
00265   {
00266     return false;
00267   }
00268 
00269   /**
00270    * @see org.objectweb.cjdbc.common.sql.AbstractWriteRequest#isUpdate()
00271    */
00272   public boolean isUpdate()
00273   {
00274     return false;
00275   }
00276 
00277   /**
00278    * Returns the table value.
00279    * 
00280    * @return Returns the table.
00281    */
00282   public DatabaseTable getDatabaseTable()
00283   {
00284     return table;
00285   }
00286   /**
00287    * Returns the column value.
00288    * 
00289    * @return Returns the column.
00290    */
00291   public DatabaseColumn getColumn()
00292   {
00293     return column;
00294   }
00295   /**
00296    * Returns the isAdd value.
00297    * 
00298    * @return Returns the isAdd.
00299    */
00300   public boolean isAdd()
00301   {
00302     return isAdd;
00303   }
00304 }

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