org.liquidsite.util.db
Class MySQLDatabaseConnector

java.lang.Object
  extended byorg.liquidsite.util.db.DatabaseConnector
      extended byorg.liquidsite.util.db.MySQLDatabaseConnector

public class MySQLDatabaseConnector
extends DatabaseConnector

A MySQL database connector.


Field Summary
 
Fields inherited from class org.liquidsite.util.db.DatabaseConnector
DEFAULT_CONNECTION_TIMEOUT, DEFAULT_QUERY_TIMEOUT
 
Constructor Summary
MySQLDatabaseConnector(java.lang.String host, java.lang.String user, java.lang.String password)
          Creates a new MySQL database connector.
MySQLDatabaseConnector(java.lang.String host, java.lang.String database, java.lang.String user, java.lang.String password)
          Creates a new MySQL database connector.
 
Method Summary
 void addAccessPrivileges(java.lang.String database, java.lang.String user)
          Adds normal access privileges to a database for a user.
 void createDatabase(java.lang.String database)
          Creates a new database.
 void createUser(java.lang.String user, java.lang.String password)
          Creates a new database user.
 void deleteDatabase(java.lang.String database)
          Deletes an existing database.
 void deleteUser(java.lang.String user)
          Deletes an existing database user.
 java.lang.String getDatabaseUser()
          Returns the database user being used.
 boolean isAdministrator()
          Checks if the database user is an administrator.
 java.util.ArrayList listDatabases()
          Lists the visible databases.
 java.util.ArrayList listTables(java.lang.String database)
          Lists the tables in a database.
 java.util.ArrayList listUsers()
          Lists the users in a database.
static void loadDriver()
          Loads the MySQL database driver.
 void removeAccessPrivilege(java.lang.String database, java.lang.String user)
          Removes normal access privileges to a database for a user.
 
Methods inherited from class org.liquidsite.util.db.DatabaseConnector
execute, execute, getConnection, getConnectionTimeout, getPoolSize, getProperties, getProperty, getUrl, loadDriver, loadFunctions, returnConnection, setConnectionTimeout, setPoolSize, setProperty, toString, update
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

MySQLDatabaseConnector

public MySQLDatabaseConnector(java.lang.String host,
                              java.lang.String user,
                              java.lang.String password)
Creates a new MySQL database connector.

Parameters:
host - the host name
user - the user name
password - the user password

MySQLDatabaseConnector

public MySQLDatabaseConnector(java.lang.String host,
                              java.lang.String database,
                              java.lang.String user,
                              java.lang.String password)
Creates a new MySQL database connector.

Parameters:
host - the host name
database - the database name
user - the user name
password - the user password
Method Detail

loadDriver

public static void loadDriver()
                       throws DatabaseConnectionException
Loads the MySQL database driver. This method must be called once before attempting to connect with the specified driver. Calling this method several times has no effect.

Throws:
DatabaseConnectionException - if the driver couldn't be found or loaded correctly

isAdministrator

public boolean isAdministrator()
                        throws DatabaseConnectionException,
                               DatabaseException
Checks if the database user is an administrator.

Returns:
true if the database user is an administrator, or false otherwise
Throws:
DatabaseConnectionException - if a database connection couldn't be established
DatabaseException - if user privileges couldn't be determined

getDatabaseUser

public java.lang.String getDatabaseUser()
                                 throws DatabaseConnectionException,
                                        DatabaseException
Returns the database user being used. This is a string in the form "user@host", where the host name is the name of the connecting host.

Returns:
the database user
Throws:
DatabaseConnectionException - if a database connection couldn't be established
DatabaseException - if the database user couldn't be determined

listDatabases

public java.util.ArrayList listDatabases()
                                  throws DatabaseConnectionException,
                                         DatabaseException
Lists the visible databases.

Returns:
a list of the database names
Throws:
DatabaseConnectionException - if a database connection couldn't be established
DatabaseException - if the list of databases couldn't be retrieved

listTables

public java.util.ArrayList listTables(java.lang.String database)
                               throws DatabaseConnectionException,
                                      DatabaseException
Lists the tables in a database. Note that this operation will return a database exception if the database user hasn't got privileges to list the tables in the database.

Parameters:
database - the database name
Returns:
a list of the database table names
Throws:
DatabaseConnectionException - if a database connection couldn't be established
DatabaseException - if the list of tables couldn't be retrieved

listUsers

public java.util.ArrayList listUsers()
                              throws DatabaseConnectionException,
                                     DatabaseException
Lists the users in a database. Note that this operation will return a database exception if the database user hasn't got administrator privileges.

Returns:
a list of database user names
Throws:
DatabaseConnectionException - if a database connection couldn't be established
DatabaseException - if the list of users couldn't be retrieved

createDatabase

public void createDatabase(java.lang.String database)
                    throws DatabaseConnectionException,
                           DatabaseException
Creates a new database. This operation requires that the database user is an administrator, or a database exception will be thrown.

Parameters:
database - the database name
Throws:
DatabaseConnectionException - if a database connection couldn't be established
DatabaseException - if the database couldn't be created properly

deleteDatabase

public void deleteDatabase(java.lang.String database)
                    throws DatabaseConnectionException,
                           DatabaseException
Deletes an existing database. This operation requires that the database user has the correct permissions to the database, or a database exception will be thrown.

Parameters:
database - the database name
Throws:
DatabaseConnectionException - if a database connection couldn't be established
DatabaseException - if the database couldn't be deleted properly

createUser

public void createUser(java.lang.String user,
                       java.lang.String password)
                throws DatabaseConnectionException,
                       DatabaseException
Creates a new database user. This operation requires that the current database user is an administrator, or a database exception will be thrown. Also note that the new user will only be given access privilege from the same host as the current user.

Parameters:
user - the new database user name
password - the new database user password
Throws:
DatabaseConnectionException - if a database connection couldn't be established
DatabaseException - if the database user couldn't be created properly

deleteUser

public void deleteUser(java.lang.String user)
                throws DatabaseConnectionException,
                       DatabaseException
Deletes an existing database user. This operation requires that the current database user is an administrator, or a database exception will be thrown.

Parameters:
user - the database user name
Throws:
DatabaseConnectionException - if a database connection couldn't be established
DatabaseException - if the database user couldn't be deleted properly

addAccessPrivileges

public void addAccessPrivileges(java.lang.String database,
                                java.lang.String user)
                         throws DatabaseConnectionException,
                                DatabaseException
Adds normal access privileges to a database for a user. The access privileges are select, insert, update and delete. This operation requires that the current database user is an administrator, or a database exception will be thrown. Also note that the user will only be given access privilege from the same host as the current user.

Parameters:
database - the database name
user - the database user name
Throws:
DatabaseConnectionException - if a database connection couldn't be established
DatabaseException - if the database privileges couldn't be set properly

removeAccessPrivilege

public void removeAccessPrivilege(java.lang.String database,
                                  java.lang.String user)
                           throws DatabaseConnectionException,
                                  DatabaseException
Removes normal access privileges to a database for a user. The access privileges are select, insert, update and delete. This operation requires that the current database user is an administrator, or a database exception will be thrown. Also note that only the access privileges from the same host as the current user will be removed.

Parameters:
database - the database name
user - the database user name
Throws:
DatabaseConnectionException - if a database connection couldn't be established
DatabaseException - if the database privileges couldn't be set properly