KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > continuent > sequoia > controller > requests > DropRequest


1 /**
2  * Sequoia: Database clustering technology.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Copyright (C) 2005-2006 Continuent, Inc.
6  * Contact: sequoia@continuent.org
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  * Initial developer(s): Julie Marguerite.
21  * Contributor(s): Mathieu Peltier.
22  */

23
24 package org.continuent.sequoia.controller.requests;
25
26 import java.io.Serializable JavaDoc;
27 import java.sql.SQLException JavaDoc;
28 import java.util.SortedSet JavaDoc;
29 import java.util.TreeSet JavaDoc;
30
31 import org.continuent.sequoia.common.i18n.Translate;
32 import org.continuent.sequoia.controller.semantic.SemanticBehavior;
33 import org.continuent.sequoia.controller.sql.schema.DatabaseSchema;
34 import org.continuent.sequoia.controller.sql.schema.DatabaseTable;
35
36 /**
37  * An <code>DropRequest</code> is an SQL request with the following syntax:
38  *
39  * <pre>
40  * DROP TABLE table-name
41  * </pre>
42  *
43  * @author <a HREF="mailto:Julie.Marguerite@inria.fr">Julie Marguerite </a>
44  * @author <a HREF="mailto:Mathieu.Peltier@inrialpes.fr">Mathieu Peltier </a>
45  * @version 1.0
46  */

