-
Notifications
You must be signed in to change notification settings - Fork 8
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
Showing
6 changed files
with
106 additions
and
11 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
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
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
84 changes: 84 additions & 0 deletions
84
src/main/java/com/mattmx/reconnect/storage/PostgreSQLStorage.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,84 @@ | ||
package com.mattmx.reconnect.storage; | ||
|
||
import com.mattmx.reconnect.ReconnectConfig; | ||
import com.mattmx.reconnect.ReconnectVelocity; | ||
import com.zaxxer.hikari.HikariConfig; | ||
import com.zaxxer.hikari.HikariDataSource; | ||
|
||
import java.sql.Connection; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.sql.Statement; | ||
|
||
public class PostgreSQLStorage extends StorageMethod { | ||
private HikariDataSource ds; | ||
|
||
@Override | ||
public void init() { | ||
ReconnectConfig config = ReconnectVelocity.get().getConfig(); | ||
|
||
HikariConfig hikariConfig = new HikariConfig(); | ||
hikariConfig.setDriverClassName(org.mariadb.jdbc.Driver.class.getName()); | ||
if (config.storage.data.connectionParameters.useJdbcString) { | ||
hikariConfig.setJdbcUrl(config.storage.data.connectionParameters.jdbcString); | ||
} else { | ||
hikariConfig.setJdbcUrl("jdbc:postgresql://" + config.storage.data.address + "/" + config.storage.data.database); | ||
} | ||
hikariConfig.setUsername(config.storage.data.username); | ||
hikariConfig.setPassword(config.storage.data.password); | ||
hikariConfig.setConnectionTimeout(config.storage.data.connectionParameters.connectionTimeout); | ||
hikariConfig.setIdleTimeout(config.storage.data.connectionParameters.idleTimeout); | ||
hikariConfig.setKeepaliveTime(config.storage.data.connectionParameters.keepAliveTime); | ||
hikariConfig.setMaxLifetime(config.storage.data.connectionParameters.maxLifetime); | ||
hikariConfig.setMinimumIdle(config.storage.data.connectionParameters.minimumIdle); | ||
hikariConfig.setMaximumPoolSize(config.storage.data.connectionParameters.maximumPoolSize); | ||
hikariConfig.setPoolName("reconnect"); | ||
|
||
ds = new HikariDataSource(hikariConfig); | ||
try (Connection con = ds.getConnection()) { | ||
Statement statement = con.createStatement(); | ||
statement.executeUpdate("CREATE TABLE IF NOT EXISTS reconnect_data(" + | ||
"uuid VARCHAR(255)," + | ||
"lastserver TEXT," + | ||
"PRIMARY KEY(uuid))"); | ||
} catch (SQLException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
@Override | ||
public void setLastServer(String uuid, String servername) { | ||
try (Connection con = ds.getConnection()) { | ||
Statement statement = con.createStatement(); | ||
statement.executeUpdate( | ||
"INSERT INTO reconnect_data VALUES ('" + uuid + "','" + servername + "')" + | ||
"ON CONFLICT (uuid) DO UPDATE SET lastserver = '" + servername + "'"); | ||
} catch (SQLException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
@Override | ||
public String getLastServer(String uuid) { | ||
try (Connection con = ds.getConnection()) { | ||
Statement statement = con.createStatement(); | ||
ResultSet rs = statement.executeQuery("SELECT lastserver FROM reconnect_data WHERE uuid = '" + uuid + "'"); | ||
if (rs.next()) { | ||
return rs.getString("lastserver"); | ||
} | ||
} catch (SQLException e) { | ||
e.printStackTrace(); | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
public void save() { | ||
ds.close(); | ||
} | ||
|
||
@Override | ||
public String getMethod() { | ||
return "postgresql"; | ||
} | ||
} |