KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > common > sql > AlterRequest


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2004 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;
26
27 import java.io.IOException JavaDoc;
28 import java.sql.SQLException JavaDoc;
29 import java.util.ArrayList JavaDoc;
30
31 import org.objectweb.cjdbc.common.sql.schema.DatabaseColumn;
32 import org.objectweb.cjdbc.common.sql.schema.DatabaseSchema;
33 import org.objectweb.cjdbc.common.sql.schema.DatabaseTable;
34 import org.objectweb.cjdbc.common.sql.schema.TableColumn;
35 import org.objectweb.cjdbc.common.stream.CJDBCInputStream;
36
37 /**
38  * This class defines a AlterRequest
39  *
40  * @author <a HREF="mailto:Nicolas.Modrzyk@inrialpes.fr">Nicolas Modrzyk </a>
41  * @version 1.0
42  */

43 public class AlterRequest extends AbstractWriteRequest
44 {
45   private static final long serialVersionUID = -6511969618172455539L;
46
47   /** The table to alter. */
48   private transient DatabaseTable table = null;
49
50   /** The column altered */
51   private transient DatabaseColumn column = null;
52
53   private transient boolean isDrop = false;
54   private transient boolean isAdd = false;
55
56   /**
57    * Creates a new <code>AlterRequest</code> instance. The caller must give an
58    * SQL request, without any leading or trailing spaces and beginning with
59    * 'alter table '
60    * <p>
61    * The request is not parsed but it can be done later by a call to
62    * {@link #parse(DatabaseSchema, int, boolean)}.
63    *
64    * @param sqlQuery the SQL request
65    * @param escapeProcessing should the driver to escape processing before
66    * sending to the database ?
67    * @param timeout an <code>int</code> value
68    * @param lineSeparator the line separator used in the query
69    * @see #parse
70    */

71   public AlterRequest(String JavaDoc sqlQuery, boolean escapeProcessing, int timeout,
72       String JavaDoc lineSeparator)
73   {
74     super(sqlQuery, escapeProcessing, timeout, lineSeparator, RequestType.ALTER);
75   }
76
77   /**
78    * Creates a new <code>AlterRequest</code> instance. The caller must give an
79    * SQL request, without any leading or trailing spaces and beginning with
80    * 'alter table '
81    * <p>
82    * If the syntax is incorrect an exception is thrown.
83    *
84    * @param sqlQuery the SQL request
85    * @param escapeProcessing should the driver to escape processing before
86    * sending to the database?
87    * @param timeout an <code>int</code> value
88    * @param lineSeparator the line separator used in the query
89    * @param schema a <code>DatabaseSchema</code> value
90    * @param granularity parsing granularity as defined in
91    * <code>ParsingGranularities</code>
92    * @param isCaseSensitive true if parsing is case sensitive
93    * @exception SQLException if an error occurs
94    */

95   public AlterRequest(String JavaDoc sqlQuery, boolean escapeProcessing, int timeout,
96       String JavaDoc lineSeparator, DatabaseSchema schema, int granularity,
97       boolean isCaseSensitive) throws SQLException JavaDoc
98   {
99     this(sqlQuery, escapeProcessing, timeout, lineSeparator);
100     parse(schema, granularity, isCaseSensitive);
101   }
102
103   /**
104    * @see AbstractWriteRequest
105    */

106   public AlterRequest(CJDBCInputStream in) throws IOException JavaDoc
107   {
108     super(in, RequestType.ALTER);
109   }
110
111   /**
112    * @see org.objectweb.cjdbc.common.sql.AbstractRequest#needsMacroProcessing()
113    */

114   public boolean needsMacroProcessing()
115   {
116     return false;
117   }
118
119   /**
120    * @see org.objectweb.cjdbc.common.sql.AbstractRequest#returnsResultSet()
121    */

122   public boolean returnsResultSet()
123   {
124     return false;
125   }
126
127   /**
128    * @see org.objectweb.cjdbc.common.sql.AbstractRequest#parse(org.objectweb.cjdbc.common.sql.schema.DatabaseSchema,
129    * int, boolean)
130    */

131   public void parse(DatabaseSchema schema, int granularity,
132       boolean isCaseSensitive) throws SQLException JavaDoc
133   {
134     /*
135      * Example Alter statement: ALTER TABLE table_name ADD column_name datatype
136      * ALTER TABLE table_name DROP COLUMN column_name
137      */

138
139     if (granularity == ParsingGranularities.NO_PARSING)
140     {
141       isParsed = true;
142       return;
143     }
144
145     String JavaDoc originalSQL = this.trimCarriageReturnAndTabs();
146     String JavaDoc sql = originalSQL.toLowerCase();
147
148     // Strip 'alter table '
149
int tableIdx = sql.indexOf("table");
150     if (tableIdx == -1)
151       throw new SQLException JavaDoc(
152           "Malformed Alter Request. Should start with [ALTER TABLE]");
153     sql = sql.substring(tableIdx + 5).trim();
154
155     // Does the query contain a add?
156
int addIdx = sql.indexOf(" add ");
157
158     // Does the query contain a drop?
159
int dropIdx = sql.indexOf(" drop ");
160
161     if (addIdx != -1)
162       isAdd = true;
163     if (dropIdx != -1)
164       isDrop = true;
165
166     if (!isAdd && !isDrop)
167       throw new SQLException JavaDoc(
168           "Malformed Alter Request. No drop or add condition");
169
170     if (isCaseSensitive) // Reverse to the original case
171
sql = originalSQL.substring(originalSQL.length() - sql.length());
172
173     int index = (isAdd) ? addIdx : dropIdx;
174
175     tableName = sql.substring(0, index).trim();
176     table = new DatabaseTable(tableName);
177
178     if (granularity > ParsingGranularities.TABLE)
179     {
180
181       int subsIndex = index + 6 + 2; // index +
182
// column.length()
183
// + space
184
if (isAdd)
185         subsIndex += 3;
186       else
187         // Drop
188
subsIndex += 4;
189
190       columns = new ArrayList JavaDoc();
191       sql = sql.substring(subsIndex).trim();
192
193       if (isAdd)
194       {
195         int colIndex = sql.indexOf(' ');
196         String JavaDoc colName = sql.substring(0, colIndex);
197
198         int uniqueIndex = sql.toLowerCase().indexOf("unique");
199         int primary = sql.toLowerCase().indexOf("primary");
200         if (uniqueIndex != -1 || primary != -1)
201           column = new DatabaseColumn(colName, true);
202         else
203           column = new DatabaseColumn(colName, false);
204         columns.add(new TableColumn(tableName, colName));
205       }
206       else if (isDrop)
207       {
208         String JavaDoc colName = sql.trim();
209         column = schema.getTable(tableName).getColumn(colName);
210         columns.add(new TableColumn(tableName, colName));
211       }
212     }
213     isParsed = true;
214   }
215
216   /**
217    * @see org.objectweb.cjdbc.common.sql.AbstractRequest#cloneParsing(org.objectweb.cjdbc.common.sql.AbstractRequest)
218    */

219   public void cloneParsing(AbstractRequest request)
220   {
221     if (!request.isParsed())
222       return;
223     AlterRequest alterRequest = (AlterRequest) request;
224     cloneTableNameAndColumns((AbstractWriteRequest) request);
225     table = alterRequest.getDatabaseTable();
226     column = alterRequest.getColumn();
227     isParsed = true;
228   }
229
230   /**
231    * Returns the table value.
232    *
233    * @return Returns the table.
234    */

235   public DatabaseTable getDatabaseTable()
236   {
237     return table;
238   }
239
240   /**
241    * Returns the column value.
242    *
243    * @return Returns the column.
244    */

245   public DatabaseColumn getColumn()
246   {
247     return column;
248   }
249
250   /**
251    * Returns the isAdd value.
252    *
253    * @return Returns the isAdd.
254    */

255   public boolean isAdd()
256   {
257     return isAdd;
258   }
259 }
Popular Tags