Some progress toward implementing store(Analysis).
[cfb.git] / prod / net / jaekl / cfb / store / DbStore.java
1 package net.jaekl.cfb.store;
2
3 import java.sql.Connection;
4 import java.sql.SQLException;
5 import java.util.List;
6
7 import net.jaekl.cfb.analyze.Analysis;
8 import net.jaekl.cfb.db.CfbSchema;
9 import net.jaekl.cfb.db.Column;
10 import net.jaekl.cfb.db.Condition;
11 import net.jaekl.cfb.db.Operation;
12 import net.jaekl.cfb.db.Row;
13 import net.jaekl.cfb.db.Table;
14 import net.jaekl.cfb.db.TypeMismatchException;
15 import net.jaekl.cfb.db.driver.DbDriver;
16 import net.jaekl.cfb.xml.BugInstance;
17 import net.jaekl.cfb.xml.SourceLine;
18 import net.jaekl.cfb.xml.messages.MessageCollection;
19
20 public class DbStore {
21         Connection m_con;
22         DbDriver m_driver;
23         MessageCollection m_msgColl;
24         
25         public DbStore(Connection con, DbDriver driver, MessageCollection msgColl) {
26                 m_con = con;
27                 m_driver = driver;
28                 m_msgColl = msgColl;
29         }
30         
31         public boolean put(Analysis analysis) throws SQLException {
32                 if (null == analysis) {
33                         return false;
34                 }
35                 
36                 // ----------------------------------
37                 // Add a run record for this analysis
38                 
39                 long runId = m_driver.nextVal(m_con, CfbSchema.RUN_SEQ);
40                 Object[][] values = { 
41                                                                 {
42                                                                         Long.valueOf(runId),
43                                                                         analysis.getBuildNumber(),
44                                                                         analysis.getStart(),
45                                                                         analysis.getEnd() 
46                                                                 } 
47                                                         };
48                 int count = m_driver.insert(m_con, CfbSchema.RUNS, values);
49                 if (1 != count) {
50                         return false;
51                 }
52                 
53                 // -------------------------------------
54                 // Add a found record for each bug found
55                 
56                 List<BugInstance> bugs = analysis.getBugCollection().getBugs();
57                 values = new Object[bugs.size()][CfbSchema.FOUND.getNumColumns()];
58                 
59                 int row = 0;
60                 for (BugInstance bug : bugs)
61                 {
62                         long foundId = m_driver.nextVal(m_con, CfbSchema.FOUND_SEQ);
63                         long bugId = m_msgColl.getPattern(bug.getType()).getId();
64                         // values[row] = { foundId, bugId, firstLocId, secondLocId, thirdLocId }; 
65                         row++;
66                 }
67                 
68                 return true;
69         }
70         
71         Location[] computeLocations(BugInstance bug)
72         {
73                 throw new UnsupportedOperationException("UNIMPLEMENTED");
74         }
75         
76         long getLocId(SourceLine loc) throws SQLException, TypeMismatchException 
77         {
78                 long locId = findLocId(loc);
79                 if (locId >= 0) {
80                         return locId;
81                 }
82
83                 return storeLoc(loc);
84         }
85         
86         long findLocId(SourceLine loc) throws SQLException, TypeMismatchException
87         {
88                 Column[] columns = { CfbSchema.LOCATIONS.getColumn(CfbSchema.LOCID) };
89                 Table[] tables = { CfbSchema.LOCATIONS };
90                 Condition[] conditions = { 
91                                                 new Condition( CfbSchema.LOCATIONS.getColumn(CfbSchema.CLASSNAME),
92                                                                loc.getClassName(),
93                                                                Operation.EQUAL 
94                                                              ),
95                                                 new Condition( CfbSchema.LOCATIONS.getColumn(CfbSchema.STARTLINE),
96                                                                        loc.getStart(),
97                                                                        Operation.EQUAL
98                                                                      ),
99                                                 new Condition( CfbSchema.LOCATIONS.getColumn(CfbSchema.ENDLINE),
100                                                                        loc.getEnd(),
101                                                                        Operation.EQUAL
102                                                                      )
103                                         };
104                 List<Row> rows = m_driver.select(m_con, columns, tables, conditions);
105                 if (rows.size() > 0) {
106                         assert(1 == rows.size());       // should only have one match
107                         
108                         return rows.get(0).getLong(0);
109                 }
110                 
111                 return (-1);    // not found
112         }
113         
114         long storeLoc(SourceLine loc) throws SQLException
115         {
116                 long locId = m_driver.nextVal(m_con, CfbSchema.LOC_SEQ);
117                 
118                 Object[][] values = { { 
119                                                         locId,
120                                                         loc.getClassName(),
121                                                         Long.valueOf(loc.getStart()),
122                                                         Long.valueOf(loc.getEnd())
123                                                 } };
124                 m_driver.insert(m_con, CfbSchema.LOCATIONS, values);
125                 
126                 return locId;
127         }
128 }