Skip to content

Commit

Permalink
Merge pull request #76 from CSC207-2022F-UofT/database_gateway_depend…
Browse files Browse the repository at this point in the history
…ency_change

[*] Revamped `MySQLDatabaseGateway` and other major modifications to use cases
  • Loading branch information
ScottCTD authored Dec 4, 2022
2 parents c8b9851 + 2a3e2b5 commit 371215a
Show file tree
Hide file tree
Showing 17 changed files with 269 additions and 566 deletions.
205 changes: 106 additions & 99 deletions src/main/java/billgates/database/MySQLDatabaseGateway.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package billgates.database;

import billgates.entities.*;
import billgates.interface_adapters.DatabaseGateway;

import billgates.entities.EntryBuilder;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -119,7 +122,7 @@ public List<QueryUserData> getUserData() {
}

@Override
public QueryBillData getBillData(int billId) {
public List<Entry> getBillData(int billId) {
Instant instantStart = Instant.ofEpochMilli(0);

// This is the date of the end of the world
Expand All @@ -134,8 +137,8 @@ public QueryBillData getBillData(int billId) {
}

@Override
public QuerySplitBillData getSplitBillData(int splitBillId) {
List<QuerySplitEntryData> entries = new ArrayList<>();
public List<SplitterEntry> getSplitBillData(int splitBillId) {
List<SplitterEntry> entries = new ArrayList<>();

try {
Statement statement = connection.createStatement();
Expand All @@ -158,12 +161,12 @@ public QuerySplitBillData getSplitBillData(int splitBillId) {
throw new RuntimeException(e);
}

return new QuerySplitBillData(splitBillId, entries);
return entries;
}

@Override
public QueryBillData getBillData(int billId, ZonedDateTime startDate, ZonedDateTime endDate) {
List<QueryEntryData> entries = new ArrayList<>();
public List<Entry> getBillData(int billId, ZonedDateTime startDate, ZonedDateTime endDate) {
List<Entry> entries = new ArrayList<>();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

try {
Expand All @@ -188,11 +191,11 @@ WHERE date BETWEEN CAST('%s' AS DATETIME) AND CAST('%s' AS DATETIME)
throw new RuntimeException(e);
}

return new QueryBillData(billId, entries);
return entries;
}

@Override
public QueryEntryData getEntryData(int billId, int entryId) {
public Entry getEntryData(int billId, int entryId) {
int splitBillId;
double value;
String currency;
Expand Down Expand Up @@ -238,19 +241,21 @@ public QueryEntryData getEntryData(int billId, int entryId) {
throw new RuntimeException(e);
}

return new QueryEntryData(entryId,
zDate,
value,
currency,
description,
from,
to,
location,
splitBillId);
return new EntryBuilder()
.setId(entryId)
.setValue(value)
.setDate(zDate)
.setCurrency(currency)
.setDescription(description)
.setFrom(from)
.setTo(to)
.setLocation(location)
.setSplitterBillId(splitBillId)
.buildEntry();
}

@Override
public QuerySplitEntryData getSplitEntryData(int billId, int entryId) {
public SplitterEntry getSplitEntryData(int billId, int entryId) {
double value;
String currency;
String description;
Expand Down Expand Up @@ -298,20 +303,22 @@ public QuerySplitEntryData getSplitEntryData(int billId, int entryId) {
throw new RuntimeException(e);
}

return new QuerySplitEntryData(entryId,
zDate,
value,
currency,
description,
from,
to,
location,
payee,
isPaidBack);
return new EntryBuilder()
.setId(entryId)
.setValue(value)
.setDate(zDate)
.setCurrency(currency)
.setDescription(description)
.setFrom(from)
.setTo(to)
.setLocation(location)
.setPayee(payee)
.setIsPaidBack(isPaidBack)
.buildSplitterEntry();
}

@Override
public void insertEntry(int billId, QueryEntryData entry) {
public void insertEntry(int billId, Entry entry) {
try {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

Expand All @@ -320,22 +327,22 @@ public void insertEntry(int billId, QueryEntryData entry) {
String query;

// This is the case where we don't want autoincrement
if (!(entry.getId() == -1)) {
if (!(entry.getId().getAttribute() == -1)) {
query = String.format("""
INSERT INTO bill_%d (entry_id, value, date, currency, description, `from`, `to`, location, split_bill_id) VALUE (
%d, %f, "%s", "%s", "%s", "%s", "%s", "%s", %d
)
""",
billId,
entry.getId(),
entry.getValue(),
entry.getDate().format(formatter),
entry.getCurrency(),
entry.getDescription(),
entry.getFrom(),
entry.getTo(),
entry.getLocation(),
entry.getSplitBillId());
entry.getId().getAttribute(),
entry.getValue().getAttribute(),
entry.getDate().getAttribute().format(formatter),
entry.getCurrency().getAttribute(),
entry.getDescription().getAttribute(),
entry.getFrom().getAttribute(),
entry.getTo().getAttribute(),
entry.getLocation().getAttribute(),
entry.getSplitterBillId().getAttribute());
} else {
// This is the case where we want auto increment
query = String.format("""
Expand All @@ -344,14 +351,14 @@ public void insertEntry(int billId, QueryEntryData entry) {
)
""",
billId,
entry.getValue(),
entry.getDate().format(formatter),
entry.getCurrency(),
entry.getDescription(),
entry.getFrom(),
entry.getTo(),
entry.getLocation(),
entry.getSplitBillId());
entry.getValue().getAttribute(),
entry.getDate().getAttribute().format(formatter),
entry.getCurrency().getAttribute(),
entry.getDescription().getAttribute(),
entry.getFrom().getAttribute(),
entry.getTo().getAttribute(),
entry.getLocation().getAttribute(),
entry.getSplitterBillId().getAttribute());
}

statement.execute(query);
Expand All @@ -362,7 +369,7 @@ public void insertEntry(int billId, QueryEntryData entry) {
}

@Override
public void insertSplitEntry(int billId, QuerySplitEntryData entry) {
public void insertSplitEntry(int billId, SplitterEntry entry) {
try {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

Expand All @@ -371,24 +378,24 @@ public void insertSplitEntry(int billId, QuerySplitEntryData entry) {
String query;

// This is the case where we don't want autoincrement
if (!(entry.getId() == -1)) {
if (!(entry.getId().getAttribute() == -1)) {
query = String.format("""
INSERT INTO bill_%d_%d (entry_id, value, date, currency, description, `from`, `to`, location, payee, paid_back) VALUE (
%d, %f, "%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s"
)
""",
this.userId,
billId,
entry.getId(),
entry.getValue(),
entry.getDate().format(formatter),
entry.getCurrency(),
entry.getDescription(),
entry.getFrom(),
entry.getTo(),
entry.getLocation(),
entry.getPayee(),
entry.getIsPaidBack());
entry.getId().getAttribute(),
entry.getValue().getAttribute(),
entry.getDate().getAttribute().format(formatter),
entry.getCurrency().getAttribute(),
entry.getDescription().getAttribute(),
entry.getFrom().getAttribute(),
entry.getTo().getAttribute(),
entry.getLocation().getAttribute(),
entry.getPayee().getAttribute(),
entry.getIsPaidBack().getAttribute());
} else {
// This is the case where we want auto increment
query = String.format("""
Expand All @@ -398,15 +405,15 @@ public void insertSplitEntry(int billId, QuerySplitEntryData entry) {
""",
this.userId,
billId,
entry.getValue(),
entry.getDate().format(formatter),
entry.getCurrency(),
entry.getDescription(),
entry.getFrom(),
entry.getTo(),
entry.getLocation(),
entry.getPayee(),
entry.getIsPaidBack());
entry.getValue().getAttribute(),
entry.getDate().getAttribute().format(formatter),
entry.getCurrency().getAttribute(),
entry.getDescription().getAttribute(),
entry.getFrom().getAttribute(),
entry.getTo().getAttribute(),
entry.getLocation().getAttribute(),
entry.getPayee().getAttribute(),
entry.getIsPaidBack().getAttribute());
}

statement.execute(query);
Expand Down Expand Up @@ -546,7 +553,7 @@ public void modifySplitEntry(int billId, int entryId, String column, String newV
}

@Override
public void modifyEntry(int billId, QueryEntryData entry) {
public void modifyEntry(int billId, Entry entry) {
try {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

Expand All @@ -566,15 +573,15 @@ public void modifyEntry(int billId, QueryEntryData entry) {
split_bill_id = %d
WHERE entry_id = %d
""", billId,
entry.getValue(),
entry.getDate().format(formatter),
entry.getCurrency(),
entry.getDescription(),
entry.getFrom(),
entry.getTo(),
entry.getLocation(),
entry.getSplitBillId(),
entry.getId());
entry.getValue().getAttribute(),
entry.getDate().getAttribute().format(formatter),
entry.getCurrency().getAttribute(),
entry.getDescription().getAttribute(),
entry.getFrom().getAttribute(),
entry.getTo().getAttribute(),
entry.getLocation().getAttribute(),
entry.getSplitterBillId().getAttribute(),
entry.getId().getAttribute());

statement.execute(query);

Expand All @@ -584,7 +591,7 @@ public void modifyEntry(int billId, QueryEntryData entry) {
}

@Override
public void modifySplitEntry(int billId, QuerySplitEntryData entry) {
public void modifySplitEntry(int billId, SplitterEntry entry) {
try {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

Expand All @@ -606,16 +613,16 @@ public void modifySplitEntry(int billId, QuerySplitEntryData entry) {
WHERE entry_id = %d
""", this.userId,
billId,
entry.getValue(),
entry.getDate().format(formatter),
entry.getCurrency(),
entry.getDescription(),
entry.getFrom(),
entry.getTo(),
entry.getLocation(),
entry.getPayee(),
entry.getIsPaidBack(),
entry.getId());
entry.getValue().getAttribute(),
entry.getDate().getAttribute().format(formatter),
entry.getCurrency().getAttribute(),
entry.getDescription().getAttribute(),
entry.getFrom().getAttribute(),
entry.getTo().getAttribute(),
entry.getLocation().getAttribute(),
entry.getPayee().getAttribute(),
entry.getIsPaidBack().getAttribute(),
entry.getId().getAttribute());

statement.execute(query);

Expand Down Expand Up @@ -688,16 +695,6 @@ public void createUsersTable() {
try {
Statement statement = connection.createStatement();

String checkQuery = "SHOW TABLES LIKE 'users'";

ResultSet resultSet = statement.executeQuery(checkQuery);

// Checks if the table already exists, if not, continue creating the table
if (resultSet.next()) {
return;
}


String query = """
CREATE TABLE IF NOT EXISTS users
(
Expand All @@ -721,4 +718,14 @@ public void setUserId(int userId) {
this.userId = userId;
}

public static void main(String[] args) {
MySQLDatabaseGateway testGateway = new MySQLDatabaseGateway();

testGateway.setUserId(9999);

Entry obtainedEntry = testGateway.getEntryData(9999, 1);

System.out.println(obtainedEntry.getDescription().getAttribute());
}

}
26 changes: 0 additions & 26 deletions src/main/java/billgates/database/QueryBillData.java

This file was deleted.

Loading

0 comments on commit 371215a

Please sign in to comment.