Search in sources :

Example 1 with LoadDataColumnConfig

use of liquibase.change.core.LoadDataColumnConfig in project liquibase by liquibase.

the class MissingDataExternalFileChangeGenerator method fixMissing.

@Override
public Change[] fixMissing(DatabaseObject missingObject, DiffOutputControl outputControl, Database referenceDatabase, Database comparisionDatabase, ChangeGeneratorChain chain) {
    Statement stmt = null;
    ResultSet rs = null;
    try {
        Data data = (Data) missingObject;
        Table table = data.getTable();
        if (referenceDatabase.isLiquibaseObject(table)) {
            return null;
        }
        String sql = "SELECT * FROM " + referenceDatabase.escapeTableName(table.getSchema().getCatalogName(), table.getSchema().getName(), table.getName());
        stmt = ((JdbcConnection) referenceDatabase.getConnection()).createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
        stmt.setFetchSize(100);
        rs = stmt.executeQuery(sql);
        List<String> columnNames = new ArrayList<String>();
        for (int i = 0; i < rs.getMetaData().getColumnCount(); i++) {
            columnNames.add(rs.getMetaData().getColumnName(i + 1));
        }
        String fileName = table.getName().toLowerCase() + ".csv";
        if (dataDir != null) {
            fileName = dataDir + "/" + fileName;
            File parentDir = new File(dataDir);
            if (!parentDir.exists()) {
                parentDir.mkdirs();
            }
            if (!parentDir.isDirectory()) {
                throw new RuntimeException(parentDir + " is not a directory");
            }
        }
        CSVWriter outputFile = new CSVWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), LiquibaseConfiguration.getInstance().getConfiguration(GlobalConfiguration.class).getOutputEncoding())));
        String[] dataTypes = new String[columnNames.size()];
        String[] line = new String[columnNames.size()];
        for (int i = 0; i < columnNames.size(); i++) {
            line[i] = columnNames.get(i);
        }
        outputFile.writeNext(line);
        int rowNum = 0;
        while (rs.next()) {
            line = new String[columnNames.size()];
            for (int i = 0; i < columnNames.size(); i++) {
                Object value = JdbcUtils.getResultSetValue(rs, i + 1);
                if (dataTypes[i] == null && value != null) {
                    if (value instanceof Number) {
                        dataTypes[i] = "NUMERIC";
                    } else if (value instanceof Boolean) {
                        dataTypes[i] = "BOOLEAN";
                    } else if (value instanceof Date) {
                        dataTypes[i] = "DATE";
                    } else {
                        dataTypes[i] = "STRING";
                    }
                }
                if (value == null) {
                    line[i] = "NULL";
                } else {
                    if (value instanceof Date) {
                        line[i] = new ISODateFormat().format(((Date) value));
                    } else {
                        line[i] = value.toString();
                    }
                }
            }
            outputFile.writeNext(line);
            rowNum++;
            if (rowNum % 5000 == 0) {
                outputFile.flush();
            }
        }
        outputFile.flush();
        outputFile.close();
        LoadDataChange change = new LoadDataChange();
        change.setFile(fileName);
        change.setEncoding(LiquibaseConfiguration.getInstance().getConfiguration(GlobalConfiguration.class).getOutputEncoding());
        if (outputControl.getIncludeCatalog()) {
            change.setCatalogName(table.getSchema().getCatalogName());
        }
        if (outputControl.getIncludeSchema()) {
            change.setSchemaName(table.getSchema().getName());
        }
        change.setTableName(table.getName());
        for (int i = 0; i < columnNames.size(); i++) {
            String colName = columnNames.get(i);
            LoadDataColumnConfig columnConfig = new LoadDataColumnConfig();
            columnConfig.setHeader(colName);
            columnConfig.setName(colName);
            columnConfig.setType(dataTypes[i]);
            change.addColumn(columnConfig);
        }
        return new Change[] { change };
    } catch (Exception e) {
        throw new UnexpectedLiquibaseException(e);
    } finally {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException ignore) {
            }
        }
        if (stmt != null) {
            try {
                stmt.close();
            } catch (SQLException ignore) {
            }
        }
    }
}
Also used : SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) CSVWriter(liquibase.util.csv.CSVWriter) BufferedWriter(java.io.BufferedWriter) ISODateFormat(liquibase.util.ISODateFormat) ResultSet(java.sql.ResultSet) LoadDataColumnConfig(liquibase.change.core.LoadDataColumnConfig) LoadDataChange(liquibase.change.core.LoadDataChange) Table(liquibase.structure.core.Table) Statement(java.sql.Statement) Data(liquibase.structure.core.Data) Change(liquibase.change.Change) LoadDataChange(liquibase.change.core.LoadDataChange) Date(java.util.Date) SQLException(java.sql.SQLException) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) DatabaseObject(liquibase.structure.DatabaseObject) File(java.io.File) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException)

