Top Inners Fields Constructors Methods
liquibase.change.core

public Class LoadDataChange

extends AbstractTableChange
implements ChangeWithColumns<LoadDataColumnConfig>
Class Inheritance
All Implemented Interfaces
liquibase.change.ChangeWithColumns
Known Direct Subclasses
liquibase.change.core.LoadUpdateDataChange
Annotations
@DatabaseChange
name:loadData
description:Loads data from a CSV file into an existing table
priority:1
appliesTo:table
since:1.7
@SuppressWarnings:java:S2583
Imports
com.opencsv.exceptions.CsvMalformedLineException, liquibase.CatalogAndSchema, .Scope, liquibase.change.*, liquibase.changelog.ChangeSet, liquibase.database.AbstractJdbcDatabase, .Database, liquibase.database.core.MSSQLDatabase, .MySQLDatabase, .PostgresDatabase, liquibase.datatype.DataTypeFactory, .LiquibaseDataType, liquibase.exception.*, liquibase.executor.ExecutorService, .LoggingExecutor, liquibase.io.EmptyLineAndCommentSkippingInputStream, liquibase.logging.Logger, liquibase.resource.Resource, .ResourceAccessor, liquibase.snapshot.InvalidExampleException, .SnapshotControl, .SnapshotGeneratorFactory, liquibase.statement.BatchDmlExecutablePreparedStatement, .ExecutablePreparedStatementBase, .InsertExecutablePreparedStatement, .SqlStatement, liquibase.statement.core.InsertOrUpdateStatement, .InsertSetStatement, .InsertStatement, liquibase.structure.core.Column, .DataType, .Table, liquibase.util.BooleanUtil, .ObjectUtil, .StreamUtil, .StringUtil, liquibase.util.csv.CSVReader, java.io.IOException, .InputStream, .Reader, java.util.*, java.util.regex.Pattern, java.util.zip.GZIPInputStream

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static enum
protected static class

Field Summary

Modifier and TypeField and Description
public static final Pattern
private List<LoadDataColumnConfig>
private String
private static final ResourceBundle
public static final String
DEFAULT_COMMENT_PATTERN

CSV Lines starting with that sign(s) will be treated as comments by default

private String
private String
private static final Logger
private String
private Boolean
private String
private Boolean
Inherited from liquibase.change.AbstractTableChange:
catalogNameschemaNametableName

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public void
addColumn(LoadDataColumnConfig column)

Implements liquibase.change.ChangeWithColumns.addColumn.

Add a column configuration to the Change.

private void
addColumnsFromHeaders(String[]
the headers of the CSV file
headers
)

Add columns if they were not specified in the loadData change, we interpolate their names from the header columns of the CSV file.

public ChangeStatus
checkStatus(Database database)

Overrides liquibase.change.AbstractChange.checkStatus.

Implements liquibase.change.Change.checkStatus.

Validate that this change executed successfully against the given database.

protected LoadDataColumnConfig

Returns:

The column having the name or header equal to "name" or the index equal to idx or a new LoadDataColumnConfig if not found
columnConfigFromName
(String
name or header of the column searched
name
,
Integer
the index of the column searched
idx
)

Get the column using the name, index or the header

protected String

Returns:

a string in the format " / column[index] (name:'columnName')" or " / column[index]" if the column name is not available.
columnIdString
(int
the index of the column
index
,
LoadDataColumnConfig
the column configuration
columnConfig
)

Returns a unique string for the column for better identification.

protected ExecutablePreparedStatementBase
createPreparedStatement(Database database, String catalogName, String schemaName, String tableName, List<LoadDataColumnConfig> columns, ChangeSet changeSet, ResourceAccessor resourceAccessor)

protected InsertStatement
createStatement(String catalogName, String schemaName, String tableName)

protected InsertSetStatement
createStatementSet(String catalogName, String schemaName, String tableName)

public CheckSum
generateCheckSum()

Overrides liquibase.change.AbstractChange.generateCheckSum.

Implements liquibase.change.Change.generateCheckSum.

