+
+ Long getVarId(BugInstance bug) throws SQLException, TypeMismatchException
+ {
+ if (null == bug) {
+ return null;
+ }
+
+ List<LocalVariable> vars = bug.getVariables();
+ if ((null == vars) || (0 == vars.size())) {
+ return null;
+ }
+
+ 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) {
+ return null;
+ }
+
+ Long result = findVarId(var);
+
+ if (null != result) {
+ return result;
+ }
+
+ return storeVar(var);
+ }
+
+ Long findVarId(LocalVariable var) throws SQLException, TypeMismatchException
+ {
+ Column[] columns = { CfbSchema.VARID_PK };
+ Table[] tables = { CfbSchema.VARIABLES };
+
+ Condition[] conditions = {
+ new Condition( CfbSchema.NAME, var.getName(), Operation.EQUAL ),
+ new Condition( CfbSchema.VARROLE, var.getRole(), 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 null; // not found
+ }
+
+ Long storeVar(LocalVariable var) throws SQLException
+ {
+ long varId = m_driver.nextVal(m_con, CfbSchema.VARIABLE_SEQ);
+
+ Object[][] values = { {
+ Long.valueOf(varId),
+ var.getName(),
+ var.getRole()
+ } };
+ int count = m_driver.insert(m_con, CfbSchema.VARIABLES, values);
+ if (1 != count) {
+ return null;
+ }
+
+ return Long.valueOf(varId);
+ }
+
+ Long getPriorId(Analysis analysis) throws SQLException, TypeMismatchException
+ {
+ Column[] columns = { CfbSchema.RUNID };
+ Table[] tables = { CfbSchema.RUNS };
+ 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;
+
+ List<Row> rows = m_driver.select(m_con, columns, tables, conditions, sorts, limit);
+ if (rows.size() < 1) {
+ return null;
+ }
+ return rows.get(0).getLong(0);
+ }
+
+ Analysis getAnalysis(Long analysisId) throws SQLException, TypeMismatchException
+ {
+ Column[] columns = { CfbSchema.PROJNAME, CfbSchema.VERSION, CfbSchema.STARTTIME, CfbSchema.ENDTIME };
+ Table[] tables = { CfbSchema.RUNS };
+ Condition[] conditions = { new Condition( CfbSchema.RUNID, analysisId, Operation.EQUAL ) };
+
+ List<Row> rows = m_driver.select(m_con, columns, tables, conditions);
+ if (rows.size() < 1) {
+ return null;
+ }
+
+ Row row = rows.get(0);
+
+ 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(projName, version);
+ prior.setId(analysisId.longValue());
+ prior.setStart(start);
+ prior.setEnd(end);
+
+ prior.setBugCollection(getBugCollection(analysisId));
+
+ return prior;
+ }
+
+ BugCollection getBugCollection(Long runId) throws SQLException, TypeMismatchException
+ {
+ 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;
+ }