weka.core.converters
Class DatabaseSaver

java.lang.Object
  extended by weka.core.converters.AbstractSaver
      extended by weka.core.converters.DatabaseSaver
All Implemented Interfaces:
java.io.Serializable, weka.core.converters.BatchConverter, weka.core.converters.DatabaseConverter, weka.core.converters.IncrementalConverter, weka.core.converters.Saver, weka.core.OptionHandler

public class DatabaseSaver
extends weka.core.converters.AbstractSaver
implements weka.core.converters.BatchConverter, weka.core.converters.IncrementalConverter, weka.core.converters.DatabaseConverter, weka.core.OptionHandler

Writes to a database (tested with MySQL, InstantDB, HSQLDB). Available options are: -T


Sets the name of teh table (default: the name of the relation)

-P
If set, a primary key column is generated automatically (containing the row number as INTEGER). The name of this columns is defined in the DatabaseUtils file.

-i
Specifies an ARFF file as input (for command line use)

Version:
$Revision: 1.2 $
Author:
Stefan Mutter (mutter@cs.waikato.ac.nz)
See Also:
Serialized Form

Field Summary
private  java.lang.String[] columnNames
           
private  java.lang.String columnNamesStr
           
private  DatabaseConnection databaseConnection
          The database connection
protected  int dbWriteMode
           
static int DELETE_INSERT
           
static int DROP_INSERT
           
static int INSERT
           
protected  java.lang.String[] keyColumnNames
           
private  int m_count
          counts the rowsand used as a primary key value
private  java.lang.String m_createDouble
          The database specific type for a double (read in from the properties file)
private  java.lang.String m_createInt
          The database specific type for an int (read in from the properties file)
private  java.lang.String m_createText
          The database specific type for a string (read in from the properties file)
private  boolean m_id
          Flag indicating if a primary key column should be added
private  java.lang.String m_idColumn
          The name of the primary key column that will be automatically generated (if enabled).
private  java.lang.String m_inputFile
          An input arff file (for command line use)
private  java.lang.String m_tableName
          The name of the tablein which the instances should be stored
private  boolean m_tabName
          Flag indicating whether the default name of the table is the relaion name or not.
protected static java.util.Properties PROPERTIES
          Properties associated with the database connection
protected static java.lang.String PROPERTY_FILE
          The property file for the database connection
static int UPDATE_INSERT
           
protected  java.lang.String version
           
protected  java.lang.String versionColumnName
           
protected  boolean versioning
           
 
Fields inherited from class weka.core.converters.AbstractSaver
CANCEL, m_retrieval, STRUCTURE_READY, WAIT, WRITE
 
Fields inherited from interface weka.core.converters.Saver
BATCH, INCREMENTAL, NONE
 
Constructor Summary
DatabaseSaver()
          Constructor
 
Method Summary
 java.lang.String autoKeyGenerationTipText()
          Returns the tip text fo this property
 void cancel()
          Cancels the incremental saving process and tries to drop the table if the write mode is CANCEL.
 void connectToDatabase()
          Opens a connection to the database
 boolean getAutoKeyGeneration()
          Gets whether or not a primary key will be generated automatically
 int getDbWriteMode()
           
 java.lang.String[] getKeyColumnNames()
           
 java.lang.String[] getOptions()
          Gets the setting
 boolean getRelationForTableName()
          Gets whether or not the relation name is used as name of the table
 java.lang.String getTableName()
          Gets the table's name
 java.lang.String getUrl()
          Gets the database URL
 java.lang.String getUser()
          Gets the database user
 java.lang.String getVersion()
           
 java.lang.String getVersionColumnName()
           
 java.lang.String globalInfo()
          Returns a string describing this Saver
 boolean isKeyColumnName(java.lang.String columnName)
           
 boolean isVersioning()
           
 java.util.Enumeration listOptions()
          Lists the available options
static void main(java.lang.String[] options)
          Main method.
 java.lang.String passwordTipText()
          Returns the tip text fo this property
private  void prepareStructure()
           
private  void printException(java.lang.Exception ex)
          Prints an exception
 java.lang.String relationForTableNameTipText()
          Returns the tip text fo this property
 void resetOptions()
          Resets the Saver ready to save a new data set
 void setAutoKeyGeneration(boolean flag)
          En/Dis-ables the automatic generation of a primary key
private  void setColumnNamesStr()
           
 void setDbWriteMode(int dbWriteMode)
           
 void setDbWriteMode(java.lang.String dbWriteMode)
           
 void setDestination()
          Sets the database url using the DatabaseUtils file
 void setDestination(java.sql.Connection connection)
          Sets the database url using the given connection
 void setDestination(java.lang.String url)
          Sets the database url
 void setDestination(java.lang.String url, java.lang.String userName, java.lang.String password)
          Sets the database url
 void setKeyColumnNames(java.lang.String[] keyColumnNames)
           
 void setOptions(java.lang.String[] options)
          Sets the options.
 void setPassword(java.lang.String password)
          Sets the database password
 void setRelationForTableName(boolean flag)
          En/Dis-ables that the relation name is used for the name of the table (default enabled).
 void setTableName(java.lang.String tn)
          Sets the table's name
 void setUrl(java.lang.String url)
          Sets the database URL
 void setUser(java.lang.String user)
          Sets the database user
 void setVersion(java.lang.String version)
           
 void setVersionColumnName(java.lang.String versionColumnName)
           
 void setVersioning(boolean versioning)
           
 java.lang.String tableNameTipText()
          Returns the tip text fo this property
