Class DBTools

java.lang.Object
br.net.dd.netherwingcore.database.util.DBTools

public class DBTools extends Object
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 Details

    • getConnection

      public static Connection getConnection(ConnectionInfos infos, boolean useRoot)
      Establishes a connection to the database using the provided ConnectionInfos and root credentials.
      Parameters:
      infos - the ConnectionInfos object containing database connection details
      useRoot - 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

      public static boolean grantUsage(ConnectionInfos infos)
      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

      public static boolean createUser(ConnectionInfos infos)
      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

      public static boolean grantAllPrivileges(ConnectionInfos infos)
      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

      public static boolean createDatabase(ConnectionInfos infos)
      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

      public static boolean executeStatement(ConnectionInfos infos, String formatedSQL, boolean useRoot)
      Executes the provided SQL statement using a connection established with the given ConnectionInfos.
      Parameters:
      infos - the ConnectionInfos object containing database connection details
      formatedSQL - 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

      public static boolean loadDump(ConnectionInfos infos, Path dumpFile)
      Loads a SQL dump file into the database specified in the ConnectionInfos object.
      Parameters:
      infos - the ConnectionInfos object containing database connection details
      dumpFile - 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

      public static boolean checkDatabase(ConnectionInfos infos)
      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

      public static boolean checkUser(ConnectionInfos infos)
      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

      public static boolean updateDatabaseFromFile(ConnectionInfos infos, Path filePath)