Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
4.8 kB
2
Indexable
Never
package me.teixayo.bedwars.api.database;

import lombok.SneakyThrows;
import org.bukkit.Bukkit;

import java.sql.*;
import java.util.UUID;

public class MySQL {

    private Connection connection=null;

    private String host;
    private String port;
    private String database;
    private String username;
    private String password;
    private boolean useSSL;

    public MySQL(String host, String port, String database, String username, String password, boolean useSSL) {
        this.host = host;
        this.port= port;
        this.database = database;
        this.username = username;
        this.password = password;
        this.useSSL = useSSL;
    }

    public boolean isConnected() {
       return connection!=null;
    }
    public void connect() {
        if(isConnected()) return;
        try {
            Class.forName("org.mariadb.jdbc.Driver").newInstance();
            connection=DriverManager.getConnection("jdbc:mariadb://" + host + ":" + port + "/" + database + "?user=" + username + "&password=" + password);

            Bukkit.getLogger().info("successfully connect from database");
        } catch (Exception e) {
            Bukkit.getLogger().warning("Failed to connect with database:");
            e.printStackTrace();
        }

    }
    public void disconnect() {
        if(!isConnected()) return;
        try {
            connection.close();
            Bukkit.getLogger().info("successfully disconnect from database");
        } catch (SQLException e) {
            Bukkit.getLogger().warning("Failed to disconnect with database:");
            e.printStackTrace();
        }
    }

    public Connection getConnection() {
        return connection;
    }

    public void createStatement(String name, String columns) {

        PreparedStatement ps;
        try {
            ps = connection.prepareStatement("CREATE TABLE IF NOT EXISTS " + name + " " + columns);
            ps.executeUpdate();
            ps.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @SneakyThrows
    public PreparedStatement prepareStatement(String sql, Object... objectsToSet) {
        PreparedStatement statement = connection.prepareStatement(sql);
        for (int i = 0; i < objectsToSet.length; i++) statement.setObject(i + 1, objectsToSet[i]);
        return statement;
    }

    @SneakyThrows
    public void createTable(String tableName, String columns) {
        String id = "id INT AUTO_INCREMENT PRIMARY KEY";
        String query = "CREATE TABLE IF NOT EXISTS " + tableName + "(" + id + ", uniqueId VARCHAR(36) NOT NULL, " + columns + ")";
        PreparedStatement statement = prepareStatement(query);

        statement.executeUpdate();
        statement.close();
    }

    @SneakyThrows
    public boolean exists(String table, UUID uniqueId) {
        String query = "SELECT * FROM " + table + " WHERE uniqueId = ?";
        PreparedStatement statement = prepareStatement(query, uniqueId.toString());
        ResultSet resultSet = statement.executeQuery();
        boolean exists = resultSet.next();

        statement.close();
        resultSet.close();

        return exists;
    }

    @SneakyThrows
    public Object get(String table, UUID uniqueId, String column) {
        String query = "SELECT * FROM " + table + " WHERE uniqueId = ?";
        PreparedStatement statement = prepareStatement(query, uniqueId.toString());
        ResultSet resultSet = statement.executeQuery();

        if (!resultSet.next()) return null;
        Object result = resultSet.getObject(column);

        statement.close();
        resultSet.close();

        return result;
    }

    @SneakyThrows
    public void setIfAbsent(String table, DatabaseContainer databaseContainer, UUID uniqueId, Object... objects) {
        if (exists(table, uniqueId)) return;

        String query = "INSERT INTO " + table + databaseContainer.getColumnsAndQuestionMarks();

        Object[] objectToSet = new Object[objects.length + 1];
        objectToSet[0] = uniqueId.toString();
        System.arraycopy(objects, 0, objectToSet, 1, objects.length);
        PreparedStatement statement = prepareStatement(query, objectToSet);

        statement.executeUpdate();
        statement.close();
    }

    @SneakyThrows
    public void update(String table, UUID uniqueId, String column, Object objectToSet) {
        if (!exists(table, uniqueId)) return;

        String query = "UPDATE " + table + " SET " + column + " = ? WHERE uniqueId = ?";
        PreparedStatement statement = prepareStatement(query, objectToSet, uniqueId.toString());

        statement.executeUpdate();
        statement.close();
    }
}