All Downloads are FREE. Search and download functionalities are using the official Maven repository.

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>(60);
                        while (rs.next()) {
                            if (rows.size() >= 50) {
                                LoadItem item = new LoadItem>>(table, rows);
                                scheduleLoad(item);
                                rows = new ArrayList>(60);
                            }
                            
                            Map row = new HashMap();
                            for (String name : names) {
                                row.put(name, rs.getObject(name));
                            }
                            rows.add(row);
                        }
                        
			rs.close();
                        
                        LoadItem item = new LoadItem>>(table, rows);
                        scheduleLoad(item);
                    } catch (Exception e) {
			LOG.log(Level.WARNING, "Failed to read data for table {0}", table);
			LOG.log(Level.WARNING, e.getMessage(), e);
                    }
                    setProgress(getProgress() + 1);
                }
                setProgress(getMaximum());
            }
            
            
            protected void loadData(LoadItem[] items) {
                for (LoadItem>> item : items) {
                    for (Map row : item.data) {
                        DataRow r  = item.table.appendRowNoEvent();
                        for (String col : row.keySet()) {
			    if (columnMappings.containsKey(col)) {
				col = columnMappings.get(col);
			    }
                            r.setValue(col, row.get(col));
                        }
                        r.setStatus(DataRow.DataRowStatus.UNCHANGED);
                        
                        
                        item.table.fireDataTableChanged(TableChangeEvent.newRowAddedEvent(item.table, r));
                    }
                    item.table.fireDataTableChanged(TableChangeEvent.newLoadCompleteEvent(item.table));                    
                }
            }
        };
    }

    
    protected SaveTask createSaveTask(DataTable[] tables) {
        return new SaveTask(tables) {
            protected void saveData(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) {
                    
		    
		    
		    
                    List rows = new ArrayList(table.getRows());
                    
                    
                    for (DataRow row : rows) {
                        PreparedStatement stmt = null;
                        switch (row.getStatus()) {
                            case UPDATED:
                                stmt = ((AbstractSqlCommand)getCommand()).getUpdateStatement(conn, row);
                                conn.executeUpdate(stmt);
                                row.setStatus(DataRow.DataRowStatus.UNCHANGED);
                                break;
                            case INSERTED:
                                stmt = ((AbstractSqlCommand)getCommand()).getInsertStatement(conn, row);
                                conn.executeUpdate(stmt);
                                row.setStatus(DataRow.DataRowStatus.UNCHANGED);
                                break;
                            case DELETED:
                                stmt = ((AbstractSqlCommand)getCommand()).getDeleteStatement(conn, row);
                                conn.executeUpdate(stmt);
                                table.discardRow(row);
                                break;
                            default:
                                
                                break;
                        }
                    }
                    table.fireDataTableChanged(TableChangeEvent.newSaveCompleteEvent(table));
                    setProgress(getProgress() + 1);
                }
                setProgress(getMaximum());
                conn.commit();
            }
        };
    }    
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy