Skip to content

Commit

Permalink
Added PostgreSQL database type
Browse files Browse the repository at this point in the history
  • Loading branch information
Poslovitch committed Jul 4, 2019
1 parent 45c78fc commit 00ac8dd
Show file tree
Hide file tree
Showing 8 changed files with 431 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/main/java/world/bentobox/bentobox/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ public class Settings implements ConfigObject {
private boolean useEconomy = true;

// Database
@ConfigComment("JSON, MYSQL, MARIADB (10.2.3+), MONGODB, SQLITE and YAML(deprecated).")
@ConfigComment("JSON, MYSQL, MARIADB (10.2.3+), MONGODB, SQLITE, POSTGRESQL and YAML(deprecated).")
@ConfigComment("Transition database options are:")
@ConfigComment(" YAML2JSON, YAML2MARIADB, YAML2MYSQL, YAML2MONGODB, YAML2SQLITE")
@ConfigComment(" JSON2MARIADB, JSON2MYSQL, JSON2MONGODB, JSON2SQLITE")
@ConfigComment(" MYSQL2JSON, MARIADB2JSON, MONGODB2JSON, SQLITE2JSON")
@ConfigComment(" JSON2MARIADB, JSON2MYSQL, JSON2MONGODB, JSON2SQLITE, JSON2POSTGRESQL")
@ConfigComment(" MYSQL2JSON, MARIADB2JSON, MONGODB2JSON, SQLITE2JSON, POSTGRESQL2JSON")
@ConfigComment("If you need others, please make a feature request.")
@ConfigComment("Transition options enable migration from one database type to another. Use /bbox migrate.")
@ConfigComment("YAML and JSON are file-based databases.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import world.bentobox.bentobox.database.mariadb.MariaDBDatabase;
import world.bentobox.bentobox.database.mongodb.MongoDBDatabase;
import world.bentobox.bentobox.database.mysql.MySQLDatabase;
import world.bentobox.bentobox.database.postgresql.PostgreSQLDatabase;
import world.bentobox.bentobox.database.sqlite.SQLiteDatabase;
import world.bentobox.bentobox.database.transition.*;
import world.bentobox.bentobox.database.yaml.YamlDatabase;
Expand Down Expand Up @@ -89,6 +90,12 @@ enum DatabaseType {
*/
JSON2SQLITE(new Json2SQLiteDatabase()),

/**
* Transition database, from JSON to PostgreSQL
* @since 1.6.0
*/
JSON2POSTGRESQL(new Json2PostgreSQLDatabase()),

MYSQL(new MySQLDatabase()),

/**
Expand Down Expand Up @@ -124,7 +131,18 @@ enum DatabaseType {
* Transition database, from SQLite to JSON
* @since 1.6.0
*/
SQLITE2JSON(new SQLite2JsonDatabase());
SQLITE2JSON(new SQLite2JsonDatabase()),

/**
* @since 1.6.0
*/
POSTGRESQL(new PostgreSQLDatabase()),

/**
* Transition database, from PostgreSQL to JSON
* @since 1.6.0
*/
POSTGRESQL2JSON(new PostgreSQL2JsonDatabase());

DatabaseSetup database;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package world.bentobox.bentobox.database.postgresql;

import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.database.AbstractDatabaseHandler;
import world.bentobox.bentobox.database.DatabaseConnectionSettingsImpl;
import world.bentobox.bentobox.database.DatabaseSetup;

/**
* @since 1.6.0
* @author Poslovitch
*/
public class PostgreSQLDatabase implements DatabaseSetup {

@Override
public <T> AbstractDatabaseHandler<T> getHandler(Class<T> dataObjectClass) {
BentoBox plugin = BentoBox.getInstance();
return new PostgreSQLDatabaseHandler<>(plugin, dataObjectClass, new PostgreSQLDatabaseConnector(new DatabaseConnectionSettingsImpl(
plugin.getSettings().getDatabaseHost(),
plugin.getSettings().getDatabasePort(),
plugin.getSettings().getDatabaseName(),
plugin.getSettings().getDatabaseUsername(),
plugin.getSettings().getDatabasePassword()
)));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package world.bentobox.bentobox.database.postgresql;

import org.bukkit.Bukkit;
import org.eclipse.jdt.annotation.NonNull;
import world.bentobox.bentobox.database.DatabaseConnectionSettingsImpl;
import world.bentobox.bentobox.database.DatabaseConnector;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
* @since 1.6.0
* @author Poslovitch
*/
public class PostgreSQLDatabaseConnector implements DatabaseConnector {

private String connectionUrl;
private DatabaseConnectionSettingsImpl dbSettings;
private static Connection connection = null;

/**
* Class for PostgreSQL database connections using the settings provided
* @param dbSettings - database settings
*/
PostgreSQLDatabaseConnector(@NonNull DatabaseConnectionSettingsImpl dbSettings) {
this.dbSettings = dbSettings;
connectionUrl = "jdbc:postgresql://" + dbSettings.getHost() + ":" + dbSettings.getPort() + "/" + dbSettings.getDatabaseName()
+ "?autoReconnect=true&useSSL=false&allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8";
}

@Override
public Object createConnection() {
// Only make one connection to the database
if (connection == null) {
try {
connection = DriverManager.getConnection(connectionUrl, dbSettings.getUsername(), dbSettings.getPassword());
} catch (SQLException e) {
Bukkit.getLogger().severe("Could not connect to the database! " + e.getMessage());
}
}
return connection;
}

@Override
public void closeConnection() {
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
Bukkit.getLogger().severe("Could not close PostgreSQL database connection");
}
}
}

@Override
public String getConnectionUrl() {
return connectionUrl;
}

@Override
public @NonNull String getUniqueId(String tableName) {
// Not used
return "";
}

@Override
public boolean uniqueIdExists(String tableName, String key) {
// Not used
return false;
}
}
Loading

0 comments on commit 00ac8dd

Please sign in to comment.