1 package net.jaekl.cfb.store;
3 import java.sql.Connection;
4 import java.sql.SQLException;
5 import java.util.ArrayList;
8 import net.jaekl.cfb.analyze.Analysis;
9 import net.jaekl.cfb.db.CfbSchema;
10 import net.jaekl.cfb.db.Column;
11 import net.jaekl.cfb.db.Condition;
12 import net.jaekl.cfb.db.Operation;
13 import net.jaekl.cfb.db.Row;
14 import net.jaekl.cfb.db.Table;
15 import net.jaekl.cfb.db.TypeMismatchException;
16 import net.jaekl.cfb.db.driver.DbDriver;
17 import net.jaekl.cfb.xml.BugClass;
18 import net.jaekl.cfb.xml.BugInstance;
19 import net.jaekl.cfb.xml.BugMethod;
20 import net.jaekl.cfb.xml.SourceLine;
21 import net.jaekl.cfb.xml.messages.MessageCollection;
23 public class DbStore {
26 MessageCollection m_msgColl;
28 public DbStore(Connection con, DbDriver driver, MessageCollection msgColl) {
34 public boolean put(Analysis analysis) throws SQLException, TypeMismatchException {
35 if (null == analysis) {
39 // ----------------------------------
40 // Add a run record for this analysis
42 long runId = m_driver.nextVal(m_con, CfbSchema.RUN_SEQ);
46 analysis.getBuildNumber(),
51 int count = m_driver.insert(m_con, CfbSchema.RUNS, values);
56 // -------------------------------------
57 // Add a found record for each bug found
59 List<BugInstance> bugs = analysis.getBugCollection().getBugs();
60 values = new Object[bugs.size()][CfbSchema.FOUND.getNumColumns()];
63 for (BugInstance bug : bugs)
65 Long foundId = Long.valueOf(m_driver.nextVal(m_con, CfbSchema.FOUND_SEQ));
66 Long bugId = Long.valueOf(m_msgColl.getPattern(bug.getType()).getId());
67 Long categoryId = Long.valueOf(m_msgColl.getCategory(bug.getCategory()).getId());
68 Location[] locs = computeLocations(bug);
69 Location firstLoc = (locs.length > 0) ? locs[0] : null;
70 Location secondLoc = (locs.length > 1) ? locs[1] : null;
71 Location thirdLoc = (locs.length > 2) ? locs[2] : null;
73 values[row][0] = foundId;
74 values[row][1] = bugId;
75 values[row][2] = categoryId;
76 values[row][3] = getLocId(firstLoc);
77 values[row][4] = getLocId(secondLoc);
78 values[row][5] = getLocId(thirdLoc);
82 count = m_driver.insert(m_con, CfbSchema.FOUND, values);
83 return (bugs.size() == count);
86 Location[] computeLocations(BugInstance bug)
88 ArrayList<Location> locs = new ArrayList<Location>();
90 for (SourceLine line : bug.getLines()) {
91 locs.add(new Location(line));
93 for (BugMethod method : bug.getMethods()) {
94 locs.add(new Location(method));
96 for (BugClass clazz : bug.getClasses()) {
97 locs.add(new Location(clazz));
100 return locs.toArray(new Location[locs.size()]);
103 Long getLocId(Location loc) throws SQLException, TypeMismatchException
108 Long locId = findLocId(loc);
113 return storeLoc(loc);
116 Long findLocId(Location loc) throws SQLException, TypeMismatchException
118 Column[] columns = { CfbSchema.LOCID };
119 Table[] tables = { CfbSchema.LOCATIONS };
120 Condition[] conditions = {
121 new Condition( CfbSchema.CLASSNAME, loc.getClassName(), Operation.EQUAL ),
122 new Condition( CfbSchema.METHODNAME, loc.getMethodName(), Operation.EQUAL ),
123 new Condition( CfbSchema.STARTLINE, loc.getStart(), Operation.EQUAL ),
124 new Condition( CfbSchema.ENDLINE, loc.getEnd(), Operation.EQUAL )
126 List<Row> rows = m_driver.select(m_con, columns, tables, conditions);
127 if (rows.size() > 0) {
128 assert(1 == rows.size()); // should only have one match
130 return rows.get(0).getLong(0);
133 return null; // not found
136 Long storeLoc(Location loc) throws SQLException
138 long locId = m_driver.nextVal(m_con, CfbSchema.LOC_SEQ);
140 Object[][] values = { {
144 Long.valueOf(loc.getStart()),
145 Long.valueOf(loc.getEnd())
147 m_driver.insert(m_con, CfbSchema.LOCATIONS, values);
149 return Long.valueOf(locId);