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.Connection;
013: import java.sql.DriverManager;
014: import java.sql.PreparedStatement;
015: import java.sql.ResultSet;
016: import java.sql.SQLException;
017: import java.sql.Statement;
018:
019: import org.hammurapi.HammurapiException;
020: import org.hammurapi.HammurapiRuntimeException;
021: import org.hammurapi.InspectorContext;
022: import org.hammurapi.results.AnnotationContext;
023: import org.hammurapi.results.AnnotationContext.FileEntry;
024:
025: import com.pavelvlasov.jsel.Parameter;
026: import com.pavelvlasov.jsel.VariableDefinition;
027: import com.pavelvlasov.review.SourceMarker;
028: import com.pavelvlasov.sql.Parameterizer;
029: import com.pavelvlasov.sql.RowProcessor;
030: import com.pavelvlasov.sql.SQLProcessor;
031:
032: /**
033: * @author 111001082
034: *
035: * TODO To change the template for this generated type comment go to
036: * Window - Preferences - Java - Code Style - Code Templates
037: */
038: public class SqlExtractorHyperSonicStanaloneServer extends
039: SqlExtractorHyperSonicDb implements
040: SqlExtractorPersistencyService {
041:
042: private static int counter = 0;
043: private Connection c = null;
044: /**
045: * Unique table name
046: */
047: private String sqlTableName = "SQL_STRING_LITERALS";
048: protected String varTableName = "VAR_DEF_LITERALS";
049: protected String[] initSQL = {
050: "CREATE CACHED TABLE "
051: + sqlTableName
052: + " (LITERAL VARCHAR(250), SOURCE VARCHAR(250), LINE INTEGER, COL INTEGER, CLASS_NAME VARCHAR(250), CLASS_FCN VARCHAR(250))",
053: "CREATE INDEX IX_" + sqlTableName + " ON " + sqlTableName
054: + " (LITERAL, SOURCE, LINE, COL)",
055:
056: "CREATE CACHED TABLE "
057: + varTableName
058: + " (VAR_NAME VARCHAR(250), VAR_VALUE VARCHAR(250), SOURCE VARCHAR(250), LINE INTEGER, COL INTEGER, CLASS_NAME VARCHAR(250), CLASS_FCN VARCHAR(250))",
059: "CREATE INDEX IX_" + varTableName + " ON " + varTableName
060: + " (VAR_NAME, VAR_VALUE, SOURCE, LINE, COL)"
061:
062: };
063: protected String[] destroySQL = {
064: "CREATE INDEX IX_" + sqlTableName,
065: "DELETE * FROM " + sqlTableName,
066:
067: "CREATE INDEX IX_" + varTableName,
068: "DELETE * FROM " + varTableName };
069:
070: public SqlExtractorHyperSonicStanaloneServer(
071: InspectorContext _context) throws HammurapiException {
072: super ();
073: context = _context;
074: init();
075: }
076:
077: public void init() throws HammurapiException {
078: // super.init();
079:
080: try {
081: Class.forName("org.hsqldb.jdbcDriver");
082: // Connection c =
083: // DriverManager.getConnection("jdbc:hsqldb:file:D:/anwend/java/hsqldb/data/myjobdb",
084: // "sa", "");
085: c = DriverManager.getConnection(
086: "jdbc:hsqldb:hsql://localhost/job", "sa", "");
087: } catch (Exception e) {
088: // TODO Auto-generated catch block
089: e.printStackTrace();
090: throw new HammurapiException(e);
091: }
092: }
093:
094: /*
095: * (non-Javadoc)
096: *
097: * @see org.hammurapi.inspectors.metrics.SqlExtractorPersistencyService#saveLanguageElement(org.hammurapi.inspectors.metrics.StringVariable)
098: */
099: public void saveLanguageElement(StringVariable strVar) {
100: System.out.println("** saveLanguageElement "
101: + strVar.langElement.toString());
102:
103: final boolean[] ret = { false };
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:
119: SourceMarker sourceMarker = (SourceMarker) strVar.langElement;
120:
121: String sqlQuery = "SELECT 'variable', SOURCE, LINE, COL, VAR_NAME, VAR_VALUE , CLASS_NAME, CLASS_FCN FROM "
122: + varTableName
123: + " WHERE VAR_NAME = '"
124: + langElementName
125: + "'"
126: + "AND CLASS_NAME = '"
127: + strVar.className
128: + "'"
129: + "AND CLASS_FCN = '"
130: + strVar.classFcn + "'";
131:
132: Statement stmt = c.createStatement();
133: ResultSet rs = stmt.executeQuery(sqlQuery);
134:
135: try {
136: String tmpStr = rs.getString("VAR_NAME");
137: ret[0] = langElementName.equals(tmpStr);
138: } catch (Exception e1) {
139: // TODO Auto-generated catch block
140: // e1.printStackTrace();
141: }
142:
143: rs.close();
144: stmt.close();
145: //-- update entry
146:
147: stmt = c.createStatement();
148: if (ret[0]) {
149: sqlQuery = "UPDATE " + varTableName + " SET "
150: + " VAR_VALUE = '"
151: + strVar.varValue.toString() + "'"
152: + " WHERE VAR_NAME = '" + langElementName + "'"
153: + "AND CLASS_NAME = '" + strVar.className + "'"
154: + "AND CLASS_FCN = '" + strVar.classFcn + "'";
155: stmt.executeQuery(sqlQuery);
156: stmt.close();
157:
158: } else {
159: // System.out.println (" KKKKKKK INSERT" );
160: //-- insert entry
161: sqlQuery = "INSERT INTO "
162: + varTableName
163: + " (VAR_NAME, VAR_VALUE, SOURCE, LINE, COL, CLASS_NAME, CLASS_FCN) "
164: + "VALUES (" + "'" + langElementName + "',"
165: + "'" + strVar.varValue.toString() + "',"
166:
167: + "'" + sourceMarker.getSourceURL() + "',"
168: + "'" + sourceMarker.getLine() + "'," + "'"
169: + sourceMarker.getColumn() + "'," + "'"
170: + strVar.className + "'," + "'"
171: + strVar.classFcn + "')";
172: stmt.executeQuery(sqlQuery);
173: stmt.close();
174:
175: } //fi
176: } catch (SQLException e) {
177: context.warn((SourceMarker) strVar.langElement, e);
178: }
179: }
180:
181: /* (non-Javadoc)
182: * @see org.hammurapi.inspectors.metrics.SqlExtractorPersistencyService#render(org.hammurapi.results.AnnotationContext, java.lang.String)
183: */
184: public void render(AnnotationContext context, String path)
185: throws HammurapiException {
186: try {
187: FileEntry a = context.getNextFile(".html");
188: path = a.getPath();
189:
190: final Writer w = new FileWriter(a.getFile());
191: try {
192: w
193: .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>");
194: Statement stmt = c.createStatement();
195: String sqlString = "SELECT * FROM ("
196: + "SELECT 'variable', SOURCE, LINE, COL, VAR_NAME, VAR_VALUE , CLASS_NAME, CLASS_FCN FROM "
197: + varTableName + ") ORDER BY SOURCE, LINE, COL";
198: ResultSet rs = stmt.executeQuery(sqlString);
199:
200: while (rs.next()) {
201: try {
202: w.write("<TR><TD>");
203: // w.write(rs.getString("LITERAL"));
204: w.write(rs.getString(5));
205: w.write("</TD><TD>");
206:
207: w.write(rs.getString(6)); // VAR_VALUE or LITERAL
208: w.write("</TD><TD>");
209: // w.write(rs.getString( 1 ));
210: // w.write("</TD><TD>");
211:
212: w.write(rs.getString("SOURCE"));
213: w.write("</TD><TD aligh=\"right\">");
214: w.write(rs.getString("LINE"));
215: w.write("</TD><TD aligh=\"right\">");
216: w.write(rs.getString("COL"));
217: w.write("</TD><TD>");
218: w.write(rs.getString("CLASS_NAME"));
219: w.write("</TD><TD>");
220: w.write(rs.getString("CLASS_FCN"));
221:
222: w.write("</TD><TR>");
223:
224: } catch (IOException e) {
225: throw new HammurapiRuntimeException(e);
226: }
227: }
228:
229: ;
230: w.write("</TABLE></BODY></HTML>");
231: } finally {
232: w.close();
233: }
234: } catch (SQLException e) {
235: throw new HammurapiException(e);
236: } catch (HammurapiRuntimeException e) {
237: throw new HammurapiException(e.getCause());
238: } catch (IOException e) {
239: throw new HammurapiException(e);
240:
241: } catch (HammurapiException e) {
242: throw new HammurapiException(e);
243: }
244:
245: }
246:
247: public void destroy() {
248: try {
249: Statement stmt = c.createStatement();
250: for (int i = 0; i < destroySQL.length; i++) {
251: try {
252: ResultSet rs = stmt.executeQuery(destroySQL[i]);
253:
254: } catch (SQLException e) {
255: System.err.println("Could not delete tables");
256: e.printStackTrace();
257: }
258: }
259: } catch (SQLException e) {
260: System.err.println("Could not delete tables");
261: e.printStackTrace();
262: }
263:
264: }
265:
266: }
|