Class DatabaseSaver

  • All Implemented Interfaces:
    java.io.Serializable, CapabilitiesHandler, CapabilitiesIgnorer, BatchConverter, DatabaseConverter, IncrementalConverter, Saver, EnvironmentHandler, OptionHandler, RevisionHandler

    public class DatabaseSaver
    extends AbstractSaver
    implements BatchConverter, IncrementalConverter, DatabaseConverter, OptionHandler, EnvironmentHandler
    Writes to a database (tested with MySQL, InstantDB, HSQLDB).

    Valid options are:

     -url <JDBC URL>
      The JDBC URL to connect to.
      (default: from DatabaseUtils.props file)
     
     -user <name>
      The user to connect with to the database.
      (default: none)
     
     -password <password>
      The password to connect with to the database.
      (default: none)
     
     -T <table name>
      The name of the table.
      (default: the relation name)
     
     -truncate
      Truncate (i.e. delete any data) in table before inserting
     
     -P
      Add an ID column as primary key. The name is specified
      in the DatabaseUtils file ('idColumn'). The DatabaseLoader
      won't load this column.
     
     -custom-props <file>
      The custom properties file to use instead of default ones,
      containing the database parameters.
      (default: none)
     
     -i <input file name>
      Input file in arff format that should be saved in database.
     
    Version:
    $Revision: 12418 $
    Author:
    Stefan Mutter (mutter@cs.waikato.ac.nz)
    See Also:
    Serialized Form
    • Constructor Detail

      • DatabaseSaver

        public DatabaseSaver()
                      throws java.lang.Exception
        Constructor.
        Throws:
        java.lang.Exception - throws Exception if property file cannot be read
    • Method Detail

      • main

        public static void main​(java.lang.String[] options)
        Main method.
        Parameters:
        options - should contain the options of a Saver.
      • setEnvironment

        public void setEnvironment​(Environment env)
        Set the environment variables to use.
        Specified by:
        setEnvironment in interface EnvironmentHandler
        Parameters:
        env - the environment variables to use
      • resetOptions

        public void resetOptions()
        Resets the Saver ready to save a new data set.
        Overrides:
        resetOptions in class 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 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
      • tableNameTipText

        public java.lang.String tableNameTipText()
        Returns the tip text for this property.
        Returns:
        the tip text for this property
      • getTruncate

        @OptionMetadata(displayName="Truncate table",
                        description="Truncate (i.e. drop and recreate) table if it already exists",
                        displayOrder=6)
        public boolean getTruncate()
        Get whether to truncate (i.e. drop and recreate) the table if it already exits. If false, then new data is appended to the table.
        Returns:
        true if the table should be truncated first (if it exists).
      • setTruncate

        public void setTruncate​(boolean t)
        Set whether to truncate (i.e. drop and recreate) the table if it already exits. If false, then new data is appended to the table.
        Parameters:
        t - true if the table should be truncated first (if it exists).
      • truncateTipText

        public java.lang.String truncateTipText()
        Returns the tip text for this property.
        Returns:
        the tip text for this property
      • getAutoKeyGeneration

        @OptionMetadata(displayName="Automatic primary key",
                        description="If set to true, a primary key column is generated automatically (containing the row number as INTEGER). The name of the key is read from DatabaseUtils (idColumn) This primary key can be used for incremental loading (requires an unique key). This primary key will not be loaded as an attribute.",
                        displayOrder=7)
        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
      • setAutoKeyGeneration

        public void setAutoKeyGeneration​(boolean flag)
        En/Dis-ables the automatic generation of a primary key.
        Parameters:
        flag - flag for automatic key-genereration
      • autoKeyGenerationTipText

        public java.lang.String autoKeyGenerationTipText()
        Returns the tip text for this property.
        Returns:
        tip text for this property
      • getRelationForTableName

        @OptionMetadata(displayName="Use relation name",
                        description="If set to true, the relation name will be used as name for the database table. Otherwise the user has to provide a table name.",
                        displayOrder=5)
        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
      • setRelationForTableName

        public void setRelationForTableName​(boolean flag)
        En/Dis-ables that the relation name is used for the name of the table (default enabled).
        Parameters:
        flag - if true the relation name is used as table name
      • relationForTableNameTipText

        public java.lang.String relationForTableNameTipText()
        Returns the tip text fo this property.
        Returns:
        the tip text for this property
      • setUrl

        public void setUrl​(java.lang.String url)
        Sets the database URL.
        Specified by:
        setUrl in interface DatabaseConverter
        Parameters:
        url - the URL
      • urlTipText

        public java.lang.String urlTipText()
        Returns the tip text for this property.
        Returns:
        the tip text for this property
      • getUser

        public java.lang.String getUser()
        Gets the database user.
        Specified by:
        getUser in interface DatabaseConverter
        Returns:
        the user name
      • userTipText

        public java.lang.String userTipText()
        Returns the tip text for this property.
        Returns:
        the tip text for this property
      • setPassword

        public void setPassword​(java.lang.String password)
        Sets the database password.
        Specified by:
        setPassword in interface DatabaseConverter
        Parameters:
        password - the password
      • passwordTipText

        public java.lang.String passwordTipText()
        Returns the tip text for this property.
        Returns:
        the tip text for this property
      • setCustomPropsFile

        public void setCustomPropsFile​(java.io.File value)
        Sets the custom properties file to use.
        Parameters:
        value - the custom props file to load database parameters from, use null or directory to disable custom properties.
      • customPropsFileTipText

        public java.lang.String customPropsFileTipText()
        The tip text for this property.
        Returns:
        the tip text
      • 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.
      • connectToDatabase

        public void connectToDatabase()
        Opens a connection to the database.
      • writeIncremental

        public void writeIncremental​(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 Saver
        Overrides:
        writeIncremental in class 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 Saver
        Specified by:
        writeBatch in class AbstractSaver
        Throws:
        java.io.IOException - throws IOException
      • getOptions

        public java.lang.String[] getOptions()
        Gets the setting.
        Specified by:
        getOptions in interface OptionHandler
        Returns:
        the current setting
      • setOptions

        public void setOptions​(java.lang.String[] options)
                        throws java.lang.Exception
        Sets the options.

        Valid options are:

         -url <JDBC URL>
          The JDBC URL to connect to.
          (default: from DatabaseUtils.props file)
         
         -user <name>
          The user to connect with to the database.
          (default: none)
         
         -password <password>
          The password to connect with to the database.
          (default: none)
         
         -T <table name>
          The name of the table.
          (default: the relation name)
         
         -truncate
          Truncate (i.e. delete any data) in table before inserting
         
         -P
          Add an ID column as primary key. The name is specified
          in the DatabaseUtils file ('idColumn'). The DatabaseLoader
          won't load this column.
         
         -custom-props <file>
          The custom properties file to use instead of default ones,
          containing the database parameters.
          (default: none)
         
         -i <input file name>
          Input file in arff format that should be saved in database.
         
        Specified by:
        setOptions in interface OptionHandler
        Parameters:
        options - the options
        Throws:
        java.lang.Exception - if options cannot be set
      • listOptions

        public java.util.Enumeration<Option> listOptions()
        Lists the available options.
        Specified by:
        listOptions in interface OptionHandler
        Returns:
        an enumeration of the available options
      • getRevision

        public java.lang.String getRevision()
        Returns the revision string.
        Specified by:
        getRevision in interface RevisionHandler
        Returns:
        the revision