Class DatabaseManager
java.lang.Object
dev.magicmq.pyspigot.manager.database.DatabaseManager
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 Summary
Modifier and TypeMethodDescriptionconnectMongo(MongoClientSettings clientSettings) Open a new connection with a Mongo database, using the provided client settings.connectMongo(String uri) Open a new connection with a Mongo database, using the provided connection string URI.connectMongo(String uri, MongoClientSettings clientSettings) Open a new connection with a Mongo database, using the provided connection string URI and client settings.connectMongo(String host, String port, String username, String password) Open a new connection with a Mongo database, using the default client settings.connectMongo(String host, String port, String username, String password, MongoClientSettings clientSettings) Open a new connection with a Mongo database, using the provided client settings.connectSql(HikariConfig hikariConfig) Open a new connection with an SQL database, using the provided configuration.connectSql(String uri) Open a new connection with an SQL database, using the provided connection URI.connectSql(String uri, HikariConfig hikariConfig) Open a new connection with an SQL database, using the provided connection URI and configuration options.Open a new connection with an SQL database, using the default configuration options.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.booleandisconnect(Database connection) Disconnect from the provided database connection.booleandisconnectAll(Script script) Disconnect from all database connections belonging to a certain script.static DatabaseManagerget()Get the singleton instance of this DatabaseManager.getConnections(Script script) Get all database connnections belonging to a script.getConnections(Script script, DatabaseType type) Get all database connnections belonging to a script of the given type.Get a newHikariConfigfor specifying configuration options.Get a newMongoClientSettings.Builderfor specifying client settings.
-
Method Details
-
newHikariConfig
Get a newHikariConfigfor 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 databaseport- The port of the SQL databasedatabase- The name of the SQL databaseusername- The username of the SQL databasepassword- The password of the SQL database- Returns:
- An
SqlDatabaseobject 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 databaseport- The port of the SQL databasedatabase- The name of the SQL databaseusername- The username of the SQL databasepassword- The password of the SQL databasehikariConfig- AHikariConfigobject representing the configuration options for the connection- Returns:
- An
SqlDatabaseobject representing an open connection to the database
-
connectSql
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
SqlDatabaseobject representing an open connection to the database
-
connectSql
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 optionshikariConfig- AHikariConfigobject representing the configuration options for the connection- Returns:
- An
SqlDatabaseobject representing an open connection to the database
-
connectSql
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
SqlDatabaseobject representing an open connection to the database
-
newMongoClientSettings
Get a newMongoClientSettings.Builderfor specifying client settings.Note: This should be called from scripts only!
- Returns:
- A new MongoClientSettings builder
-
connectMongo
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 databaseport- The port of the Mongo databaseusername- The username of the Mongo databasepassword- The password of the Mongo database- Returns:
- An
MongoDatabaseobject representing an open connection to the database
-
connectMongo
public MongoDatabase connectMongo(String host, String port, String username, String password, 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 databaseport- The port of the Mongo databaseusername- The username of the Mongo databasepassword- The password of the Mongo databaseclientSettings- AMongoClientSettingsobject representing the client settings for the connection- Returns:
- An
MongoDatabaseobject representing an open connection to the database
-
connectMongo
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
MongoDatabaseobject representing an open connection to the database
-
connectMongo
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 optionsclientSettings- AMongoClientSettingsobject representing the client settings for the connection- Returns:
- An
MongoDatabaseobject representing an open connection to the database
-
connectMongo
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
MongoDatabaseobject representing an open connection to the database
-
disconnect
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
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
Get all database connnections belonging to a script.- Parameters:
script- The script to get database connections from- Returns:
- An immutable List of
Databasecontaining all database connections associated with the script. Will return an empty list if there are no open database connections associated with the script
-
getConnections
Get all database connnections belonging to a script of the given type.- Parameters:
script- The script to get database connections fromtype- The type of database connection to filter by- Returns:
- An immutable List of
Databasecontaining all database connections of the given type associated with the script. Will return an empty list if there are no open database connections of the given type associated with the script
-
get
Get the singleton instance of this DatabaseManager.- Returns:
- The instance
-