001: /*
002: * Created on Oct 16, 2004
003: *
004: * TODO To change the template for this generated file go to
005: * Window - Preferences - Java - Code Style - Code Templates
006: */
007: package org.hammurapi.inspectors.metrics;
008:
009: import java.io.FileWriter;
010: import java.io.IOException;
011: import java.io.Writer;
012: import java.sql.PreparedStatement;
013: import java.sql.ResultSet;
014: import java.sql.SQLException;
015:
016: import org.hammurapi.HammurapiException;
017: import org.hammurapi.HammurapiRuntimeException;
018: import org.hammurapi.InspectorContext;
019: import org.hammurapi.results.AnnotationContext;
020: import org.hammurapi.results.AnnotationContext.FileEntry;
021:
022: import com.pavelvlasov.jsel.Parameter;
023: import com.pavelvlasov.jsel.VariableDefinition;
024: import com.pavelvlasov.persistence.CompositeStorage;
025: import com.pavelvlasov.review.SourceMarker;
026: import com.pavelvlasov.sql.JdbcStorage;
027: import com.pavelvlasov.sql.Parameterizer;
028: import com.pavelvlasov.sql.RowProcessor;
029: import com.pavelvlasov.sql.SQLProcessor;
030:
031: /**
032: * @author 111001082
033: *
034: * TODO To change the template for this generated type comment go to
035: * Window - Preferences - Java - Code Style - Code Templates
036: */
037: public class SqlExtractorHyperSonicInMemoryDb extends
038: SqlExtractorHyperSonicDb implements
039: SqlExtractorPersistencyService {
040:
041: private static int counter = 0;
042: /**
043: * Unique table name
044: */
045: private String sqlTableName = "SQL_STRING_LITERALS_"
046: + Long.toString(System.currentTimeMillis(), 32) + "_"
047: + ++counter;
048: protected String varTableName = "VAR_DEF_LITERALS_"
049: + Long.toString(System.currentTimeMillis(), 32) + "_"
050: + ++counter;
051: protected String[] initSQL = {
052: "CREATE CACHED TABLE "
053: + sqlTableName
054: + " (LITERAL VARCHAR(250), SOURCE VARCHAR(250), LINE INTEGER, COL INTEGER, CLASS_NAME VARCHAR(250), CLASS_FCN VARCHAR(250))",
055: "CREATE INDEX IX_" + sqlTableName + " ON " + sqlTableName
056: + " (LITERAL, SOURCE, LINE, COL)",
057:
058: "CREATE CACHED TABLE "
059: + varTableName
060: + " (VAR_NAME VARCHAR(250), VAR_VALUE VARCHAR(250), SOURCE VARCHAR(250), LINE INTEGER, COL INTEGER, CLASS_NAME VARCHAR(250), CLASS_FCN VARCHAR(250))",
061: "CREATE INDEX IX_" + varTableName + " ON " + varTableName
062: + " (VAR_NAME, VAR_VALUE, SOURCE, LINE, COL)"
063:
064: };
065: protected String[] destroySQL = {
066: "CREATE INDEX IX_" + sqlTableName,
067: "DROP TABLE " + sqlTableName,
068:
069: "CREATE INDEX IX_" + varTableName,
070: "DROP TABLE " + varTableName };
071:
072: public SqlExtractorHyperSonicInMemoryDb(InspectorContext _context)
073: throws HammurapiException {
074: super ();
075: context = _context;
076: init();
077: }
078:
079: public void init() throws HammurapiException {
080: // super.init();
081: SQLProcessor processor = getProcessor(null);
082: if (processor == null) {
083: throw new HammurapiException(
084: "Could not obtain SQLProcessor");
085: }
086:
087: for (int i = 0; i < initSQL.length; i++) {
088: try {
089: System.out.println(" init " + initSQL[i]);
090: processor.processUpdate(initSQL[i], null);
091: } catch (SQLException e) {
092: throw new HammurapiException(e);
093: }
094: }
095: }
096:
097: public void saveLanguageElement(final StringVariable strVar) {
098: System.out.println("** saveLanguageElement "
099: + strVar.langElement.toString());
100:
101: final boolean[] ret = { false };
102: SQLProcessor processor = getProcessor((SourceMarker) strVar.langElement);
103: if (processor != null) {
104: try {
105: String templangElementName = "<unresolved>";
106: if (strVar.langElement instanceof VariableDefinition) {
107: templangElementName = ((VariableDefinition) strVar.langElement)
108: .getName();
109: } else if (strVar.langElement instanceof Parameter) {
110: templangElementName = ((Parameter) strVar.langElement)
111: .getName();
112: }
113: final String langElementName = templangElementName;
114: // System.out.println(langElementName + " :- "+ currentStringValue );
115: // check for String Only !
116:
117: //-- is entry already available?
118: processor
119: .processSelect(
120: "SELECT 'variable', SOURCE, LINE, COL, VAR_NAME, VAR_VALUE , CLASS_NAME, CLASS_FCN FROM "
121: + varTableName
122: + " WHERE VAR_NAME = ? "
123: + "AND CLASS_NAME = ?"
124: + "AND CLASS_FCN = ?",
125: (new Parameterizer() {
126: public void parameterize(
127: PreparedStatement ps)
128: throws SQLException {
129: ps
130: .setString(1,
131: langElementName);
132: ps.setString(2,
133: strVar.className);
134: ps
135: .setString(3,
136: strVar.classFcn);
137: }
138: }), new RowProcessor() {
139: public boolean process(ResultSet rs)
140: throws SQLException {
141: try {
142: String tmpStr = rs
143: .getString("VAR_NAME");
144: // System.out.println (" KKKKKKK " + tmpStr + " --"+ currentStringValue );
145:
146: ret[0] = langElementName
147: .equals(tmpStr);
148: return true;
149: } catch (Exception e) {
150: throw new HammurapiRuntimeException(
151: e);
152: }
153: }
154: });
155:
156: //-- update entry
157:
158: if (ret[0]) {
159: // System.out.println (" KKKKKKK UPDATE" );
160: processor.processUpdate(
161:
162: "UPDATE " + varTableName + " SET "
163: + " VAR_VALUE = ?"
164: + " WHERE VAR_NAME = ? "
165: + "AND CLASS_NAME = ?"
166: + "AND CLASS_FCN = ?",
167:
168: new Parameterizer() {
169: public void parameterize(PreparedStatement ps)
170: throws SQLException {
171: ps.setString(1, strVar.varValue.toString());
172: ps.setString(2, langElementName);
173: ps.setString(3, strVar.className);
174: ps.setString(4, strVar.classFcn);
175: }
176: });
177:
178: } else {
179: // System.out.println (" KKKKKKK INSERT" );
180: //-- insert entry
181: processor
182: .processUpdate(
183:
184: "INSERT INTO "
185: + varTableName
186: + " (VAR_NAME, VAR_VALUE, SOURCE, LINE, COL, CLASS_NAME, CLASS_FCN) "
187: + "VALUES (?,?,?,?,?,?,?)",
188: new Parameterizer() {
189: public void parameterize(
190: PreparedStatement ps)
191: throws SQLException {
192: ps.setString(1,
193: langElementName);
194: ps
195: .setString(
196: 2,
197: strVar.varValue
198: .toString());
199: SourceMarker sourceMarker = (SourceMarker) strVar.langElement;
200: ps
201: .setString(
202: 3,
203: sourceMarker
204: .getSourceURL());
205: ps.setInt(4, sourceMarker
206: .getLine());
207: ps.setInt(5, sourceMarker
208: .getColumn());
209: ps.setString(6,
210: strVar.className);
211: ps.setString(7,
212: strVar.classFcn);
213: }
214: });
215: } //fi
216: } catch (SQLException e) {
217: context.warn((SourceMarker) strVar.langElement, e);
218: }
219: }
220:
221: }
222:
223: public void render(AnnotationContext context, String path)
224: throws HammurapiException {
225: final SQLProcessor processor = getProcessor(null);
226:
227: try {
228: FileEntry a = context.getNextFile(".html");
229: path = a.getPath();
230:
231: final Writer w = new FileWriter(a.getFile());
232: try {
233: w
234: .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>");
235: processor
236: .processSelect(
237: "SELECT * FROM ("
238: +
239: // "SELECT 'constant', SOURCE, LINE, COL, '<->', LITERAL, CLASS_NAME, CLASS_FCN FROM "+sqlTableName
240: // +" UNION " +
241: "SELECT 'variable', SOURCE, LINE, COL, VAR_NAME, VAR_VALUE , CLASS_NAME, CLASS_FCN FROM "
242: + varTableName
243: + ") ORDER BY SOURCE, LINE, COL",
244: null, new RowProcessor() {
245: public boolean process(ResultSet rs)
246: throws SQLException {
247: try {
248: w.write("<TR><TD>");
249: // w.write(rs.getString("LITERAL"));
250: w.write(rs.getString(5));
251: w.write("</TD><TD>");
252:
253: w.write(rs.getString(6)); // VAR_VALUE or LITERAL
254: w.write("</TD><TD>");
255: // w.write(rs.getString( 1 ));
256: // w.write("</TD><TD>");
257:
258: w
259: .write(rs
260: .getString("SOURCE"));
261: w
262: .write("</TD><TD aligh=\"right\">");
263: w.write(rs
264: .getString("LINE"));
265: w
266: .write("</TD><TD aligh=\"right\">");
267: w
268: .write(rs
269: .getString("COL"));
270: w.write("</TD><TD>");
271: w
272: .write(rs
273: .getString("CLASS_NAME"));
274: w.write("</TD><TD>");
275: w
276: .write(rs
277: .getString("CLASS_FCN"));
278:
279: w.write("</TD><TR>");
280: return true;
281: } catch (IOException e) {
282: throw new HammurapiRuntimeException(
283: e);
284: }
285: }
286: });
287: /*
288: processor.processSelect(
289: "SELECT * FROM "+varTableName+" ORDER BY SOURCE, LINE, COL, VAR_VALUE, VAR_NAME",
290: null,
291: new RowProcessor() {
292: public boolean process(ResultSet rs) throws SQLException {
293: try {
294: w.write("<TR><TD>");
295: w.write(rs.getString("VAR_VALUE"));
296: w.write(" :- ");
297: w.write(rs.getString("VAR_NAME"));
298: w.write("</TD><TD>");
299: w.write(rs.getString("SOURCE"));
300: w.write("</TD><TD aligh=\"right\">");
301: w.write(rs.getString("LINE"));
302: w.write("</TD><TD aligh=\"right\">");
303: w.write(rs.getString("COL"));
304: w.write("</TD><TR>");
305: return true;
306: } catch (IOException e) {
307: throw new HammurapiRuntimeException(e);
308: }
309: }
310: });
311: */
312: w.write("</TABLE></BODY></HTML>");
313: } finally {
314: w.close();
315: }
316: } catch (SQLException e) {
317: throw new HammurapiException(e);
318: } catch (HammurapiRuntimeException e) {
319: throw new HammurapiException(e.getCause());
320: } catch (IOException e) {
321: throw new HammurapiException(e);
322:
323: } catch (HammurapiException e) {
324: throw new HammurapiException(e);
325: }
326: }
327:
328: public void destroy() {
329: SQLProcessor processor = getProcessor(null);
330: if (processor == null) {
331: System.err.println("Could not obtain SQLProcessor");
332: }
333:
334: for (int i = 0; i < destroySQL.length; i++) {
335: try {
336: processor.processUpdate(initSQL[i], null);
337: } catch (SQLException e) {
338: System.err.println("Could not delete tables");
339: e.printStackTrace();
340: }
341: }
342: }
343: }
|