Example 2 with LoadDataColumnConfig

use of liquibase.change.core.LoadDataColumnConfig in project liquibase by liquibase.

the class ChangeParameterMetaData method getExampleValue.

public Object getExampleValue(Database database) {
    if (exampleValues != null) {
        Object exampleValue = null;
        for (Map.Entry<String, Object> entry : exampleValues.entrySet()) {
            if (entry.getKey().equalsIgnoreCase("all")) {
                exampleValue = entry.getValue();
            } else if (DatabaseList.definitionMatches(entry.getKey(), database, false)) {
                return entry.getValue();
            }
        }
        if (exampleValue != null) {
            return exampleValue;
        }
    }
    Map standardExamples = new HashMap();
    standardExamples.put("tableName", "person");
    standardExamples.put("schemaName", "public");
    standardExamples.put("tableSchemaName", "public");
    standardExamples.put("catalogName", "cat");
    standardExamples.put("tableCatalogName", "cat");
    standardExamples.put("columnName", "id");
    standardExamples.put("columnNames", "id, name");
    standardExamples.put("indexName", "idx_address");
    standardExamples.put("columnDataType", "int");
    standardExamples.put("dataType", "int");
    standardExamples.put("sequenceName", "seq_id");
    standardExamples.put("viewName", "v_person");
    standardExamples.put("constraintName", "const_name");
    standardExamples.put("primaryKey", "pk_id");
    if (standardExamples.containsKey(parameterName)) {
        return standardExamples.get(parameterName);
    }
    for (String prefix : new String[] { "base", "referenced", "new", "old" }) {
        if (parameterName.startsWith(prefix)) {
            String mainName = StringUtils.lowerCaseFirst(parameterName.replaceFirst("^" + prefix, ""));
            if (standardExamples.containsKey(mainName)) {
                return standardExamples.get(mainName);
            }
        }
    }
    if (dataType.equals("string")) {
        return "A String";
    } else if (dataType.equals("integer")) {
        return 3;
    } else if (dataType.equals("boolean")) {
        return true;
    } else if (dataType.equals("bigInteger")) {
        return new BigInteger("371717");
    } else if (dataType.equals("list")) {
        //"TODO";
        return null;
    } else if (dataType.equals("sequenceNextValueFunction")) {
        return new SequenceNextValueFunction("seq_name");
    } else if (dataType.equals("databaseFunction")) {
        return new DatabaseFunction("now");
    } else if (dataType.equals("list of columnConfig")) {
        ArrayList<ColumnConfig> list = new ArrayList<ColumnConfig>();
        list.add(new ColumnConfig().setName("id").setType("int"));
        return list;
    } else if (dataType.equals("list of addColumnConfig")) {
        ArrayList<ColumnConfig> list = new ArrayList<ColumnConfig>();
        list.add(new AddColumnConfig().setName("id").setType("int"));
        return list;
    } else if (dataType.equals("list of loadDataColumnConfig")) {
        ArrayList<ColumnConfig> list = new ArrayList<ColumnConfig>();
        list.add(new LoadDataColumnConfig().setName("id").setType("int"));
        return list;
    } else {
        throw new UnexpectedLiquibaseException("Unknown dataType " + dataType + " for " + getParameterName());
    }
}
Also used : DatabaseFunction(liquibase.statement.DatabaseFunction) LoadDataColumnConfig(liquibase.change.core.LoadDataColumnConfig) LoadDataColumnConfig(liquibase.change.core.LoadDataColumnConfig) BigInteger(java.math.BigInteger) SequenceNextValueFunction(liquibase.statement.SequenceNextValueFunction) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException)

Aggregations

LoadDataColumnConfig (liquibase.change.core.LoadDataColumnConfig)2 UnexpectedLiquibaseException (liquibase.exception.UnexpectedLiquibaseException)2 BufferedWriter (java.io.BufferedWriter)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 BigInteger (java.math.BigInteger)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 Statement (java.sql.Statement)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 Change (liquibase.change.Change)1 LoadDataChange (liquibase.change.core.LoadDataChange)1 DatabaseFunction (liquibase.statement.DatabaseFunction)1 SequenceNextValueFunction (liquibase.statement.SequenceNextValueFunction)1 DatabaseObject (liquibase.structure.DatabaseObject)1 Data (liquibase.structure.core.Data)1 Table (liquibase.structure.core.Table)1 ISODateFormat (liquibase.util.ISODateFormat)1