Database Repository -> Share Connection to DB
This commit is contained in:
parent
35b97d1a25
commit
0bfba6d99c
13 changed files with 67 additions and 65 deletions
|
@ -11,6 +11,7 @@ import java.util.regex.Matcher;
|
|||
import dev.peerat.backend.bonus.extract.RouteExtracter;
|
||||
import dev.peerat.backend.model.Group;
|
||||
import dev.peerat.backend.model.PeerAtUser;
|
||||
import dev.peerat.backend.repository.ConnectionManager;
|
||||
import dev.peerat.backend.repository.DatabaseRepository;
|
||||
import dev.peerat.framework.Context;
|
||||
import dev.peerat.framework.DependencyInjector;
|
||||
|
@ -39,7 +40,7 @@ public class Main{
|
|||
|
||||
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>()
|
||||
.activeReOrdering().
|
||||
addDefaultHeaders(RequestType.GET, "Access-Control-Allow-Origin: *").
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
package dev.peerat.backend.repository;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.ResultSetMetaData;
|
||||
|
@ -17,26 +16,19 @@ import dev.peerat.backend.model.Puzzle;
|
|||
|
||||
public class BaseDatabaseQuery{
|
||||
|
||||
Connection con;
|
||||
private ConnectionManager con;
|
||||
private Configuration config;
|
||||
|
||||
public BaseDatabaseQuery(Connection con, Configuration config){
|
||||
public BaseDatabaseQuery(ConnectionManager con){
|
||||
this.con = con;
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
|
||||
|
||||
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 Connection ensureConnection() throws SQLException{
|
||||
return this.con.ensureConnection();
|
||||
}
|
||||
|
||||
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 {
|
||||
|
@ -86,7 +78,7 @@ public class BaseDatabaseQuery{
|
|||
}
|
||||
|
||||
// ADD rank
|
||||
PreparedStatement completionsStmt = con.prepareStatement(DatabasePlayerRepository.GET_PLAYER_RANK());
|
||||
PreparedStatement completionsStmt = con.ensureConnection().prepareStatement(DatabasePlayerRepository.GET_PLAYER_RANK());
|
||||
completionsStmt.setInt(1, id);
|
||||
ResultSet result = completionsStmt.executeQuery();
|
||||
while (result.next()) {
|
||||
|
|
26
src/dev/peerat/backend/repository/ConnectionManager.java
Normal file
26
src/dev/peerat/backend/repository/ConnectionManager.java
Normal 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;
|
||||
}
|
||||
}
|
|
@ -56,15 +56,14 @@ public class DatabaseAdminRepository extends BaseDatabaseQuery{
|
|||
|
||||
private Configuration config;
|
||||
|
||||
public DatabaseAdminRepository(Connection con, Configuration config){
|
||||
super(con, config);
|
||||
public DatabaseAdminRepository(ConnectionManager con, Configuration config){
|
||||
super(con);
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
//ADMIN
|
||||
public Chapter getAdminChapter(int id){
|
||||
try {
|
||||
ensureConnection();
|
||||
PreparedStatement chapterStmt = Query.GET_CHAPTER.prepare(this);
|
||||
chapterStmt.setInt(1, id);
|
||||
ResultSet chapterResult = chapterStmt.executeQuery();
|
||||
|
@ -80,7 +79,6 @@ public class DatabaseAdminRepository extends BaseDatabaseQuery{
|
|||
|
||||
public Puzzle getAdminPuzzle(int id){
|
||||
try {
|
||||
ensureConnection();
|
||||
PreparedStatement chapterStmt = Query.GET_PUZZLE.prepare(this);
|
||||
chapterStmt.setInt(1, id);
|
||||
ResultSet chapterResult = chapterStmt.executeQuery();
|
||||
|
@ -95,7 +93,6 @@ public class DatabaseAdminRepository extends BaseDatabaseQuery{
|
|||
|
||||
public List<Puzzle> getAdminPuzzles(){
|
||||
try {
|
||||
ensureConnection();
|
||||
PreparedStatement chapterStmt = Query.GET_PUZZLES.prepare(this);
|
||||
ResultSet chapterResult = chapterStmt.executeQuery();
|
||||
List<Puzzle> list = new ArrayList<>();
|
||||
|
@ -111,7 +108,6 @@ public class DatabaseAdminRepository extends BaseDatabaseQuery{
|
|||
|
||||
public List<Tag> getAdminTags(){
|
||||
try {
|
||||
ensureConnection();
|
||||
PreparedStatement chapterStmt = Query.GET_TAGS.prepare(this);
|
||||
ResultSet chapterResult = chapterStmt.executeQuery();
|
||||
List<Tag> list = new ArrayList<>();
|
||||
|
@ -126,7 +122,6 @@ public class DatabaseAdminRepository extends BaseDatabaseQuery{
|
|||
}
|
||||
|
||||
public boolean adminAddChapter(Chapter chapter) throws SQLException {
|
||||
ensureConnection();
|
||||
PreparedStatement statement = Query.ADD_CHAPTER.prepare(this);
|
||||
statement.setString(1, chapter.getName());
|
||||
statement.setTimestamp(2, chapter.getStartDate());
|
||||
|
@ -135,7 +130,6 @@ public class DatabaseAdminRepository extends BaseDatabaseQuery{
|
|||
}
|
||||
|
||||
public boolean adminAddPuzzle(Puzzle puzzle, int chapter) throws SQLException {
|
||||
ensureConnection();
|
||||
PreparedStatement statement = Query.ADD_PUZZLE.prepare(this);
|
||||
statement.setString(1, puzzle.getName());
|
||||
statement.setString(2, puzzle.getContent());
|
||||
|
@ -147,14 +141,12 @@ public class DatabaseAdminRepository extends BaseDatabaseQuery{
|
|||
}
|
||||
|
||||
public boolean adminAddTag(String name) throws SQLException {
|
||||
ensureConnection();
|
||||
PreparedStatement statement = Query.ADD_TAG.prepare(this);
|
||||
statement.setString(1, name);
|
||||
return (statement.executeUpdate() >= 0);
|
||||
}
|
||||
|
||||
public boolean adminUpdateChapter(int id, Chapter chapter) throws SQLException {
|
||||
ensureConnection();
|
||||
PreparedStatement statement = Query.EDIT_CHAPTER.prepare(this);
|
||||
statement.setString(1, chapter.getName());
|
||||
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 {
|
||||
ensureConnection();
|
||||
PreparedStatement statement = Query.EDIT_PUZZLE.prepare(this);
|
||||
statement.setString(1, puzzle.getName());
|
||||
statement.setString(2, puzzle.getContent());
|
||||
|
@ -177,7 +168,6 @@ public class DatabaseAdminRepository extends BaseDatabaseQuery{
|
|||
}
|
||||
|
||||
public boolean adminUpdateTag(Tag tag) throws SQLException {
|
||||
ensureConnection();
|
||||
PreparedStatement statement = Query.EDIT_TAG.prepare(this);
|
||||
statement.setString(1, tag.getName());
|
||||
statement.setInt(2, tag.getId());
|
||||
|
@ -185,21 +175,18 @@ public class DatabaseAdminRepository extends BaseDatabaseQuery{
|
|||
}
|
||||
|
||||
public boolean adminDeleteChapter(int id) throws SQLException {
|
||||
ensureConnection();
|
||||
PreparedStatement statement = Query.DELETE_CHAPTER.prepare(this);
|
||||
statement.setInt(1, id);
|
||||
return (statement.executeUpdate() >= 0);
|
||||
}
|
||||
|
||||
public boolean adminDeletePuzzle(int id) throws SQLException {
|
||||
ensureConnection();
|
||||
PreparedStatement statement = Query.DELETE_PUZZLE.prepare(this);
|
||||
statement.setInt(1, id);
|
||||
return (statement.executeUpdate() >= 0);
|
||||
}
|
||||
|
||||
public boolean adminDeleteTag(int id) throws SQLException {
|
||||
ensureConnection();
|
||||
PreparedStatement statement = Query.DELETE_TAG.prepare(this);
|
||||
statement.setInt(1, id);
|
||||
return (statement.executeUpdate() >= 0);
|
||||
|
|
|
@ -45,11 +45,10 @@ public class DatabaseAuthRepository extends BaseDatabaseQuery{
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
private Configuration config;
|
||||
|
||||
public DatabaseAuthRepository(Connection con, Configuration config){
|
||||
super(con, config);
|
||||
public DatabaseAuthRepository(ConnectionManager con, Configuration config){
|
||||
super(con);
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
|
@ -75,8 +74,7 @@ public class DatabaseAuthRepository extends BaseDatabaseQuery{
|
|||
|
||||
private boolean checkAvailability(String queriedString, String correspondingQuery) {
|
||||
try {
|
||||
ensureConnection();
|
||||
PreparedStatement statement = con.prepareStatement(correspondingQuery);
|
||||
PreparedStatement statement = prepare(correspondingQuery);
|
||||
statement.setString(1, queriedString);
|
||||
ResultSet result = statement.executeQuery();
|
||||
return !result.next();
|
||||
|
@ -104,7 +102,7 @@ public class DatabaseAuthRepository extends BaseDatabaseQuery{
|
|||
try {
|
||||
String pass = Password.hash(password).withArgon2().getResult();
|
||||
System.out.println("pass("+pass.length()+") "+pass);
|
||||
ensureConnection();
|
||||
Connection con = ensureConnection();
|
||||
con.setAutoCommit(false);
|
||||
try (PreparedStatement playerStatement = con.prepareStatement(Query.REGISTER_QUERY.toString(),
|
||||
Statement.RETURN_GENERATED_KEYS)) {
|
||||
|
@ -153,7 +151,7 @@ public class DatabaseAuthRepository extends BaseDatabaseQuery{
|
|||
public int login(String username, String password) {
|
||||
try {
|
||||
ensureConnection();
|
||||
PreparedStatement statement = con.prepareStatement(Query.CHECK_PASSWORD.toString());
|
||||
PreparedStatement statement = prepare(Query.CHECK_PASSWORD.toString());
|
||||
statement.setString(1, username);
|
||||
ResultSet result = statement.executeQuery();
|
||||
if (result.next()) {
|
||||
|
|
|
@ -47,8 +47,8 @@ public class DatabaseBadgeRepository extends BaseDatabaseQuery{
|
|||
|
||||
private Configuration config;
|
||||
|
||||
public DatabaseBadgeRepository(Connection con, Configuration config){
|
||||
super(con, config);
|
||||
public DatabaseBadgeRepository(ConnectionManager con, Configuration config){
|
||||
super(con);
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
|
|
|
@ -48,8 +48,8 @@ public class DatabaseChapterRepository extends BaseDatabaseQuery{
|
|||
private Configuration config;
|
||||
private DatabasePuzzleRepository puzzleRepo;
|
||||
|
||||
public DatabaseChapterRepository(Connection con, Configuration config, DatabasePuzzleRepository puzzleRepo){
|
||||
super(con, config);
|
||||
public DatabaseChapterRepository(ConnectionManager con, Configuration config, DatabasePuzzleRepository puzzleRepo){
|
||||
super(con);
|
||||
this.config = config;
|
||||
this.puzzleRepo = puzzleRepo;
|
||||
}
|
||||
|
|
|
@ -56,8 +56,8 @@ public class DatabaseCompletionRepository extends BaseDatabaseQuery{
|
|||
private Configuration config;
|
||||
private DatabaseChapterRepository chapterRepo;
|
||||
|
||||
public DatabaseCompletionRepository(Connection con, Configuration config, DatabaseChapterRepository chapterRepo){
|
||||
super(con, config);
|
||||
public DatabaseCompletionRepository(ConnectionManager con, Configuration config, DatabaseChapterRepository chapterRepo){
|
||||
super(con);
|
||||
this.config = config;
|
||||
this.chapterRepo = chapterRepo;
|
||||
}
|
||||
|
|
|
@ -47,8 +47,8 @@ public class DatabaseGroupRepository extends BaseDatabaseQuery{
|
|||
|
||||
private Configuration config;
|
||||
|
||||
public DatabaseGroupRepository(Connection con, Configuration config){
|
||||
super(con, config);
|
||||
public DatabaseGroupRepository(ConnectionManager con, Configuration config){
|
||||
super(con);
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
|
|
|
@ -43,11 +43,10 @@ public class DatabaseLeaderboardRepository extends BaseDatabaseQuery{
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
private Configuration config;
|
||||
|
||||
public DatabaseLeaderboardRepository(Connection con, Configuration config){
|
||||
super(con, config);
|
||||
public DatabaseLeaderboardRepository(ConnectionManager con, Configuration config){
|
||||
super(con);
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
|
|
|
@ -60,8 +60,8 @@ public class DatabasePlayerRepository extends BaseDatabaseQuery{
|
|||
|
||||
private Configuration config;
|
||||
|
||||
public DatabasePlayerRepository(Connection con, Configuration config){
|
||||
super(con, config);
|
||||
public DatabasePlayerRepository(ConnectionManager con, Configuration config){
|
||||
super(con);
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
|
@ -163,7 +163,7 @@ public class DatabasePlayerRepository extends BaseDatabaseQuery{
|
|||
if (player == null) {
|
||||
id = result.getInt("id_player");
|
||||
player = makePlayer(result, id);
|
||||
completionsStmt = con.prepareStatement(DatabaseBadgeRepository.GET_BADGES_OF_PLAYER());
|
||||
completionsStmt = prepare(DatabaseBadgeRepository.GET_BADGES_OF_PLAYER());
|
||||
completionsStmt.setInt(1, id);
|
||||
ResultSet resultBadges = completionsStmt.executeQuery();
|
||||
while (resultBadges.next()) {
|
||||
|
|
|
@ -39,11 +39,10 @@ public class DatabasePuzzleRepository extends BaseDatabaseQuery{
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
private Configuration config;
|
||||
|
||||
public DatabasePuzzleRepository(Connection con, Configuration config){
|
||||
super(con, config);
|
||||
public DatabasePuzzleRepository(ConnectionManager con, Configuration config){
|
||||
super(con);
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
|
|
|
@ -25,19 +25,19 @@ public class DatabaseRepository extends BaseDatabaseQuery{
|
|||
private DatabaseLeaderboardRepository leaderboardRepo;
|
||||
private DatabasePlayerRepository playerRepo;
|
||||
|
||||
public DatabaseRepository(Configuration config) {
|
||||
super(null, config);
|
||||
public DatabaseRepository(ConnectionManager con, Configuration config) {
|
||||
super(con);
|
||||
this.config = config;
|
||||
|
||||
this.puzzleRepo = new DatabasePuzzleRepository(null, config);
|
||||
this.chapterRepo = new DatabaseChapterRepository(null, config, puzzleRepo);
|
||||
this.adminRepo = new DatabaseAdminRepository(null, config);
|
||||
this.authRepo = new DatabaseAuthRepository(null, config);
|
||||
this.badgeRepo = new DatabaseBadgeRepository(null, config);
|
||||
this.completionRepo = new DatabaseCompletionRepository(null, config, chapterRepo);
|
||||
this.groupRepo = new DatabaseGroupRepository(null, config);
|
||||
this.leaderboardRepo = new DatabaseLeaderboardRepository(null, config);
|
||||
this.playerRepo = new DatabasePlayerRepository(null, config);
|
||||
this.puzzleRepo = new DatabasePuzzleRepository(con, config);
|
||||
this.chapterRepo = new DatabaseChapterRepository(con, config, puzzleRepo);
|
||||
this.adminRepo = new DatabaseAdminRepository(con, config);
|
||||
this.authRepo = new DatabaseAuthRepository(con, config);
|
||||
this.badgeRepo = new DatabaseBadgeRepository(con, config);
|
||||
this.completionRepo = new DatabaseCompletionRepository(con, config, chapterRepo);
|
||||
this.groupRepo = new DatabaseGroupRepository(con, config);
|
||||
this.leaderboardRepo = new DatabaseLeaderboardRepository(con, config);
|
||||
this.playerRepo = new DatabasePlayerRepository(con, config);
|
||||
}
|
||||
|
||||
public DatabasePuzzleRepository getPuzzleRepository(){
|
||||
|
|
Loading…
Add table
Reference in a new issue