Add the concept of "Project Name" to the RUNS table in the database.
[cfb.git] / prod / net / jaekl / cfb / store / DbStore.java
index fed1f9d9f8b7ab7f8f4700791a8b7b223e09c83e..3860ca5637e5c2fd31f93bec95b1d59900dec6d5 100644 (file)
@@ -2,7 +2,6 @@ package net.jaekl.cfb.store;
 
 import java.sql.Connection;
 import java.sql.SQLException;
-import java.util.ArrayList;
 import java.util.List;
 
 import net.jaekl.cfb.analyze.Analysis;
@@ -15,12 +14,9 @@ import net.jaekl.cfb.db.Sort;
 import net.jaekl.cfb.db.Table;
 import net.jaekl.cfb.db.TypeMismatchException;
 import net.jaekl.cfb.db.driver.DbDriver;
-import net.jaekl.cfb.xml.BugClass;
 import net.jaekl.cfb.xml.BugCollection;
 import net.jaekl.cfb.xml.BugInstance;
-import net.jaekl.cfb.xml.BugMethod;
 import net.jaekl.cfb.xml.LocalVariable;
-import net.jaekl.cfb.xml.SourceLine;
 import net.jaekl.cfb.xml.messages.MessageCollection;
 
 public class DbStore {
@@ -58,6 +54,7 @@ public class DbStore {
                Object[][] values = { 
                                                                {
                                                                        Long.valueOf(runId),
+                                                                       analysis.getProjectName(),
                                                                        analysis.getBuildNumber(),
                                                                        analysis.getStart(),
                                                                        analysis.getEnd() 
@@ -80,18 +77,19 @@ public class DbStore {
                        Long foundId = Long.valueOf(m_driver.nextVal(m_con, CfbSchema.FOUND_SEQ));
                        Long bugId = Long.valueOf(m_msgColl.getPattern(bug.getType()).getId());
                        Long categoryId = Long.valueOf(m_msgColl.getCategory(bug.getCategory()).getId());
-                       Location[] locs = computeLocations(bug);
-                       Location firstLoc  = (locs.length > 0) ? locs[0] : null;
-                       Location secondLoc = (locs.length > 1) ? locs[1] : null;
-                       Location thirdLoc  = (locs.length > 2) ? locs[2] : null;
+                       List<Location> locs = bug.getLocations();
+                       Location firstLoc  = (locs.size() > 0) ? locs.get(0) : null;
+                       Location secondLoc = (locs.size() > 1) ? locs.get(1) : null;
+                       Location thirdLoc  = (locs.size() > 2) ? locs.get(2) : null;
                        
                        values[row][0] = foundId;
-                       values[row][1] = bugId;
-                       values[row][2] = categoryId;
-                       values[row][3] = getLocId(firstLoc);
-                       values[row][4] = getLocId(secondLoc);
-                       values[row][5] = getLocId(thirdLoc);
-                       values[row][6] = getVarId(bug);
+                       values[row][1] = runId;
+                       values[row][2] = bugId;
+                       values[row][3] = categoryId;
+                       values[row][4] = getLocId(firstLoc);
+                       values[row][5] = getLocId(secondLoc);
+                       values[row][6] = getLocId(thirdLoc);
+                       values[row][7] = getVarId(bug);
                        row++;
                }
                
@@ -99,61 +97,47 @@ public class DbStore {
                return (bugs.size() == count);
        }
        
-       Location[] computeLocations(BugInstance bug)
+       String getBugType(Long bugPatternId) throws SQLException, TypeMismatchException
        {
-               ArrayList<Location> locs = new ArrayList<Location>();
-               
-               /*
-                       Somewhat unfortunate special case.
-                       The primary "location" for a bug instance is split between tags.
-                       Most bugs have a pattern like this:
-                       <BugInstance>
-                               ...
-                               <Method>
-                                       <SourceLine .../>
-                               </Method>
-                               ...
-                               <SourceLine .../>
-                       </BugInstance>
-                       
-                       The primary location for a bug is given by the <Method> with no role attribute, 
-                       but the <SourceLine/> inside that method describes the whole range of lines 
-                       covered by that Method, not the spot where the bug is located--that is given 
-                       by the <SourceLine/> that is a direct child fo the <BugInstance/>.
-                */
-               
-               BugMethod primaryMethod = null;
-               SourceLine primaryLine = null;
-               
-               for (BugMethod method : bug.getMethods()) {
-                       if (null != method.getRole()) {
-                               primaryMethod = method;
-                               break;
-                       }
-               }
-               if (bug.getLines().size() > 0) {
-                       primaryLine = bug.getLines().get(0);
+               Column[] columns = { CfbSchema.TYPE };
+               Table[] tables = { CfbSchema.BUGS };
+               Condition[] conditions = { new Condition(CfbSchema.BUGID, bugPatternId, Operation.EQUAL) }; 
+               Row row = m_driver.selectExactlyOne(m_con, columns, tables, conditions);
+               String type = row.getString(0);
+               return type;
+       }
+       
+       String getCategoryName(Long categoryId) throws SQLException, TypeMismatchException
+       {
+               Column[] columns = { CfbSchema.CATEGORY };
+               Table[] tables = { CfbSchema.CATEGORIES };
+               Condition[] conditions = { new Condition(CfbSchema.CATEGORYID, categoryId, Operation.EQUAL) };
+               Row row = m_driver.selectExactlyOne(m_con, columns, tables, conditions);
+               String name = row.getString(0);
+               return name;
+       }
+       
+       
+       Location getLoc(Long locId) throws SQLException, TypeMismatchException
+       {
+               if (null == locId) {
+                       return null;
                }
                
-               if ((null != primaryMethod) && (null != primaryLine)) {
-                       locs.add(new Location(primaryMethod, primaryLine));
-               }
+               Column[] columns = { CfbSchema.CLASSNAME, CfbSchema.METHODNAME, CfbSchema.METHODROLE, CfbSchema.STARTLINE, CfbSchema.ENDLINE };
+               Table[] tables = { CfbSchema.LOCATIONS };
+               Condition[] conditions = { new Condition(CfbSchema.LOCID, locId, Operation.EQUAL) };
                
-               for (BugMethod method : bug.getMethods()) {
-                       if (primaryMethod != method) {
-                               locs.add(new Location(method));                         
-                       }
-               }
-               for (SourceLine line : bug.getLines()) {
-                       if (primaryLine != line) {
-                               locs.add(new Location(line));
-                       }
-               }
-               for (BugClass clazz : bug.getClasses()) {
-                       locs.add(new Location(clazz));
-               }
+               Row row = m_driver.selectExactlyOne(m_con, columns, tables, conditions);
                
-               return locs.toArray(new Location[locs.size()]);
+               String className = row.getString(0);
+               String methodName = row.getString(1);
+               String methodRole = row.getString(2);
+               Integer startLine = row.getInt(3);
+               Integer endLine = row.getInt(4);
+               
+               Location loc = new Location(locId, className, methodName, methodRole, startLine, endLine);
+               return loc;
        }
        
        Long getLocId(Location loc) throws SQLException, TypeMismatchException 
@@ -225,6 +209,30 @@ public class DbStore {
                return getVarId(vars.get(0));
        }
        
+       LocalVariable getVar(Long varId) throws SQLException, TypeMismatchException
+       {
+               if (null == varId) {
+                       return null;
+               }
+               
+               Column[] columns = { CfbSchema.NAME, CfbSchema.VARROLE };
+               Table[] tables = { CfbSchema.VARIABLES };
+               Condition[] conditions = { new Condition(CfbSchema.VARID_PK, varId, Operation.EQUAL) };
+               
+               List<Row> result = m_driver.select(m_con, columns, tables, conditions);
+               if (result.size() < 1) {
+                       throw new SQLException("No variable found for ID " + varId);
+               }
+               if (result.size() > 1) {
+                       throw new SQLException("Too many matches (" + result.size() + ") found for variable ID " + varId);
+               }
+               
+               String varName = result.get(0).getString(0);
+               String varRole = result.get(0).getString(1);
+               
+               return new LocalVariable(varId, varName, varRole);
+       }
+       
        Long getVarId(LocalVariable var) throws SQLException, TypeMismatchException
        {
                if (null == var) {
@@ -280,7 +288,10 @@ public class DbStore {
        {
                Column[] columns = { CfbSchema.RUNID };
                Table[] tables = { CfbSchema.RUNS };
-               Condition[] conditions = { new Condition( CfbSchema.STARTTIME, analysis.getStart(), Operation.LESS_THAN ) };
+               Condition[] conditions = {
+                       new Condition( CfbSchema.PROJNAME, analysis.getProjectName(), Operation.EQUAL ),
+                       new Condition( CfbSchema.STARTTIME, analysis.getStart(), Operation.LESS_THAN ) 
+               };
                Sort[] sorts = { new Sort( CfbSchema.STARTTIME, Sort.Direction.DESCENDING ) };
                int limit = 1;
                
@@ -291,11 +302,11 @@ public class DbStore {
                return rows.get(0).getLong(0);
        }
        
-       Analysis getAnalysis(Long priorId) throws SQLException, TypeMismatchException
+       Analysis getAnalysis(Long analysisId) throws SQLException, TypeMismatchException
        {
-               Column[] columns = { CfbSchema.VERSION, CfbSchema.STARTTIME, CfbSchema.ENDTIME };
+               Column[] columns = { CfbSchema.PROJNAME, CfbSchema.VERSION, CfbSchema.STARTTIME, CfbSchema.ENDTIME };
                Table[] tables = { CfbSchema.RUNS };
-               Condition[] conditions = { new Condition( CfbSchema.RUNID, priorId, Operation.EQUAL ) };
+               Condition[] conditions = { new Condition( CfbSchema.RUNID, analysisId, Operation.EQUAL ) };
                
                List<Row> rows = m_driver.select(m_con, columns, tables, conditions);
                if (rows.size() < 1) {
@@ -304,22 +315,62 @@ public class DbStore {
                
                Row row = rows.get(0);
                
-               String version = row.getString(0);
-               java.util.Date start= row.getDate(1);
-               java.util.Date end = row.getDate(2);
+               String projName = row.getString(0);
+               String version = row.getString(1);
+               java.util.Date start= row.getDate(2);
+               java.util.Date end = row.getDate(3);
                
-               Analysis prior = new Analysis(version);
-               prior.setId(priorId.longValue());
+               Analysis prior = new Analysis(projName, version);
+               prior.setId(analysisId.longValue());
                prior.setStart(start);
                prior.setEnd(end);
                
-               prior.setBugCollection(getBugCollection(priorId));
+               prior.setBugCollection(getBugCollection(analysisId));
                
                return prior;
        }
        
        BugCollection getBugCollection(Long runId) throws SQLException, TypeMismatchException 
        {
-               throw new UnsupportedOperationException("Not yet implemented");
+               Column[] columns = {
+                               CfbSchema.FOUNDID,
+                               CfbSchema.BUGID,
+                               CfbSchema.CATEGORYID,
+                               CfbSchema.FIRSTLOCID,
+                               CfbSchema.SECONDLOCID,
+                               CfbSchema.THIRDLOCID,
+                               CfbSchema.VARID_FK 
+               };
+               Table[] tables = {
+                               CfbSchema.FOUND
+               };
+               Condition[] conditions = {
+                               new Condition(CfbSchema.RUNID, runId, Operation.EQUAL)
+               };
+               
+               BugCollection coll = new BugCollection();
+               
+               List<Row> rows = m_driver.select(m_con, columns, tables, conditions);
+               
+               for (Row row : rows) {
+                       // long foundId = row.getLong(0);
+                       Long bugId = row.getLong(1);
+                       Long categoryId = row.getLong(2);
+                       Long firstLocId = row.getLong(3);
+                       Long secondLocId = row.getLong(4);
+                       Long thirdLocId = row.getLong(5);
+                       Long varId = row.getLong(6);
+                       
+                       String bugType = getBugType(bugId);
+                       String category = getCategoryName(categoryId);
+                       Location[] locations = { getLoc(firstLocId), getLoc(secondLocId), getLoc(thirdLocId) };
+                       LocalVariable[] vars = { getVar(varId) };
+
+                       
+                       BugInstance bug = new BugInstance(bugId, category, bugType, locations, vars);
+                       coll.add(bug);
+               }
+               
+               return coll;
        }
 }