Class DatabaseManager

java.lang.Object
dev.magicmq.pyspigot.manager.database.DatabaseManager

public class DatabaseManager extends Object
Manager that allows connection to and interact with a variety of database types. Primarily used by scripts to interact with external databases, such as SQL and MongoDB.
  • Method Details

    • newHikariConfig

      public HikariConfig newHikariConfig()
      Get a new HikariConfig for specifying configuration options.

      Note: This should be called from scripts only!

      Returns:
      A new HikariConfig
    • connectSql

      public SqlDatabase connectSql(String host, String port, String database, String username, String password)
      Open a new connection with an SQL database, using the default configuration options.

      Note: This should be called from scripts only!

      Parameters:
      host - The host URL or IP of the SQL database
      port - The port of the SQL database
      database - The name of the SQL database
      username - The username of the SQL database
      password - The password of the SQL database
      Returns:
      An SqlDatabase object representing an open connection to the database
    • connectSql

      public SqlDatabase connectSql(String host, String port, String database, String username, String password, HikariConfig hikariConfig)
      Open a new connection with an SQL database, using the specified configuration options.

      Note: This should be called from scripts only!

      Parameters:
      host - The host URL or IP of the SQL database
      port - The port of the SQL database
      database - The name of the SQL database
      username - The username of the SQL database
      password - The password of the SQL database
      hikariConfig - A HikariConfig object representing the configuration options for the connection
      Returns:
      An SqlDatabase object representing an open connection to the database
    • connectSql

      public SqlDatabase connectSql(String uri)
      Open a new connection with an SQL database, using the provided connection URI.

      Note: This should be called from scripts only!

      Parameters:
      uri - The connection string to define the connection, including options
      Returns:
      An SqlDatabase object representing an open connection to the database
    • connectSql

      public SqlDatabase connectSql(String uri, HikariConfig hikariConfig)
      Open a new connection with an SQL database, using the provided connection URI and configuration options.

      Note: This should be called from scripts only!

      Parameters:
      uri - The connection string to define the connection, including options
      hikariConfig - A HikariConfig object representing the configuration options for the connection
      Returns:
      An SqlDatabase object representing an open connection to the database
    • connectSql

      public SqlDatabase connectSql(HikariConfig hikariConfig)
      Open a new connection with an SQL database, using the provided configuration.

      Note: This should be called from scripts only!

      Parameters:
      hikariConfig - The configuration for the connection
      Returns:
      An SqlDatabase object representing an open connection to the database
    • newMongoClientSettings

      public com.mongodb.MongoClientSettings.Builder newMongoClientSettings()
      Get a new MongoClientSettings.Builder for specifying client settings.

      Note: This should be called from scripts only!

      Returns:
      A new MongoClientSettings builder
    • connectMongo

      public MongoDatabase connectMongo(String host, String port, String username, String password)
      Open a new connection with a Mongo database, using the default client settings.

      Note: This should be called from scripts only!

      Parameters:
      host - The host URL or IP of the Mongo database
      port - The port of the Mongo database
      username - The username of the Mongo database
      password - The password of the Mongo database
      Returns:
      An MongoDatabase object representing an open connection to the database
    • connectMongo

      public MongoDatabase connectMongo(String host, String port, String username, String password, com.mongodb.MongoClientSettings clientSettings)
      Open a new connection with a Mongo database, using the provided client settings.

      Note: This should be called from scripts only!

      Parameters:
      host - The host URL or IP of the Mongo database
      port - The port of the Mongo database
      username - The username of the Mongo database
      password - The password of the Mongo database
      clientSettings - A MongoClientSettings object representing the client settings for the connection
      Returns:
      An MongoDatabase object representing an open connection to the database
    • connectMongo

      public MongoDatabase connectMongo(String uri)
      Open a new connection with a Mongo database, using the provided connection string URI.

      Note: This should be called from scripts only!

      Parameters:
      uri - The connection string to define the connection, including options
      Returns:
      An MongoDatabase object representing an open connection to the database
    • connectMongo

      public MongoDatabase connectMongo(String uri, com.mongodb.MongoClientSettings clientSettings)
      Open a new connection with a Mongo database, using the provided connection string URI and client settings.

      Note: This should be called from scripts only!

      Parameters:
      uri - The connection string to define the connection, including options
      clientSettings - A MongoClientSettings object representing the client settings for the connection
      Returns:
      An MongoDatabase object representing an open connection to the database
    • connectMongo

      public MongoDatabase connectMongo(com.mongodb.MongoClientSettings clientSettings)
      Open a new connection with a Mongo database, using the provided client settings.

      Note: This should be called from scripts only!

      Parameters:
      clientSettings - The client settings for the connection
      Returns:
      An MongoDatabase object representing an open connection to the database
    • disconnect

      public boolean disconnect(Database connection)
      Disconnect from the provided database connection. Should be called when no longer using the database connection.

      Note: This should be called from scripts only!

      Parameters:
      connection - The database connection to disconnect from
      Returns:
      True if the disconnection was successful, false if otherwise
    • disconnectAll

      public boolean disconnectAll(Script script)
      Disconnect from all database connections belonging to a certain script.
      Parameters:
      script - The script whose database connections should be disconnected
      Returns:
      True if all disconnections were successful, false if one or more connections were not closed successfully or if the script had no database connections to close
    • getConnections

      public List<Database> getConnections(Script script)
      Get all database connnections belonging to a script.
      Parameters:
      script - The script to get database connections from
      Returns:
      An immutable List of Database containing all database connections associated with the script. Will return null if there are no open database connections associated with the script
    • getConnections

      public List<Database> getConnections(Script script, DatabaseType type)
      Get all database connnections belonging to a script of the given type.
      Parameters:
      script - The script to get database connections from
      type - The type of database connection to filter by
      Returns:
      An immutable List of Database containing all database connections of the given type associated with the script. Will return null if there are no open database connections of the given type associated with the script
    • get

      public static DatabaseManager get()
      Get the singleton instance of this DatabaseManager.
      Returns:
      The instance