Skip to content

Commit

Permalink
moved queries to strings
Browse files Browse the repository at this point in the history
  • Loading branch information
AndyThomas-Dev committed May 11, 2020
1 parent 9d647c6 commit 3e185cd
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 30 deletions.
51 changes: 21 additions & 30 deletions src/uk/ac/bris/cs/databases/cwk2/API.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,27 +121,10 @@ public Result addNewPerson(String name, String username, String studentId) {
return Result.failure(validation);
}

// if (studentId != null && studentId.equals("")) {
// return Result.failure("StudentId can be null, but cannot be the empty string.");
// }
//
// if (name == null || name.equals("")) {
// return Result.failure("Name cannot be empty.");
// }
// if (username == null || username.equals("")) {
// return Result.failure("Username cannot be empty.");
// }
// if (name.length() > getMaxNameLength()) {
// return Result.failure("Name is too long. Max length a hundred characters.");
// }
//
// if (username.length() > getMaxUsernameSize()) {
// return Result.failure("Username is too long. Max length ten characters.");
// }
String query = "SELECT count(1) AS c FROM Person WHERE username = ?";

try (PreparedStatement p = c.prepareStatement(
"SELECT count(1) AS c FROM Person WHERE username = ?"
)) {
try (PreparedStatement p = c.prepareStatement(query))
{
p.setString(1, username);
ResultSet r = p.executeQuery();

Expand All @@ -152,9 +135,9 @@ public Result addNewPerson(String name, String username, String studentId) {
return Result.fatal(e.getMessage());
}

try (PreparedStatement p = c.prepareStatement(
"INSERT INTO Person (name, username, stuId) VALUES (?, ?, ?)"
)) {
query = "INSERT INTO Person (name, username, stuId) VALUES (?, ?, ?)";

try (PreparedStatement p = c.prepareStatement(query)) {
p.setString(1, name);
p.setString(2, username);
p.setString(3, studentId);
Expand Down Expand Up @@ -182,8 +165,10 @@ public Result<PersonView> getPersonView(String username) {
return Result.failure("Username cannot be empty.");
}

String query = "SELECT * FROM Person WHERE username = '" + username + "'";

try (Statement s = c.createStatement()) {
ResultSet r = s.executeQuery("SELECT * FROM Person WHERE username = '" + username + "'");
ResultSet r = s.executeQuery(query);

List<PersonView> data = new ArrayList<PersonView>();

Expand All @@ -206,9 +191,10 @@ public Result<PersonView> getPersonView(String username) {
@Override
public Result<List<ForumSummaryView>> getForums() {

try (Statement s = c.createStatement()) {
ResultSet r = s.executeQuery("SELECT id, title FROM Forum");
String query = "SELECT id, title FROM Forum";

try (Statement s = c.createStatement()) {
ResultSet r = s.executeQuery(query);
List<ForumSummaryView> data = new ArrayList<ForumSummaryView>();

while (r.next()) {
Expand Down Expand Up @@ -258,11 +244,13 @@ public Result<TopicView> getTopic(int topicId) {

List<SimplePostView> posts = new ArrayList<SimplePostView>();

String query = "SELECT Post.username, Post.postedAt, Post.text, Posts_In_Topic.topicid FROM Post \n" +
"JOIN Posts_In_Topic ON Posts_In_Topic.postid = Post.id\n" +
"WHERE Posts_In_Topic.topicid = " + topicId;

try (Statement s = c.createStatement()) {

ResultSet r = s.executeQuery("SELECT Post.username, Post.postedAt, Post.text, Posts_In_Topic.topicid FROM Post \n" +
"JOIN Posts_In_Topic ON Posts_In_Topic.postid = Post.id\n" +
"WHERE Posts_In_Topic.topicid = " + topicId);
ResultSet r = s.executeQuery(query);

int counter = 1;

Expand All @@ -277,9 +265,12 @@ public Result<TopicView> getTopic(int topicId) {
return Result.fatal("database error - " + ex.getMessage());
}

query = "SELECT id, title FROM Topic WHERE id = " + topicId;

try (Statement s = c.createStatement()) {

// Determines link to create new post
ResultSet r = s.executeQuery("SELECT id, title FROM Topic WHERE id = " + topicId);
ResultSet r = s.executeQuery(query);

List<TopicView> data = new ArrayList<TopicView>();

Expand Down
Binary file modified target/uk/ac/bris/cs/databases/cwk2/API.class
Binary file not shown.

0 comments on commit 3e185cd

Please sign in to comment.