Basic rewrite of MySQL statements
- Migrated MySQL to use https://github.com/odinfather/Database Instead of custom - Closed all SQL statements (Hopefully, will have to double-check)
This commit is contained in:
324
src/main/java/me/majrly/database/Database.java
Normal file
324
src/main/java/me/majrly/database/Database.java
Normal file
@@ -0,0 +1,324 @@
|
||||
package me.majrly.database;
|
||||
|
||||
import com.zaxxer.hikari.HikariConfig;
|
||||
import com.zaxxer.hikari.HikariDataSource;
|
||||
import me.majrly.database.params.Parameter;
|
||||
import me.majrly.database.statements.Query;
|
||||
import me.majrly.database.statements.Statement;
|
||||
|
||||
import java.sql.*;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* Database API
|
||||
*
|
||||
* @author Majrly
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public class Database {
|
||||
|
||||
// Variables
|
||||
private String name;
|
||||
private String hostname;
|
||||
private String username;
|
||||
private String password;
|
||||
private String database;
|
||||
|
||||
private int port = 3306;
|
||||
private int timeout = 60 * 1000;
|
||||
|
||||
private HikariDataSource source;
|
||||
private HikariConfig config = new HikariConfig();
|
||||
|
||||
/**
|
||||
* Database API
|
||||
*
|
||||
* @param name The name of this database instance
|
||||
* @param hostname The ip to use when connecting
|
||||
* @param username The username to authenticate as
|
||||
* @param password The password to authenticate yourself
|
||||
* @param database The name of the database to switch to
|
||||
* @param port The port to use when connecting
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public Database(String name, String hostname, String username, String password, String database, int port, HikariConfig config) {
|
||||
this.config = config;
|
||||
this.config.setJdbcUrl("jdbc:" + (this.name = name) + "://" + (this.hostname = hostname) + ":" + (this.port = port) + "/" + (this.database = database));
|
||||
this.config.setDriverClassName("com.mysql.jdbc.Driver");
|
||||
this.config.setUsername(this.username = username);
|
||||
this.config.setPassword(this.password = password);
|
||||
this.source = new HikariDataSource(config);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the database options class
|
||||
*
|
||||
* @return A reference to {@link DatabaseOptions}
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public static DatabaseOptions options() {
|
||||
return new DatabaseOptions();
|
||||
}
|
||||
|
||||
/**
|
||||
* Connects to the database
|
||||
*
|
||||
* @return Whether it connected or not
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public void init() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a query to the database
|
||||
*
|
||||
* @param statement The statement to send the database
|
||||
* @return Either the int of an update, or the ResultSet of a query
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public Optional<?> send(Statement statement) {
|
||||
Optional<PreparedStatement> optional = prepare(statement);
|
||||
if (!optional.isPresent()) return Optional.empty();
|
||||
PreparedStatement preparedStatement = optional.get();
|
||||
try {
|
||||
if (statement instanceof Query) {
|
||||
return Optional.of(preparedStatement.executeQuery());
|
||||
} else {
|
||||
return Optional.of(preparedStatement.executeUpdate());
|
||||
}
|
||||
} catch (SQLException exception) {
|
||||
exception.printStackTrace();
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Close a prepared statement
|
||||
*
|
||||
* @param preparedStatement The prepared statement you wish to close
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public void closeStatement(PreparedStatement preparedStatement) {
|
||||
try {
|
||||
if (preparedStatement != null && !preparedStatement.isClosed()) {
|
||||
preparedStatement.close();
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
// Can't handle closing statement
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare a statement
|
||||
*
|
||||
* @param statement The statement with parameters you wish to prepare
|
||||
* @return The optional value of {@link PreparedStatement}
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public Optional<PreparedStatement> prepare(Statement statement) {
|
||||
try {
|
||||
PreparedStatement preparedStatement = source.getConnection().prepareStatement(statement.getSQL());
|
||||
for (Map.Entry<Integer, Parameter> parameter : statement.getParameters().entrySet()) {
|
||||
switch (parameter.getValue().getType()) {
|
||||
case STRING:
|
||||
preparedStatement.setString(parameter.getKey(), (String) parameter.getValue().getData());
|
||||
break;
|
||||
case INTEGER:
|
||||
preparedStatement.setInt(parameter.getKey(), (Integer) parameter.getValue().getData());
|
||||
break;
|
||||
case DOUBLE:
|
||||
preparedStatement.setDouble(parameter.getKey(), (Double) parameter.getValue().getData());
|
||||
break;
|
||||
case LONG:
|
||||
preparedStatement.setLong(parameter.getKey(), (Long) parameter.getValue().getData());
|
||||
break;
|
||||
case BLOB:
|
||||
preparedStatement.setBlob(parameter.getKey(), (Blob) parameter.getValue().getData());
|
||||
break;
|
||||
case FLOAT:
|
||||
preparedStatement.setFloat(parameter.getKey(), (Float) parameter.getValue().getData());
|
||||
break;
|
||||
case BOOLEAN:
|
||||
preparedStatement.setBoolean(parameter.getKey(), (Boolean) parameter.getValue().getData());
|
||||
break;
|
||||
case DATE:
|
||||
preparedStatement.setDate(parameter.getKey(), (Date) parameter.getValue().getData());
|
||||
break;
|
||||
case OBJECT:
|
||||
preparedStatement.setObject(parameter.getKey(), parameter.getValue().getData());
|
||||
break;
|
||||
default:
|
||||
preparedStatement.setObject(parameter.getKey(), parameter.getValue().getData());
|
||||
break;
|
||||
}
|
||||
}
|
||||
return Optional.of(preparedStatement);
|
||||
} catch (SQLException exception) {
|
||||
exception.printStackTrace();
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare a statement
|
||||
*
|
||||
* @param sql The statement you want to prepare
|
||||
* @return The optional value of {@link PreparedStatement}
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public Optional<PreparedStatement> prepare(String sql) {
|
||||
try {
|
||||
return Optional.of(source.getConnection().prepareStatement(sql));
|
||||
} catch (SQLException exception) {
|
||||
exception.printStackTrace();
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the connection of MySQL
|
||||
*
|
||||
* @return The optional value of {@link Connection}
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public Optional<Connection> getConnection() {
|
||||
try {
|
||||
return Optional.of(source.getConnection());
|
||||
} catch (SQLException e) {
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Closes the database
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public void close() {
|
||||
source.close();
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public HikariConfig getConfig() {
|
||||
return config;
|
||||
}
|
||||
|
||||
public HikariDataSource getSource() {
|
||||
return source;
|
||||
}
|
||||
|
||||
/**
|
||||
* Database options used for {@link Database}
|
||||
*
|
||||
* @author Majrly
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public static class DatabaseOptions {
|
||||
|
||||
// Variables
|
||||
private HikariConfig config = new HikariConfig();
|
||||
|
||||
private String name;
|
||||
private String hostname = "127.0.0.1";
|
||||
private String username = "root";
|
||||
private String password;
|
||||
private String database;
|
||||
|
||||
private int port = 3306;
|
||||
private int timeout = 60 * 1000;
|
||||
|
||||
/**
|
||||
* Set a key/value in the HikariConfig
|
||||
*
|
||||
* @param key The key you want to set a value to
|
||||
* @param value The value you want to set
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public DatabaseOptions set(String key, String value) {
|
||||
config.addDataSourceProperty(key, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the hostname / port to connect
|
||||
*
|
||||
* @param hostname The hostname of the database
|
||||
* @param port The port of the database
|
||||
* @return This object
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public DatabaseOptions hostname(String hostname, int port) {
|
||||
this.database = database;
|
||||
this.port = port;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the authentication username and password
|
||||
*
|
||||
* @param username The user you want to authenticate as
|
||||
* @param password The password you want to authenticate with
|
||||
* @return This object
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public DatabaseOptions auth(String username, String password) {
|
||||
this.username = username;
|
||||
this.password = password;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the database to switch to
|
||||
*
|
||||
* @param database The database you want to switch to
|
||||
* @return This object
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public DatabaseOptions database(String database) {
|
||||
this.database = database;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the name of the database connection
|
||||
*
|
||||
* @param name The name of the database connection
|
||||
* @return This object
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public DatabaseOptions type(String name) {
|
||||
this.name = name;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the timeout of the connection
|
||||
*
|
||||
* @param timeout The max amount of time to connect
|
||||
* @return This object
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public DatabaseOptions timeout(int timeout) {
|
||||
this.timeout = timeout;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build this class
|
||||
*
|
||||
* @return The database object
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public Database build() {
|
||||
if (username.isEmpty()) {
|
||||
username = "root";
|
||||
}
|
||||
return new Database(name, hostname, username, password, database, port, config);
|
||||
}
|
||||
}
|
||||
}
|
||||
40
src/main/java/me/majrly/database/DatabaseManager.java
Normal file
40
src/main/java/me/majrly/database/DatabaseManager.java
Normal file
@@ -0,0 +1,40 @@
|
||||
package me.majrly.database;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
/**
|
||||
* Apart of the database api to manage all your databases
|
||||
*
|
||||
* @author Majrly
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public class DatabaseManager {
|
||||
|
||||
private static HashMap<String, Database> databases = new HashMap<>();
|
||||
|
||||
/**
|
||||
* Add a database to {@link #databases}
|
||||
*
|
||||
* @param database The database you want to add
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public static void addDatabase(Database database) {
|
||||
databases.put(database.getName(), database);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a database with specified name from {@link #databases}
|
||||
*
|
||||
* @param name The name of the database you want to obtain
|
||||
* @return The database wrapper
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public static Database getDatabase(String name) {
|
||||
return databases.get(name);
|
||||
}
|
||||
|
||||
// Getters
|
||||
public static HashMap<String, Database> getDatabases() {
|
||||
return databases;
|
||||
}
|
||||
}
|
||||
54
src/main/java/me/majrly/database/params/Parameter.java
Normal file
54
src/main/java/me/majrly/database/params/Parameter.java
Normal file
@@ -0,0 +1,54 @@
|
||||
package me.majrly.database.params;
|
||||
|
||||
/**
|
||||
* Apart of the Database API to set parameters (AKA those little question marks in sql statements)
|
||||
*
|
||||
* @author Majrly
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public class Parameter {
|
||||
|
||||
// Variables
|
||||
private Object data;
|
||||
private Type type = Type.OBJECT;
|
||||
|
||||
/**
|
||||
* Used to set parameters
|
||||
*
|
||||
* @param type The type of data you want sent
|
||||
* @param data The data
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public Parameter(Type type, Object data) {
|
||||
this.type = type;
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Used to set parameters
|
||||
*
|
||||
* @param data The object data
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public Parameter(Object data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
/**
|
||||
* The type of data you want sent
|
||||
*
|
||||
* @author Majrly
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public enum Type {
|
||||
STRING, BLOB, INTEGER, DOUBLE, FLOAT, DATE, LONG, BOOLEAN, OBJECT;
|
||||
}
|
||||
|
||||
public Object getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
public Type getType() {
|
||||
return type;
|
||||
}
|
||||
}
|
||||
14
src/main/java/me/majrly/database/statements/Query.java
Normal file
14
src/main/java/me/majrly/database/statements/Query.java
Normal file
@@ -0,0 +1,14 @@
|
||||
package me.majrly.database.statements;
|
||||
|
||||
/**
|
||||
* Apart of the Database API to differentiate between various Statements
|
||||
*
|
||||
* @author Majrly
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public class Query extends Statement {
|
||||
|
||||
public Query(String sql) {
|
||||
super(sql);
|
||||
}
|
||||
}
|
||||
120
src/main/java/me/majrly/database/statements/Statement.java
Normal file
120
src/main/java/me/majrly/database/statements/Statement.java
Normal file
@@ -0,0 +1,120 @@
|
||||
package me.majrly.database.statements;
|
||||
|
||||
import me.majrly.database.params.Parameter;
|
||||
|
||||
import java.sql.Blob;
|
||||
import java.sql.Date;
|
||||
import java.util.HashMap;
|
||||
|
||||
/**
|
||||
* Apart of the Database API to create MySQL statements
|
||||
*
|
||||
* @author Majrly
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public class Statement {
|
||||
|
||||
// Variables
|
||||
private String sql;
|
||||
private int amount = 1;
|
||||
private HashMap<Integer, Parameter> parameters = new HashMap<Integer, Parameter>();
|
||||
|
||||
/**
|
||||
* Used to create SQL statements
|
||||
*
|
||||
* @param sql The SQL string
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public Statement(String sql) {
|
||||
this.sql = sql;
|
||||
}
|
||||
|
||||
/**
|
||||
* Escape a string
|
||||
*
|
||||
* @param data The string you want to escape
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public void setString(String data) {
|
||||
this.parameters.put(amount++, new Parameter(Parameter.Type.STRING, data));
|
||||
}
|
||||
|
||||
/**
|
||||
* Escape a string
|
||||
*
|
||||
* @param data The Blob you want to escape
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public void setBlob(Blob data) {
|
||||
this.parameters.put(amount++, new Parameter(Parameter.Type.BLOB, data));
|
||||
}
|
||||
|
||||
/**
|
||||
* Escape a double
|
||||
*
|
||||
* @param data The double you want to escape
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public void setDouble(double data) {
|
||||
this.parameters.put(amount++, new Parameter(Parameter.Type.DOUBLE, data));
|
||||
}
|
||||
|
||||
/**
|
||||
* Escape an integer
|
||||
*
|
||||
* @param data The integer you want to escape
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public void setInteger(int data) {
|
||||
this.parameters.put(amount++, new Parameter(Parameter.Type.INTEGER, data));
|
||||
}
|
||||
|
||||
/**
|
||||
* Escape a float
|
||||
*
|
||||
* @param data The float you want to escape
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public void setFloat(float data) {
|
||||
this.parameters.put(amount++, new Parameter(Parameter.Type.FLOAT, data));
|
||||
}
|
||||
|
||||
/**
|
||||
* Escape a date
|
||||
*
|
||||
* @param data The date you want to escape
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public void setDate(Date data) {
|
||||
this.parameters.put(amount++, new Parameter(Parameter.Type.DATE, data));
|
||||
}
|
||||
|
||||
/**
|
||||
* Escape a boolean
|
||||
*
|
||||
* @param data The boolean you want to escape
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public void setBoolean(boolean data) {
|
||||
this.parameters.put(amount++, new Parameter(Parameter.Type.BOOLEAN, data));
|
||||
}
|
||||
|
||||
/**
|
||||
* Escape an object
|
||||
*
|
||||
* @param data The object you want to escape
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public void setObject(Object data) {
|
||||
this.parameters.put(amount++, new Parameter(Parameter.Type.OBJECT, data));
|
||||
}
|
||||
|
||||
// Getters
|
||||
public String getSQL() {
|
||||
return sql;
|
||||
}
|
||||
|
||||
public HashMap<Integer, Parameter> getParameters() {
|
||||
return parameters;
|
||||
}
|
||||
}
|
||||
14
src/main/java/me/majrly/database/statements/Update.java
Normal file
14
src/main/java/me/majrly/database/statements/Update.java
Normal file
@@ -0,0 +1,14 @@
|
||||
package me.majrly.database.statements;
|
||||
|
||||
/**
|
||||
* Apart of the Database API to differentiate between various Statements
|
||||
*
|
||||
* @author Majrly
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public class Update extends Statement {
|
||||
|
||||
public Update(String sql) {
|
||||
super(sql);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user