KickJava   Java API By Example, From Geeks To Geeks.

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


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): Julie Marguerite.
22  * Contributor(s): Mathieu Peltier.
23  */

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

46 public class DropRequest extends AbstractWriteRequest implements Serializable JavaDoc
47 {
48   private static final long serialVersionUID = 7362381853446232926L;
49
50   /**
51    * Creates a new <code>DropRequest</code> instance. The caller must give an
52    * SQL request, without any leading or trailing spaces and beginning with
53    * 'drop table ' (it will not be checked).
54    * <p>
55    * If the syntax is incorrect an exception is thrown.
56    *
57    * @param sqlQuery a <code>String</code> value
58    * @param escapeProcessing should the driver to escape processing before
59    * sending to the database ?
60    * @param timeout an <code>int</code> value
61    * @param lineSeparator the line separator used in the query
62    * @param schema a <code>DatabaseSchema</code> value
63    * @param granularity parsing granularity as defined in
64    * <code>ParsingGranularities</code>
65    * @param isCaseSensitive true if parsing is case sensitive
66    * @exception SQLException if an error occurs
67    */

68   public DropRequest(String JavaDoc sqlQuery, boolean escapeProcessing, int timeout,
69       String JavaDoc lineSeparator, DatabaseSchema schema, int granularity,
70       boolean isCaseSensitive) throws SQLException JavaDoc
71   {
72     this(sqlQuery, escapeProcessing, timeout, lineSeparator);
73     parse(schema, granularity, isCaseSensitive);
74   }
75
76   /**
77    * Creates a new <code>DropRequest</code> instance. The caller must give an
78    * SQL request, without any leading or trailing spaces and beginning with
79    * 'create table ' (it will not be checked).
80    * <p>
81    * The request is not parsed but it can be done later by a call to
82    * {@link #parse(DatabaseSchema, int, boolean)}.
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    * @see #parse
90    */

91   public DropRequest(String JavaDoc sqlQuery, boolean escapeProcessing, int timeout,
92       String JavaDoc lineSeparator)
93   {
94     super(sqlQuery, escapeProcessing, timeout, lineSeparator, RequestType.DROP);
95   }
96
97   /**
98    * @see AbstractWriteRequest
99    */

100   public DropRequest(CJDBCInputStream in) throws IOException JavaDoc
101   {
102     super(in, RequestType.DROP);
103   }
104
105   /**
106    * @see org.objectweb.cjdbc.common.sql.AbstractRequest#parse(org.objectweb.cjdbc.common.sql.schema.DatabaseSchema,
107    * int, boolean)
108    */

109   public void parse(DatabaseSchema schema, int granularity,
110       boolean isCaseSensitive) throws SQLException JavaDoc
111   {
112     if (granularity == ParsingGranularities.NO_PARSING)
113     {
114       isParsed = true;
115       return;
116     }
117
118     String JavaDoc originalSQL = this.trimCarriageReturnAndTabs();
119     String JavaDoc dropTable = originalSQL.toLowerCase();
120
121     // Strip 'drop (temporary) table '
122
int tableIdx = dropTable.indexOf("table");
123     if (tableIdx < 0)
124       throw new SQLException JavaDoc("TABLE not found in this DROP statement: '"
125           + sqlQuery + "'");
126
127     if (isCaseSensitive)
128       dropTable = originalSQL.substring(tableIdx + 5).trim();
129     else
130       dropTable = dropTable.substring(tableIdx + 5).trim();
131
132     if (schema == null)
133       tableName = dropTable;
134     else
135     {
136       // Get the table on which DROP occurs
137
DatabaseTable t = schema.getTable(dropTable, isCaseSensitive);
138       if (t == null)
139         throw new SQLException JavaDoc("Unknown table '" + dropTable
140             + "' in this DROP statement '" + sqlQuery + "'");
141       else
142         tableName = t.getName();
143     }
144     isParsed = true;
145   }
146
147   /**
148    * @see AbstractRequest#cloneParsing(AbstractRequest)
149    */

150   public void cloneParsing(AbstractRequest request)
151   {
152     if (!request.isParsed())
153       return;
154     cloneTableNameAndColumns((AbstractWriteRequest) request);
155     isParsed = true;
156   }
157
158   /**
159    * @see org.objectweb.cjdbc.common.sql.AbstractRequest#needsMacroProcessing()
160    */

161   public boolean needsMacroProcessing()
162   {
163     return false;
164   }
165
166   /**
167    * @see org.objectweb.cjdbc.common.sql.AbstractRequest#returnsResultSet()
168    */

169   public boolean returnsResultSet()
170   {
171     return false;
172   }
173
174   /**
175    * Displays some debugging information about this request.
176    */

177   public void debug()
178   {
179     super.debug();
180     if (tableName != null)
181       System.out.println("Dropped table '" + tableName + "'");
182     else
183       System.out.println("No information about dropped table");
184
185     System.out.println();
186   }
187 }
Popular Tags