private  void updateInstance(weka.core.Instance inst)
           
 java.lang.String urlTipText()
          Returns the tip text fo this property
 java.lang.String userTipText()
          Returns the tip text fo this property
 void writeBatch()
          Writes a Batch of instances
 void writeIncremental(weka.core.Instance inst)
          Saves an instances incrementally.
private  void writeInstance(weka.core.Instance inst)
           
private  void writeStructure()
          Writes the structure (header information) to a database by creating a new table.
 
Methods inherited from class weka.core.converters.AbstractSaver
filePrefix, getFileExtension, getInstances, getRetrieval, getWriteMode, resetStructure, retrieveDir, setDestination, setDestination, setDir, setDirAndPrefix, setFile, setFilePrefix, setInstances, setRetrieval, setStructure, setWriteMode
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

databaseConnection

private DatabaseConnection databaseConnection
The database connection


m_tableName

private java.lang.String m_tableName
The name of the tablein which the instances should be stored


m_inputFile

private java.lang.String m_inputFile
An input arff file (for command line use)


m_createText

private java.lang.String m_createText
The database specific type for a string (read in from the properties file)


m_createDouble

private java.lang.String m_createDouble
The database specific type for a double (read in from the properties file)


m_createInt

private java.lang.String m_createInt
The database specific type for an int (read in from the properties file)


m_idColumn

private java.lang.String m_idColumn
The name of the primary key column that will be automatically generated (if enabled). The name is read from DatabaseUtils.


m_count

private int m_count
counts the rowsand used as a primary key value


m_id

private boolean m_id
Flag indicating if a primary key column should be added


m_tabName

private boolean m_tabName
Flag indicating whether the default name of the table is the relaion name or not.


PROPERTY_FILE

protected static java.lang.String PROPERTY_FILE
The property file for the database connection


PROPERTIES

protected static java.util.Properties PROPERTIES
Properties associated with the database connection


dbWriteMode

protected int dbWriteMode

keyColumnNames

protected java.lang.String[] keyColumnNames

versioning

protected boolean versioning

versionColumnName

protected java.lang.String versionColumnName

version

protected java.lang.String version

DROP_INSERT

public static final int DROP_INSERT
See Also:
Constant Field Values

DELETE_INSERT

public static final int DELETE_INSERT
See Also:
Constant Field Values

INSERT

public static final int INSERT
See Also:
Constant Field Values

UPDATE_INSERT

public static final int UPDATE_INSERT
See Also:
Constant Field Values

columnNamesStr

private java.lang.String columnNamesStr

columnNames

private java.lang.String[] columnNames
Constructor Detail

DatabaseSaver

public DatabaseSaver()
              throws java.lang.Exception
Constructor

Throws:
java.lang.Exception - throws Exception if property file cannot be read
Method Detail

setDbWriteMode

public void setDbWriteMode(java.lang.String dbWriteMode)

resetOptions

public void resetOptions()
Resets the Saver ready to save a new data set

Overrides:
resetOptions in class weka.core.converters.AbstractSaver

cancel

public void cancel()
Cancels the incremental saving process and tries to drop the table if the write mode is CANCEL.

Overrides:
cancel in class weka.core.converters.AbstractSaver

globalInfo

public java.lang.String globalInfo()
Returns a string describing this Saver

Returns:
a description of the Saver suitable for displaying in the explorer/experimenter gui

setTableName

public void setTableName(java.lang.String tn)
Sets the table's name

Parameters:
tn - the name of the table

getTableName

public java.lang.String getTableName()
Gets the table's name

Returns:
the table's name

tableNameTipText

public java.lang.String tableNameTipText()
Returns the tip text fo this property


setAutoKeyGeneration

public void setAutoKeyGeneration(boolean flag)
En/Dis-ables the automatic generation of a primary key

Parameters:
boolean - flag

getAutoKeyGeneration

public boolean getAutoKeyGeneration()
Gets whether or not a primary key will be generated automatically

Returns:
true if a primary key column will be generated, false otherwise

autoKeyGenerationTipText

public java.lang.String autoKeyGenerationTipText()
Returns the tip text fo this property


setRelationForTableName

public void setRelationForTableName(boolean flag)
En/Dis-ables that the relation name is used for the name of the table (default enabled).

Parameters:
boolean - flag

getRelationForTableName

