Added initial version

This commit is contained in:
Ceikry 2021-03-07 20:37:32 -06:00
commit b1dccb3fed
14153 changed files with 1285206 additions and 0 deletions

View file

@ -0,0 +1,121 @@
package ms.system.mysql;
/**
* Represents a column used in an SQL table.
* @author Emperor
*
*/
public final class SQLColumn {
/**
* The name.
*/
private final String name;
/**
* The data type.
*/
private final Class<?> type;
/**
* If this column should never be updated in the database.
*/
private final boolean neverUpdate;
/**
* The value.
*/
private Object value;
/**
* If the column value changed.
*/
private boolean changed;
/**
* Constructs a new {@code SQLColumn} {@code Object}.
* @param name The column name.
* @param type The data type.
*/
public SQLColumn(String name, Class<?> type) {
this(name, type, false);
}
/**
* Constructs a new {@code SQLColumn} {@code Object}.
* @param name The column name.
* @param type The data type.
* @param neverUpdate If this column should never be updated in the database.
*/
public SQLColumn(String name, Class<?> type, boolean neverUpdate) {
this.name = name;
this.type = type;
this.neverUpdate = neverUpdate;
}
/**
* Gets the name.
* @return The name.
*/
public String getName() {
return name;
}
/**
* Updates the value.
* @param value The value.
*/
public void updateValue(Object value) {
this.changed = value != this.value;
this.value = value;
}
/**
* Gets the value.
* @return The value.
*/
public Object getValue() {
return value;
}
/**
* Sets the value.
* @param value The value to set.
*/
public void setValue(Object value) {
this.value = value;
this.changed = false;
}
/**
* Gets the changed.
* @return The changed.
*/
public boolean isChanged() {
return changed;
}
/**
* Sets the changed.
* @param changed The changed to set.
*/
public void setChanged(boolean changed) {
this.changed = changed;
}
/**
* Gets the type.
* @return The type.
*/
public Class<?> getType() {
return type;
}
/**
* Gets the neverUpdate.
* @return The neverUpdate.
*/
public boolean isNeverUpdate() {
return neverUpdate;
}
}

View file

@ -0,0 +1,202 @@
package ms.system.mysql;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* Handles an SQL entry.
* @author Emperor
* @param <T> The entry type.
*/
public abstract class SQLEntryHandler<T> {
/**
* The connection.
*/
protected Connection connection;
/**
* The entry.
*/
protected T entry;
/**
* The result set.
*/
protected ResultSet result;
/**
* The table name.
*/
protected String table;
/**
* The column name.
*/
private String column;
/**
* The value to use.
*/
protected String value;
/**
* Constructs a new {@code SQLEntryHandler} {@code Object}.
* @param entry The entry.
* @param table The table name.
* @param column column name.
* @param value The column value.
*/
public SQLEntryHandler(T entry, String table, String column, String value) {
this.entry = entry;
this.table = table;
this.column = column;
this.value = value;
}
/**
* Reads the SQL entry.
* @param entry The entry.
*/
public static void read(SQLEntryHandler<?> entry) {
entry.connection = entry.getConnection();
if (entry.connection == null) {
//System.err.println("Could not read SQL data: connection is null!");
return;
}
try {
entry.read();
if (entry.result == null || !entry.result.next()) {
entry.create();
} else {
entry.parse();
}
} catch (SQLException e) {
e.printStackTrace();
}
SQLManager.close(entry.connection);
entry.connection = null;
}
/**
* Writes the entry data on the SQL database.
* @param entry The entry.
*/
public static void write(SQLEntryHandler<?> entry) {
write(entry, entry.getConnection(), true);
}
/**
* Reads the SQL entry.
* @param entry The entry.
*/
public static void write(SQLEntryHandler<?> entry, Connection connection, boolean commit) {
if (connection == null) {
//System.err.println("Could not write SQL data: connection is null!");
return;
}
entry.connection = connection;
try {
if (!commit) {
entry.connection.setAutoCommit(false);
}
entry.save();
} catch (SQLException e) {
e.printStackTrace();
}
if (commit) {
SQLManager.close(entry.connection);
}
entry.connection = null;
}
/**
* Reads the table from the SQL database.
* @throws SQLException When an SQL exception occurs.
*/
public void read() throws SQLException {
PreparedStatement statement = connection.prepareStatement("SELECT " + getReadSelection() + " FROM " + table + " WHERE " + column + " = ?");
statement.setString(1, value);
result = statement.executeQuery();
}
/**
* Gets the result.
* @return the result.
*/
public ResultSet getResult() {
return result;
}
/**
* Gets the selection.
* @return The selection.
*/
public String getReadSelection() {
return "*";
}
/**
* Gets the writing statement.
* @param create If we are creating a new row.
* @param columns The columns to update.
*/
public PreparedStatement getWritingStatement(boolean create, String...columns) {
PreparedStatement statement = null;
try {
StringBuilder sb = new StringBuilder();
if (create) {
sb.append("INSERT INTO ").append(table).append(" (").append(column);
for (String name : columns) {
sb.append(",").append(name);
}
sb.append(") VALUES (?");
for (int i = 0; i < columns.length; i++) {
sb.append(",?");
}
sb.append(")");
} else {
sb.append("UPDATE ").append(table).append(" SET ");
for (int i = 0; i < columns.length; i++) {
sb.append(columns[i]).append("=?");
if (i < columns.length - 1) {
sb.append(",");
}
}
sb.append(" WHERE ").append(column).append("=?");
}
statement = connection.prepareStatement(sb.toString());
statement.setString(create ? 1 : columns.length + 1, value);
} catch (SQLException e) {
e.printStackTrace();
}
return statement;
}
/**
* Parses the entry.
* @throws SQLException When an SQL exception occurs.
*/
public abstract void parse() throws SQLException;
/**
* Creates a new row in the database.
* @throws SQLException When an SQL exception occurs.
*/
public abstract void create() throws SQLException;
/**
* Saves the entry.
* @throws SQLException When an SQL exception occurs.
*/
public abstract void save() throws SQLException;
/**
* Gets the connection.
* @return The connection.
*/
public abstract Connection getConnection();
}