47 public class DropRequest extends AbstractWriteRequest implements Serializable JavaDoc
48 {
49   private static final long serialVersionUID = 5702833689405251895L;
50
51   // Be conservative, if we cannot parse the query, assumes it invalidates
52
// everything
53
protected boolean altersAggregateList = true;
54   protected boolean altersDatabaseCatalog = true;
55   protected boolean altersDatabaseSchema = true;
56   protected boolean altersMetadataCache = true;
57   protected boolean altersQueryResultCache = true;
58   protected boolean altersSomething = true;
59   protected boolean altersStoredProcedureList = true;
60   protected boolean altersUserDefinedTypes = true;
61   protected boolean altersUsers = true;
62
63   /**
64    * Sorted list of table names that are being dropped. This will be used to
65    * remove these tables from the schema.
66    */

67   protected SortedSet JavaDoc tablesToDrop = null;
68
69   /**
70    * Creates a new <code>DropRequest</code> instance. The caller must give an
71    * SQL request, without any leading or trailing spaces and beginning with
72    * 'create table ' (it will not be checked).
73    * <p>
74    * The request is not parsed but it can be done later by a call to
75    * {@link #parse(DatabaseSchema, int, boolean)}.
76    *
77    * @param sqlQuery the SQL request
78    * @param escapeProcessing should the driver to escape processing before
79    * sending to the database ?
80    * @param timeout an <code>int</code> value
81    * @param lineSeparator the line separator used in the query
82    * @see #parse
83    */

84   public DropRequest(String JavaDoc sqlQuery, boolean escapeProcessing, int timeout,
85       String JavaDoc lineSeparator)
86   {
87     super(sqlQuery, escapeProcessing, timeout, lineSeparator, RequestType.DROP);
88     setMacrosAreProcessed(true); // no macro processing needed
89
}
90
91   /**
92    * @see org.continuent.sequoia.controller.requests.AbstractRequest#altersAggregateList()
93    */

94   public boolean altersAggregateList()
95   {
96     return altersAggregateList;
97   }
98
99   /**
100    * @see org.continuent.sequoia.controller.requests.AbstractRequest#altersDatabaseCatalog()
101    */

102   public boolean altersDatabaseCatalog()
103   {
104     return altersDatabaseCatalog;
105   }
106
107   /**
108    * @see org.continuent.sequoia.controller.requests.AbstractRequest#altersDatabaseSchema()
109    */

110   public boolean altersDatabaseSchema()
111   {
112     return altersDatabaseSchema;
113   }
114
115   /**
116    * @see org.continuent.sequoia.controller.requests.AbstractRequest#altersMetadataCache()
117    */

118   public boolean altersMetadataCache()
119   {
120     return altersMetadataCache;
121   }
122
123   /**
124    * @see org.continuent.sequoia.controller.requests.AbstractRequest#altersQueryResultCache()
125    */

126   public boolean altersQueryResultCache()
127   {
128     return altersQueryResultCache;
129   }
130
131   /**
132    * @see org.continuent.sequoia.controller.requests.AbstractRequest#altersSomething()
133    */

134   public boolean altersSomething()
135   {
136     return altersSomething;
137   }
138
139   /**
140    * @see org.continuent.sequoia.controller.requests.AbstractRequest#altersStoredProcedureList()
141    */

142   public boolean altersStoredProcedureList()
143   {
144     return altersStoredProcedureList;
145   }
146
147   /**
148    * @see org.continuent.sequoia.controller.requests.AbstractRequest#altersUserDefinedTypes()
149    */

150   public boolean altersUserDefinedTypes()
151   {
152     return altersUserDefinedTypes;
153   }
154
155   /**
156    * @see org.continuent.sequoia.controller.requests.AbstractRequest#altersUsers()
157    */

158   public boolean altersUsers()
159   {
160     return altersUsers;
161   }
162
163   /**
164    * @see AbstractRequest#cloneParsing(AbstractRequest)
165    */

166   public void cloneParsing(AbstractRequest request)
167   {
168     if (!request.isParsed())
169       return;
170     cloneParsingCommons(request);
171     cloneTableNameAndColumns((AbstractWriteRequest) request);
172     isParsed = true;
173   }
174
175   /**
176    * @see org.continuent.sequoia.controller.requests.AbstractRequest#needsMacroProcessing()
177    */

178   public boolean needsMacroProcessing()
179   {
180     return false;
181   }
182
183   /**
184    * @see org.continuent.sequoia.controller.requests.AbstractRequest#parse(org.continuent.sequoia.controller.sql.schema.DatabaseSchema,
185    * int, boolean)
186    */

187   public void parse(DatabaseSchema schema, int granularity,
188       boolean isCaseSensitive) throws SQLException JavaDoc
189   {
190     if (granularity == ParsingGranularities.NO_PARSING)
191     {
192       isParsed = true;
193       return;
194     }
195
196     try
197     {
198       String JavaDoc originalSQL = this.trimCarriageReturnAndTabs();
199       String JavaDoc sql = originalSQL.toLowerCase();
200
201       // Strip drop
202
sql = sql.substring("drop".length()).trim();
203       originalSQL = originalSQL.substring("drop".length()).trim();
204
205       // Check what kind of create we are facing
206
if (sql.startsWith("database"))
207       { // DROP DATABASE alters only the database catalog
208
altersDatabaseCatalog = true;
209         altersDatabaseSchema = false;
210         altersStoredProcedureList = false;
211         altersUserDefinedTypes = false;
212         altersUsers = false;
213         return;
214       }
215
216       if (sql.startsWith("index") || sql.startsWith("role")
217           || sql.startsWith("sequence"))
218       { // Does not alter anything :
219
// DROP INDEX
220
// DROP ROLE
221
// DROP SEQUENCE
222
altersSomething = false;
223         return;
224       }
225       if (sql.startsWith("function") || sql.startsWith("method")
226           || sql.startsWith("procedure") || sql.startsWith("trigger")
227           || sql.startsWith("type"))
228       { // DROP FUNCTION, DROP METHOD, DROP PROCEDURE, DROP TRIGGER and
229
// DROP TYPE only alters definitions
230
altersDatabaseCatalog = false;
231         altersDatabaseSchema = false;
232         altersStoredProcedureList = true;
233         altersUserDefinedTypes = true;
234         altersUsers = false;
235         return;
236       }
237
238       // From this point on, only the schema is affected
239
altersDatabaseCatalog = false;
240       altersDatabaseSchema = true;
241       altersStoredProcedureList = false;
242       altersUserDefinedTypes = false;
243       altersUsers = false;
244       if (sql.startsWith("schema") || sql.startsWith("view"))
245         // No parsing to do
246
return;
247
248       // Strip 'drop (temporary) table '
249
int tableIdx = sql.indexOf("table");
250       if (tableIdx < 0)
251         throw new SQLException JavaDoc("Unsupported DROP statement: '"
252             + sqlQueryOrTemplate + "'");
253
254       if (isCaseSensitive)
255         sql = originalSQL.substring(tableIdx + 5).trim();
256       else
257         sql = sql.substring(tableIdx + 5).trim();
258
259       int endTableName = sql.indexOf(" ");
260
261       if (endTableName >= 0)
262         sql = sql.substring(0, endTableName).trim();
263
264       if (schema == null)
265         tableName = sql;
266       else
267       {
268         // Get the table on which DROP occurs
269
DatabaseTable t = schema.getTable(sql, isCaseSensitive);
270         if (t == null)
271           throw new SQLException JavaDoc("Unknown table '" + sql
272               + "' in this DROP statement '" + sqlQueryOrTemplate + "'",
273               "42P01");
274         else
275           tableName = t.getName();
276       }
277       writeLockedTables = new TreeSet JavaDoc();
278       tablesToDrop = new TreeSet JavaDoc();
279       writeLockedTables.add(tableName);
280       tablesToDrop.add(tableName);
281       addDependingTables(schema, writeLockedTables);
282       isParsed = true;
283     }
284     finally
285     {
286       if (isParsed)
287       {
288         setSemantic(new SemanticBehavior(null, writeLockedTables, null,
289             altersDatabaseSchema(), altersMetadataCache(),
290             altersQueryResultCache(), altersUsers(), isReadOnly,
291             needsMacroProcessing(), SemanticBehavior.SERIALIZABLE_ORDER,
292             requiresConnectionPoolFlush
293                 ? SemanticBehavior.FLUSH_ALL_USERS
294                 : SemanticBehavior.FLUSH_NONE));
295       }
296     }
297   }
298
299   /**
300    * Does this request returns a ResultSet?
301    *
302    * @return false
303    */

304   public boolean returnsResultSet()
305   {
306     return false;
307   }
308
309   /**
310    * @see org.continuent.sequoia.controller.requests.AbstractRequest#getParsingResultsAsString()
311    */

312   public String JavaDoc getParsingResultsAsString()
313   {
314     StringBuffer JavaDoc sb = new StringBuffer JavaDoc(super.getParsingResultsAsString());
315     sb.append(Translate.get("request.alters",
316         new String JavaDoc[]{String.valueOf(altersAggregateList()),
317             String.valueOf(altersDatabaseCatalog()),
318             String.valueOf(altersDatabaseSchema()),
319             String.valueOf(altersMetadataCache()),
320             String.valueOf(altersQueryResultCache()),
321             String.valueOf(altersSomething()),
322             String.valueOf(altersStoredProcedureList()),
323             String.valueOf(altersUserDefinedTypes()),
324             String.valueOf(altersUsers())}));
325     return sb.toString();
326   }
327
328   /**
329    * Displays some debugging information about this request.
330    */

331   public void debug()
332   {
333     super.debug();
334     if (tableName != null)
335       System.out.println("Dropped table '" + tableName + "'");
336     else
337       System.out.println("No information about dropped table");
338
339     System.out.println();
340   }
341
342   /**
343    * Return the set of tables that are dropped by current request. This set is
344    * different of writeLockedTables, as the latest also contains tables that
345    * need to be locked (because of foreign key constraints).
346    *
347    * @return set of tables that are being dropped by this request
348    */

349   public SortedSet JavaDoc getTablesToDrop()
350   {
351     return tablesToDrop;
352   }
353 }
Popular Tags