net.sourceforge.jdbdump.dump
Class Table

java.lang.Object
  extended by net.sourceforge.jdbdump.dump.Table
All Implemented Interfaces:
java.io.Serializable

public class Table
extends java.lang.Object
implements java.io.Serializable

Represents a table in a database. Objects of this class should be created either by a DatabaseConnector during the execution of dump() method, or by a DumpFileManager while recreating a Dump from a saved backup file. A table must have its name assigned, and should also have a number of columns (handled by Column class). Optionally a table can also have one or more Triggers.

Table is different from other classes in this package in that it is the only one which has the ability to download more information from the database after the Dump structure is created. This is done to let a DumpFileManager download parts of data and immediately store them in a file (or, in the same way, load data from a file and insert them into database). Otherwise the entire contents of a database (often many megabytes or even gigabytes) would have to be kept in memory.

Database data is loaded using methods initializeData() and getDataLine(). The former is executed once per table to prepare the data stream, and the latter is called for each record of data. Data can be saved to a dump file just after they are downloaded, so only one record occupies the memory at the same time.

Example code:

 DatabaseConnectorFactory factory = DatabaseConnectorFactory.getInstance();
 if (connectorTypes.length > 0) {
     DatabaseConnector connector = factory.createConnector(
         "net.sourceforge.jdbdump.connect.connectors.MysqlConnector");
     connector.connect("jdbc:mysql://localhost/base", "admin", "qwerty");
     Dump dump = connector.dump();
     File dumpFile = new File("dump.bin");
     // ...write the structure to a file...
     for (Table t : dump.getTables()) {
         t.initializeData();
         while (true) {
             String record[] = t.getDataLine();
             if (record == null) break;
             // ...write one line with an insert command...
         }
     }
     connector.disconnect();
 } else {
     // error - no connectors
 }
 

Author:
jsuder
See Also:
Serialized Form

Constructor Summary
Table(java.lang.String name, Dump dump)
          Creates a new table with specified name.
 
Method Summary
 void addConstraint(Constraint constraint)
          Adds a new constraint to this table.
 java.util.Vector<Column> getColumns()
          Returns number of columns in the table.
 java.util.Vector<Constraint> getConstraints()
          Returns a list of this table's constraints (including primary and foreign keys).
 java.lang.String[] getDataLine()
          Downloads one record of data from this table in database or from a file (depending on the value of readFromDatabase attribute).
 java.lang.String[] getDataLineFromDatabase()
          Downloads one record of data from this table in database.
 java.lang.String[] getDataLineFromFile()
          Reads one record of data from a backup file.
 java.lang.String getName()
          Returns table's name.
 void initializeData()
          Prepares table's data for downloading using getDataLine().
 void setDump(Dump dump)
          Sets a reference to a Dump object which contains this Table.
 void setName(java.lang.String name)
          Sets table's name.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Table

public Table(java.lang.String name,
             Dump dump)
Creates a new table with specified name.

Parameters:
name - table's name
connection - database connection which will be used to dump table's data
Method Detail

setDump

public void setDump(Dump dump)
Sets a reference to a Dump object which contains this Table.

Parameters:
dump - a database dump

getName

public java.lang.String getName()
Returns table's name.

Returns:
table's name

setName

public void setName(java.lang.String name)
Sets table's name.


getConstraints

public java.util.Vector<Constraint> getConstraints()
Returns a list of this table's constraints (including primary and foreign keys).

Returns:
a list of table's constraints

addConstraint

public void addConstraint(Constraint constraint)
Adds a new constraint to this table.

Parameters:
constraint - a constraint to be added to the list

initializeData

public void initializeData()
                    throws java.sql.SQLException
Prepares table's data for downloading using getDataLine().

Throws:
java.sql.SQLException - if data download can't be initialized because of a connection error or an error in the database

getDataLine

public java.lang.String[] getDataLine()
                               throws java.lang.Exception
Downloads one record of data from this table in database or from a file (depending on the value of readFromDatabase attribute).

Returns:
next record from the table, or null if there are no more records
Throws:
java.lang.Exception - if data record can't be downloaded because of a connection error or an error in the database, or because of an IO error during the reading from a backup file

getDataLineFromDatabase

public java.lang.String[] getDataLineFromDatabase()
                                           throws java.sql.SQLException
Downloads one record of data from this table in database.

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

getDataLineFromFile

public java.lang.String[] getDataLineFromFile()
                                       throws java.io.IOException,
                                              java.lang.ClassNotFoundException
Reads one record of data from a backup file.

Returns:
next record from the table, or null if there are no more records
Throws:
java.io.IOException - if data can't be read from the file
java.lang.ClassNotFoundException - if the read object doesn't match the required class

getColumns

public java.util.Vector<Column> getColumns()
Returns number of columns in the table.

Returns:
number of columns in the table


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