1 16 package scriptella.driver.text; 17 18 import scriptella.spi.ConnectionParameters; 19 import scriptella.spi.ParametersCallback; 20 import scriptella.spi.ProviderException; 21 import scriptella.spi.QueryCallback; 22 import scriptella.spi.Resource; 23 import scriptella.util.IOUtils; 24 25 import java.io.IOException ; 26 import java.io.Reader ; 27 28 35 public class TextConnection extends AbstractTextConnection { 36 private TextScriptExecutor out; 37 38 41 protected TextConnection() { 42 } 43 44 public TextConnection(ConnectionParameters parameters) { 45 super(Driver.DIALECT, parameters); 46 } 47 48 public void executeScript(final Resource scriptContent, final ParametersCallback parametersCallback) throws ProviderException { 49 initOut(); 50 Reader reader = null; 51 try { 52 reader = scriptContent.open(); 53 out.execute(reader, parametersCallback, counter); 54 } catch (IOException e) { 55 throw new TextProviderException("Failed to produce a text file", e); 56 } finally { 57 IOUtils.closeSilently(reader); 58 } 59 } 60 61 64 protected void initOut() { 65 if (out == null) { 66 try { 67 out = new TextScriptExecutor(IOUtils.getWriter(IOUtils.getOutputStream(url), encoding), trim, eol); 68 } catch (IOException e) { 69 throw new TextProviderException("Unable to open file " + url + " for writing", e); 70 } 71 } 72 } 73 74 public void executeQuery(Resource queryContent, ParametersCallback parametersCallback, QueryCallback queryCallback) throws ProviderException { 75 if (out != null) { 76 throw new TextProviderException("Cannot query and update a Text file simultaneously"); 77 } 78 79 Reader in; 80 try { 81 in = IOUtils.getReader(url.openStream(), encoding); 82 } catch (IOException e) { 83 throw new TextProviderException("Cannot open a text file for reading", e); 84 } 85 Reader q; 86 try { 87 q = queryContent.open(); 88 } catch (IOException e) { 89 throw new TextProviderException("Cannot read a text query", e); 90 } 91 92 TextQueryExecutor tq = null; 93 try { 94 tq = new TextQueryExecutor(q, trim, in , parametersCallback); 95 tq.execute(queryCallback, counter); 96 } finally { 97 if (tq!=null) { 98 IOUtils.closeSilently(tq); 99 } else { 100 IOUtils.closeSilently(in); 101 } 102 } 103 } 104 105 public void close() throws ProviderException { 106 IOUtils.closeSilently(out); 107 out = null; 108 } 109 } 110 | Popular Tags |