net.sourceforge.jdbdump.connect.connectors
Class MysqlConnector

java.lang.Object
  extended by net.sourceforge.jdbdump.connect.DatabaseConnector
      extended by net.sourceforge.jdbdump.connect.connectors.MysqlConnector

public class MysqlConnector
extends DatabaseConnector

This is a class handling connection to the database, an implementation of the abstract class DatabaseConnector. It is a specific connector, providing connection to MySQL databases.

Author:
jsuder
See Also:
DatabaseConnector

Field Summary
protected  int columns
          Number of columns in the current table.
protected  java.sql.ResultSet dataSet
          A ResultSet storing table data when it is being downloaded.
static int FETCH_SIZE
          Maximum number of table records downloaded at a time.
protected  int position
          Number of current record in a table while it is being downloaded.
protected  java.lang.String query
          The query used to download next parts of a table into dataSet.
 
Fields inherited from class net.sourceforge.jdbdump.connect.DatabaseConnector
connection, meta
 
Constructor Summary
MysqlConnector()
           
 
Method Summary
protected  void clearConstraints(Table table)
          Removes constraints from the database's tables (if they exist), before they are dropped from the database and then inserted from a dump.
protected  void clearDatabase(Dump dump)
          Clears tables from the database if they already exist, before they are inserted from a dump.
 void connect(java.lang.String url, java.lang.String user, java.lang.String pass)
          Opens a connection to the database, using the provided connection data.
 java.lang.String createURL(DatabaseConnectionData data)
          Creates a MySQL-specific JDBC connection url (it has the form: jdbc:mysql://host:port/database).
 Dump dump()
          Creates a backup of the structure of the entire database.
protected  void dumpColumn(Column column, java.sql.ResultSet rsColumns)
          Downloads the structure of a single table column from the database.
protected  void dumpTableForeignKeys(Table table)
          Downloads the table's foreign (imported) keys from the database.
protected  void dumpTablePrimaryKeys(Table table)
          Downloads the table's primary key from the database.
 java.lang.String[] getTableDataLine()
          Downloads one record of data from the previously initialized table in database.
 void initializeTableData(java.lang.String tableName)
          Prepares the given table for downloading its data.
 void restore(Dump dump)
          Restores the structure of the database from a Dump object loaded from a previously created backup file.
protected  void restoreTableConstraints(Table table)
          Restores the table's constraints (e.g.
protected  void restoreTableData(Table table)
          Restores the table's data into the database.
protected  void restoreTableStructure(Table table)
          Creates a single table using information from a dump.
 
Methods inherited from class net.sourceforge.jdbdump.connect.DatabaseConnector
connect, disconnect, dumpTable, dumpTables, printResultSet
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

dataSet

protected java.sql.ResultSet dataSet
A ResultSet storing table data when it is being downloaded.


query

protected java.lang.String query
The query used to download next parts of a table into dataSet.


position

protected int position
Number of current record in a table while it is being downloaded.


columns

protected int columns
Number of columns in the current table.


FETCH_SIZE

public static final int FETCH_SIZE
Maximum number of table records downloaded at a time.

See Also:
Constant Field Values
Constructor Detail

MysqlConnector

public MysqlConnector()
Method Detail

connect

public void connect(java.lang.String url,
                    java.lang.String user,
                    java.lang.String pass)
             throws java.sql.SQLException,
                    java.lang.ClassNotFoundException
Opens a connection to the database, using the provided connection data.

Specified by:
connect in class DatabaseConnector
Parameters:
url - a correct connection URL
user - user's login in the database
pass - user's password
Throws:
java.sql.SQLException - if the connection is not possible
java.lang.ClassNotFoundException - if there is a problem with loading a driver

dump

public Dump dump()
Creates a backup of the structure of the entire database. It doesn't backup any data.

Specified by:
dump in class DatabaseConnector
Returns:
a Dump object containing information about database structure
Throws:
java.sql.SQLException - if the backup process is interrupted by a critical error

dumpTablePrimaryKeys

protected void dumpTablePrimaryKeys(Table table)
                             throws java.sql.SQLException
Downloads the table's primary key from the database. This method implements the empty method dumpTablePrimaryKeys() in the superclass, so is will be called by superclass's dumpTable() after it retrieves all the columns.

Overrides:
dumpTablePrimaryKeys in class DatabaseConnector
Parameters:
table - a table which should be checked for primary keys
Throws:
java.sql.SQLException - if the backup process is interrupted by a critical error

dumpTableForeignKeys

protected void dumpTableForeignKeys(Table table)
                             throws java.sql.SQLException
Downloads the table's foreign (imported) keys from the database. This method implements the empty method dumpTableForeignKeys() in the superclass, so is will be called by superclass's dumpTable() after it retrieves all the columns.

Overrides:
dumpTableForeignKeys in class DatabaseConnector
Parameters:
table - a table which should be checked for foreign keys
Throws:
java.sql.SQLException - if the backup process is interrupted by a critical error

dumpColumn

protected void dumpColumn(Column column,
                          java.sql.ResultSet rsColumns)
                   throws java.sql.SQLException
Downloads the structure of a single table column from the database. This method overrides the method in superclass and will be called by superclass's dumpTable() for each column in a table. The specific behaviour which is needed for MySQL is handling the enum data type, because enum's possible options can't be retrieved directly through DatabaseMetaData.getColumns(), but only using a SHOW COLUMNS query.

Overrides:
dumpColumn in class DatabaseConnector
Parameters:
column - a column which should have its parameters downloaded
rsColumns - a ResultSet returned by DatabaseMetaData.getColumns(), set on this column's record
Throws:
java.sql.SQLException - if the backup process is interrupted by a critical error

restore

public void restore(Dump dump)
             throws java.sql.SQLException
Restores the structure of the database from a Dump object loaded from a previously created backup file. It doesn't restore any data.

Specified by:
restore in class DatabaseConnector
Parameters:
dump - a Dump object containing information about database structure
Throws:
java.sql.SQLException - if the restore process is interrupted by a critical error

clearDatabase

protected void clearDatabase(Dump dump)
                      throws java.sql.SQLException
Clears tables from the database if they already exist, before they are inserted from a dump. This method is called by restore().

Parameters:
dump - the dump which contains names of the tables which should be cleared
Throws:
java.sql.SQLException - if the clear process is interrupted by a critical error

clearConstraints

protected void clearConstraints(Table table)
                         throws java.sql.SQLException
Removes constraints from the database's tables (if they exist), before they are dropped from the database and then inserted from a dump. This method is called by clearDatabase() and is needed because it may not be possible to drop some of the tables if they have constraints binding them to other tables.

Parameters:
table - the table which contains constraints which should be removed
Throws:
java.sql.SQLException - if the clear process is interrupted by a critical error

restoreTableStructure

protected void restoreTableStructure(Table table)
                              throws java.sql.SQLException
Creates a single table using information from a dump. It restores all the fields, also adds a primary key if there is one, but doesn't upload any data yet and doesn't add constraints other than the primary key.

Parameters:
table - the table which should be added to the database
Throws:
java.sql.SQLException - if the create process is interrupted by a critical error

restoreTableConstraints

protected void restoreTableConstraints(Table table)
                                throws java.sql.SQLException
Restores the table's constraints (e.g. foreign keys). This method is called at the end of restoring process, after the data is uploaded, because otherwise we would have to find a correct order in which to download the data to prevent any conflicts.

Parameters:
table - the table whose constraints should be added
Throws:
java.sql.SQLException

restoreTableData

protected void restoreTableData(Table table)
                         throws java.sql.SQLException
Restores the table's data into the database. The data are loaded from the backup file record after record just before they are uploaded into the database.

Parameters:
table - the table whose data should be uploaded
Throws:
java.sql.SQLException - when the data can't be uploaded

createURL

public java.lang.String createURL(DatabaseConnectionData data)
Creates a MySQL-specific JDBC connection url (it has the form: jdbc:mysql://host:port/database).

Specified by:
createURL in class DatabaseConnector
Parameters:
data - an object providing all the data necessary to open the connection
Returns:
a connection url string generated from the data

initializeTableData

public void initializeTableData(java.lang.String tableName)
                         throws java.sql.SQLException
Prepares the given table for downloading its data. That means, it runs a query "SELECT * FROM `tablename`" and stores the received result set.

Specified by:
initializeTableData in class DatabaseConnector
Parameters:
tableName - name of the table from which data should be downloaded
Throws:
java.sql.SQLException

getTableDataLine

public java.lang.String[] getTableDataLine()
                                    throws java.sql.SQLException
Downloads one record of data from the previously initialized table in database.

Specified by:
getTableDataLine in class DatabaseConnector
Returns:
next record from the table, or null if there are no more records
Throws:
java.sql.SQLException - if data record can't be downloaded because of a connection error or an error in the database


Copyright © 2005-2006 AGH International University of Science and Technology. All Rights Reserved.