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
(com.mongodb.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, com.mongodb.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, com.mongodb.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.boolean
disconnect
(Database connection) Disconnect from the provided database connection.boolean
disconnectAll
(Script script) Disconnect from all database connections belonging to a certain script.static DatabaseManager
get()
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 newHikariConfig
for specifying configuration options.com.mongodb.MongoClientSettings.Builder
Get a newMongoClientSettings.Builder
for specifying client settings.
-
Method Details
-
newHikariConfig
Get a newHikariConfig
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 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
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 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
- AHikariConfig
object representing the configuration options for the connection- Returns:
- An
SqlDatabase
object 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
SqlDatabase
object 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
- AHikariConfig
object representing the configuration options for the connection- Returns:
- An
SqlDatabase
object 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
SqlDatabase
object representing an open connection to the database
-
newMongoClientSettings
public com.mongodb.MongoClientSettings.Builder newMongoClientSettings()Get a newMongoClientSettings.Builder
for 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
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 databaseport
- The port of the Mongo databaseusername
- The username of the Mongo databasepassword
- The password of the Mongo databaseclientSettings
- AMongoClientSettings
object representing the client settings for the connection- Returns:
- An
MongoDatabase
object 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
MongoDatabase
object 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
- AMongoClientSettings
object representing the client settings for the connection- Returns:
- An
MongoDatabase
object 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
MongoDatabase
object 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
Database
containing all database connections associated with the script. Will return null 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
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
Get the singleton instance of this DatabaseManager.- Returns:
- The instance
-