Implementation generates checksum by serializing the change with StringChangeLogSerializer

public boolean
generateRollbackStatementsVolatile(Database database)

Overrides liquibase.change.AbstractChange.generateRollbackStatementsVolatile.

Implements liquibase.change.Change.generateRollbackStatementsVolatile.

Implementation delegates logic to the liquibase.sqlgenerator.SqlGenerator#generateRollbackStatementsIsVolatile(Database) method on the SqlStatement objects returned by generateStatements If no or null SqlStatements are returned by generateRollbackStatements then this method returns false.

public SqlStatement[]
generateStatements(Database database)

Implements liquibase.change.Change.generateStatements.

Generates the SqlStatement objects required to run the change for the given database.

protected SqlStatement[]
public boolean
protected LoadDataColumnConfig
getColumnConfig(int index, String header)

public List<LoadDataColumnConfig>
getColumns()

Implements liquibase.change.ChangeWithColumns.getColumns.

Return all the ColumnConfig objects defined for this Change

public String
public String
getConfirmationMessage()

Implements liquibase.change.Change.getConfirmationMessage.

Confirmation message to be displayed after the change is executed.

public CSVReader
public String
public String
public String
protected String
public String
public String
public String
public Boolean
protected static String

Returns:

if the value is empty or the string "NULL" (case-insensitive), return the empty string. If not, the value "toString()" representation (trimmed of spaces left and right) is returned.
getValueToWrite
(Object
the value to transform
value
)

Transform a value read from a CSV file into a string to be written into the database if the column type is not known.

protected boolean
private boolean
private boolean
public Boolean
private boolean
preferPreparedStatements(Database database)

If the loaded data does not require a prepared statement, and the user did not specify whether to use them or not, should we use prepared statements as a default?

private void
retrieveMissingColumnLoadTypes(List<LoadDataColumnConfig>
a list of LoadDataColumnConfigs to process
columns
,
Database database)

Iterate through the List of LoadDataColumnConfig and ask the database for any column types that we have no data type of.

public void
public void
setCommentLineStartsWith(String commentLineStartsWith)

public void
setEncoding(String encoding)

public void
public void
setQuotchar(String quotchar)

public void
setRelativeToChangelogFile(Boolean relativeToChangelogFile)

public void
setSeparator(String separator)

public void
setUsePreparedStatements(Boolean usePreparedStatements)

public boolean
protected boolean
public ValidationErrors
validate(Database database)

Overrides liquibase.change.AbstractChange.validate.

Implements liquibase.change.Change.validate.

Implementation checks the ChangeParameterMetaData for declared required fields and also delegates logic to the liquibase.sqlgenerator.SqlGenerator#validate(liquibase.statement.SqlStatement, liquibase.database.Database, liquibase.sqlgenerator.SqlGeneratorChain) method on the SqlStatement objects returned by generateStatements.

public void
validateColumn(LoadDataColumnConfig columnConfig, ValidationErrors validationErrors, String columnIDString)

protected ValidationErrors

Returns:

validationErrors
validateColumns
(ValidationErrors
ValidationErrors to collect errors
validationErrors
)

Validate all columns and collect errors in 'validationErrors'

public Warnings
Inherited from liquibase.change.AbstractTableChange:
getCatalogNamegetSchemaNamesetCatalogNamesetSchemaNamesetTableName

Field Detail

BASE64_PATTERNback to summary
public static final Pattern BASE64_PATTERN
columnsback to summary
private List<LoadDataColumnConfig> columns
commentLineStartsWithback to summary
private String commentLineStartsWith
coreBundleback to summary
private static final ResourceBundle coreBundle
DEFAULT_COMMENT_PATTERNback to summary
public static final String DEFAULT_COMMENT_PATTERN

CSV Lines starting with that sign(s) will be treated as comments by default

encodingback to summary
private String encoding
fileback to summary
private String file
LOGback to summary
private static final Logger LOG
quotcharback to summary
private String quotchar
relativeToChangelogFileback to summary
private Boolean relativeToChangelogFile
separatorback to summary
private String separator
usePreparedStatementsback to summary
private Boolean usePreparedStatements

