KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derby.impl.sql.GenericActivationHolder
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.sql.conn.LanguageConnectionContext;
25
26 import org.apache.derby.iapi.types.DataValueFactory;
27
28 import org.apache.derby.iapi.sql.execute.ExecPreparedStatement;
29 import org.apache.derby.iapi.sql.execute.ExecRow;
30 import org.apache.derby.iapi.sql.execute.ExecutionFactory;
31 import org.apache.derby.iapi.sql.execute.NoPutResultSet;
32 import org.apache.derby.iapi.sql.execute.ConstantAction;
33
34 import org.apache.derby.impl.sql.execute.BaseActivation;
35
36 import org.apache.derby.iapi.types.DataTypeDescriptor;
37 import org.apache.derby.iapi.sql.ParameterValueSet;
38 import org.apache.derby.iapi.sql.ResultSet;
39 import org.apache.derby.iapi.sql.ResultDescription;
40 import org.apache.derby.iapi.sql.Activation;
41 import org.apache.derby.iapi.sql.execute.CursorResultSet;
42 import org.apache.derby.iapi.sql.execute.TemporaryRowHolder;
43
44 import org.apache.derby.iapi.sql.dictionary.IndexRowGenerator;
45 import org.apache.derby.iapi.sql.dictionary.TableDescriptor;
46
47 import org.apache.derby.iapi.reference.SQLState;
48
49 import org.apache.derby.iapi.error.StandardException;
50
51 import org.apache.derby.iapi.services.loader.GeneratedClass;
52 import org.apache.derby.iapi.services.context.Context;
53
54 import org.apache.derby.iapi.store.access.ConglomerateController;
55 import org.apache.derby.iapi.store.access.ScanController;
56
57 import org.apache.derby.iapi.types.RowLocation;
58
59 import org.apache.derby.iapi.services.sanity.SanityManager;
60
61 import org.apache.derby.iapi.store.access.TransactionController;
62
63 import java.sql.SQLWarning JavaDoc;
64 import java.util.Enumeration JavaDoc;
65 import java.util.Vector JavaDoc;
66 import java.util.Hashtable JavaDoc;
67
68 /**
69  * This class holds an Activation, and passes through most of the calls
70  * to the activation. The purpose of this class is to allow a PreparedStatement
71  * to be recompiled without the caller having to detect this and get a new
72  * activation.
73  *
74  * In addition to the Activation, this class holds a reference to the
75  * PreparedStatement that created it, along with a reference to the
76  * GeneratedClass that was associated with the PreparedStatement at the time
77  * this holder was created. These references are used to validate the
78  * Activation, to ensure that an activation is used only with the
79  * PreparedStatement that created it, and to detect when recompilation has
80  * happened.
81  *
82  * We detect recompilation by checking whether the GeneratedClass has changed.
83  * If it has, we try to let the caller continue to use this ActivationHolder.
84  * We create a new instance of the new GeneratedClass (that is, we create a
85  * new Activation), and we compare the number and type of parameters. If these
86  * are compatible, we copy the parameters from the old to the new Activation.
87  * If they are not compatible, we throw an exception telling the user that
88  * the Activation is out of date, and they need to get a new one.
89  *
90  * @author Jeff Lichtman
91  */

