1 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 ; 37 import java.sql.ResultSetMetaData ; 38 import java.sql.SQLException ; 39 import java.sql.Timestamp ; 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 ; 46 import java.util.Enumeration ; 47 48 73 public final class StatementCache extends VTITemplate { 74 75 private int position = -1; 76 private Vector data; 77 private GenericPreparedStatement currentPs; 78 private boolean wasNull; 79 80 87 public static void emptyCache() throws SQLException { 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 { 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 stmtCache = (java.util.Hashtable ) lcc.statementCache; 103 data = new Vector (stmtCache.size()); 104 for (Enumeration 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 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 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 return true; 165 case 5: 166 return currentPs.isValid(); 167 default: 168 return false; 169 } 170 } 171 172 public Timestamp getTimestamp(int colId) { 173 174 Timestamp ts = currentPs.getEndCompileTimestamp(); 175 wasNull = (ts == null); 176 return ts; 177 } 178 179 public boolean wasNull() { 180 return wasNull; 181 } 182 183 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 |