Class DBTools
java.lang.Object
br.net.dd.netherwingcore.database.util.DBTools
The
DBTools class provides utility methods for managing database users and databases.
It includes methods to check for the existence of users and databases, create users and databases,
and grant privileges to users. The class uses JDBC to interact with the database and relies on
configuration settings for database connection details.-
Method Summary
Modifier and TypeMethodDescriptionstatic booleancheckDatabase(ConnectionInfos infos) Checks if the specified database exists by executing a query against the INFORMATION_SCHEMA.SCHEMATA table.static booleancheckUser(ConnectionInfos infos) Checks if the specified user exists by executing a query against the mysql.user table.static booleancreateDatabase(ConnectionInfos infos) Creates a new database with the name specified in the ConnectionInfos object.static booleancreateUser(ConnectionInfos infos) Creates a new user in the database with the username, host, and password specified in the ConnectionInfos object.static booleanexecuteStatement(ConnectionInfos infos, String formatedSQL, boolean useRoot) Executes the provided SQL statement using a connection established with the given ConnectionInfos.static ConnectiongetConnection(ConnectionInfos infos, boolean useRoot) Establishes a connection to the database using the provided ConnectionInfos and root credentials.static booleanGrants all privileges on the specified database to the user defined in the ConnectionInfos object.static booleangrantUsage(ConnectionInfos infos) Grants USAGE privileges to the user defined in the ConnectionInfos object.static booleanloadDump(ConnectionInfos infos, Path dumpFile) Loads a SQL dump file into the database specified in the ConnectionInfos object.static booleanupdateDatabaseFromFile(ConnectionInfos infos, Path filePath)
-
Method Details
-
getConnection
Establishes a connection to the database using the provided ConnectionInfos and root credentials.- Parameters:
infos- the ConnectionInfos object containing database connection detailsuseRoot- a boolean indicating whether to use root credentials for the connection- Returns:
- a Connection object representing the established connection to the database
- Throws:
RuntimeException- if there is an error during connection establishment
-
grantUsage
Grants USAGE privileges to the user defined in the ConnectionInfos object.- Parameters:
infos- the ConnectionInfos object containing user details- Returns:
- true if the privileges were granted successfully, false otherwise
- Throws:
RuntimeException- if there is an error during privilege granting
-
createUser
Creates a new user in the database with the username, host, and password specified in the ConnectionInfos object.- Parameters:
infos- the ConnectionInfos object containing user details- Returns:
- true if the user was created successfully, false otherwise
- Throws:
RuntimeException- if there is an error during user creation
-
grantAllPrivileges
Grants all privileges on the specified database to the user defined in the ConnectionInfos object.- Parameters:
infos- the ConnectionInfos object containing database and user details- Returns:
- true if the privileges were granted successfully, false otherwise
- Throws:
RuntimeException- if there is an error during privilege granting
-
createDatabase
Creates a new database with the name specified in the ConnectionInfos object.- Parameters:
infos- the ConnectionInfos object containing database details- Returns:
- true if the database was created successfully, false otherwise
- Throws:
RuntimeException- if there is an error during database creation
-
executeStatement
Executes the provided SQL statement using a connection established with the given ConnectionInfos.- Parameters:
infos- the ConnectionInfos object containing database connection detailsformatedSQL- the SQL statement to execute- Returns:
- true if the statement was executed successfully, false otherwise
- Throws:
RuntimeException- if there is an error during statement execution or connection handling
-
loadDump
Loads a SQL dump file into the database specified in the ConnectionInfos object.- Parameters:
infos- the ConnectionInfos object containing database connection detailsdumpFile- the Path to the SQL dump file to be loaded- Returns:
- true if the dump was loaded successfully, false otherwise
- Throws:
RuntimeException- if there is an error during dump loading or file handling
-
checkDatabase
Checks if the specified database exists by executing a query against the INFORMATION_SCHEMA.SCHEMATA table.- Parameters:
infos- the ConnectionInfos object containing database details- Returns:
- true if the database exists, false otherwise
- Throws:
RuntimeException- if there is an error during the database existence check
-
checkUser
Checks if the specified user exists by executing a query against the mysql.user table.- Parameters:
infos- the ConnectionInfos object containing user details- Returns:
- true if the user exists, false otherwise
- Throws:
RuntimeException- if there is an error during the user existence check
-
updateDatabaseFromFile
-