View file

@ -0,0 +1,100 @@
package ms.system.mysql;
import ms.ServerConstants;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
/**
* Manages the sql connections.
* @author Vexia
*
*/
public final class SQLManager {
/**
* If the sql manager is locally hosted.
*/
public static final boolean LOCAL = true;
/**
* The database URL.
*/
public static final String DATABASE_URL = (LOCAL ? "127.0.0.1" : "keldagrim.org") + ":3306/" + (SQLManager.LOCAL ? "global" : ServerConstants.DATABASE_NAMES[1]);
/**
* The username of the user.
*/
private static final String USERNAME = (LOCAL ? "root" : "keldagr1_user");
/**
* The password of the user.
*/
private static final String PASSWORD = (LOCAL ? "" : "2jf4wkz$");
/**
* IF the sql manager is initialized.
*/
private static boolean initialized;
/**
* Constructs a new {@code SQLManager} {@code Object}
*/
public SQLManager() {
/**
* empty.
*/
}
/**
* Initializes the sql manager.
*/
public static void init() {
initialized = true;
WorldListSQLHandler.clearWorldList();
}
/**
* Gets a connection from the pool.
* @return The connection.
*/
public static Connection getConnection() {
try {
return DriverManager.getConnection("jdbc:mysql://" + DATABASE_URL + "?useTimezone=true&serverTimezone=UTC", USERNAME, PASSWORD);
} catch (SQLException e) {
System.out.println("Error: Mysql error message=" + e.getMessage() + ".");
}
return null;
}
/**
* Releases the connection so it's available for usage.
* @param connection The connection.
*/
public static void close(Connection connection) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* Gets the initialized.
* @return the initialized
*/
public static boolean isInitialized() {
return initialized;
}
/**
* Sets the bainitialized.
* @param initialized the initialized to set.
*/
public static void setInitialized(boolean initialized) {
SQLManager.initialized = initialized;
}
}

View file

@ -0,0 +1,63 @@
package ms.system.mysql;
import java.util.ArrayList;
import java.util.List;
/**
* Represents an SQL table.
* @author Emperor
*
*/
public final class SQLTable {
/**
* The columns.
*/
private final SQLColumn[] columns;
/**
* Constructs a new {@code SQLTable} {@code Object}.
* @param columns The columns.
*/
public SQLTable(SQLColumn...columns) {
this.columns = columns;
}
/**
* Gets the column for the given name.
* @param name The column name.
* @return The column.
*/
public SQLColumn getColumn(String name) {
for (SQLColumn column : columns) {
if (column.getName().equals(name)) {
return column;
}
}
return null;
}
/**
* Gets the changed columns.
* @return The columns.
*/
public List<SQLColumn> getChanged() {
List<SQLColumn> updated = new ArrayList<>();
for (int i = 0; i < columns.length; i++) {
SQLColumn column = columns[i];
if (column.isChanged()) {
updated.add(column);
}
}
return updated;
}
/**
* Gets the columns.
* @return The columns.
*/
public SQLColumn[] getColumns() {
return columns;
}
}

View file

@ -0,0 +1,80 @@
package ms.system.mysql;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import ms.world.GameServer;
import ms.world.info.WorldInfo;
/**
* Handles the world list SQL database.
* @author Emperor
*
*/
public final class WorldListSQLHandler extends SQLEntryHandler<GameServer> {
/**
* The table for this sql entry.
*/
private static final String TABLE = "worlds";
/**
* Constructs a new {@code WorldListSQLHandler} {@code Object}.
* @param entry The game server entry.
*/
public WorldListSQLHandler(GameServer entry) {
super(entry, TABLE, "world", "" + entry.getInfo().getWorldId());
}
/**
* Clears the world list.
*/
public static void clearWorldList() {
try {
Connection connection = SQLManager.getConnection();
PreparedStatement statement = connection.prepareStatement("DELETE FROM " + TABLE);
statement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void parse() throws SQLException {
}
@Override
public void create() throws SQLException {
PreparedStatement statement = connection.prepareStatement("INSERT "+ table + "(world, ip, players, country, member, revision) VALUES('" + value + "', '" + entry.getInfo().getAddress() + "', '" + entry.getPlayerAmount() + "', '" + entry.getInfo().getCountry().getId() + "', '" + (entry.getInfo().isMembers() ? 1 : 0) + "', '" + entry.getInfo().getRevision() + "')");
statement.executeUpdate();
SQLManager.close(statement.getConnection());
}
@Override
public void save() throws SQLException {
super.read();
if (result == null || !result.next()) {
create();
return;
}
int players = entry.getPlayerAmount();
WorldInfo info = entry.getInfo();
if (!entry.isActive()) {
players = -1;
}
if (players <= 0) {
PreparedStatement statement = connection.prepareStatement("UPDATE members SET online='0' WHERE lastWorld='" + value + "'");
statement.executeUpdate();
}
PreparedStatement statement = connection.prepareStatement("UPDATE " + table + " SET players='" + players + "', ip='" + info.getAddress() + "', country='" + info.getCountry().getId() + "', member='" + (info.isMembers() ? 1 : 0) + "', revision='" + info.getRevision() + "' WHERE world='" + value + "'");
statement.executeUpdate();
SQLManager.close(statement.getConnection());
}
@Override
public Connection getConnection() {
return SQLManager.getConnection();
}
}