Add found bugs to database.
[cfb.git] / prod / net / jaekl / cfb / store / DbStore.java
index 7591c88d5ef8633ee4cfeafbd3a94b10070528a1..2238568c001bec9d9207419eb36806055a8d0abd 100644 (file)
@@ -2,6 +2,7 @@ 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;
@@ -13,7 +14,9 @@ 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.BugClass;
 import net.jaekl.cfb.xml.BugInstance;
+import net.jaekl.cfb.xml.BugMethod;
 import net.jaekl.cfb.xml.SourceLine;
 import net.jaekl.cfb.xml.messages.MessageCollection;
 
@@ -28,7 +31,7 @@ public class DbStore {
                m_msgColl = msgColl;
        }
        
-       public boolean put(Analysis analysis) throws SQLException {
+       public boolean put(Analysis analysis) throws SQLException, TypeMismatchException {
                if (null == analysis) {
                        return false;
                }
@@ -59,31 +62,58 @@ public class DbStore {
                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 }; 
+                       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;
+                       
+                       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);
                        row++;
                }
                
-               return true;
+               count = m_driver.insert(m_con, CfbSchema.FOUND, values);
+               return (bugs.size() == count);
        }
        
        Location[] computeLocations(BugInstance bug)
        {
-               throw new UnsupportedOperationException("UNIMPLEMENTED");
+               ArrayList<Location> locs = new ArrayList<Location>();
+               
+               for (SourceLine line : bug.getLines()) {
+                       locs.add(new Location(line));
+               }
+               for (BugMethod method : bug.getMethods()) {
+                       locs.add(new Location(method));
+               }
+               for (BugClass clazz : bug.getClasses()) {
+                       locs.add(new Location(clazz));
+               }
+               
+               return locs.toArray(new Location[locs.size()]);
        }
        
-       long getLocId(SourceLine loc) throws SQLException, TypeMismatchException 
+       Long getLocId(Location loc) throws SQLException, TypeMismatchException 
        {
-               long locId = findLocId(loc);
-               if (locId >= 0) {
+               if (null == loc) {
+                       return null;
+               }
+               Long locId = findLocId(loc);
+               if (null != locId) {
                        return locId;
                }
 
                return storeLoc(loc);
        }
        
-       long findLocId(SourceLine loc) throws SQLException, TypeMismatchException
+       Long findLocId(Location loc) throws SQLException, TypeMismatchException
        {
                Column[] columns = { CfbSchema.LOCATIONS.getColumn(CfbSchema.LOCID) };
                Table[] tables = { CfbSchema.LOCATIONS };
@@ -92,6 +122,10 @@ public class DbStore {
                                                               loc.getClassName(),
                                                               Operation.EQUAL 
                                                             ),
+                                               new Condition( CfbSchema.LOCATIONS.getColumn(CfbSchema.METHODNAME),
+                                                                      loc.getMethodName(),
+                                                                      Operation.EQUAL
+                                                                    ),
                                                new Condition( CfbSchema.LOCATIONS.getColumn(CfbSchema.STARTLINE),
                                                                       loc.getStart(),
                                                                       Operation.EQUAL
@@ -108,21 +142,22 @@ public class DbStore {
                        return rows.get(0).getLong(0);
                }
                
-               return (-1);    // not found
+               return null;    // not found
        }
        
-       long storeLoc(SourceLine loc) throws SQLException
+       Long storeLoc(Location loc) throws SQLException
        {
                long locId = m_driver.nextVal(m_con, CfbSchema.LOC_SEQ);
                
                Object[][] values = { { 
-                                                       locId,
+                                                       Long.valueOf(locId),
                                                        loc.getClassName(),
+                                                       loc.getMethodName(),
                                                        Long.valueOf(loc.getStart()),
                                                        Long.valueOf(loc.getEnd())
                                                } };
                m_driver.insert(m_con, CfbSchema.LOCATIONS, values);
                
-               return locId;
+               return Long.valueOf(locId);
        }
 }