mirror of
https://github.com/plexusorg/Plex.git
synced 2025-07-03 16:36:41 +00:00
fix mongo support
This commit is contained in:
@ -1,5 +1,6 @@
|
||||
package dev.plex.storage;
|
||||
|
||||
import com.mongodb.*;
|
||||
import com.mongodb.client.MongoClient;
|
||||
import com.mongodb.client.MongoClients;
|
||||
import dev.morphia.Datastore;
|
||||
@ -7,6 +8,7 @@ import dev.morphia.Morphia;
|
||||
import dev.morphia.mapping.MapperOptions;
|
||||
import dev.plex.PlexBase;
|
||||
import dev.plex.player.PlexPlayer;
|
||||
import dev.plex.util.PlexLog;
|
||||
|
||||
public class MongoConnection extends PlexBase
|
||||
{
|
||||
@ -24,9 +26,22 @@ public class MongoConnection extends PlexBase
|
||||
String password = plugin.config.getString("data.central.password");
|
||||
String database = plugin.config.getString("data.central.db");
|
||||
|
||||
String connectionString = "mongodb://" + username + ":" + password + "@" + host + ":" + port + "/?authSource=" + database;
|
||||
String connectionString;
|
||||
if (username != null && password != null && !username.isEmpty() && !password.isEmpty())
|
||||
{
|
||||
if (database != null && !database.isEmpty())
|
||||
{
|
||||
connectionString = "mongodb://" + username + ":" + password + "@" + host + ":" + port + "/?authSource=" + database;
|
||||
} else {
|
||||
connectionString = "mongodb://" + username + ":" + password + "@" + host + ":" + port + "/";
|
||||
}
|
||||
} else {
|
||||
connectionString = "mongodb://" + host + ":" + port + "/";
|
||||
}
|
||||
connectionString += "?uuidRepresentation=STANDARD";
|
||||
PlexLog.debug("Using mongo connection string: " + connectionString);
|
||||
MongoClient client = MongoClients.create(connectionString);
|
||||
Datastore datastore = Morphia.createDatastore(client, database, MapperOptions.DEFAULT);
|
||||
Datastore datastore = Morphia.createDatastore(client, database == null ? "admin" : database, MapperOptions.DEFAULT);
|
||||
datastore.getMapper().map(PlexPlayer.class);
|
||||
datastore.ensureIndexes();
|
||||
plugin.setStorageType(StorageType.MONGODB);
|
||||
|
@ -4,16 +4,24 @@ import com.zaxxer.hikari.HikariConfig;
|
||||
import com.zaxxer.hikari.HikariDataSource;
|
||||
import dev.plex.Plex;
|
||||
import dev.plex.PlexBase;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.io.File;
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
|
||||
@Getter
|
||||
public class SQLConnection extends PlexBase
|
||||
{
|
||||
private HikariDataSource dataSource;
|
||||
|
||||
public SQLConnection()
|
||||
{
|
||||
if (!plugin.config.getString("data.central.storage").equalsIgnoreCase("sqlite") && !plugin.config.getString("data.central.storage").equalsIgnoreCase("mariadb"))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
String host = plugin.config.getString("data.central.hostname");
|
||||
int port = plugin.config.getInt("data.central.port");
|
||||
String username = plugin.config.getString("data.central.user");
|
||||
@ -37,8 +45,7 @@ public class SQLConnection extends PlexBase
|
||||
{
|
||||
dataSource.setJdbcUrl("jdbc:sqlite:" + new File(plugin.getDataFolder(), "database.db").getAbsolutePath());
|
||||
plugin.setStorageType(StorageType.SQLITE);
|
||||
}
|
||||
else if (plugin.config.getString("data.central.storage").equalsIgnoreCase("mariadb"))
|
||||
} else if (plugin.config.getString("data.central.storage").equalsIgnoreCase("mariadb"))
|
||||
{
|
||||
Class.forName("org.mariadb.jdbc.Driver");
|
||||
dataSource.setJdbcUrl("jdbc:mariadb://" + host + ":" + port + "/" + database);
|
||||
@ -46,8 +53,7 @@ public class SQLConnection extends PlexBase
|
||||
dataSource.setPassword(password);
|
||||
Plex.get().setStorageType(StorageType.MARIADB);
|
||||
}
|
||||
}
|
||||
catch (ClassNotFoundException throwables)
|
||||
} catch (ClassNotFoundException throwables)
|
||||
{
|
||||
throwables.printStackTrace();
|
||||
}
|
||||
@ -60,6 +66,7 @@ public class SQLConnection extends PlexBase
|
||||
"`login_msg` VARCHAR(2000), " +
|
||||
"`prefix` VARCHAR(2000), " +
|
||||
"`rank` VARCHAR(20), " +
|
||||
"`adminActive` BOOLEAN," +
|
||||
"`ips` VARCHAR(2000), " +
|
||||
"`coins` BIGINT, " +
|
||||
"`vanished` BOOLEAN, " +
|
||||
@ -83,8 +90,7 @@ public class SQLConnection extends PlexBase
|
||||
"`note` VARCHAR(2000), " +
|
||||
"`timestamp` BIGINT" +
|
||||
");").execute();
|
||||
}
|
||||
catch (SQLException throwables)
|
||||
} catch (SQLException throwables)
|
||||
{
|
||||
throwables.printStackTrace();
|
||||
}
|
||||
@ -92,11 +98,14 @@ public class SQLConnection extends PlexBase
|
||||
|
||||
public Connection getCon()
|
||||
{
|
||||
if (this.dataSource == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
try
|
||||
{
|
||||
return dataSource.getConnection();
|
||||
}
|
||||
catch (SQLException e)
|
||||
} catch (SQLException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
Reference in New Issue
Block a user