KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > diag > StatementCache


1 /*
2
3    Derby - Class org.apache.derby.diag.StatementCache
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.diag;
23
24 import org.apache.derby.vti.VTITemplate;
25
26 import org.apache.derby.iapi.sql.conn.ConnectionUtil;
27 import org.apache.derby.iapi.sql.conn.LanguageConnectionContext;
28 import org.apache.derby.impl.sql.GenericPreparedStatement;
29 import org.apache.derby.impl.sql.GenericStatement;
30
31 import org.apache.derby.iapi.sql.ResultColumnDescriptor;
32 import org.apache.derby.impl.jdbc.EmbedResultSetMetaData;
33 import org.apache.derby.iapi.reference.Limits;
34 import org.apache.derby.iapi.util.StringUtil;
35
36 import java.sql.Types JavaDoc;
37 import java.sql.ResultSetMetaData JavaDoc;
38 import java.sql.SQLException JavaDoc;
39 import java.sql.Timestamp JavaDoc;
40
41 import org.apache.derby.impl.sql.conn.CachedStatement;
42 import org.apache.derby.impl.services.cache.CachedItem;
43
44
45 import java.util.Vector JavaDoc;
46 import java.util.Enumeration JavaDoc;
47
48 /**
49     StatementCache is a virtual table that shows the contents of the SQL statement cache.
50     
51     This virtual table can be invoked by calling it directly.
52     <PRE> select * from new org.apache.derby.diag.StatementCache() t</PRE>
53
54
55     <P>The StatementCache virtual table has the following columns:
56     <UL>
57     <LI> ID CHAR(36) - not nullable. Internal identifier of the compiled statement.
58     <LI> SCHEMANAME VARCHAR(128) - nullable. Schema the statement was compiled in.
59     <LI> SQL_TEXT VARCHAR(32672) - not nullable. Text of the statement
60     <LI> UNICODE BIT/BOOLEAN - not nullable. True if the statement is compiled as a pure unicode string, false if it handled unicode escapes.
61     <LI> VALID BIT/BOOLEAN - not nullable. True if the statement is currently valid, false otherwise
62     <LI> COMPILED_AT TIMESTAMP nullable - time statement was compiled, requires STATISTICS TIMING to be enabled.
63
64
65     </UL>
66     <P>
67     The internal identifier of a cached statement matches the toString() method of a PreparedStatement object for a Cloudscape database.
68
69     <P>
70     This class also provides a static method to empty the statement cache, StatementCache.emptyCache()
71
72 */

73 public final class StatementCache extends VTITemplate {
74
75     private int position = -1;
76     private Vector JavaDoc data;
77     private GenericPreparedStatement currentPs;
78     private boolean wasNull;
79
80     /**
81         Empty the statement cache. Must be called from a SQL statement, e.g.
82         <PRE>
83         CALL org.apache.derby.diag.StatementCache::emptyCache()
84         </PRE>
85
86     */

87     public static void emptyCache() throws SQLException JavaDoc {
88
89         org.apache.derby.impl.sql.conn.GenericLanguageConnectionContext lcc =
90             (org.apache.derby.impl.sql.conn.GenericLanguageConnectionContext) ConnectionUtil.getCurrentLCC();
91
92         lcc.emptyCache();
93     }
94
95     public StatementCache() throws SQLException JavaDoc {
96
97         org.apache.derby.impl.sql.conn.GenericLanguageConnectionContext lcc =
98             (org.apache.derby.impl.sql.conn.GenericLanguageConnectionContext) ConnectionUtil.getCurrentLCC();
99
100         if (lcc.statementCache != null) {
101
102             java.util.Hashtable JavaDoc stmtCache = (java.util.Hashtable JavaDoc) lcc.statementCache;
103             data = new Vector JavaDoc(stmtCache.size());
104             for (Enumeration JavaDoc e = stmtCache.elements(); e.hasMoreElements(); ) {
105
106
107                 CachedItem ci = (CachedItem) e.nextElement();
108                 CachedStatement cs = (CachedStatement) ci.getEntry();
109
110                 GenericPreparedStatement ps = (GenericPreparedStatement) cs.getPreparedStatement();
111
112                 data.addElement(ps);
113             }
114         }
115
116     }
117
118     public boolean next() {
119
120         if (data == null)
121             return false;
122
123         position++;
124
125         for (; position < data.size(); position++) {
126             currentPs = (GenericPreparedStatement) data.elementAt(position);
127     
128             if (currentPs != null)
129                 return true;
130         }
131
132         data = null;
133         return false;
134     }
135
136     public void close() {
137         data = null;
138         currentPs = null;
139     }
140
141
142     public String JavaDoc getString(int colId) {
143         wasNull = false;
144         switch (colId) {
145         case 1:
146             return currentPs.getObjectName();
147         case 2:
148             return ((GenericStatement) currentPs.statement).getCompilationSchema();
149         case 3:
150             String JavaDoc sql = currentPs.getSource();
151             sql = StringUtil.truncate(sql, Limits.DB2_VARCHAR_MAXWIDTH);
152             return sql;
153         default:
154             return null;
155         }
156     }
157
158     public boolean getBoolean(int colId) {
159         wasNull = false;
160         switch (colId) {
161         case 4:
162             // was/is UniCode column, but since Derby 10.0 all
163
// statements are compiled and submitted as UniCode.
164
return true;
165         case 5:
166             return currentPs.isValid();
167         default:
168             return false;
169         }
170     }
171
172     public Timestamp JavaDoc getTimestamp(int colId) {
173
174         Timestamp JavaDoc ts = currentPs.getEndCompileTimestamp();
175         wasNull = (ts == null);
176         return ts;
177     }
178
179     public boolean wasNull() {
180         return wasNull;
181     }
182
183     /*
184     ** Metadata
185     */

186     private static final ResultColumnDescriptor[] columnInfo = {
187
188         EmbedResultSetMetaData.getResultColumnDescriptor("ID", Types.CHAR, false, 36),
189         EmbedResultSetMetaData.getResultColumnDescriptor("SCHEMANAME", Types.VARCHAR, true, 128),
190         EmbedResultSetMetaData.getResultColumnDescriptor("SQL_TEXT", Types.VARCHAR, false, Limits.DB2_VARCHAR_MAXWIDTH),
191         EmbedResultSetMetaData.getResultColumnDescriptor("UNICODE", Types.BIT, false),
192         EmbedResultSetMetaData.getResultColumnDescriptor("VALID", Types.BIT, false),
193         EmbedResultSetMetaData.getResultColumnDescriptor("COMPILED_AT", Types.TIMESTAMP, true),
194
195     };
196     
197     private static final ResultSetMetaData metadata = new EmbedResultSetMetaData(columnInfo);
198
199     public ResultSetMetaData getMetaData() {
200
201         return metadata;
202     }
203 }
204
Popular Tags