KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > sql > GenericStorablePreparedStatement


1 /*
2
3    Derby - Class org.apache.derby.impl.sql.GenericStorablePreparedStatement
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. 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  */

21
22 package org.apache.derby.impl.sql;
23
24 import org.apache.derby.iapi.services.monitor.ModuleFactory;
25 import org.apache.derby.iapi.services.monitor.Monitor;
26
27 import org.apache.derby.iapi.services.sanity.SanityManager;
28
29 import org.apache.derby.catalog.UUID;
30 import org.apache.derby.iapi.services.uuid.UUIDFactory;
31 import org.apache.derby.iapi.util.ByteArray;
32
33 import org.apache.derby.iapi.sql.dictionary.DataDictionary;
34
35 import org.apache.derby.iapi.sql.Activation;
36 import org.apache.derby.iapi.sql.ResultColumnDescriptor;
37 import org.apache.derby.iapi.sql.ResultDescription;
38 import org.apache.derby.iapi.sql.ResultSet;
39 import org.apache.derby.iapi.sql.PreparedStatement;
40 import org.apache.derby.iapi.sql.Statement;
41 import org.apache.derby.iapi.sql.StorablePreparedStatement;
42
43 import org.apache.derby.iapi.sql.execute.ConstantAction;
44 import org.apache.derby.iapi.sql.conn.LanguageConnectionContext;
45
46 import org.apache.derby.iapi.error.StandardException;
47
48 import org.apache.derby.iapi.reference.SQLState;
49
50 import org.apache.derby.iapi.services.loader.GeneratedClass;
51 import org.apache.derby.iapi.services.loader.ClassFactory;
52 import org.apache.derby.iapi.services.context.ContextService;
53
54 import org.apache.derby.iapi.services.io.StoredFormatIds;
55 import org.apache.derby.iapi.services.io.FormatIdUtil;
56 import org.apache.derby.iapi.services.io.ArrayUtil;
57 import org.apache.derby.iapi.services.io.Formatable;
58
59 import org.apache.derby.iapi.services.monitor.Monitor;
60
61 import java.sql.Timestamp JavaDoc;
62 import java.io.ObjectOutput JavaDoc;
63 import java.io.ObjectInput JavaDoc;
64 import java.io.IOException JavaDoc;
65
66 import org.apache.derby.iapi.services.loader.ClassFactory;
67 import org.apache.derby.iapi.services.loader.GeneratedClass;
68 import org.apache.derby.iapi.services.loader.GeneratedMethod;
69 /**
70  * Prepared statement that can be made persistent.
71  * @author jamie
72  */

