package net.jaekl.cfb.store;
import java.sql.Connection;
+import java.sql.SQLException;
+import java.util.List;
import net.jaekl.cfb.analyze.Analysis;
+import net.jaekl.cfb.db.CfbSchema;
+import net.jaekl.cfb.db.Column;
+import net.jaekl.cfb.db.Condition;
+import net.jaekl.cfb.db.Operation;
+import net.jaekl.cfb.db.Row;
+import net.jaekl.cfb.db.Table;
+import net.jaekl.cfb.db.TypeMismatchException;
+import net.jaekl.cfb.db.driver.DbDriver;
+import net.jaekl.cfb.xml.BugInstance;
+import net.jaekl.cfb.xml.SourceLine;
+import net.jaekl.cfb.xml.messages.MessageCollection;
public class DbStore {
- Connection m_conn;
+ Connection m_con;
+ DbDriver m_driver;
+ MessageCollection m_msgColl;
- public DbStore(Connection conn) {
- m_conn = conn;
+ public DbStore(Connection con, DbDriver driver, MessageCollection msgColl) {
+ m_con = con;
+ m_driver = driver;
+ m_msgColl = msgColl;
}
- public boolean put(Analysis analysis) {
+ public boolean put(Analysis analysis) throws SQLException {
if (null == analysis) {
return false;
}
+ // ----------------------------------
+ // Add a run record for this analysis
+ long runId = m_driver.nextVal(m_con, CfbSchema.RUN_SEQ);
+ Object[][] values = {
+ {
+ Long.valueOf(runId),
+ analysis.getBuildNumber(),
+ analysis.getStart(),
+ analysis.getEnd()
+ }
+ };
+ int count = m_driver.insert(m_con, CfbSchema.RUNS, values);
+ if (1 != count) {
+ return false;
+ }
+
+ // -------------------------------------
+ // Add a found record for each bug found
+
+ List<BugInstance> bugs = analysis.getBugCollection().getBugs();
+ values = new Object[bugs.size()][CfbSchema.FOUND.getNumColumns()];
+
+ int row = 0;
+ for (BugInstance bug : bugs)
+ {
+ long foundId = m_driver.nextVal(m_con, CfbSchema.FOUND_SEQ);
+ long bugId = m_msgColl.getPattern(bug.getType()).getId();
+ // values[row] = { foundId, bugId, firstLocId, secondLocId, thirdLocId };
+ row++;
+ }
return true;
}
+
+ Location[] computeLocations(BugInstance bug)
+ {
+ throw new UnsupportedOperationException("UNIMPLEMENTED");
+ }
+
+ long getLocId(SourceLine loc) throws SQLException, TypeMismatchException
+ {
+ long locId = findLocId(loc);
+ if (locId >= 0) {
+ return locId;
+ }
+
+ return storeLoc(loc);
+ }
+
+ long findLocId(SourceLine loc) throws SQLException, TypeMismatchException
+ {
+ Column[] columns = { CfbSchema.LOCATIONS.getColumn(CfbSchema.LOCID) };
+ Table[] tables = { CfbSchema.LOCATIONS };
+ Condition[] conditions = {
+ new Condition( CfbSchema.LOCATIONS.getColumn(CfbSchema.CLASSNAME),
+ loc.getClassName(),
+ Operation.EQUAL
+ ),
+ new Condition( CfbSchema.LOCATIONS.getColumn(CfbSchema.STARTLINE),
+ loc.getStart(),
+ Operation.EQUAL
+ ),
+ new Condition( CfbSchema.LOCATIONS.getColumn(CfbSchema.ENDLINE),
+ loc.getEnd(),
+ Operation.EQUAL
+ )
+ };
+ List<Row> rows = m_driver.select(m_con, columns, tables, conditions);
+ if (rows.size() > 0) {
+ assert(1 == rows.size()); // should only have one match
+
+ return rows.get(0).getLong(0);
+ }
+
+ return (-1); // not found
+ }
+
+ long storeLoc(SourceLine loc) throws SQLException
+ {
+ long locId = m_driver.nextVal(m_con, CfbSchema.LOC_SEQ);
+
+ Object[][] values = { {
+ locId,
+ loc.getClassName(),
+ Long.valueOf(loc.getStart()),
+ Long.valueOf(loc.getEnd())
+ } };
+ m_driver.insert(m_con, CfbSchema.LOCATIONS, values);
+
+ return locId;
+ }
}