1 7 package org.hammurapi.inspectors.metrics; 8 9 import java.io.FileWriter ; 10 import java.io.IOException ; 11 import java.io.Writer ; 12 import java.sql.PreparedStatement ; 13 import java.sql.ResultSet ; 14 import java.sql.SQLException ; 15 16 import org.hammurapi.HammurapiException; 17 import org.hammurapi.HammurapiRuntimeException; 18 import org.hammurapi.InspectorContext; 19 import org.hammurapi.results.AnnotationContext; 20 import org.hammurapi.results.AnnotationContext.FileEntry; 21 22 import com.pavelvlasov.jsel.Parameter; 23 import com.pavelvlasov.jsel.VariableDefinition; 24 import com.pavelvlasov.persistence.CompositeStorage; 25 import com.pavelvlasov.review.SourceMarker; 26 import com.pavelvlasov.sql.JdbcStorage; 27 import com.pavelvlasov.sql.Parameterizer; 28 import com.pavelvlasov.sql.RowProcessor; 29 import com.pavelvlasov.sql.SQLProcessor; 30 31 37 public class SqlExtractorHyperSonicInMemoryDb 38 extends SqlExtractorHyperSonicDb 39 implements SqlExtractorPersistencyService { 40 41 private static int counter = 0; 42 45 private String sqlTableName = "SQL_STRING_LITERALS_"+Long.toString(System.currentTimeMillis(),32)+"_"+ ++counter; 46 protected String varTableName = "VAR_DEF_LITERALS_"+Long.toString(System.currentTimeMillis(),32)+"_"+ ++counter; 47 protected String [] initSQL = { 48 "CREATE CACHED TABLE "+sqlTableName+ 49 " (LITERAL VARCHAR(250), SOURCE VARCHAR(250), LINE INTEGER, COL INTEGER, CLASS_NAME VARCHAR(250), CLASS_FCN VARCHAR(250))", 50 "CREATE INDEX IX_"+sqlTableName+" ON "+sqlTableName+" (LITERAL, SOURCE, LINE, COL)" , 51 52 "CREATE CACHED TABLE "+varTableName+ 53 " (VAR_NAME VARCHAR(250), VAR_VALUE VARCHAR(250), SOURCE VARCHAR(250), LINE INTEGER, COL INTEGER, CLASS_NAME VARCHAR(250), CLASS_FCN VARCHAR(250))", 54 "CREATE INDEX IX_"+varTableName+" ON "+varTableName+" (VAR_NAME, VAR_VALUE, SOURCE, LINE, COL)" 55 56 }; 57 protected String [] destroySQL = { 58 "CREATE INDEX IX_"+sqlTableName, 59 "DROP TABLE "+sqlTableName, 60 61 "CREATE INDEX IX_"+varTableName, 62 "DROP TABLE "+varTableName 63 }; 64 65 public SqlExtractorHyperSonicInMemoryDb(InspectorContext _context) 66 throws HammurapiException { 67 super(); 68 context = _context; 69 init(); 70 } 71 72 public void init() throws HammurapiException { 73 SQLProcessor processor=getProcessor(null); 75 if (processor==null) { 76 throw new HammurapiException("Could not obtain SQLProcessor"); 77 } 78 79 for (int i=0; i<initSQL.length; i++) { 80 try { 81 System.out.println(" init " + initSQL[i]); 82 processor.processUpdate(initSQL[i], null); 83 } catch (SQLException e) { 84 throw new HammurapiException(e); 85 } 86 } 87 } 88 89 public void saveLanguageElement(final StringVariable strVar ) { 90 System.out.println("** saveLanguageElement "+ strVar.langElement.toString() ); 91 92 93 final boolean[] ret={false}; 94 SQLProcessor processor = getProcessor((SourceMarker) strVar.langElement); 95 if (processor != null) { 96 try { 97 String templangElementName = "<unresolved>"; 98 if( strVar.langElement instanceof VariableDefinition) { 99 templangElementName = ((VariableDefinition) strVar.langElement).getName(); 100 } else if ( strVar.langElement instanceof Parameter ) { 101 templangElementName = ((Parameter) strVar.langElement).getName(); 102 } 103 final String langElementName = templangElementName; 104 107 processor.processSelect( 109 "SELECT 'variable', SOURCE, LINE, COL, VAR_NAME, VAR_VALUE , CLASS_NAME, CLASS_FCN FROM "+varTableName 110 + " WHERE VAR_NAME = ? " + 111 "AND CLASS_NAME = ?" + 112 "AND CLASS_FCN = ?", 113 (new Parameterizer() { 114 public void parameterize(PreparedStatement ps) 115 throws SQLException { 116 ps.setString(1, langElementName); 117 ps.setString(2, strVar.className); 118 ps.setString(3, strVar.classFcn); 119 }}), 120 new RowProcessor() { 121 public boolean process(ResultSet rs) throws SQLException { 122 try { 123 String tmpStr = rs.getString( "VAR_NAME"); 124 126 ret[0] = langElementName.equals(tmpStr) ; 127 return true; 128 } catch (Exception e) { 129 throw new HammurapiRuntimeException(e); 130 } 131 } 132 }); 133 134 135 137 if( ret[0] ){ 138 processor.processUpdate( 140 141 "UPDATE " + varTableName 142 + " SET " 143 + " VAR_VALUE = ?" 144 + " WHERE VAR_NAME = ? " + 145 "AND CLASS_NAME = ?" + 146 "AND CLASS_FCN = ?", 147 148 new Parameterizer() { 149 public void parameterize(PreparedStatement ps) 150 throws SQLException { 151 ps.setString(1, strVar.varValue.toString()); 152 ps.setString(2, langElementName); 153 ps.setString(3, strVar.className); 154 ps.setString(4, strVar.classFcn); 155 } 156 }); 157 158 } else { 159 processor.processUpdate( 162 163 "INSERT INTO " + varTableName 164 + " (VAR_NAME, VAR_VALUE, SOURCE, LINE, COL, CLASS_NAME, CLASS_FCN) " 165 + "VALUES (?,?,?,?,?,?,?)", new Parameterizer() { 166 public void parameterize(PreparedStatement ps) 167 throws SQLException { 168 ps.setString(1, langElementName); 169 ps.setString(2, strVar.varValue.toString()); 170 SourceMarker sourceMarker = (SourceMarker) strVar.langElement; 171 ps.setString(3, sourceMarker.getSourceURL()); 172 ps.setInt(4, sourceMarker.getLine()); 173 ps.setInt(5, sourceMarker.getColumn()); 174 ps.setString(6, strVar.className); 175 ps.setString(7, strVar.classFcn); 176 } 177 }); 178 } } catch (SQLException e) { 180 context.warn((SourceMarker) strVar.langElement, e); 181 } 182 } 183 184 } 185 186 public void render(AnnotationContext context, String path) throws HammurapiException{ 187 final SQLProcessor processor=getProcessor(null); 188 189 try { 190 FileEntry a = context.getNextFile(".html"); 191 path=a.getPath(); 192 193 final Writer w=new FileWriter (a.getFile()); 194 try { 195 w.write("<HTML><BODY><TABLE border=\"1\"><TR><TH>Var Name</TH><TH>Var Value</TH><TH>File</TH><TH>Line</TH><TH>Column</TH><TH>Class</TH><TH>FCN</TH></TR>"); 196 processor.processSelect( 197 "SELECT * FROM (" + 198 "SELECT 'variable', SOURCE, LINE, COL, VAR_NAME, VAR_VALUE , CLASS_NAME, CLASS_FCN FROM "+varTableName 201 + ") ORDER BY SOURCE, LINE, COL", 202 null, 203 new RowProcessor() { 204 public boolean process(ResultSet rs) throws SQLException { 205 try { 206 w.write("<TR><TD>"); 207 w.write(rs.getString( 5 )); 209 w.write("</TD><TD>"); 210 211 w.write(rs.getString( 6 )); w.write("</TD><TD>"); 213 216 w.write(rs.getString("SOURCE")); 217 w.write("</TD><TD aligh=\"right\">"); 218 w.write(rs.getString("LINE")); 219 w.write("</TD><TD aligh=\"right\">"); 220 w.write(rs.getString("COL")); 221 w.write("</TD><TD>"); 222 w.write(rs.getString("CLASS_NAME")); 223 w.write("</TD><TD>"); 224 w.write(rs.getString("CLASS_FCN")); 225 226 w.write("</TD><TR>"); 227 return true; 228 } catch (IOException e) { 229 throw new HammurapiRuntimeException(e); 230 } 231 } 232 }); 233 258 w.write("</TABLE></BODY></HTML>"); 259 } finally { 260 w.close(); 261 } 262 } catch (SQLException e) { 263 throw new HammurapiException(e); 264 } catch (HammurapiRuntimeException e) { 265 throw new HammurapiException(e.getCause()); 266 } catch (IOException e) { 267 throw new HammurapiException(e); 268 269 } catch (HammurapiException e) { 270 throw new HammurapiException(e); 271 } 272 } 273 274 public void destroy() { 275 SQLProcessor processor=getProcessor(null); 276 if (processor==null) { 277 System.err.println("Could not obtain SQLProcessor"); 278 } 279 280 for (int i=0; i<destroySQL.length; i++) { 281 try { 282 processor.processUpdate(initSQL[i], null); 283 } catch (SQLException e) { 284 System.err.println("Could not delete tables"); 285 e.printStackTrace(); 286 } 287 } 288 } 289 } 290 | Popular Tags |