public boolean getRelationForTableName()
Gets whether or not the relation name is used as name of the table

Returns:
true if the relation name is used as the name of the table, false otherwise

relationForTableNameTipText

public java.lang.String relationForTableNameTipText()
Returns the tip text fo this property


setUrl

public void setUrl(java.lang.String url)
Sets the database URL

Specified by:
setUrl in interface weka.core.converters.DatabaseConverter
Parameters:
the - URL

getUrl

public java.lang.String getUrl()
Gets the database URL

Specified by:
getUrl in interface weka.core.converters.DatabaseConverter
Returns:
the URL

urlTipText

public java.lang.String urlTipText()
Returns the tip text fo this property


setUser

public void setUser(java.lang.String user)
Sets the database user

Specified by:
setUser in interface weka.core.converters.DatabaseConverter
Parameters:
the - user name

getUser

public java.lang.String getUser()
Gets the database user

Specified by:
getUser in interface weka.core.converters.DatabaseConverter
Returns:
the user name

userTipText

public java.lang.String userTipText()
Returns the tip text fo this property


setPassword

public void setPassword(java.lang.String password)
Sets the database password

Specified by:
setPassword in interface weka.core.converters.DatabaseConverter
Parameters:
the - password

passwordTipText

public java.lang.String passwordTipText()
Returns the tip text fo this property


setDestination

public void setDestination(java.lang.String url,
                           java.lang.String userName,
                           java.lang.String password)
Sets the database url

Parameters:
url - the database url
userName - the user name
password - the password

setDestination

public void setDestination(java.lang.String url)
Sets the database url

Parameters:
url - the database url

setDestination

public void setDestination()
Sets the database url using the DatabaseUtils file


setDestination

public void setDestination(java.sql.Connection connection)
Sets the database url using the given connection


connectToDatabase

public void connectToDatabase()
Opens a connection to the database


writeStructure

private void writeStructure()
                     throws java.lang.Exception
Writes the structure (header information) to a database by creating a new table.

Throws:
java.lang.Exception

prepareStructure

private void prepareStructure()
                       throws java.lang.Exception
Throws:
java.lang.Exception

setColumnNamesStr

private void setColumnNamesStr()
                        throws java.lang.Exception
Throws:
java.lang.Exception

writeInstance

private void writeInstance(weka.core.Instance inst)
                    throws java.lang.Exception
Throws:
java.lang.Exception

updateInstance

private void updateInstance(weka.core.Instance inst)
                     throws java.lang.Exception
Throws:
java.lang.Exception

writeIncremental

public void writeIncremental(weka.core.Instance inst)
                      throws java.io.IOException
Saves an instances incrementally. Structure has to be set by using the setStructure() method or setInstances() method. When a structure is set, a table is created.

Specified by:
writeIncremental in interface weka.core.converters.Saver
Overrides:
writeIncremental in class weka.core.converters.AbstractSaver
Parameters:
inst - the instance to save
Throws:
java.io.IOException - throws IOEXception.

writeBatch

public void writeBatch()
                throws java.io.IOException
Writes a Batch of instances

Specified by:
writeBatch in interface weka.core.converters.Saver
Specified by:
writeBatch in class weka.core.converters.AbstractSaver
Throws:
java.io.IOException - throws IOException

printException

private void printException(java.lang.Exception ex)
Prints an exception

Parameters:
ex - the exception to print

getOptions

public java.lang.String[] getOptions()
Gets the setting

Specified by:
getOptions in interface weka.core.OptionHandler
Returns:
the current setting

listOptions

public java.util.Enumeration listOptions()
Lists the available options

Specified by:
listOptions in interface weka.core.OptionHandler
Returns:
an enumeration of the available options

setOptions

public void setOptions(java.lang.String[] options)
                throws java.lang.Exception
Sets the options. Available options are: -T
Sets the name of teh table (default: the name of the relation)

-P
If set, a primary key column is generated automatically (containing the row number as INTEGER)

-i
Specifies an ARFF file as input (for command line use)

Specified by:
setOptions in interface weka.core.OptionHandler
Parameters:
options - the options
Throws:
java.lang.Exception - if options cannot be set

main

public static void main(java.lang.String[] options)
Main method.

Parameters:
options - should contain the options of a Saver.

getDbWriteMode

public int getDbWriteMode()

setDbWriteMode

public void setDbWriteMode(int dbWriteMode)

getVersion

public java.lang.String getVersion()

setVersion

public void setVersion(java.lang.String version)

getVersionColumnName

public java.lang.String getVersionColumnName()

setVersionColumnName

public void setVersionColumnName(java.lang.String versionColumnName)

isVersioning

public boolean isVersioning()

setVersioning

public void setVersioning(boolean versioning)

getKeyColumnNames

public java.lang.String[] getKeyColumnNames()

setKeyColumnNames

public void setKeyColumnNames(java.lang.String[] keyColumnNames)

isKeyColumnName

public boolean isKeyColumnName(java.lang.String columnName)