1 16 package scriptella.driver.csv; 17 18 import au.com.bytecode.opencsv.CSVReader; 19 import au.com.bytecode.opencsv.CSVWriter; 20 import scriptella.core.EtlCancelledException; 21 import scriptella.driver.text.AbstractTextConnection; 22 import scriptella.expression.PropertiesSubstitutor; 23 import scriptella.spi.ConnectionParameters; 24 import scriptella.spi.ParametersCallback; 25 import scriptella.spi.ProviderException; 26 import scriptella.spi.QueryCallback; 27 import scriptella.spi.Resource; 28 import scriptella.util.IOUtils; 29 import scriptella.util.StringUtils; 30 31 import java.io.IOException ; 32 import java.io.Reader ; 33 import java.util.Arrays ; 34 import java.util.logging.Level ; 35 import java.util.logging.Logger ; 36 37 44 public class CsvConnection extends AbstractTextConnection { 45 private static final Logger LOG = Logger.getLogger(CsvConnection.class.getName()); 46 private CSVWriter out; 47 48 49 53 public static final String SEPARATOR = "separator"; 54 59 public static final String QUOTE = "quote"; 60 61 66 public static final String HEADERS = "headers"; 67 68 private final char separator; 69 private final char quote; 70 private final boolean headers; 71 72 73 public CsvConnection(ConnectionParameters parameters) { 74 super(Driver.DIALECT, parameters); 75 String sep = parameters.getStringProperty(SEPARATOR); 76 if (sep != null && sep.length() > 0) { 77 separator = sep.charAt(0); 78 } else { 79 separator = ','; 80 } 81 String q = parameters.getStringProperty(QUOTE); 82 if (q == null) { 83 quote = '\"'; } else if (q.length() > 0) { 85 quote = q.charAt(0); 86 } else { quote = CSVWriter.NO_QUOTE_CHARACTER; 88 } 89 90 headers = parameters.getBooleanProperty(HEADERS, true); 91 } 92 93 public void executeScript(Resource scriptContent, ParametersCallback parametersCallback) throws ProviderException { 94 try { 95 executeScript(scriptContent.open(), parametersCallback); 96 } catch (IOException e) { 97 throw new CsvProviderException("Cannot read script.", e); 98 } 99 } 100 101 102 void executeScript(Reader reader, ParametersCallback parametersCallback) throws IOException { 103 104 CSVReader r = new CSVReader(reader); CSVWriter out = getOut(); 106 PropertiesSubstitutor ps = new PropertiesSubstitutor(parametersCallback); 107 for (String [] row; (row = r.readNext()) != null;) { 108 EtlCancelledException.checkEtlCancelled(); 109 for (int i = 0; i < row.length; i++) { 110 String field = row[i]; 111 if (field != null) { 112 if (trim) { field = field.trim(); 114 } 115 row[i] = ps.substitute(field); 116 } 117 } 118 if (row.length==1 && StringUtils.isAsciiWhitespacesOnly(row[0])) { 120 continue; 121 } 122 123 if (isReadonly()) { 124 if (LOG.isLoggable(Level.INFO)) { 125 LOG.info("Readonly Mode - " + Arrays.deepToString(row) + " has been skipped."); 126 } 127 } else { 128 try { 129 out.writeNext(row); 130 counter.statements++; 131 } catch (Exception e) { 132 throw new CsvProviderException("Failed to write CSV row ", e); 133 } 134 } 135 } 136 } 137 138 public void executeQuery(Resource queryContent, ParametersCallback parametersCallback, QueryCallback queryCallback) throws ProviderException { 139 if (out != null) { 140 throw new CsvProviderException("Cannot query and update a CSV file simultaneously."); 141 } 142 try { 143 CSVReader in = new CSVReader(IOUtils.getReader(url.openStream(), encoding), separator, quote); 144 CsvQuery q = new CsvQuery(in, headers, trim); 145 try { 146 q.execute(new CSVReader(queryContent.open()), parametersCallback, queryCallback, counter); 147 } catch (IOException e) { 148 throw new CsvProviderException("Cannot read query " + queryContent, e); 149 } finally { 150 IOUtils.closeSilently(q); 151 } 152 } catch (IOException e) { 153 throw new CsvProviderException("Unable to open URL " + url + " for input", e); 154 } 155 } 156 157 160 protected CSVWriter getOut() { 161 if (out == null) { 162 try { 163 out = new CSVWriter(IOUtils.getWriter(IOUtils.getOutputStream(url), encoding), separator, quote, eol); 164 } catch (IOException e) { 165 throw new CsvProviderException("Unable to open URL " + url + " for output", e); 166 } 167 } 168 return out; 169 } 170 171 172 public void close() throws ProviderException { 173 if (out != null) { 174 try { 175 out.close(); 176 } catch (Exception e) { 177 LOG.log(Level.INFO, "A problem occured while trying to close CSV writer", e); 178 } 179 } 180 } 181 } 182 | Popular Tags |