001: /*
002:
003: Derby - Class org.apache.derby.diag.StatementCache
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to You under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derby.diag;
023:
024: import org.apache.derby.vti.VTITemplate;
025:
026: import org.apache.derby.iapi.sql.conn.ConnectionUtil;
027: import org.apache.derby.iapi.sql.conn.LanguageConnectionContext;
028: import org.apache.derby.impl.sql.GenericPreparedStatement;
029: import org.apache.derby.impl.sql.GenericStatement;
030:
031: import org.apache.derby.iapi.sql.ResultColumnDescriptor;
032: import org.apache.derby.impl.jdbc.EmbedResultSetMetaData;
033: import org.apache.derby.iapi.reference.Limits;
034: import org.apache.derby.iapi.util.StringUtil;
035:
036: import java.sql.Types;
037: import java.sql.ResultSetMetaData;
038: import java.sql.SQLException;
039: import java.sql.Timestamp;
040:
041: import org.apache.derby.impl.sql.conn.CachedStatement;
042: import org.apache.derby.impl.services.cache.CachedItem;
043:
044: import java.util.Vector;
045: import java.util.Enumeration;
046:
047: /**
048: StatementCache is a virtual table that shows the contents of the SQL statement cache.
049:
050: This virtual table can be invoked by calling it directly.
051: <PRE> select * from new org.apache.derby.diag.StatementCache() t</PRE>
052:
053:
054: <P>The StatementCache virtual table has the following columns:
055: <UL>
056: <LI> ID CHAR(36) - not nullable. Internal identifier of the compiled statement.
057: <LI> SCHEMANAME VARCHAR(128) - nullable. Schema the statement was compiled in.
058: <LI> SQL_TEXT VARCHAR(32672) - not nullable. Text of the statement
059: <LI> UNICODE BIT/BOOLEAN - not nullable. True if the statement is compiled as a pure unicode string, false if it handled unicode escapes.
060: <LI> VALID BIT/BOOLEAN - not nullable. True if the statement is currently valid, false otherwise
061: <LI> COMPILED_AT TIMESTAMP nullable - time statement was compiled, requires STATISTICS TIMING to be enabled.
062:
063:
064: </UL>
065: <P>
066: The internal identifier of a cached statement matches the toString() method of a PreparedStatement object for a Cloudscape database.
067:
068: <P>
069: This class also provides a static method to empty the statement cache, StatementCache.emptyCache()
070:
071: */
072: public final class StatementCache extends VTITemplate {
073:
074: private int position = -1;
075: private Vector data;
076: private GenericPreparedStatement currentPs;
077: private boolean wasNull;
078:
079: /**
080: Empty the statement cache. Must be called from a SQL statement, e.g.
081: <PRE>
082: CALL org.apache.derby.diag.StatementCache::emptyCache()
083: </PRE>
084:
085: */
086: public static void emptyCache() throws SQLException {
087:
088: org.apache.derby.impl.sql.conn.GenericLanguageConnectionContext lcc = (org.apache.derby.impl.sql.conn.GenericLanguageConnectionContext) ConnectionUtil
089: .getCurrentLCC();
090:
091: lcc.emptyCache();
092: }
093:
094: public StatementCache() throws SQLException {
095:
096: org.apache.derby.impl.sql.conn.GenericLanguageConnectionContext lcc = (org.apache.derby.impl.sql.conn.GenericLanguageConnectionContext) ConnectionUtil
097: .getCurrentLCC();
098:
099: if (lcc.statementCache != null) {
100:
101: java.util.Hashtable stmtCache = (java.util.Hashtable) lcc.statementCache;
102: data = new Vector(stmtCache.size());
103: for (Enumeration e = stmtCache.elements(); e
104: .hasMoreElements();) {
105:
106: CachedItem ci = (CachedItem) e.nextElement();
107: CachedStatement cs = (CachedStatement) ci.getEntry();
108:
109: GenericPreparedStatement ps = (GenericPreparedStatement) cs
110: .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
127: .elementAt(position);
128:
129: if (currentPs != null)
130: return true;
131: }
132:
133: data = null;
134: return false;
135: }
136:
137: public void close() {
138: data = null;
139: currentPs = null;
140: }
141:
142: public String getString(int colId) {
143: wasNull = false;
144: switch (colId) {
145: case 1:
146: return currentPs.getObjectName();
147: case 2:
148: return ((GenericStatement) currentPs.statement)
149: .getCompilationSchema();
150: case 3:
151: String sql = currentPs.getSource();
152: sql = StringUtil.truncate(sql, Limits.DB2_VARCHAR_MAXWIDTH);
153: return sql;
154: default:
155: return null;
156: }
157: }
158:
159: public boolean getBoolean(int colId) {
160: wasNull = false;
161: switch (colId) {
162: case 4:
163: // was/is UniCode column, but since Derby 10.0 all
164: // statements are compiled and submitted as UniCode.
165: return true;
166: case 5:
167: return currentPs.isValid();
168: default:
169: return false;
170: }
171: }
172:
173: public Timestamp getTimestamp(int colId) {
174:
175: Timestamp ts = currentPs.getEndCompileTimestamp();
176: wasNull = (ts == null);
177: return ts;
178: }
179:
180: public boolean wasNull() {
181: return wasNull;
182: }
183:
184: /*
185: ** Metadata
186: */
187: private static final ResultColumnDescriptor[] columnInfo = {
188:
189: EmbedResultSetMetaData.getResultColumnDescriptor("ID",
190: Types.CHAR, false, 36),
191: EmbedResultSetMetaData.getResultColumnDescriptor(
192: "SCHEMANAME", Types.VARCHAR, true, 128),
193: EmbedResultSetMetaData.getResultColumnDescriptor(
194: "SQL_TEXT", Types.VARCHAR, false,
195: Limits.DB2_VARCHAR_MAXWIDTH),
196: EmbedResultSetMetaData.getResultColumnDescriptor("UNICODE",
197: Types.BIT, false),
198: EmbedResultSetMetaData.getResultColumnDescriptor("VALID",
199: Types.BIT, false),
200: EmbedResultSetMetaData.getResultColumnDescriptor(
201: "COMPILED_AT", Types.TIMESTAMP, true),
202:
203: };
204:
205: private static final ResultSetMetaData metadata = new EmbedResultSetMetaData(
206: columnInfo);
207:
208: public ResultSetMetaData getMetaData() {
209:
210: return metadata;
211: }
212: }
|