Skip to content

Commit

Permalink
#10 - turned queries for commit into lazy eval
Browse files Browse the repository at this point in the history
  • Loading branch information
schadr committed Jun 19, 2012
1 parent c520c31 commit 056a4ff
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 15 deletions.
22 changes: 7 additions & 15 deletions src/db/DbConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;

import org.hamcrest.core.IsSame;

import models.Change;
import models.Commit;
import models.CommitDiff;
Expand Down Expand Up @@ -621,21 +623,11 @@ public String getLastCommit()

public Commit getCommit(String CommitId)
{
try
{
String sql = "SELECT commit_id, author, author_email, comments, commit_date, branch_id FROM Commits where commit_id=? and (branch_id=? or branch_id is NULL);";
String[] parms = {CommitId, this.branchID};
ResultSet rs = execPreparedQuery(sql, parms);
if (!rs.next())
return null;
else
return new Commit(rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getTimestamp(5), rs.getString(6));
}
catch (SQLException e)
{
e.printStackTrace();
return null;
}
String sql = "SELECT commit_id, author, author_email, comments, commit_date, branch_id FROM Commits where commit_id=? and (branch_id=? or branch_id is NULL);";
IPSSetter[] params = {new StringSetter(1,CommitId), new StringSetter(2,this.branchID)};
PreparedStatementExecutionItem ei = new PreparedStatementExecutionItem(sql, params);
this.addExecutionItem(ei);
return new Commit(ei);
}

public void insertOwnerRecord(String CommitId, String Author, String FileId, int ChangeStart, int ChangeEnd, ChangeType changeType)
Expand Down
38 changes: 38 additions & 0 deletions src/models/Commit.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package models;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;

import db.DbConnection.PreparedStatementExecutionItem;

public class Commit {

private int id;
Expand All @@ -12,6 +16,8 @@ public class Commit {
private Timestamp commit_date;

private String branch_id;
private PreparedStatementExecutionItem executionItem;
private boolean wasRetrieved = false;

public Commit() { }

Expand All @@ -38,7 +44,12 @@ public Commit(String commit_id, String author, String author_email,
this.branch_id = branch_id;
}

public Commit(PreparedStatementExecutionItem ei) {
this.executionItem = ei;
}

public String getBranch_id() {
retrieve();
return branch_id;
}

Expand All @@ -47,6 +58,7 @@ public void setBranch_id(String branchId) {
}

public int getId() {
retrieve();
return id;
}

Expand All @@ -55,6 +67,7 @@ public void setId(int id) {
}

public String getCommit_id() {
retrieve();
return commit_id;
}

Expand All @@ -63,6 +76,7 @@ public void setCommit_id(String commit_id) {
}

public String getAuthor() {
retrieve();
return author;
}

Expand All @@ -71,6 +85,7 @@ public void setAuthor(String author) {
}

public String getAuthor_email() {
retrieve();
return author_email;
}

Expand All @@ -79,6 +94,7 @@ public void setAuthor_email(String author_email) {
}

public String getComment() {
retrieve();
return comment;
}

Expand All @@ -87,10 +103,32 @@ public void setComment(String comment) {
}

public Timestamp getCommit_date() {
retrieve();
return commit_date;
}

public void setCommit_date(Timestamp commit_date) {
this.commit_date = commit_date;
}

private void retrieve() {
if (!wasRetrieved ) {
this.executionItem.waitUntilExecuted();
wasRetrieved = true;
ResultSet rs = this.executionItem.getResult();

try {
if (rs.next()) {
this.commit_id = rs.getString("commit_id");
this.author = rs.getString("author");
this.author_email = rs.getString("author_email");
this.comment = rs.getString("comments");
this.commit_date = rs.getTimestamp("commit_date");
this.branch_id = rs.getString("branch_id");
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}

0 comments on commit 056a4ff

Please sign in to comment.