73 public class GenericStorablePreparedStatement
74     extends GenericPreparedStatement implements Formatable, StorablePreparedStatement
75 {
76
77     // formatable
78
private ByteArray byteCode;
79     private String JavaDoc className;
80
81     /**
82      * Niladic constructor, for formatable
83      * only.
84      */

85     public GenericStorablePreparedStatement()
86     {
87         super();
88     }
89
90     GenericStorablePreparedStatement(Statement stmt)
91     {
92         super(stmt);
93     }
94
95     /**
96      * Get our byte code array. Used
97      * by others to save off our byte
98      * code for us.
99      *
100      * @return the byte code saver
101      */

102     ByteArray getByteCodeSaver()
103     {
104         if (byteCode == null) {
105             byteCode = new ByteArray();
106         }
107         return byteCode;
108     }
109
110     /**
111      * Get and load the activation class. Will always
112      * return a loaded/valid class or null if the class
113      * cannot be loaded.
114      *
115      * @return the generated class, or null if the
116      * class cannot be loaded
117      *
118      * @exception StandardException on error
119      */

120     public GeneratedClass getActivationClass()
121         throws StandardException
122     {
123         if (activationClass == null)
124             loadGeneratedClass();
125
126         return activationClass;
127     }
128
129     void setActivationClass(GeneratedClass ac) {
130
131         super.setActivationClass(ac);
132         if (ac != null) {
133             className = ac.getName();
134
135             // see if this is an pre-compiled class
136
if (byteCode != null && byteCode.getArray() == null)
137                 byteCode = null;
138         }
139     }
140
141     /////////////////////////////////////////////////////////////
142
//
143
// STORABLEPREPAREDSTATEMENT INTERFACE
144
//
145
/////////////////////////////////////////////////////////////
146

147     /**
148      * Load up the class from the saved bytes.
149      *
150      * @exception StandardException on error
151      */

152     public void loadGeneratedClass()
153         throws StandardException
154     {
155         LanguageConnectionContext lcc =
156             (LanguageConnectionContext) ContextService.getContext
157                                                   (LanguageConnectionContext.CONTEXT_ID);
158         ClassFactory classFactory = lcc.getLanguageConnectionFactory().getClassFactory();
159
160         GeneratedClass gc = classFactory.loadGeneratedClass(className, byteCode);
161
162         /*
163         ** No special try catch logic to write out bad classes
164         ** here. We don't expect any problems, and in any
165         ** event, we don't have the class builder available
166         ** here.
167         */

168         setActivationClass(gc);
169     }
170
171
172     /////////////////////////////////////////////////////////////
173
//
174
// EXTERNALIZABLE INTERFACE
175
//
176
/////////////////////////////////////////////////////////////
177
/**
178      *
179      * @exception IOException on error
180      */

181     public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc
182     {
183         out.writeObject(getCursorInfo());
184         out.writeBoolean(needsSavepoint());
185         out.writeBoolean(isAtomic);
186         out.writeObject(executionConstants);
187         out.writeObject(resultDesc);
188
189         // savedObjects may be null
190
if (savedObjects == null)
191         {
192             out.writeBoolean(false);
193         }
194         else
195         {
196             out.writeBoolean(true);
197             ArrayUtil.writeArrayLength(out, savedObjects);
198             ArrayUtil.writeArrayItems(out, savedObjects);
199         }
200
201         /*
202         ** Write out the class name and byte code
203         ** if we have them. They might be null if
204         ** we don't want to write out the plan, and
205         ** would prefer it just write out null (e.g.
206         ** we know the plan is invalid).
207         */

208         out.writeObject(className);
209         out.writeBoolean(byteCode != null);
210         if (byteCode != null)
211             byteCode.writeExternal(out);
212     }
213
214      
215     /**
216      * @see java.io.Externalizable#readExternal
217      *
218      * @exception IOException on error
219      * @exception ClassNotFoundException on error
220      */

221     public void readExternal(ObjectInput JavaDoc in)
222         throws IOException JavaDoc, ClassNotFoundException JavaDoc
223     {
224         setCursorInfo((CursorInfo)in.readObject());
225         setNeedsSavepoint(in.readBoolean());
226         isAtomic = (in.readBoolean());
227         executionConstants = (ConstantAction) in.readObject();
228         resultDesc = (ResultDescription) in.readObject();
229
230         if (in.readBoolean())
231         {
232             savedObjects = new Object JavaDoc[ArrayUtil.readArrayLength(in)];
233             ArrayUtil.readArrayItems(in, savedObjects);
234         }
235
236         className = (String JavaDoc)in.readObject();
237         if (in.readBoolean()) {
238             byteCode = new ByteArray();
239             byteCode.readExternal(in);
240         }
241         else
242             byteCode = null;
243     }
244
245     /////////////////////////////////////////////////////////////
246
//
247
// FORMATABLE INTERFACE
248
//
249
/////////////////////////////////////////////////////////////
250
/**
251      * Get the formatID which corresponds to this class.
252      *
253      * @return the formatID of this class
254      */

255     public int getTypeFormatId() { return StoredFormatIds.STORABLE_PREPARED_STATEMENT_V01_ID; }
256
257     /////////////////////////////////////////////////////////////
258
//
259
// MISC
260
//
261
/////////////////////////////////////////////////////////////
262
public boolean isStorable() {
263         return true;
264     }
265     public String JavaDoc toString()
266     {
267         if (SanityManager.DEBUG)
268         {
269             String JavaDoc acn;
270             if (activationClass ==null)
271                 acn = "null";
272             else
273                 acn = activationClass.getName();
274
275             return "GSPS " + System.identityHashCode(this) + " activationClassName="+acn+" className="+className;
276         }
277         else
278         {
279             return "";
280         }
281     }
282 }
283
Popular Tags