-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
49 changed files
with
409 additions
and
736 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
.../euphyllia/skyllia/utils/EntityUtils.java → ...hyllia/skyllia/api/utils/EntityUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package fr.euphyllia.skyllia.utils; | ||
package fr.euphyllia.skyllia.api.utils; | ||
|
||
import org.bukkit.entity.*; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...kyllia/utils/models/CallBackPosition.java → ...ia/api/utils/models/CallBackPosition.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
.../skyllia/utils/models/CallbackEntity.java → ...llia/api/utils/models/CallbackEntity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
plugins { | ||
id("java") | ||
} | ||
group = "fr.euphyllia.skyllia"; | ||
version = "1.3"; | ||
|
||
dependencies { | ||
compileOnly("io.papermc.paper:paper-api:1.21.3-R0.1-SNAPSHOT") | ||
} | ||
|
||
java { | ||
sourceCompatibility = JavaVersion.VERSION_21 | ||
targetCompatibility = JavaVersion.VERSION_21 | ||
} | ||
|
||
tasks { | ||
compileJava { | ||
options.encoding = "UTF-8" | ||
} | ||
} |
113 changes: 113 additions & 0 deletions
113
database/src/main/java/fr/euphyllia/skyllia/sgbd/DatabaseLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package fr.euphyllia.skyllia.sgbd; | ||
|
||
import fr.euphyllia.skyllia.sgbd.exceptions.DatabaseException; | ||
|
||
import java.math.BigDecimal; | ||
import java.net.URL; | ||
import java.sql.*; | ||
import java.util.List; | ||
import java.util.concurrent.CompletableFuture; | ||
|
||
import fr.euphyllia.skyllia.sgbd.stream.AsciiStream; | ||
import fr.euphyllia.skyllia.sgbd.stream.BinaryStream; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
public class DatabaseLoader { | ||
|
||
private final MariaDB mariaDB; | ||
|
||
public DatabaseLoader(MariaDB mariaDB) { | ||
this.mariaDB = mariaDB; | ||
} | ||
|
||
public boolean loadDatabase() throws DatabaseException { | ||
if (mariaDB != null && !mariaDB.isConnected()) { | ||
return mariaDB.onLoad(); | ||
} | ||
return false; | ||
} | ||
|
||
public void closeDatabase() { | ||
if (mariaDB != null) { | ||
mariaDB.onClose(); | ||
} | ||
} | ||
|
||
@Nullable | ||
public Connection getMariaDBConnection() throws DatabaseException { | ||
if (mariaDB != null && mariaDB.isConnected()) { | ||
return mariaDB.getConnection(); | ||
} | ||
return null; | ||
} | ||
|
||
public int executeInt(Connection connection, String query, List<?> param) throws SQLException { | ||
return this.getStatementFinal(connection, query, param).join().executeUpdate(); | ||
} | ||
|
||
|
||
private CompletableFuture<PreparedStatement> getStatementFinal(Connection connection, String query, List<?> param) throws SQLException { | ||
CompletableFuture<PreparedStatement> completableFuture = new CompletableFuture<>(); | ||
PreparedStatement statement = connection.prepareStatement(query); | ||
try { | ||
if (param != null) { | ||
int i = 1; | ||
for (Object value : param) { | ||
this.insertStatement(i++, statement, value); | ||
} | ||
} | ||
} finally { | ||
completableFuture.complete(statement); | ||
} | ||
return completableFuture; | ||
} | ||
|
||
private void insertStatement(int i, PreparedStatement statement, Object value) throws SQLException { | ||
if (value instanceof byte[] valueBytes) { | ||
statement.setBytes(i, valueBytes); | ||
} else if (value instanceof Timestamp valueTimes) { | ||
statement.setTimestamp(i, valueTimes); | ||
} else if (value instanceof String valueString) { | ||
statement.setString(i, valueString); | ||
} else if (value instanceof Integer valueInt) { | ||
statement.setInt(i, valueInt); | ||
} else if (value instanceof Double valueDouble) { | ||
statement.setDouble(i, valueDouble); | ||
} else if (value instanceof Long valueLong) { | ||
statement.setLong(i, valueLong); | ||
} else if (value instanceof Byte valueByte) { | ||
statement.setByte(i, valueByte); | ||
} else if (value instanceof Short valueShort) { | ||
statement.setShort(i, valueShort); | ||
} else if (value instanceof Float valueFloat) { | ||
statement.setFloat(i, valueFloat); | ||
} else if (value instanceof BigDecimal valueBigDecimal) { | ||
statement.setBigDecimal(i, valueBigDecimal); | ||
} else if (value instanceof Time valueTime) { | ||
statement.setTime(i, valueTime); | ||
} else if (value instanceof AsciiStream valueAsciiStream) { | ||
statement.setAsciiStream(i, valueAsciiStream.x(), valueAsciiStream.length()); | ||
} else if (value instanceof BinaryStream valueBinaryStream) { | ||
statement.setBinaryStream(i, valueBinaryStream.x(), valueBinaryStream.length()); | ||
} else if (value instanceof Blob valueBlob) { | ||
statement.setBlob(i, valueBlob); | ||
} else if (value instanceof Clob valueClob) { | ||
statement.setClob(i, valueClob); | ||
} else if (value instanceof Array valueArray) { | ||
statement.setArray(i, valueArray); | ||
} else if (value instanceof URL valueURL) { | ||
statement.setURL(i, valueURL); | ||
} else { | ||
statement.setString(i, String.valueOf(value)); | ||
} | ||
} | ||
|
||
public @Nullable ResultSet execute(Connection connection, String query, List<?> param) throws SQLException { | ||
PreparedStatement statement = this.getStatementFinal(connection, query, param).join(); | ||
boolean hasResult = statement.execute(); | ||
if (hasResult) { | ||
return statement.getResultSet(); | ||
} | ||
return null; | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
database/src/main/java/fr/euphyllia/skyllia/sgbd/MariaDB.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package fr.euphyllia.skyllia.sgbd; | ||
|
||
import com.zaxxer.hikari.HikariDataSource; | ||
import fr.euphyllia.skyllia.sgbd.configuration.MariaDBConfig; | ||
import fr.euphyllia.skyllia.sgbd.exceptions.DatabaseException; | ||
import fr.euphyllia.skyllia.sgbd.model.DBConnect; | ||
import fr.euphyllia.skyllia.sgbd.model.DBInterface; | ||
import org.apache.logging.log4j.Level; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.sql.Connection; | ||
import java.sql.SQLException; | ||
|
||
public class MariaDB implements DBConnect, DBInterface { | ||
|
||
private final Logger logger = LogManager.getLogger(MariaDB.class); | ||
private final MariaDBConfig mariaDBConfig; | ||
private HikariDataSource pool; | ||
private boolean connected; | ||
|
||
public MariaDB(final MariaDBConfig configMariaDB) { | ||
this.mariaDBConfig = configMariaDB; | ||
this.connected = false; | ||
} | ||
|
||
|
||
@Override | ||
public boolean onLoad() throws DatabaseException { | ||
this.pool = new HikariDataSource(); | ||
this.pool.setDriverClassName("org.mariadb.jdbc.Driver"); | ||
this.pool.setJdbcUrl("jdbc:mariadb://%s:%s/".formatted(mariaDBConfig.hostname(), mariaDBConfig.port())); | ||
this.pool.setUsername(mariaDBConfig.user()); | ||
this.pool.setPassword(mariaDBConfig.pass()); | ||
this.pool.setConnectionTimeout(mariaDBConfig.timeOut()); | ||
|
||
this.pool.setMaximumPoolSize(mariaDBConfig.maxPool()); | ||
this.pool.setMinimumIdle(mariaDBConfig.maxPool()); | ||
|
||
try (Connection connection = pool.getConnection()) { | ||
if (connection.isValid(1)) { | ||
this.connected = true; | ||
this.logger.log(Level.INFO, "MariaDB pool initialized (%s)".formatted(mariaDBConfig.maxPool())); | ||
return true; | ||
} | ||
} catch (SQLException e) { | ||
logger.log(Level.FATAL, e.getMessage(), e); | ||
throw new DatabaseException(e); | ||
} | ||
return false; | ||
} | ||
|
||
@Override | ||
public void onClose() { | ||
if (this.isConnected()) { | ||
this.pool.close(); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean isConnected() { | ||
return this.connected; | ||
} | ||
|
||
@Override | ||
public @Nullable Connection getConnection() throws DatabaseException { | ||
try { | ||
if (pool.isClosed()) { | ||
return null; | ||
} | ||
return pool.getConnection(); | ||
} catch (SQLException e) { | ||
logger.log(Level.FATAL, e.getMessage(), e); | ||
throw new DatabaseException(e); | ||
} | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
database/src/main/java/fr/euphyllia/skyllia/sgbd/configuration/MariaDBConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package fr.euphyllia.skyllia.sgbd.configuration; | ||
|
||
public record MariaDBConfig(String hostname, String port, String user, String pass, | ||
Boolean useSSL, | ||
Integer maxPool, Integer timeOut, String database, int dbVersion) { | ||
} |
16 changes: 16 additions & 0 deletions
16
database/src/main/java/fr/euphyllia/skyllia/sgbd/exceptions/DatabaseException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package fr.euphyllia.skyllia.sgbd.exceptions; | ||
|
||
public class DatabaseException extends Exception { | ||
|
||
public DatabaseException(String message) { | ||
super(message); | ||
} | ||
|
||
public DatabaseException(Throwable cause) { | ||
super(cause); | ||
} | ||
|
||
public DatabaseException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
} |
Oops, something went wrong.