Arcturus-Community/src/main/java/com/eu/habbo/database/Database.java

114 lines
3.6 KiB
Java
Raw Normal View History

2018-07-06 15:30:00 +02:00
package com.eu.habbo.database;
import com.eu.habbo.Emulator;
import com.eu.habbo.core.ConfigurationManager;
import com.zaxxer.hikari.HikariDataSource;
import gnu.trove.map.hash.THashMap;
import gnu.trove.set.hash.THashSet;
2020-05-03 01:46:07 +02:00
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
2018-07-06 15:30:00 +02:00
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
2019-05-26 20:14:53 +02:00
public class Database {
2018-07-08 23:32:00 +02:00
2020-05-03 01:46:07 +02:00
private static final Logger LOGGER = LoggerFactory.getLogger(Database.class);
2018-07-08 23:32:00 +02:00
2020-05-03 01:46:07 +02:00
private HikariDataSource dataSource;
2018-07-06 15:30:00 +02:00
private DatabasePool databasePool;
2019-05-26 20:14:53 +02:00
public Database(ConfigurationManager config) {
2018-07-06 15:30:00 +02:00
long millis = System.currentTimeMillis();
boolean SQLException = false;
2019-05-26 20:14:53 +02:00
try {
2018-07-06 15:30:00 +02:00
this.databasePool = new DatabasePool();
2019-05-26 20:14:53 +02:00
if (!this.databasePool.getStoragePooling(config)) {
2020-05-03 01:46:07 +02:00
LOGGER.info("Failed to connect to the database. Please check config.ini and make sure the MySQL process is running. Shutting down...");
2018-07-06 15:30:00 +02:00
SQLException = true;
return;
}
this.dataSource = this.databasePool.getDatabase();
2019-05-26 20:14:53 +02:00
} catch (Exception e) {
2018-07-06 15:30:00 +02:00
SQLException = true;
2020-05-03 01:46:07 +02:00
LOGGER.error("Failed to connect to your database.", e);
2019-05-26 20:14:53 +02:00
} finally {
2020-05-03 01:46:07 +02:00
if (SQLException) {
2018-07-06 15:30:00 +02:00
Emulator.prepareShutdown();
2020-05-03 01:46:07 +02:00
}
2018-07-06 15:30:00 +02:00
}
2020-05-03 01:46:07 +02:00
LOGGER.info("Database -> Connected! ({} MS)", System.currentTimeMillis() - millis);
2018-07-06 15:30:00 +02:00
}
2019-05-26 20:14:53 +02:00
public void dispose() {
if (this.databasePool != null) {
2018-07-06 15:30:00 +02:00
this.databasePool.getDatabase().close();
}
this.dataSource.close();
}
2019-05-26 20:14:53 +02:00
public HikariDataSource getDataSource() {
2018-07-06 15:30:00 +02:00
return this.dataSource;
}
2019-05-26 20:14:53 +02:00
public DatabasePool getDatabasePool() {
2018-07-06 15:30:00 +02:00
return this.databasePool;
}
public static PreparedStatement preparedStatementWithParams(Connection connection, String query, THashMap<String, Object> queryParams) throws SQLException {
THashMap<Integer, Object> params = new THashMap<Integer, Object>();
THashSet<String> quotedParams = new THashSet<>();
for(String key : queryParams.keySet()) {
quotedParams.add(Pattern.quote(key));
}
String regex = "(" + String.join("|", quotedParams) + ")";
Matcher m = Pattern.compile(regex).matcher(query);
int i = 1;
while (m.find()) {
try {
params.put(i, queryParams.get(m.group(1)));
i++;
}
catch (Exception ignored) { }
}
PreparedStatement statement = connection.prepareStatement(query.replaceAll(regex, "?"));
for(Map.Entry<Integer, Object> set : params.entrySet()) {
if(set.getValue().getClass() == String.class) {
statement.setString(set.getKey(), (String)set.getValue());
}
else if(set.getValue().getClass() == Integer.class) {
statement.setInt(set.getKey(), (Integer)set.getValue());
}
else if(set.getValue().getClass() == Double.class) {
statement.setDouble(set.getKey(), (Double)set.getValue());
}
else if(set.getValue().getClass() == Float.class) {
statement.setFloat(set.getKey(), (Float)set.getValue());
}
else if(set.getValue().getClass() == Long.class) {
statement.setLong(set.getKey(), (Long)set.getValue());
}
else {
statement.setObject(set.getKey(), set.getValue());
}
}
return statement;
}
2018-07-06 15:30:00 +02:00
}