Package adams.data.io.output
Class SqlDumpSpreadSheetWriter
- java.lang.Object
-
- adams.core.logging.LoggingObject
-
- adams.core.logging.CustomLoggingLevelObject
-
- adams.core.option.AbstractOptionHandler
-
- adams.data.io.output.AbstractSpreadSheetWriter
-
- adams.data.io.output.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
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class adams.data.io.output.AbstractSpreadSheetWriter
AbstractSpreadSheetWriter.OutputType
-
-
Field Summary
Fields Modifier and Type Field Description 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 AbstractTypeMapper
m_TypeMapper
the type mapper to use.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.data.io.output.AbstractSpreadSheetWriter
m_Encoding, m_Stopped
-
Fields inherited from class adams.core.option.AbstractOptionHandler
m_OptionManager
-
Fields inherited from class adams.core.logging.LoggingObject
m_Logger, m_LoggingIsEnabled, m_LoggingLevel
-
-
Constructor Summary
Constructors Constructor Description SqlDumpSpreadSheetWriter()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description 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.SpreadSheetReader
getCorrespondingReader()
Returns, if available, the corresponding reader.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.protected AbstractSpreadSheetWriter.OutputType
getOutputType()
Returns how the data is written.String
getStringColumnSQL()
Returns the SQL type for string columns for the CREATE statement.String
getTable()
Returns the name of the table.AbstractTypeMapper
getTypeMapper()
Returns the type mapper in use.boolean
getUseBackslashes()
Returns whether to use backslashes for escaping quotes rather than doubling them.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.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
setTypeMapper(AbstractTypeMapper value)
Sets the type mapper to use.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
typeMapperTipText()
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
canCompress, doWrite, doWrite, encodingTipText, getAdditionalInformation, getDefaultFormatExtension, getEncoding, getWriters, isStopped, preWriteFile, setEncoding, stopExecution, supportsCompressedOutput, write, write, write, write
-
Methods inherited from class adams.core.option.AbstractOptionHandler
cleanUpOptions, destroy, finishInit, getDefaultLoggingLevel, getOptionManager, loggingLevelTipText, newOptionManager, setLoggingLevel, toCommandLine, toString
-
Methods inherited from class adams.core.logging.LoggingObject
configureLogger, getLogger, getLoggingLevel, initializeLogging, isLoggingEnabled, sizeOf
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface adams.core.Destroyable
destroy
-
Methods inherited from interface adams.core.logging.LoggingLevelHandler
getLoggingLevel
-
Methods inherited from interface adams.core.option.OptionHandler
cleanUpOptions, getOptionManager, toCommandLine
-
-
-
-
Field Detail
-
m_TypeMapper
protected AbstractTypeMapper m_TypeMapper
the type mapper to use.
-
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.
-
-
Method Detail
-
globalInfo
public String globalInfo()
Returns a string describing the object.- Specified by:
globalInfo
in interfaceGlobalInfoSupporter
- Specified by:
globalInfo
in classAbstractOptionHandler
- 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 interfaceOptionHandler
- Overrides:
defineOptions
in classAbstractOptionHandler
-
initialize
protected void initialize()
Initializes the members.- Overrides:
initialize
in classAbstractSpreadSheetWriter
-
reset
public void reset()
Resets the writer.- Specified by:
reset
in interfaceSpreadSheetWriter
- Overrides:
reset
in classAbstractSpreadSheetWriter
-
getFormatDescription
public String getFormatDescription()
Returns a string describing the format (used in the file chooser).- Specified by:
getFormatDescription
in interfaceSpreadSheetWriter
- Specified by:
getFormatDescription
in classAbstractSpreadSheetWriter
- 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 interfaceSpreadSheetWriter
- Specified by:
getFormatExtensions
in classAbstractSpreadSheetWriter
- Returns:
- the extension (without the dot!)
-
getCorrespondingReader
public SpreadSheetReader getCorrespondingReader()
Returns, if available, the corresponding reader.- Specified by:
getCorrespondingReader
in interfaceSpreadSheetWriter
- Returns:
- the reader, null if none available
-
canAppend
public boolean canAppend(SpreadSheet sheet)
Checks whether we can append the specified spreadsheet to the existing file.- Specified by:
canAppend
in interfaceAppendableSpreadSheetWriter
- 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 interfaceAppendableSpreadSheetWriter
- Parameters:
value
- true if to append
-
isAppending
public boolean isAppending()
Returns whether the next spreadsheet will get appended.- Specified by:
isAppending
in interfaceAppendableSpreadSheetWriter
- Returns:
- true if append is active
-
appendingTipText
public String appendingTipText()
Returns the tip text for this property.- Specified by:
appendingTipText
in interfaceAppendableSpreadSheetWriter
- 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 interfaceAppendableSpreadSheetWriter
- 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 interfaceAppendableSpreadSheetWriter
- Returns:
- true if existing file is kept
-
keepExistingTipText
public String keepExistingTipText()
Returns the tip text for this property.- Specified by:
keepExistingTipText
in interfaceAppendableSpreadSheetWriter
- 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 interfaceAppendableSpreadSheetWriter
- Parameters:
value
- true if the output file already exists
-
getFileExists
public boolean getFileExists()
Returns whether the output file already exists.- Specified by:
getFileExists
in interfaceAppendableSpreadSheetWriter
- 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
-
getOutputType
protected AbstractSpreadSheetWriter.OutputType getOutputType()
Returns how the data is written.- Specified by:
getOutputType
in classAbstractSpreadSheetWriter
- Returns:
- the type
-
doWrite
protected boolean doWrite(SpreadSheet content, Writer writer)
Performs the actual writing. The caller must ensure that the writer gets closed.- Overrides:
doWrite
in classAbstractSpreadSheetWriter
- Parameters:
content
- the spreadsheet to writewriter
- the writer to write the spreadsheet to- Returns:
- true if successfully written
-
-