Skip to content

Instantly share code, notes, and snippets.

@TommyAlmeida
Last active September 11, 2019 08:53
Show Gist options
  • Save TommyAlmeida/c5d3f3cc9f637f3f00e9d7fcb61b9e90 to your computer and use it in GitHub Desktop.
Save TommyAlmeida/c5d3f3cc9f637f3f00e9d7fcb61b9e90 to your computer and use it in GitHub Desktop.
Vamos supor que o teu plugin poderá utilizar Sql numas coisas e MongoDb noutras, isto vai resolver o teu problema de uma maneira correta.
import java.util.Properties;
import java.util.function.Consumer;
public interface DatabaseMetaBuilder {
DatabaseMetaBuilder with(Consumer<DatabaseMetaBuilder> consumer);
void addProperty(String key, String value);
Properties wrap();
class Impl implements DatabaseMetaBuilder {
private Properties connectionProps;
public Impl() {
connectionProps = new Properties();
}
@Override
public DatabaseMetaBuilder with(Consumer<DatabaseMetaBuilder> consumer) {
consumer.accept(this);
return this;
}
@Override
public void addProperty(String key, String value) {
connectionProps.setProperty(key, value);
}
@Override
public Properties wrap() {
return connectionProps;
}
}
}
import java.util.function.Consumer;
public interface DatabaseProvider {
void connect();
void close();
}
import eu.unionmc.nations.database.DatabaseProvider;
import eu.unionmc.nations.database.SqlProvider;
import eu.unionmc.nations.database.DatabaseMetaBuilder;
import org.bukkit.plugin.java.JavaPlugin;
public class Main extends JavaPlugin {
private DatabaseProvider databaseProvider;
@Override
public void onLoad() {
DatabaseMetaBuilder metaBuilder = new DatabaseMetaBuilder.Impl().with(
$ -> {
$.addProperty("hostname", "localhost");
$.addProperty("username", "root");
$.addProperty("password", "123");
$.addProperty("port", SqlProvider.DEFAULT_PORT);
}
);
databaseProvider = new SqlProvider(metaBuilder.wrap());
}
@Override
public void onEnable() {
databaseProvider.connect();
}
@Override
public void onDisable() {
databaseProvider.close();
}
}
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
import java.util.function.Consumer;
public class SqlProvider implements DatabaseProvider {
public static final String DEFAULT_PORT = "3306";
private final String DEFAULT_DBMS = "mysql";
private Connection connection;
private Properties connectionProps;
public SqlProvider(Properties connectionProps) {
connection = null;
this.connectionProps = connectionProps;
}
@Override
public void connect() {
String hostname = connectionProps.getProperty("hostname");
String portNumber = connectionProps.getProperty("port");
if(hostname.isEmpty() || portNumber.isEmpty()){
throw new IllegalArgumentException("Hostname or port are invalid");
}
try {
connection = DriverManager.getConnection(
"jdbc:" + DEFAULT_DBMS + "://" +
hostname +
":" + portNumber + "/",
connectionProps);
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void close() {
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment