-
-
Notifications
You must be signed in to change notification settings - Fork 134
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
45c78fc
commit 00ac8dd
Showing
8 changed files
with
431 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
src/main/java/world/bentobox/bentobox/database/postgresql/PostgreSQLDatabase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
))); | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
src/main/java/world/bentobox/bentobox/database/postgresql/PostgreSQLDatabaseConnector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.