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) {
}
}
}
}
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());
}
}
Aggregations