001: package liquibase.change;
002:
003: import liquibase.database.Database;
004: import liquibase.database.sql.InsertStatement;
005: import liquibase.database.sql.SqlStatement;
006: import liquibase.database.structure.DatabaseObject;
007: import liquibase.database.structure.Table;
008: import liquibase.exception.UnsupportedChangeException;
009: import org.w3c.dom.Document;
010: import org.w3c.dom.Element;
011:
012: import java.util.*;
013:
014: /**
015: * Inserts data into an existing table.
016: */
017: public class InsertDataChange extends AbstractChange {
018:
019: private String schemaName;
020: private String tableName;
021: private List<ColumnConfig> columns;
022:
023: public InsertDataChange() {
024: super ("insert", "Insert Row");
025: columns = new ArrayList<ColumnConfig>();
026: }
027:
028: public String getSchemaName() {
029: return schemaName;
030: }
031:
032: public void setSchemaName(String schemaName) {
033: this .schemaName = schemaName;
034: }
035:
036: public String getTableName() {
037: return tableName;
038: }
039:
040: public void setTableName(String tableName) {
041: this .tableName = tableName;
042: }
043:
044: public List<ColumnConfig> getColumns() {
045: return columns;
046: }
047:
048: public void setColumns(List<ColumnConfig> columns) {
049: this .columns = columns;
050: }
051:
052: public void addColumn(ColumnConfig column) {
053: columns.add(column);
054: }
055:
056: public void removeColumn(ColumnConfig column) {
057: columns.remove(column);
058: }
059:
060: public SqlStatement[] generateStatements(Database database)
061: throws UnsupportedChangeException {
062:
063: InsertStatement statement = new InsertStatement(
064: getSchemaName() == null ? database
065: .getDefaultSchemaName() : getSchemaName(),
066: getTableName());
067:
068: for (ColumnConfig column : columns) {
069: statement.addColumnValue(column.getName(), column
070: .getValueObject());
071: }
072:
073: return new SqlStatement[] { statement };
074: }
075:
076: public String getConfirmationMessage() {
077: return "New row inserted into " + getTableName();
078: }
079:
080: public Element createNode(Document currentChangeLogFileDOM) {
081: Element node = currentChangeLogFileDOM.createElement("insert");
082: if (getSchemaName() != null) {
083: node.setAttribute("schemaName", getSchemaName());
084: }
085:
086: node.setAttribute("tableName", getTableName());
087:
088: for (ColumnConfig col : getColumns()) {
089: Element subNode = col.createNode(currentChangeLogFileDOM);
090: node.appendChild(subNode);
091: }
092: return node;
093: }
094:
095: public Set<DatabaseObject> getAffectedDatabaseObjects() {
096: Table dbObject = new Table(getTableName());
097:
098: return new HashSet<DatabaseObject>(Arrays.asList(dbObject));
099: }
100:
101: }
|