Database Repository -> Share Connection to DB

This commit is contained in:
jeffcheasey88 2025-01-25 12:11:20 +01:00
parent 35b97d1a25
commit 0bfba6d99c
13 changed files with 67 additions and 65 deletions

View file

@ -11,6 +11,7 @@ import java.util.regex.Matcher;
import dev.peerat.backend.bonus.extract.RouteExtracter; import dev.peerat.backend.bonus.extract.RouteExtracter;
import dev.peerat.backend.model.Group; import dev.peerat.backend.model.Group;
import dev.peerat.backend.model.PeerAtUser; import dev.peerat.backend.model.PeerAtUser;
import dev.peerat.backend.repository.ConnectionManager;
import dev.peerat.backend.repository.DatabaseRepository; import dev.peerat.backend.repository.DatabaseRepository;
import dev.peerat.framework.Context; import dev.peerat.framework.Context;
import dev.peerat.framework.DependencyInjector; import dev.peerat.framework.DependencyInjector;
@ -39,7 +40,7 @@ public class Main{
Class.forName("com.mysql.cj.jdbc.Driver"); Class.forName("com.mysql.cj.jdbc.Driver");
DatabaseRepository repo = new DatabaseRepository(config); DatabaseRepository repo = new DatabaseRepository(new ConnectionManager(config), config);
Router<PeerAtUser> router = new Router<PeerAtUser>() Router<PeerAtUser> router = new Router<PeerAtUser>()
.activeReOrdering(). .activeReOrdering().
addDefaultHeaders(RequestType.GET, "Access-Control-Allow-Origin: *"). addDefaultHeaders(RequestType.GET, "Access-Control-Allow-Origin: *").

View file

@ -1,7 +1,6 @@
package dev.peerat.backend.repository; package dev.peerat.backend.repository;
import java.sql.Connection; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.ResultSetMetaData; import java.sql.ResultSetMetaData;
@ -17,26 +16,19 @@ import dev.peerat.backend.model.Puzzle;
public class BaseDatabaseQuery{ public class BaseDatabaseQuery{
Connection con; private ConnectionManager con;
private Configuration config; private Configuration config;
public BaseDatabaseQuery(Connection con, Configuration config){ public BaseDatabaseQuery(ConnectionManager con){
this.con = con; this.con = con;
this.config = config;
} }
public Connection ensureConnection() throws SQLException{
return this.con.ensureConnection();
public void ensureConnection() throws SQLException {
if (con == null || (!con.isValid(5))) {
this.con = DriverManager.getConnection(
"jdbc:mysql://" + config.getDbHost() + ":" + config.getDbPort() + "/" + config.getDbDatabase() + "",
config.getDbUser(), config.getDbPassword());
}
} }
public PreparedStatement prepare(String request) throws SQLException{ public PreparedStatement prepare(String request) throws SQLException{
return this.con.prepareStatement(request); return this.con.ensureConnection().prepareStatement(request);
} }
public Puzzle makePuzzle(ResultSet puzzleResult) throws SQLException { public Puzzle makePuzzle(ResultSet puzzleResult) throws SQLException {
@ -86,7 +78,7 @@ public class BaseDatabaseQuery{
} }
// ADD rank // ADD rank
PreparedStatement completionsStmt = con.prepareStatement(DatabasePlayerRepository.GET_PLAYER_RANK()); PreparedStatement completionsStmt = con.ensureConnection().prepareStatement(DatabasePlayerRepository.GET_PLAYER_RANK());
completionsStmt.setInt(1, id); completionsStmt.setInt(1, id);
ResultSet result = completionsStmt.executeQuery(); ResultSet result = completionsStmt.executeQuery();
while (result.next()) { while (result.next()) {

View file

@ -0,0 +1,26 @@
package dev.peerat.backend.repository;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import dev.peerat.backend.Configuration;
public class ConnectionManager {
private Connection con;
private Configuration config;
public ConnectionManager(Configuration config){
this.config = config;
}
public Connection ensureConnection() throws SQLException {
if (con == null || (!con.isValid(5))) {
this.con = DriverManager.getConnection(
"jdbc:mysql://" + config.getDbHost() + ":" + config.getDbPort() + "/" + config.getDbDatabase() + "",
config.getDbUser(), config.getDbPassword());
}
return this.con;
}
}

View file

@ -56,15 +56,14 @@ public class DatabaseAdminRepository extends BaseDatabaseQuery{
private Configuration config; private Configuration config;
public DatabaseAdminRepository(Connection con, Configuration config){ public DatabaseAdminRepository(ConnectionManager con, Configuration config){
super(con, config); super(con);
this.config = config; this.config = config;
} }
//ADMIN //ADMIN
public Chapter getAdminChapter(int id){ public Chapter getAdminChapter(int id){
try { try {
ensureConnection();
PreparedStatement chapterStmt = Query.GET_CHAPTER.prepare(this); PreparedStatement chapterStmt = Query.GET_CHAPTER.prepare(this);
chapterStmt.setInt(1, id); chapterStmt.setInt(1, id);
ResultSet chapterResult = chapterStmt.executeQuery(); ResultSet chapterResult = chapterStmt.executeQuery();
@ -80,7 +79,6 @@ public class DatabaseAdminRepository extends BaseDatabaseQuery{
public Puzzle getAdminPuzzle(int id){ public Puzzle getAdminPuzzle(int id){
try { try {
ensureConnection();
PreparedStatement chapterStmt = Query.GET_PUZZLE.prepare(this); PreparedStatement chapterStmt = Query.GET_PUZZLE.prepare(this);
chapterStmt.setInt(1, id); chapterStmt.setInt(1, id);
ResultSet chapterResult = chapterStmt.executeQuery(); ResultSet chapterResult = chapterStmt.executeQuery();
@ -95,7 +93,6 @@ public class DatabaseAdminRepository extends BaseDatabaseQuery{
public List<Puzzle> getAdminPuzzles(){ public List<Puzzle> getAdminPuzzles(){
try { try {
ensureConnection();
PreparedStatement chapterStmt = Query.GET_PUZZLES.prepare(this); PreparedStatement chapterStmt = Query.GET_PUZZLES.prepare(this);
ResultSet chapterResult = chapterStmt.executeQuery(); ResultSet chapterResult = chapterStmt.executeQuery();
List<Puzzle> list = new ArrayList<>(); List<Puzzle> list = new ArrayList<>();
@ -111,7 +108,6 @@ public class DatabaseAdminRepository extends BaseDatabaseQuery{
public List<Tag> getAdminTags(){ public List<Tag> getAdminTags(){
try { try {
ensureConnection();
PreparedStatement chapterStmt = Query.GET_TAGS.prepare(this); PreparedStatement chapterStmt = Query.GET_TAGS.prepare(this);
ResultSet chapterResult = chapterStmt.executeQuery(); ResultSet chapterResult = chapterStmt.executeQuery();
List<Tag> list = new ArrayList<>(); List<Tag> list = new ArrayList<>();
@ -126,7 +122,6 @@ public class DatabaseAdminRepository extends BaseDatabaseQuery{
} }
public boolean adminAddChapter(Chapter chapter) throws SQLException { public boolean adminAddChapter(Chapter chapter) throws SQLException {
ensureConnection();
PreparedStatement statement = Query.ADD_CHAPTER.prepare(this); PreparedStatement statement = Query.ADD_CHAPTER.prepare(this);
statement.setString(1, chapter.getName()); statement.setString(1, chapter.getName());
statement.setTimestamp(2, chapter.getStartDate()); statement.setTimestamp(2, chapter.getStartDate());
@ -135,7 +130,6 @@ public class DatabaseAdminRepository extends BaseDatabaseQuery{
} }
public boolean adminAddPuzzle(Puzzle puzzle, int chapter) throws SQLException { public boolean adminAddPuzzle(Puzzle puzzle, int chapter) throws SQLException {
ensureConnection();
PreparedStatement statement = Query.ADD_PUZZLE.prepare(this); PreparedStatement statement = Query.ADD_PUZZLE.prepare(this);
statement.setString(1, puzzle.getName()); statement.setString(1, puzzle.getName());
statement.setString(2, puzzle.getContent()); statement.setString(2, puzzle.getContent());
@ -147,14 +141,12 @@ public class DatabaseAdminRepository extends BaseDatabaseQuery{
} }
public boolean adminAddTag(String name) throws SQLException { public boolean adminAddTag(String name) throws SQLException {
ensureConnection();
PreparedStatement statement = Query.ADD_TAG.prepare(this); PreparedStatement statement = Query.ADD_TAG.prepare(this);
statement.setString(1, name); statement.setString(1, name);
return (statement.executeUpdate() >= 0); return (statement.executeUpdate() >= 0);
} }
public boolean adminUpdateChapter(int id, Chapter chapter) throws SQLException { public boolean adminUpdateChapter(int id, Chapter chapter) throws SQLException {
ensureConnection();
PreparedStatement statement = Query.EDIT_CHAPTER.prepare(this); PreparedStatement statement = Query.EDIT_CHAPTER.prepare(this);
statement.setString(1, chapter.getName()); statement.setString(1, chapter.getName());
statement.setTimestamp(2, chapter.getStartDate()); statement.setTimestamp(2, chapter.getStartDate());
@ -164,7 +156,6 @@ public class DatabaseAdminRepository extends BaseDatabaseQuery{
} }
public boolean adminUpdatePuzzle(int id, Puzzle puzzle, int chapter) throws SQLException { public boolean adminUpdatePuzzle(int id, Puzzle puzzle, int chapter) throws SQLException {
ensureConnection();
PreparedStatement statement = Query.EDIT_PUZZLE.prepare(this); PreparedStatement statement = Query.EDIT_PUZZLE.prepare(this);
statement.setString(1, puzzle.getName()); statement.setString(1, puzzle.getName());
statement.setString(2, puzzle.getContent()); statement.setString(2, puzzle.getContent());
@ -177,7 +168,6 @@ public class DatabaseAdminRepository extends BaseDatabaseQuery{
} }
public boolean adminUpdateTag(Tag tag) throws SQLException { public boolean adminUpdateTag(Tag tag) throws SQLException {
ensureConnection();
PreparedStatement statement = Query.EDIT_TAG.prepare(this); PreparedStatement statement = Query.EDIT_TAG.prepare(this);
statement.setString(1, tag.getName()); statement.setString(1, tag.getName());
statement.setInt(2, tag.getId()); statement.setInt(2, tag.getId());
@ -185,21 +175,18 @@ public class DatabaseAdminRepository extends BaseDatabaseQuery{
} }
public boolean adminDeleteChapter(int id) throws SQLException { public boolean adminDeleteChapter(int id) throws SQLException {
ensureConnection();
PreparedStatement statement = Query.DELETE_CHAPTER.prepare(this); PreparedStatement statement = Query.DELETE_CHAPTER.prepare(this);
statement.setInt(1, id); statement.setInt(1, id);
return (statement.executeUpdate() >= 0); return (statement.executeUpdate() >= 0);
} }
public boolean adminDeletePuzzle(int id) throws SQLException { public boolean adminDeletePuzzle(int id) throws SQLException {
ensureConnection();
PreparedStatement statement = Query.DELETE_PUZZLE.prepare(this); PreparedStatement statement = Query.DELETE_PUZZLE.prepare(this);
statement.setInt(1, id); statement.setInt(1, id);
return (statement.executeUpdate() >= 0); return (statement.executeUpdate() >= 0);
} }
public boolean adminDeleteTag(int id) throws SQLException { public boolean adminDeleteTag(int id) throws SQLException {
ensureConnection();
PreparedStatement statement = Query.DELETE_TAG.prepare(this); PreparedStatement statement = Query.DELETE_TAG.prepare(this);
statement.setInt(1, id); statement.setInt(1, id);
return (statement.executeUpdate() >= 0); return (statement.executeUpdate() >= 0);

View file

@ -45,11 +45,10 @@ public class DatabaseAuthRepository extends BaseDatabaseQuery{
} }
} }
private Configuration config; private Configuration config;
public DatabaseAuthRepository(Connection con, Configuration config){ public DatabaseAuthRepository(ConnectionManager con, Configuration config){
super(con, config); super(con);
this.config = config; this.config = config;
} }
@ -75,8 +74,7 @@ public class DatabaseAuthRepository extends BaseDatabaseQuery{
private boolean checkAvailability(String queriedString, String correspondingQuery) { private boolean checkAvailability(String queriedString, String correspondingQuery) {
try { try {
ensureConnection(); PreparedStatement statement = prepare(correspondingQuery);
PreparedStatement statement = con.prepareStatement(correspondingQuery);
statement.setString(1, queriedString); statement.setString(1, queriedString);
ResultSet result = statement.executeQuery(); ResultSet result = statement.executeQuery();
return !result.next(); return !result.next();
@ -104,7 +102,7 @@ public class DatabaseAuthRepository extends BaseDatabaseQuery{
try { try {
String pass = Password.hash(password).withArgon2().getResult(); String pass = Password.hash(password).withArgon2().getResult();
System.out.println("pass("+pass.length()+") "+pass); System.out.println("pass("+pass.length()+") "+pass);
ensureConnection(); Connection con = ensureConnection();
con.setAutoCommit(false); con.setAutoCommit(false);
try (PreparedStatement playerStatement = con.prepareStatement(Query.REGISTER_QUERY.toString(), try (PreparedStatement playerStatement = con.prepareStatement(Query.REGISTER_QUERY.toString(),
Statement.RETURN_GENERATED_KEYS)) { Statement.RETURN_GENERATED_KEYS)) {
@ -153,7 +151,7 @@ public class DatabaseAuthRepository extends BaseDatabaseQuery{
public int login(String username, String password) { public int login(String username, String password) {
try { try {
ensureConnection(); ensureConnection();
PreparedStatement statement = con.prepareStatement(Query.CHECK_PASSWORD.toString()); PreparedStatement statement = prepare(Query.CHECK_PASSWORD.toString());
statement.setString(1, username); statement.setString(1, username);
ResultSet result = statement.executeQuery(); ResultSet result = statement.executeQuery();
if (result.next()) { if (result.next()) {

View file

@ -47,8 +47,8 @@ public class DatabaseBadgeRepository extends BaseDatabaseQuery{
private Configuration config; private Configuration config;
public DatabaseBadgeRepository(Connection con, Configuration config){ public DatabaseBadgeRepository(ConnectionManager con, Configuration config){
super(con, config); super(con);
this.config = config; this.config = config;
} }

View file

@ -48,8 +48,8 @@ public class DatabaseChapterRepository extends BaseDatabaseQuery{
private Configuration config; private Configuration config;
private DatabasePuzzleRepository puzzleRepo; private DatabasePuzzleRepository puzzleRepo;
public DatabaseChapterRepository(Connection con, Configuration config, DatabasePuzzleRepository puzzleRepo){ public DatabaseChapterRepository(ConnectionManager con, Configuration config, DatabasePuzzleRepository puzzleRepo){
super(con, config); super(con);
this.config = config; this.config = config;
this.puzzleRepo = puzzleRepo; this.puzzleRepo = puzzleRepo;
} }

View file

@ -56,8 +56,8 @@ public class DatabaseCompletionRepository extends BaseDatabaseQuery{
private Configuration config; private Configuration config;
private DatabaseChapterRepository chapterRepo; private DatabaseChapterRepository chapterRepo;
public DatabaseCompletionRepository(Connection con, Configuration config, DatabaseChapterRepository chapterRepo){ public DatabaseCompletionRepository(ConnectionManager con, Configuration config, DatabaseChapterRepository chapterRepo){
super(con, config); super(con);
this.config = config; this.config = config;
this.chapterRepo = chapterRepo; this.chapterRepo = chapterRepo;
} }

View file

@ -47,8 +47,8 @@ public class DatabaseGroupRepository extends BaseDatabaseQuery{
private Configuration config; private Configuration config;
public DatabaseGroupRepository(Connection con, Configuration config){ public DatabaseGroupRepository(ConnectionManager con, Configuration config){
super(con, config); super(con);
this.config = config; this.config = config;
} }

View file

@ -43,11 +43,10 @@ public class DatabaseLeaderboardRepository extends BaseDatabaseQuery{
} }
} }
private Configuration config; private Configuration config;
public DatabaseLeaderboardRepository(Connection con, Configuration config){ public DatabaseLeaderboardRepository(ConnectionManager con, Configuration config){
super(con, config); super(con);
this.config = config; this.config = config;
} }

View file

@ -60,8 +60,8 @@ public class DatabasePlayerRepository extends BaseDatabaseQuery{
private Configuration config; private Configuration config;
public DatabasePlayerRepository(Connection con, Configuration config){ public DatabasePlayerRepository(ConnectionManager con, Configuration config){
super(con, config); super(con);
this.config = config; this.config = config;
} }
@ -163,7 +163,7 @@ public class DatabasePlayerRepository extends BaseDatabaseQuery{
if (player == null) { if (player == null) {
id = result.getInt("id_player"); id = result.getInt("id_player");
player = makePlayer(result, id); player = makePlayer(result, id);
completionsStmt = con.prepareStatement(DatabaseBadgeRepository.GET_BADGES_OF_PLAYER()); completionsStmt = prepare(DatabaseBadgeRepository.GET_BADGES_OF_PLAYER());
completionsStmt.setInt(1, id); completionsStmt.setInt(1, id);
ResultSet resultBadges = completionsStmt.executeQuery(); ResultSet resultBadges = completionsStmt.executeQuery();
while (resultBadges.next()) { while (resultBadges.next()) {

View file

@ -39,11 +39,10 @@ public class DatabasePuzzleRepository extends BaseDatabaseQuery{
} }
} }
private Configuration config; private Configuration config;
public DatabasePuzzleRepository(Connection con, Configuration config){ public DatabasePuzzleRepository(ConnectionManager con, Configuration config){
super(con, config); super(con);
this.config = config; this.config = config;
} }

View file

@ -25,19 +25,19 @@ public class DatabaseRepository extends BaseDatabaseQuery{
private DatabaseLeaderboardRepository leaderboardRepo; private DatabaseLeaderboardRepository leaderboardRepo;
private DatabasePlayerRepository playerRepo; private DatabasePlayerRepository playerRepo;
public DatabaseRepository(Configuration config) { public DatabaseRepository(ConnectionManager con, Configuration config) {
super(null, config); super(con);
this.config = config; this.config = config;
this.puzzleRepo = new DatabasePuzzleRepository(null, config); this.puzzleRepo = new DatabasePuzzleRepository(con, config);
this.chapterRepo = new DatabaseChapterRepository(null, config, puzzleRepo); this.chapterRepo = new DatabaseChapterRepository(con, config, puzzleRepo);
this.adminRepo = new DatabaseAdminRepository(null, config); this.adminRepo = new DatabaseAdminRepository(con, config);
this.authRepo = new DatabaseAuthRepository(null, config); this.authRepo = new DatabaseAuthRepository(con, config);
this.badgeRepo = new DatabaseBadgeRepository(null, config); this.badgeRepo = new DatabaseBadgeRepository(con, config);
this.completionRepo = new DatabaseCompletionRepository(null, config, chapterRepo); this.completionRepo = new DatabaseCompletionRepository(con, config, chapterRepo);
this.groupRepo = new DatabaseGroupRepository(null, config); this.groupRepo = new DatabaseGroupRepository(con, config);
this.leaderboardRepo = new DatabaseLeaderboardRepository(null, config); this.leaderboardRepo = new DatabaseLeaderboardRepository(con, config);
this.playerRepo = new DatabasePlayerRepository(null, config); this.playerRepo = new DatabasePlayerRepository(con, config);
} }
public DatabasePuzzleRepository getPuzzleRepository(){ public DatabasePuzzleRepository getPuzzleRepository(){