Constructor Detail

LoadDataChangeback to summary
public LoadDataChange()

Method Detail

addColumnback to summary
public void addColumn(LoadDataColumnConfig column)

Implements liquibase.change.ChangeWithColumns.addColumn.

Doc from liquibase.change.ChangeWithColumns.addColumn.

Add a column configuration to the Change.

Annotations
@Override
addColumnsFromHeadersback to summary
private void addColumnsFromHeaders(String[] headers)

Add columns if they were not specified in the loadData change, we interpolate their names from the header columns of the CSV file.

Parameters
headers:String[]

the headers of the CSV file

checkStatusback to summary
public ChangeStatus checkStatus(Database database)

Overrides liquibase.change.AbstractChange.checkStatus.

Implements liquibase.change.Change.checkStatus.

Doc from liquibase.change.Change.checkStatus.

Validate that this change executed successfully against the given database. This will check that the update completed at a high level plus check details of the change. For example, a change to add a column will check that the column exists plus data type, default values, etc.

Annotations
@Override
columnConfigFromNameback to summary
protected LoadDataColumnConfig columnConfigFromName(String name, Integer idx)

Get the column using the name, index or the header

Parameters
name:String

name or header of the column searched

idx:Integer

the index of the column searched

Returns:LoadDataColumnConfig

The column having the name or header equal to "name" or the index equal to idx or a new LoadDataColumnConfig if not found

columnIdStringback to summary
protected String columnIdString(int index, LoadDataColumnConfig columnConfig)

Returns a unique string for the column for better identification. The returned string includes the index of the column and, if available, its name.

Parameters
index:int

the index of the column

columnConfig:LoadDataColumnConfig

the column configuration

Returns:String

a string in the format " / column[index] (name:'columnName')" or " / column[index]" if the column name is not available.

createPreparedStatementback to summary
protected ExecutablePreparedStatementBase createPreparedStatement(Database database, String catalogName, String schemaName, String tableName, List<LoadDataColumnConfig> columns, ChangeSet changeSet, ResourceAccessor resourceAccessor)
createStatementback to summary
protected InsertStatement createStatement(String catalogName, String schemaName, String tableName)
createStatementSetback to summary
protected InsertSetStatement createStatementSet(String catalogName, String schemaName, String tableName)
generateCheckSumback to summary
public CheckSum generateCheckSum()

Overrides liquibase.change.AbstractChange.generateCheckSum.

Implements liquibase.change.Change.generateCheckSum.

Doc from liquibase.change.AbstractChange.generateCheckSum.

Implementation generates checksum by serializing the change with StringChangeLogSerializer

Annotations
@Override
generateRollbackStatementsVolatileback to summary
public boolean generateRollbackStatementsVolatile(Database database)

Overrides liquibase.change.AbstractChange.generateRollbackStatementsVolatile.

Implements liquibase.change.Change.generateRollbackStatementsVolatile.

Doc from liquibase.change.AbstractChange.generateRollbackStatementsVolatile.

Implementation delegates logic to the liquibase.sqlgenerator.SqlGenerator#generateRollbackStatementsIsVolatile(Database) method on the SqlStatement objects returned by generateStatements If no or null SqlStatements are returned by generateRollbackStatements then this method returns false.

Annotations
@Override
generateStatementsback to summary
public SqlStatement[] generateStatements(Database database)

Implements liquibase.change.Change.generateStatements.

Doc from liquibase.change.Change.generateStatements.

Generates the SqlStatement objects required to run the change for the given database.

NOTE: This method may be called multiple times throughout the changelog execution process and may be called in documentation generation and other integration points as well.

If this method reads from the current database state or uses any other logic that will be affected by whether previous changeSets have ran or not, you must return true from generateStatementsVolatile.
Annotations
@Override
generateStatementsFromRowsback to summary
protected SqlStatement[] generateStatementsFromRows(Database database, List<LoadDataChange.LoadDataRowConfig> rows)
generateStatementsVolatileback to summary
public boolean generateStatementsVolatile(Database database)

