adams.data.io.output
Class SqlDumpSpreadSheetWriter

java.lang.Object
  extended by adams.core.ConsoleObject
      extended by adams.core.option.AbstractOptionHandler
          extended by adams.data.io.output.AbstractSpreadSheetWriter
              extended by adams.data.io.output.SqlDumpSpreadSheetWriter
All Implemented Interfaces:
Debuggable, Destroyable, OptionHandler, SizeOfHandler, 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: 7019 $
Author:
fracpete (fracpete at waikato dot ac dot nz)
See Also:
Serialized Form

Field Summary
protected  boolean m_AddCreateTable
          whether to add a CREATE TABLE statement.
protected  boolean m_Appending
          whether to append spreadsheets.
protected  ColumnNameConversion m_ColumnNameConversion
          the column name conversion.
protected  String[] m_ColumnNames
          the column names (shortened, disambiguated).
protected  boolean m_FileExists
          whether the file already exists.
protected  SpreadSheet m_Header
          the header of the first spreadsheet written to file, if appending is active.
protected  boolean m_KeepExisting
          whether to keep existing files the first time the writer is called.
protected  int m_MaxStringLength
          the maximum length for strings.
protected  String m_StringColumnSQL
          the SQL type for string columns.
protected  String m_Table
          the name of the table.
protected  Cell.ContentType[] m_Types
          the type used for the table.
protected  boolean m_UseBackslashes
          whether to use backslashes for escaping.
 
Fields inherited from class adams.core.option.AbstractOptionHandler
m_DebugLevel, m_OptionManager
 
Constructor Summary
SqlDumpSpreadSheetWriter()
           
 
Method Summary
 String addCreateTableTipText()
          Returns the tip text for this property.
 String appendingTipText()
          Returns the tip text for this property.
 boolean canAppend(SpreadSheet sheet)
          Checks whether we can append the specified spreadsheet to the existing file.
 String columnNameConversionTipText()
          Returns the tip text for this property.
 void defineOptions()
          Adds options to the internal list of options.
protected  boolean doWrite(SpreadSheet content, Writer writer)
          Performs the actual writing.
protected  String format(double value)
          Formats the number according to the format and returns the generated textual representation.
 boolean getAddCreateTable()
          Returns whether a CREATE TABLE statement is output.
 ColumnNameConversion getColumnNameConversion()
          Returns how to convert the column headers into SQL table column names.
 boolean getFileExists()
          Returns whether the output file already exists.
 String getFormatDescription()
          Returns a string describing the format (used in the file chooser).
 String[] getFormatExtensions()
          Returns the extension(s) of the format.
 boolean getKeepExisting()
          Returns whether any existing file is kept on first execution.
 int getMaxStringLength()
          Returns the maximum length for strings.
 String getStringColumnSQL()
          Returns the SQL type for string columns for the CREATE statement.
 String getTable()
          Returns the name of the table.
 boolean getUseBackslashes()
          Returns whether to use backslashes for escaping quotes rather than doubling them.
protected  boolean getUseOutputStream()
          Returns whether to write to an OutputStream rather than a Writer when using a file name.
 String globalInfo()
          Returns a string describing the object.
protected  void initialize()
          Initializes the members.
 boolean isAppending()
          Returns whether the next spreadsheet will get appended.
 String keepExistingTipText()
          Returns the tip text for this property.
 String maxStringLengthTipText()
          Returns the tip text for this property.
protected  String quoteString(String s)
          Quotes the string if necessary.
protected  void reset()
          Resets the writer.
 void setAddCreateTable(boolean value)
          Sets whether to add a CREATE TABLE statement.
 void setAppending(boolean value)
          Sets whether the next write call is to append the data to the existing file.
 void setColumnNameConversion(ColumnNameConversion value)
          Sets how to convert the column headers into SQL table column names.
 void setFileExists(boolean value)
          Sets whether the output file already exists.
 void setKeepExisting(boolean value)
          Sets whether to keep any existing file on first execution.
 void setMaxStringLength(int value)
          Sets the maximum length for strings.
 void setStringColumnSQL(String value)
          Sets the SQL type for string columns for the CREATE statement.
 void setTable(String value)
          Sets the name of the table.
 void setUseBackslashes(boolean value)
          Sets whether to use backslashes for escaping quotes rather than doubling them.
 String stringColumnSQLTipText()
          Returns the tip text for this property.
 String tableTipText()
          Returns the tip text for this property.
 String useBackslashesTipText()
          Returns the tip text for this property.
 
Methods inherited from class adams.data.io.output.AbstractSpreadSheetWriter
doWrite, getWriters, write, write, write, write
 
Methods inherited from class adams.core.option.AbstractOptionHandler
cleanUpOptions, debug, debug, debugLevelTipText, destroy, finishInit, getDebugLevel, getOptionManager, isDebugOn, newOptionManager, setDebugLevel, toCommandLine, toString
 
Methods inherited from class adams.core.ConsoleObject
getDebugging, getSystemErr, getSystemOut, sizeOf
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface adams.core.option.OptionHandler
cleanUpOptions, getOptionManager
 
Methods inherited from interface adams.core.Destroyable
destroy
 

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_Types

protected Cell.ContentType[] m_Types
the type used for 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

globalInfo

public String globalInfo()
Returns a string describing the object.

Specified by:
globalInfo in class AbstractOptionHandler
Returns:
a description suitable for displaying in the gui

defineOptions

public void defineOptions()
Adds options to the internal list of options.

Specified by:
defineOptions in interface OptionHandler
Overrides:
defineOptions in class AbstractOptionHandler

initialize

protected void initialize()
Initializes the members.

Overrides:
initialize in class AbstractOptionHandler

reset

protected void reset()
Resets the writer.

Overrides:
reset in class AbstractOptionHandler

getFormatDescription

public String getFormatDescription()
Returns a string describing the format (used in the file chooser).

Specified by:
getFormatDescription in interface SpreadSheetWriter
Specified by:
getFormatDescription in class AbstractSpreadSheetWriter
Returns:
a description suitable for displaying in the file chooser

getFormatExtensions

public String[] getFormatExtensions()
Returns the extension(s) of the format.

Specified by:
getFormatExtensions in interface SpreadSheetWriter
Specified by:
getFormatExtensions in class AbstractSpreadSheetWriter
Returns:
the extension (without the dot!)

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

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

getUseOutputStream

protected boolean getUseOutputStream()
Returns whether to write to an OutputStream rather than a Writer when using a file name.

Specified by:
getUseOutputStream in class AbstractSpreadSheetWriter
Returns:
true if to write to an OutputStream

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


Copyright © 2013 University of Waikato, Hamilton, NZ. All Rights Reserved.