92
93 final class GenericActivationHolder implements Activation
94 {
95     BaseActivation ac;
96     ExecPreparedStatement ps;
97     GeneratedClass gc;
98     DataTypeDescriptor[] paramTypes;
99     private final LanguageConnectionContext lcc;
100
101     /**
102      * Constructor for an ActivationHolder
103      *
104      * @param gc The GeneratedClass of the Activation
105      * @param ps The PreparedStatement this ActivationHolder is associated
106      * with
107      *
108      * @exception StandardException Thrown on error
109      */

110     GenericActivationHolder(LanguageConnectionContext lcc, GeneratedClass gc, ExecPreparedStatement ps, boolean scrollable)
111             throws StandardException
112     {
113         this.lcc = lcc;
114         if (SanityManager.DEBUG)
115         {
116             SanityManager.ASSERT(gc != null, "generated class is null , ps is a " + ps.getClass());
117         }
118
119         this.gc = gc;
120         this.ps = ps;
121
122         ac = (BaseActivation) gc.newInstance(lcc);
123         ac.setupActivation(ps, scrollable);
124         paramTypes = ps.getParameterTypes();
125     }
126
127     /* Activation interface */
128
129     /**
130      * @see Activation#reset
131      *
132      * @exception StandardException thrown on failure
133      */

134     public void reset() throws StandardException
135     {
136         ac.reset();
137     }
138
139     /**
140      * Temporary tables can be declared with ON COMMIT DELETE ROWS. But if the table has a held curosr open at
141      * commit time, data should not be deleted from the table. This method, (gets called at commit time) checks if this
142      * activation held cursor and if so, does that cursor reference the passed temp table name.
143      *
144      * @return true if this activation has held cursor and if it references the passed temp table name
145      */

146     public boolean checkIfThisActivationHasHoldCursor(String JavaDoc tableName)
147     {
148         return ac.checkIfThisActivationHasHoldCursor(tableName);
149     }
150
151     /**
152      * @see Activation#setCursorName
153      *
154      */

155     public void setCursorName(String JavaDoc cursorName)
156     {
157         ac.setCursorName(cursorName);
158     }
159
160     /**
161      * @see Activation#getCursorName
162      */

163     public String JavaDoc getCursorName()
164     {
165         return ac.getCursorName();
166     }
167
168     /**
169      * @see Activation#setResultSetHoldability
170      *
171      */

172     public void setResultSetHoldability(boolean resultSetHoldability)
173     {
174         ac.setResultSetHoldability(resultSetHoldability);
175     }
176
177     /**
178      * @see Activation#getResultSetHoldability
179      */

180     public boolean getResultSetHoldability()
181     {
182         return ac.getResultSetHoldability();
183     }
184
185     /** @see Activation#setAutoGeneratedKeysResultsetInfo */
186     public void setAutoGeneratedKeysResultsetInfo(int[] columnIndexes, String JavaDoc[] columnNames)
187     {
188         ac.setAutoGeneratedKeysResultsetInfo(columnIndexes, columnNames);
189     }
190
191     /** @see Activation#getAutoGeneratedKeysResultsetMode */
192     public boolean getAutoGeneratedKeysResultsetMode()
193     {
194         return ac.getAutoGeneratedKeysResultsetMode();
195     }
196
197     /** @see Activation#getAutoGeneratedKeysColumnIndexes */
198     public int[] getAutoGeneratedKeysColumnIndexes()
199     {
200         return ac.getAutoGeneratedKeysColumnIndexes();
201     }
202
203     /** @see Activation#getAutoGeneratedKeysColumnNames */
204     public String JavaDoc[] getAutoGeneratedKeysColumnNames()
205     {
206         return ac.getAutoGeneratedKeysColumnNames();
207     }
208
209     /** @see org.apache.derby.iapi.sql.Activation#getLanguageConnectionContext */
210     public LanguageConnectionContext getLanguageConnectionContext()
211     {
212         return lcc;
213     }
214
215     public TransactionController getTransactionController()
216     {
217         return ac.getTransactionController();
218     }
219
220     /** @see Activation#getExecutionFactory */
221     public ExecutionFactory getExecutionFactory()
222     {
223         return ac.getExecutionFactory();
224     }
225
226     /**
227      * @see Activation#getParameterValueSet
228      */

229     public ParameterValueSet getParameterValueSet()
230     {
231         return ac.getParameterValueSet();
232     }
233
234     /**
235      * @see Activation#setParameters
236      */

237     public void setParameters(ParameterValueSet parameterValues, DataTypeDescriptor[] parameterTypes) throws StandardException
238     {
239         ac.setParameters(parameterValues, parameterTypes);
240     }
241
242     /**
243      * @see Activation#execute
244      *
245      * @exception StandardException Thrown on failure
246      */

247     public ResultSet execute() throws StandardException
248     {
249         /*
250         ** Synchronize to avoid problems if another thread is preparing
251         ** the statement at the same time we're trying to execute it.
252         */

253         // synchronized (ps)
254
{
255             /* Has the activation class changed? */
256             if (gc != ps.getActivationClass())
257             {
258
259                 // ensure the statement is valid by rePreparing it.
260
ps.rePrepare(getLanguageConnectionContext());
261                 
262                 /*
263                 ** If we get here, it means the PreparedStatement has been
264                 ** recompiled. Get a new Activation and check whether the
265                 ** parameters are compatible. If so, transfer the parameters
266                 ** from the old Activation to the new one, and make that the
267                 ** current Activation. If not, throw an exception.
268                 */

269                 GeneratedClass newGC = ps.getActivationClass();
270
271                 BaseActivation newAC = (BaseActivation) newGC.newInstance(lcc);
272
273                 DataTypeDescriptor[] newParamTypes = ps.getParameterTypes();
274
275                 /*
276                 ** Link the new activation to the prepared statement.
277                 */

278                 newAC.setupActivation(ps, ac.getScrollable());
279
280                 newAC.setParameters(ac.getParameterValueSet(), paramTypes);
281
282
283                 /*
284                 ** IMPORTANT
285                 **
286                 ** Copy any essential state from the old activation
287                 ** to the new activation. This must match the state
288                 ** setup in EmbedStatement.
289                 ** singleExecution, cursorName, holdability, maxRows.
290                 */

291
292                 if (ac.isSingleExecution())
293                     newAC.setSingleExecution();
294
295                 newAC.setCursorName(ac.getCursorName());
296
297                 newAC.setResultSetHoldability(ac.getResultSetHoldability());
298                 if (ac.getAutoGeneratedKeysResultsetMode()) //Need to do copy only if auto generated mode is on
299
newAC.setAutoGeneratedKeysResultsetInfo(ac.getAutoGeneratedKeysColumnIndexes(),
300                     ac.getAutoGeneratedKeysColumnNames());
301                 newAC.setMaxRows(ac.getMaxRows());
302
303                 // break the link with the prepared statement
304
ac.setupActivation(null, false);
305                 ac.close();
306
307                 /* Remember the new class information */
308                 ac = newAC;
309                 gc = newGC;
310                 paramTypes = newParamTypes;
311             }
312         }
313
314         String JavaDoc cursorName = ac.getCursorName();
315         if (cursorName != null)
316         {
317             // have to see if another activation is open
318
// with the same cursor name. If so we can't use this name
319

320             Activation activeCursor = lcc.lookupCursorActivation(cursorName);
321
322             if ((activeCursor != null) && (activeCursor != ac)) {
323                 throw StandardException.newException(SQLState.LANG_CURSOR_ALREADY_EXISTS, cursorName);
324             }
325         }
326
327         return ac.execute();
328     }
329
330     /**
331      * @see Activation#getResultSet
332      *
333      * @return the current ResultSet of this activation.
334      */

335     public ResultSet getResultSet()
336     {
337         return ac.getResultSet();
338     }
339
340     /**
341      * @see Activation#clearResultSet
342      */

343     public void clearResultSet()
344     {
345         ac.clearResultSet();
346     }
347
348     /**
349      * @see Activation#setCurrentRow
350      *
351      */

352     public void setCurrentRow(ExecRow currentRow, int resultSetNumber)
353     {
354         ac.setCurrentRow(currentRow, resultSetNumber);
355     }
356
357     /**
358      * @see Activation#clearCurrentRow
359      */

360     public void clearCurrentRow(int resultSetNumber)
361     {
362         ac.clearCurrentRow(resultSetNumber);
363     }
364
365     /**
366      * @see Activation#getPreparedStatement
367      */

368     public ExecPreparedStatement getPreparedStatement()
369     {
370         return ps;
371     }
372
373     public void checkStatementValidity() throws StandardException {
374         ac.checkStatementValidity();
375     }
376
377     /**
378      * @see Activation#getResultDescription
379      */

380     public ResultDescription getResultDescription()
381     {
382         return ac.getResultDescription();
383     }
384
385     /**
386      * @see Activation#getDataValueFactory
387      */

388     public DataValueFactory getDataValueFactory()
389     {
390         return ac.getDataValueFactory();
391     }
392
393     /**
394      * @see Activation#getRowLocationTemplate
395      */

396     public RowLocation getRowLocationTemplate(int itemNumber)
397     {
398         return ac.getRowLocationTemplate(itemNumber);
399     }
400
401     /**
402      * @see Activation#getHeapConglomerateController
403      */

404     public ConglomerateController getHeapConglomerateController()
405     {
406         return ac.getHeapConglomerateController();
407     }
408
409     /**
410      * @see Activation#setHeapConglomerateController
411      */

412     public void setHeapConglomerateController(ConglomerateController updateHeapCC)
413     {
414         ac.setHeapConglomerateController(updateHeapCC);
415     }
416
417     /**
418      * @see Activation#clearHeapConglomerateController
419      */

420     public void clearHeapConglomerateController()
421     {
422         ac.clearHeapConglomerateController();
423     }
424
425     /**
426      * @see Activation#getIndexScanController
427      */

428     public ScanController getIndexScanController()
429     {
430         return ac.getIndexScanController();
431     }
432
433     /**
434      * @see Activation#setIndexScanController
435      */

436     public void setIndexScanController(ScanController indexSC)
437     {
438         ac.setIndexScanController(indexSC);
439     }
440
441     /**
442      * @see Activation#getIndexConglomerateNumber
443      */

444     public long getIndexConglomerateNumber()
445     {
446         return ac.getIndexConglomerateNumber();
447     }
448
449     /**
450      * @see Activation#setIndexConglomerateNumber
451      */

452     public void setIndexConglomerateNumber(long indexConglomerateNumber)
453     {
454         ac.setIndexConglomerateNumber(indexConglomerateNumber);
455     }
456
457     /**
458      * @see Activation#clearIndexScanInfo
459      */

460     public void clearIndexScanInfo()
461     {
462         ac.clearIndexScanInfo();
463     }
464
465     /**
466      * @see Activation#close
467      *
468      * @exception StandardException Thrown on error
469      */

470     public void close() throws StandardException
471     {
472         ac.close();
473     }
474
475     /**
476      * @see Activation#isClosed
477      */

478     public boolean isClosed()
479     {
480         return ac.isClosed();
481     }
482
483     /**
484         Set the activation for a single execution.
485
486         @see Activation#setSingleExecution
487     */

488     public void setSingleExecution() {
489         ac.setSingleExecution();
490     }
491
492     /**
493         Is the activation set up for a single execution.
494
495         @see Activation#isSingleExecution
496     */

497     public boolean isSingleExecution() {
498         return ac.isSingleExecution();
499     }
500
501     /**
502         Get the number of subqueries in the entire query.
503         @return int The number of subqueries in the entire query.
504      */

505     public int getNumSubqueries() {
506         return ac.getNumSubqueries();
507     }
508
509     /**
510      * @see Activation#setForCreateTable()
511      */

512     public void setForCreateTable()
513     {
514         ac.setForCreateTable();
515     }
516
517     /**
518      * @see Activation#getForCreateTable()
519      */

520     public boolean getForCreateTable()
521     {
522         return ac.getForCreateTable();
523     }
524
525     /**
526      * @see Activation#setDDLTableDescriptor
527      */

528     public void setDDLTableDescriptor(TableDescriptor td)
529     {
530         ac.setDDLTableDescriptor(td);
531     }
532
533     /**
534      * @see Activation#getDDLTableDescriptor
535      */

536     public TableDescriptor getDDLTableDescriptor()
537     {
538         return ac.getDDLTableDescriptor();
539     }
540
541     /**
542      * @see Activation#setMaxRows
543      */

544     public void setMaxRows(int maxRows)
545     {
546         ac.setMaxRows(maxRows);
547     }
548
549     /**
550      * @see Activation#getMaxRows
551      */

552     public int getMaxRows()
553     {
554         return ac.getMaxRows();
555     }
556
557     public void setTargetVTI(java.sql.ResultSet JavaDoc targetVTI)
558     {
559         ac.setTargetVTI(targetVTI);
560     }
561
562     public java.sql.ResultSet JavaDoc getTargetVTI()
563     {
564         return ac.getTargetVTI();
565     }
566
567     /* Class implementation */
568
569
570     /**
571      * Mark the activation as unused.
572      */

573     public void markUnused()
574     {
575         ac.markUnused();
576     }
577
578     /**
579      * Is the activation in use?
580      *
581      * @return true/false
582      */

583     public boolean isInUse()
584     {
585         return ac.isInUse();
586     }
587     /**
588       @see org.apache.derby.iapi.sql.Activation#addWarning
589       */

590     public void addWarning(SQLWarning JavaDoc w)
591     {
592         ac.addWarning(w);
593     }
594
595     /**
596       @see org.apache.derby.iapi.sql.Activation#getWarnings
597       */

598     public SQLWarning JavaDoc getWarnings()
599     {
600         return ac.getWarnings();
601     }
602
603     /**
604       @see org.apache.derby.iapi.sql.Activation#clearWarnings
605       */

606     public void clearWarnings()
607     {
608         ac.clearWarnings();
609     }
610
611     /**
612         @see Activation#informOfRowCount
613         @exception StandardException Thrown on error
614      */

615     public void informOfRowCount(NoPutResultSet resultSet, long rowCount)
616                     throws StandardException
617     {
618         ac.informOfRowCount(resultSet, rowCount);
619     }
620
621     /**
622      * @see Activation#isCursorActivation
623      */

624     public boolean isCursorActivation()
625     {
626         return ac.isCursorActivation();
627     }
628
629     public ConstantAction getConstantAction() {
630         return ac.getConstantAction();
631     }
632
633     public void setParentResultSet(TemporaryRowHolder rs, String JavaDoc resultSetId)
634     {
635         ac.setParentResultSet(rs, resultSetId);
636     }
637
638
639     public Vector JavaDoc getParentResultSet(String JavaDoc resultSetId)
640     {
641         return ac.getParentResultSet(resultSetId);
642     }
643
644     public void clearParentResultSets()
645     {
646         ac.clearParentResultSets();
647     }
648
649     public Hashtable JavaDoc getParentResultSets()
650     {
651         return ac.getParentResultSets();
652     }
653
654     public void setForUpdateIndexScan(CursorResultSet forUpdateResultSet)
655     {
656         ac.setForUpdateIndexScan(forUpdateResultSet);
657     }
658
659     public CursorResultSet getForUpdateIndexScan()
660     {
661         return ac.getForUpdateIndexScan();
662     }
663
664     public java.sql.ResultSet JavaDoc[][] getDynamicResults() {
665         return ac.getDynamicResults();
666     }
667     public int getMaxDynamicResults() {
668         return ac.getMaxDynamicResults();
669     }
670
671 }
672
Popular Tags