Overrides liquibase.change.AbstractChange.generateStatementsVolatile.

Implements liquibase.change.Change.generateStatementsVolatile.

Doc from liquibase.change.AbstractChange.generateStatementsVolatile.

Implementation delegates logic to the liquibase.sqlgenerator.SqlGenerator#generateStatementsIsVolatile(Database) method on the SqlStatement objects returned by generateStatements. If zero or null SqlStatements are returned by generateStatements then this method returns false.

Annotations
@Override
getColumnConfigback to summary
protected LoadDataColumnConfig getColumnConfig(int index, String header)
getColumnsback to summary
public List<LoadDataColumnConfig> getColumns()

Implements liquibase.change.ChangeWithColumns.getColumns.

Doc from liquibase.change.ChangeWithColumns.getColumns.

Return all the ColumnConfig objects defined for this Change

Annotations
@Override
@DatabaseChangeProperty
supportsDatabase:all
serializationType:NESTED_OBJECT
description:Column mapping and defaults can be defined. \'header\' or \'index\' attributes need to be defined. If the header name in the CSV is different than the column, \'name\' needs to be inserted. If column \'type\' is not defined, the type is taken from the database. Otherwise, for non-string columns, the type definition might be required. The \'defaultValue*\' attributes can define values for empty fields.
getCommentLineStartsWithback to summary
public String getCommentLineStartsWith()
Annotations
@DatabaseChangeProperty
supportsDatabase:all
description:Lines starting with this are treated as comments and ignored. To disable comments, set \'commentLineStartsWith\' to an empty value. Default: #
getConfirmationMessageback to summary
public String getConfirmationMessage()

Implements liquibase.change.Change.getConfirmationMessage.

Doc from liquibase.change.Change.getConfirmationMessage.

Confirmation message to be displayed after the change is executed. Should include relevant configuration settings to make it as helpful as possible. This method may be called outside the changelog execution process, such as in documentation generation.

Annotations
@Override
getCSVReaderback to summary
public CSVReader getCSVReader() throws IOException, LiquibaseException
getEncodingback to summary
public String getEncoding()
Annotations
@DatabaseChangeProperty
exampleValue:UTF-8
supportsDatabase:all
description:Encoding of the CSV file (defaults to UTF-8)
getFileback to summary
public String getFile()
Annotations
@DatabaseChangeProperty
description:CSV file to load
exampleValue:com/example/users.csv
requiredForDatabase:all
getQuotcharback to summary
public String getQuotchar()
Annotations
@DatabaseChangeProperty
exampleValue:\'
supportsDatabase:all
description:The quote character for string fields containing the separator character. Default: "
getRelativeToback to summary
protected String getRelativeTo()
getSeparatorback to summary
public String getSeparator()
Annotations
@DatabaseChangeProperty
exampleValue:,
supportsDatabase:all
description:Character separating the fields. Default: ,
getSerializedObjectNamespaceback to summary
public String getSerializedObjectNamespace()

Overrides liquibase.change.AbstractChange.getSerializedObjectNamespace.

Implements liquibase.serializer.LiquibaseSerializable.getSerializedObjectNamespace.

Annotations
@Override
getTableNameback to summary
public String getTableName()

Overrides liquibase.change.AbstractTableChange.getTableName.

Annotations
@Override
@DatabaseChangeProperty
description:Name of the table to insert data into
requiredForDatabase:all
mustEqualExisting:table
getUsePreparedStatementsback to summary
public Boolean getUsePreparedStatements()
Annotations
@DatabaseChangeProperty
description:Whether to use prepared statements instead of INSERT statement strings (if the database supports it)
getValueToWriteback to summary
protected static String getValueToWrite(Object value)

Transform a value read from a CSV file into a string to be written into the database if the column type is not known.

Parameters
value:Object

the value to transform

Returns:String

if the value is empty or the string "NULL" (case-insensitive), return the empty string. If not, the value "toString()" representation (trimmed of spaces left and right) is returned.

