KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > continuent > sequoia > common > sql > Request


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

21
22 package org.continuent.sequoia.common.sql;
23
24 import java.io.IOException JavaDoc;
25
26 import org.continuent.sequoia.common.stream.DriverBufferedInputStream;
27 import org.continuent.sequoia.common.stream.DriverBufferedOutputStream;
28
29 /**
30  * This class defines a Request object. This basically carries the SQL statement
31  * and the SQL template if this is a PreparedStatement.
32  *
33  * @author <a HREF="mailto:emmanuel.cecchet@continuent.com">Emmanuel Cecchet</a>
34  * @version 1.0
35  */

36 public class Request
37 {
38   /**
39    * SQL query if this is a statement (should be set in constructor) or query
40    * template if this is a PreparedStatement.
41    */

42   private String JavaDoc sqlQueryOrTemplate;
43
44   /**
45    * PreparedStatement parameters
46    */

47   private String JavaDoc preparedStatementParameters = null;
48
49   //
50
// Connection related parameters
51
//
52

53   /** True if the connection has been set to read-only */
54   private boolean isReadOnly = false;
55
56   /**
57    * Whether this request has been sent in <code>autocommit</code> mode or
58    * not.
59    */

60   private boolean isAutoCommit = true;
61
62   /**
63    * Transaction isolation level to use when executing the query inside a
64    * transaction. The value is set by the VirtualDatabaseWorkerThread.
65    */

66   private int transactionIsolation;
67
68   /**
69    * Timeout for this request in seconds, value 0 means no timeout (should be
70    * set in constructor). This timeout is in seconds, reflecting the jdbc-spec,
71    * and is passed as-is to the backends jdbc-driver. Internally converted to ms
72    * via getTimeoutMs().
73    */

74   private int timeoutInSeconds;
75
76   /**
77    * Should the backend driver do escape processing before sending to the
78    * database? Simply forwarded to backend driver. No setter for this member,
79    * should be set in constructor.
80    *
81    * @see java.sql.Statement#setEscapeProcessing(boolean)
82    */

83   private boolean escapeProcessing = true;
84
85   /**
86    * Request identifier only used for failover purposes. This id is sent back by
87    * the controller.
88    */

89   private long id;
90
91   //
92
// Constructors
93
//
94

95   /**
96    * Creates a new <code>Request</code> object
97    *
98    * @param sqlTemplate the SQL template (using ? as parameter placeholders) or
99    * null
100    * @param parameters the prepared statement parameters
101    * @param escapeProcessing Should the backend driver do escape processing
102    * before sending to the database?
103    * @param timeoutInSeconds Timeout for this request in seconds, value 0 means
104    * no timeout
105    */

106   public Request(String JavaDoc sqlTemplate, String JavaDoc parameters,
107       boolean escapeProcessing, int timeoutInSeconds)
108   {
109     this.sqlQueryOrTemplate = sqlTemplate;
110     this.preparedStatementParameters = parameters;
111     this.escapeProcessing = escapeProcessing;
112     this.timeoutInSeconds = timeoutInSeconds;
113   }
114
115   //
116
// Serialization
117
//
118

119   /**
120    * Creates a new <code>Request</code> object, deserializing it from an input
121    * stream. Has to mirror the serialization method below.
122    *
123    * @param in the input stream to read from
124    * @throws IOException if a network error occurs
125    */

126
127   public Request(DriverBufferedInputStream in) throws IOException JavaDoc
128   {
129     this.sqlQueryOrTemplate = in.readLongUTF();
130     this.escapeProcessing = in.readBoolean();
131     this.timeoutInSeconds = in.readInt();
132
133     this.isAutoCommit = in.readBoolean();
134
135     // Does this request has a template with question marks "?"
136
// true for PreparedStatements
137
// (AND did we ask for them at connection time?)
138
if (in.readBoolean())
139       this.preparedStatementParameters = in.readLongUTF();
140
141     // success, we received it all
142
}
143
144   /**
145    * Serialize the request on the output stream by sending only the needed
146    * parameters to reconstruct it on the controller. Has to mirror the
147    * deserialization method above.
148    *
149    * @param out destination DriverBufferedOutputStream
150    * @throws IOException if fails
151    */

152
153   public void sendToStream(DriverBufferedOutputStream out) throws IOException JavaDoc
154   {
155     out.writeLongUTF(sqlQueryOrTemplate);
156     out.writeBoolean(escapeProcessing);
157     out.writeInt(timeoutInSeconds);
158
159     out.writeBoolean(isAutoCommit);
160
161     // Send the "un-"PreparedStatement if the controller wants it:
162
// - either for better parsing
163
// - or to process it
164
// - && this is a PreparedStatement (of course)
165
if (preparedStatementParameters != null)
166     {
167       out.writeBoolean(true);
168       out.writeLongUTF(preparedStatementParameters);
169     }
170     else
171       out.writeBoolean(false);
172   }
173
174   //
175
// Getters/Setters
176
//
177

178   /**
179    * Returns the escapeProcessing value.
180    *
181    * @return Returns the escapeProcessing.
182    */

183   public final boolean isEscapeProcessing()
184   {
185     return escapeProcessing;
186   }
187
188   /**
189    * Sets the escapeProcessing value.
190    *
191    * @param escapeProcessing The escapeProcessing to set.
192    */

193   public final void setEscapeProcessing(boolean escapeProcessing)
194   {
195     this.escapeProcessing = escapeProcessing;
196   }
197
198   /**
199    * Returns the isAutoCommit value.
200    *
201    * @return Returns the isAutoCommit.
202    */

203   public final boolean isAutoCommit()
204   {
205     return isAutoCommit;
206   }
207
208   /**
209    * Sets the isAutoCommit value.
210    *
211    * @param isAutoCommit The isAutoCommit to set.
212    */

213   public final void setIsAutoCommit(boolean isAutoCommit)
214   {
215     this.isAutoCommit = isAutoCommit;
216   }
217
218   /**
219    * Returns the isReadOnly value.
220    *
221    * @return Returns the isReadOnly.
222    */

223   public final boolean isReadOnly()
224   {
225     return isReadOnly;
226   }
227
228   /**
229    * Sets the isReadOnly value.
230    *
231    * @param isReadOnly The isReadOnly to set.
232    */

233   public final void setIsReadOnly(boolean isReadOnly)
234   {
235     this.isReadOnly = isReadOnly;
236   }
237
238   /**
239    * Returns the id value.
240    *
241    * @return Returns the id.
242    */

243   public long getId()
244   {
245     return id;
246   }
247
248   /**
249    * Sets the id value.
250    *
251    * @param id The id to set.
252    */

253   public void setId(long id)
254   {
255     this.id = id;
256   }
257
258   /**
259    * Returns the sqlQuery value.
260    *
261    * @return Returns the sqlQuery.
262    */

263   public final String JavaDoc getSqlQueryOrTemplate()
264   {
265     return sqlQueryOrTemplate;
266   }
267
268   /**
269    * Get a short form of this request if the SQL statement exceeds
270    * nbOfCharacters.
271    *
272    * @param nbOfCharacters number of characters to include in the short form.
273    * @return the nbOfCharacters first characters of the SQL statement
274    */

275   public String JavaDoc getSqlShortForm(int nbOfCharacters)
276   {
277     if ((nbOfCharacters == 0) || (sqlQueryOrTemplate.length() < nbOfCharacters))
278       return sqlQueryOrTemplate;
279     else
280       return sqlQueryOrTemplate.substring(0, nbOfCharacters) + "...";
281   }
282
283   /**
284    * Returns the sqlTemplate value.
285    *
286    * @return Returns the sqlTemplate.
287    */

288   public final String JavaDoc getPreparedStatementParameters()
289   {
290     return preparedStatementParameters;
291   }
292
293   /**
294    * Returns the timeoutInSeconds value.
295    *
296    * @return Returns the timeoutInSeconds.
297    */

298   public final int getTimeoutInSeconds()
299   {
300     return timeoutInSeconds;
301   }
302
303   /**
304    * Sets the timeoutInSeconds value.
305    *
306    * @param timeoutInSeconds The timeoutInSeconds to set.
307    */

308   public final void setTimeoutInSeconds(int timeoutInSeconds)
309   {
310     this.timeoutInSeconds = timeoutInSeconds;
311   }
312
313   /**
314    * Returns the transactionIsolation value.
315    *
316    * @return Returns the transactionIsolation.
317    */

318   public final int getTransactionIsolation()
319   {
320     return transactionIsolation;
321   }
322
323   /**
324    * Sets the transactionIsolation value.
325    *
326    * @param transactionIsolation The transactionIsolation to set.
327    */

328   public final void setTransactionIsolation(int transactionIsolation)
329   {
330     this.transactionIsolation = transactionIsolation;
331   }
332
333 }
334
Popular Tags