com.datastax.data.dataset.provider.sql.SQLDataProvider Maven / Gradle / Ivy
package com.datastax.data.dataset.provider.sql;
import com.datastax.data.dataset.DataColumn;
import com.datastax.data.dataset.DataProvider;
import com.datastax.data.dataset.DataRow;
import com.datastax.data.dataset.DataTable;
import com.datastax.data.dataset.event.TableChangeEvent;
import com.datastax.data.dataset.provider.LoadTask;
import com.datastax.data.dataset.provider.SaveTask;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
public class SQLDataProvider extends DataProvider {
private static final Logger LOG = Logger.getLogger(SQLDataProvider.class.getName());
private Map columnMappings = new HashMap();
public SQLDataProvider() {
}
public SQLDataProvider(String tableName) {
TableCommand tableCommand = new TableCommand(tableName);
setCommand(tableCommand);
}
public SQLDataProvider(String tableName, String whereClause) {
TableCommand tableCommand = new TableCommand(tableName, whereClause);
setCommand(tableCommand);
}
public void addColumnNameMapping(String pseudonym, String realName) {
columnMappings.put(pseudonym, realName);
}
public void removeColumnNameMapping(String pseudonym) {
columnMappings.remove(pseudonym);
}
protected LoadTask createLoadTask(DataTable[] tables) {
return new LoadTask(tables) {
protected void readData(DataTable[] tables) throws Exception {
JDBCDataConnection conn = (JDBCDataConnection)getConnection();
if (conn == null) {
return;
}
if (getCommand() == null) {
return;
}
setMinimum(0);
setMaximum(tables.length);
for (DataTable table : tables) {
try {
PreparedStatement stmt = ((AbstractSqlCommand)getCommand()).getSelectStatement(conn);
ResultSet rs = stmt.executeQuery();
Set colNames = new HashSet();
colNames.addAll(columnMappings.keySet());
for (DataColumn col : table.getColumns()) {
colNames.add(col.getName());
}
ResultSetMetaData md = rs.getMetaData();
List names = new ArrayList();
for (String name : colNames) {
for (int i=0; i> rows = new ArrayList