hasPreparedStatementsImplementedback to summary
protected boolean hasPreparedStatementsImplemented()
isLineCommentedback to summary
private boolean isLineCommented(String[] line)
isLoggingExecutorback to summary
private boolean isLoggingExecutor(Database database)
isRelativeToChangelogFileback to summary
public Boolean isRelativeToChangelogFile()
Annotations
@DatabaseChangeProperty
supportsDatabase:all
description:Specifies whether the file path is relative to the changelog file rather than looked up in the search path. Default: false.
preferPreparedStatementsback to summary
private boolean preferPreparedStatements(Database database)

If the loaded data does not require a prepared statement, and the user did not specify whether to use them or not, should we use prepared statements as a default?

retrieveMissingColumnLoadTypesback to summary
private void retrieveMissingColumnLoadTypes(List<LoadDataColumnConfig> columns, Database database) throws DatabaseException

Iterate through the List of LoadDataColumnConfig and ask the database for any column types that we have no data type of.

Parameters
columns:List<LoadDataColumnConfig>

a list of LoadDataColumnConfigs to process

Annotations
@SuppressWarnings:CommentedOutCodeLine
setColumnsback to summary
public void setColumns(List<LoadDataColumnConfig> columns)

Implements liquibase.change.ChangeWithColumns.setColumns.

Annotations
@Override
setCommentLineStartsWithback to summary
public void setCommentLineStartsWith(String commentLineStartsWith)
setEncodingback to summary
public void setEncoding(String encoding)
setFileback to summary
public void setFile(String file)
setQuotcharback to summary
public void setQuotchar(String quotchar)
setRelativeToChangelogFileback to summary
public void setRelativeToChangelogFile(Boolean relativeToChangelogFile)
setSeparatorback to summary
public void setSeparator(String separator)
setUsePreparedStatementsback to summary
public void setUsePreparedStatements(Boolean usePreparedStatements)
supportsback to summary
public boolean supports(Database database)

Overrides liquibase.change.AbstractChange.supports.

Implements liquibase.change.Change.supports.

Doc from liquibase.change.AbstractChange.supports.

Implementation delegates logic to the liquibase.sqlgenerator.SqlGenerator#supports(liquibase.statement.SqlStatement, liquibase.database.Database) method on the SqlStatement objects returned by generateStatements. If no or null SqlStatements are returned by generateStatements then this method returns true. If generateStatementsVolatile(liquibase.database.Database) returns true, we cannot call generateStatements and so assume true.

Annotations
@Override
supportsBatchUpdatesback to summary
protected boolean supportsBatchUpdates(Database database)
validateback to summary
public ValidationErrors validate(Database database)

Overrides liquibase.change.AbstractChange.validate.

Implements liquibase.change.Change.validate.

Doc from liquibase.change.AbstractChange.validate.

Implementation checks the ChangeParameterMetaData for declared required fields and also delegates logic to the liquibase.sqlgenerator.SqlGenerator#validate(liquibase.statement.SqlStatement, liquibase.database.Database, liquibase.sqlgenerator.SqlGeneratorChain) method on the SqlStatement objects returned by generateStatements. If no or null SqlStatements are returned by generateStatements then this method returns no errors. If there are no parameters than this method returns no errors

Annotations
@Override
validateColumnback to summary
public void validateColumn(LoadDataColumnConfig columnConfig, ValidationErrors validationErrors, String columnIDString)
validateColumnsback to summary
protected ValidationErrors validateColumns(ValidationErrors validationErrors)

Validate all columns and collect errors in 'validationErrors'

Parameters
validationErrors:ValidationErrors

ValidationErrors to collect errors

Returns:ValidationErrors

validationErrors

warnback to summary
public Warnings warn(Database database)

Overrides liquibase.change.AbstractChange.warn.

Implements liquibase.change.Change.warn.

Doc from liquibase.change.AbstractChange.warn.

Implementation delegates logic to the liquibase.sqlgenerator.SqlGenerator#warn(liquibase.statement.SqlStatement, liquibase.database.Database, liquibase.sqlgenerator.SqlGeneratorChain) method on the SqlStatement objects returned by generateStatements. If a generated statement is not supported for the given database, no warning will be added since that is a validation error. If no or null SqlStatements are returned by generateStatements then this method returns no warnings.

Annotations
@Override
liquibase.change.core back to summary

public final Enum LoadDataChange.LOAD_DATA_TYPE

extends Enum<LoadDataChange.LOAD_DATA_TYPE>
Class Inheritance
Annotations
@SuppressWarnings:HardCodedStringLiteral

Field Summary

Modifier and TypeField and Description
public static final LoadDataChange.LOAD_DATA_TYPE
public static final LoadDataChange.LOAD_DATA_TYPE
public static final LoadDataChange.LOAD_DATA_TYPE
public static final LoadDataChange.LOAD_DATA_TYPE
public static final LoadDataChange.LOAD_DATA_TYPE
public static final LoadDataChange.LOAD_DATA_TYPE
public static final LoadDataChange.LOAD_DATA_TYPE
public static final LoadDataChange.LOAD_DATA_TYPE
public static final LoadDataChange.LOAD_DATA_TYPE
public static final LoadDataChange.LOAD_DATA_TYPE
public static final LoadDataChange.LOAD_DATA_TYPE
public static final LoadDataChange.LOAD_DATA_TYPE

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static LoadDataChange.LOAD_DATA_TYPE
public static LoadDataChange.LOAD_DATA_TYPE[]
Inherited from java.lang.Enum:
clonecompareTodescribeConstableequalsfinalizegetDeclaringClasshashCodenameordinaltoStringvalueOf

Field Detail

BLOBback to summary
public static final LoadDataChange.LOAD_DATA_TYPE BLOB
BOOLEANback to summary
public static final LoadDataChange.LOAD_DATA_TYPE BOOLEAN
CLOBback to summary
public static final LoadDataChange.LOAD_DATA_TYPE CLOB
COMPUTEDback to summary
public static final LoadDataChange.LOAD_DATA_TYPE COMPUTED
DATEback to summary
public static final LoadDataChange.LOAD_DATA_TYPE DATE
NUMERICback to summary
public static final LoadDataChange.LOAD_DATA_TYPE NUMERIC
OTHERback to summary
public static final LoadDataChange.LOAD_DATA_TYPE OTHER
SEQUENCEback to summary
public static final LoadDataChange.LOAD_DATA_TYPE SEQUENCE
SKIPback to summary
public static final LoadDataChange.LOAD_DATA_TYPE SKIP
STRINGback to summary
public static final LoadDataChange.LOAD_DATA_TYPE STRING
UNKNOWNback to summary
public static final LoadDataChange.LOAD_DATA_TYPE UNKNOWN
UUIDback to summary
public static final LoadDataChange.LOAD_DATA_TYPE UUID

Constructor Detail

LOAD_DATA_TYPEback to summary
private LOAD_DATA_TYPE()

Method Detail

valueOfback to summary
public static LoadDataChange.LOAD_DATA_TYPE valueOf(String name)
valuesback to summary
public static LoadDataChange.LOAD_DATA_TYPE[] values()
liquibase.change.core back to summary

protected Class LoadDataChange.LoadDataRowConfig

extends Object
Class Inheritance

Field Summary

Modifier and TypeField and Description
private final List<LoadDataColumnConfig>
private final boolean

Constructor Summary

AccessConstructor and Description
public
LoadDataRowConfig(boolean needsPreparedStatement, List<LoadDataColumnConfig> columns)

Method Summary

Modifier and TypeMethod and Description
public List<LoadDataColumnConfig>
public boolean
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

columnsback to summary
private final List<LoadDataColumnConfig> columns
needsPreparedStatementback to summary
private final boolean needsPreparedStatement

Constructor Detail

LoadDataRowConfigback to summary
public LoadDataRowConfig(boolean needsPreparedStatement, List<LoadDataColumnConfig> columns)

Method Detail

getColumnsback to summary
public List<LoadDataColumnConfig> getColumns()
needsPreparedStatementback to summary
public boolean needsPreparedStatement()