Class SqlDumpSpreadSheetWriter

  • All Implemented Interfaces:
    AdditionalInformationHandler, Destroyable, GlobalInfoSupporter, EncodingSupporter, LoggingLevelHandler, LoggingSupporter, OptionHandler, SizeOfHandler, Stoppable, StoppableWithFeedback, AppendableSpreadSheetWriter, SpreadSheetWriter, Serializable

    public class SqlDumpSpreadSheetWriter
    extends AbstractSpreadSheetWriter
    implements AppendableSpreadSheetWriter
    Generates an SQL dump from the spreadsheet, which can be imported into a database.

    Valid options are:

    -D <int> (property: debugLevel)
        The greater the number the more additional info the scheme may output to
        the console (0 = off).
        default: 0
        minimum: 0
     
    -appending (property: appending)
        If enabled, multiple spreadsheets with the same structure can be written
        to the same file.
     
    -keep-existing (property: keepExisting)
        If enabled, any output file that exists when the writer is executed for
        the first time won't get replaced with the current header; useful when outputting
        data in multiple locations in the flow, but one needs to be cautious as
        to not stored mixed content (eg varying number of columns, etc).
     
    -table <java.lang.String> (property: table)
        The name of the table.
        default: blah
     
    -column-name-conversion <AS_IS|LOWER_CASE|UPPER_CASE> (property: columnNameConversion)
        How to convert the column headers into SQL table column names.
        default: UPPER_CASE
     
    -max-string-length <int> (property: maxStringLength)
        The maximum length for strings to enforce; can be used as @MAX in the 'stringColumnsSQL'
         property.
        default: 50
        minimum: 1
     
    -string-column-sql <java.lang.String> (property: stringColumnSQL)
        The SQL type to use for STRING columns in the CREATE statement; you can
        use the @MAX placeholder to tie the type to the 'naxStringLength' property;
         see also: http://en.wikipedia.org/wiki/SQL
        default: VARCHAR(@MAX)
     
    -add-create-table (property: addCreateTable)
        If enabled, a CREATE TABLE statement is output as well.
     
    -use-backslashes (property: useBackslashes)
        If enabled, backslashes are used to escape single quotes, rather than doubling
        up the single quotes.
     
    Version:
    $Revision$
    Author:
    fracpete (fracpete at waikato dot ac dot nz)
    See Also:
    Serialized Form
    • Field Detail

      • m_Appending

        protected boolean m_Appending
        whether to append spreadsheets.
      • m_Header

        protected SpreadSheet m_Header
        the header of the first spreadsheet written to file, if appending is active.
      • m_KeepExisting

        protected boolean m_KeepExisting
        whether to keep existing files the first time the writer is called.
      • m_FileExists

        protected boolean m_FileExists
        whether the file already exists.
      • m_Table

        protected String m_Table
        the name of the table.
      • m_ColumnNames

        protected String[] m_ColumnNames
        the column names (shortened, disambiguated).
      • m_ColumnNameConversion

        protected ColumnNameConversion m_ColumnNameConversion
        the column name conversion.
      • m_StringColumnSQL

        protected String m_StringColumnSQL
        the SQL type for string columns.
      • m_MaxStringLength

        protected int m_MaxStringLength
        the maximum length for strings.
      • m_AddCreateTable

        protected boolean m_AddCreateTable
        whether to add a CREATE TABLE statement.
      • m_UseBackslashes

        protected boolean m_UseBackslashes
        whether to use backslashes for escaping.
    • Constructor Detail

      • SqlDumpSpreadSheetWriter

        public SqlDumpSpreadSheetWriter()
    • Method Detail

      • canAppend

        public boolean canAppend​(SpreadSheet sheet)
        Checks whether we can append the specified spreadsheet to the existing file.
        Specified by:
        canAppend in interface AppendableSpreadSheetWriter
        Parameters:
        sheet - the spreadsheet to append to the existing one
        Returns:
        true if appending is possible
      • setTypeMapper

        public void setTypeMapper​(AbstractTypeMapper value)
        Sets the type mapper to use.
        Parameters:
        value - the mapper
      • getTypeMapper

        public AbstractTypeMapper getTypeMapper()
        Returns the type mapper in use.
        Returns:
        the mapper
      • typeMapperTipText

        public String typeMapperTipText()
        Returns the tip text for this property.
        Returns:
        tip text for this property suitable for displaying in the GUI or for listing the options.
      • setAppending

        public void setAppending​(boolean value)
        Sets whether the next write call is to append the data to the existing file.
        Specified by:
        setAppending in interface AppendableSpreadSheetWriter
        Parameters:
        value - true if to append
      • isAppending

        public boolean isAppending()
        Returns whether the next spreadsheet will get appended.
        Specified by:
        isAppending in interface AppendableSpreadSheetWriter
        Returns:
        true if append is active
      • appendingTipText

        public String appendingTipText()
        Returns the tip text for this property.
        Specified by:
        appendingTipText in interface AppendableSpreadSheetWriter
        Returns:
        tip text for this property suitable for displaying in the explorer/experimenter gui
      • setKeepExisting

        public void setKeepExisting​(boolean value)
        Sets whether to keep any existing file on first execution.
        Specified by:
        setKeepExisting in interface AppendableSpreadSheetWriter
        Parameters:
        value - if true then existing file is kept
      • getKeepExisting

        public boolean getKeepExisting()
        Returns whether any existing file is kept on first execution.
        Specified by:
        getKeepExisting in interface AppendableSpreadSheetWriter
        Returns:
        true if existing file is kept
      • keepExistingTipText

        public String keepExistingTipText()
        Returns the tip text for this property.
        Specified by:
        keepExistingTipText in interface AppendableSpreadSheetWriter
        Returns:
        tip text for this property suitable for displaying in the GUI or for listing the options.
      • setTable

        public void setTable​(String value)
        Sets the name of the table.
        Parameters:
        value - the name
      • getTable

        public String getTable()
        Returns the name of the table.
        Returns:
        the name
      • tableTipText

        public String tableTipText()
        Returns the tip text for this property.
        Returns:
        tip text for this property suitable for displaying in the GUI or for listing the options.
      • setColumnNameConversion

        public void setColumnNameConversion​(ColumnNameConversion value)
        Sets how to convert the column headers into SQL table column names.
        Parameters:
        value - the conversion
      • getColumnNameConversion

        public ColumnNameConversion getColumnNameConversion()
        Returns how to convert the column headers into SQL table column names.
        Returns:
        the conversion
      • columnNameConversionTipText

        public String columnNameConversionTipText()
        Returns the tip text for this property.
        Returns:
        tip text for this property suitable for displaying in the GUI or for listing the options.
      • setMaxStringLength

        public void setMaxStringLength​(int value)
        Sets the maximum length for strings.
        Parameters:
        value - the maximum
      • getMaxStringLength

        public int getMaxStringLength()
        Returns the maximum length for strings.
        Returns:
        the maximum
      • maxStringLengthTipText

        public String maxStringLengthTipText()
        Returns the tip text for this property.
        Returns:
        tip text for this property suitable for displaying in the GUI or for listing the options.
      • setStringColumnSQL

        public void setStringColumnSQL​(String value)
        Sets the SQL type for string columns for the CREATE statement.
        Parameters:
        value - the SQL type
      • getStringColumnSQL

        public String getStringColumnSQL()
        Returns the SQL type for string columns for the CREATE statement.
        Returns:
        the SQL type
      • stringColumnSQLTipText

        public String stringColumnSQLTipText()
        Returns the tip text for this property.
        Returns:
        tip text for this property suitable for displaying in the GUI or for listing the options.
      • setAddCreateTable

        public void setAddCreateTable​(boolean value)
        Sets whether to add a CREATE TABLE statement.
        Parameters:
        value - if true then a CREATE TABLE statement is output as well
      • getAddCreateTable

        public boolean getAddCreateTable()
        Returns whether a CREATE TABLE statement is output.
        Returns:
        true if statement is output
      • addCreateTableTipText

        public String addCreateTableTipText()
        Returns the tip text for this property.
        Returns:
        tip text for this property suitable for displaying in the GUI or for listing the options.
      • setUseBackslashes

        public void setUseBackslashes​(boolean value)
        Sets whether to use backslashes for escaping quotes rather than doubling them.
        Parameters:
        value - if true then backslashes are used
      • getUseBackslashes

        public boolean getUseBackslashes()
        Returns whether to use backslashes for escaping quotes rather than doubling them.
        Returns:
        true if backslashes are used
      • useBackslashesTipText

        public String useBackslashesTipText()
        Returns the tip text for this property.
        Returns:
        tip text for this property suitable for displaying in the GUI or for listing the options.
      • setFileExists

        public void setFileExists​(boolean value)
        Sets whether the output file already exists.
        Specified by:
        setFileExists in interface AppendableSpreadSheetWriter
        Parameters:
        value - true if the output file already exists
      • getFileExists

        public boolean getFileExists()
        Returns whether the output file already exists.
        Specified by:
        getFileExists in interface AppendableSpreadSheetWriter
        Returns:
        true if the output file already exists
      • format

        protected String format​(double value)
        Formats the number according to the format and returns the generated textual representation.
        Parameters:
        value - the double value to turn into a string
        Returns:
        the generated string
      • quoteString

        protected String quoteString​(String s)
        Quotes the string if necessary.
        Parameters:
        s - the string to quote, if necessary
        Returns:
        the potentially quoted string
      • doWrite

        protected boolean doWrite​(SpreadSheet content,
                                  Writer writer)
        Performs the actual writing. The caller must ensure that the writer gets closed.
        Overrides:
        doWrite in class AbstractSpreadSheetWriter
        Parameters:
        content - the spreadsheet to write
        writer - the writer to write the spreadsheet to
        Returns:
        true if successfully written