rapture.repo.jdbc.TestDataLoader Maven / Gradle / Ivy
/**
* Copyright (C) 2011-2015 Incapture Technologies LLC
*
* This is an autogenerated license statement. When copyright notices appear below
* this one that copyright supercedes this statement.
*
* Unless required by applicable law or agreed to in writing, software is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied.
*
* Unless explicit permission obtained in writing this software cannot be distributed.
*/
package rapture.repo.jdbc;
import java.net.HttpURLConnection;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Types;
import java.util.List;
import org.antlr.runtime.RecognitionException;
import org.apache.log4j.Logger;
import rapture.common.exception.RaptureException;
import rapture.common.exception.RaptureExceptionFactory;
import rapture.common.exception.RaptureExceptionFormatter;
import rapture.parser.CSVExtractor;
/**
* While we are testing all of this out we need this class to load up some
* default data into a table, so that we can pretend that this data is actually
* generated elsewhere.
*
* @author amkimian
*
*/
public class TestDataLoader {
private static Logger log = Logger.getLogger(TestDataLoader.class);
private Connection connection;
public TestDataLoader(Connection connection) {
this.connection = connection;
}
public void loadStandardTable(String resourceContent, String tableName) {
List> results;
try {
results = CSVExtractor.getCSV(resourceContent);
} catch (RecognitionException e1) {
RaptureException raptException = RaptureExceptionFactory.create(HttpURLConnection.HTTP_INTERNAL_ERROR, "Error reading config: " + resourceContent);
log.error(RaptureExceptionFormatter.getExceptionMessage(raptException, e1));
throw raptException;
}
List fields = results.get(0);
StringBuilder createTable = new StringBuilder();
StringBuilder insertStatement = new StringBuilder();
createTable.append("create table ");
createTable.append(tableName);
createTable.append(" (");
insertStatement.append("insert into ");
insertStatement.append(tableName);
insertStatement.append(" values(");
boolean first = true;
for (String f : fields) {
if (!first) {
createTable.append(",");
insertStatement.append(",");
} else {
first = false;
}
insertStatement.append("?");
f = f.replaceAll(" ", "");
char firstChar = f.charAt(0);
if (Character.isDigit(firstChar)) {
f = "n" + f;
createTable.append(f);
createTable.append(" double");
} else {
createTable.append(f);
createTable.append(" varchar(100)");
}
}
createTable.append(")");
insertStatement.append(")");
try {
log.info("Creating table using " + createTable.toString());
connection.createStatement().executeUpdate(createTable.toString());
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Now add the data
try {
int totalCount = 0;
PreparedStatement ps = connection.prepareStatement(insertStatement.toString());
for (int i = 1; i < results.size(); i++) {
List vals = results.get(i);
if (vals.size() != fields.size()) {
continue;
}
ps.clearParameters();
// First two are strings
ps.setString(1, vals.get(0));
ps.setString(2, vals.get(1));
for (int j = 2; j < vals.size(); j++) {
String v = vals.get(j);
if (v.isEmpty()) {
ps.setNull(j + 1, Types.DOUBLE);
} else {
ps.setDouble(j + 1, Double.valueOf(v));
}
}
// log.info("Inserting using " + ps.toString());
totalCount += ps.executeUpdate();
}
log.info("Inserted " + totalCount + " records");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy