/** * Use COPY FROM STDIN for very fast copying from an InputStream into a database table. * @param sql COPY FROM STDIN statement * @param from a CSV file or such * @param bufferSize number of bytes to buffer and push over network to server at once * @return number of rows updated for server 8.2 or newer; -1 for older * @throws SQLException on database usage issues * @throws IOException upon input stream or database connection failure */ public long copyIn(final String sql, InputStream from, int bufferSize) throws SQLException, IOException { byte[] buf = new byte[bufferSize]; int len; CopyIn cp = copyIn(sql); try { while( (len = from.read(buf)) > 0 ) { cp.writeToCopy(buf, 0, len); } return cp.endCopy(); } finally { // see to it that we do not leave the connection locked if(cp.isActive()) cp.cancelCopy(); } } }
public void flush() throws IOException { try { op.writeToCopy(copyBuffer, 0, at); at = 0; op.flushCopy(); } catch (SQLException e) { IOException ioe = new IOException("Unable to flush stream"); ioe.initCause(e); throw ioe; } }
public long getHandledRowCount() { return op.getHandledRowCount(); }
public long endCopy() throws SQLException { if (at > 0) { op.writeToCopy(copyBuffer, 0, at); } op.endCopy(); return getHandledRowCount(); }
public void writeToCopy(byte[] buf, int off, int siz) throws SQLException { if(at > 0 && siz > copyBuffer.length - at) { // would not fit into rest of our buf, so flush buf op.writeToCopy(copyBuffer, 0, at); at = 0; } if(siz > copyBuffer.length) { // would still not fit into buf, so just pass it through op.writeToCopy(buf, off, siz); } else { // fits into our buf, so save it there System.arraycopy(buf, off, copyBuffer, at, siz); at += siz; } }
public void cancelCopy() throws SQLException { op.cancelCopy(); }
public void flushCopy() throws SQLException { op.flushCopy(); }
public boolean isActive() { return op.isActive(); }
protected void endCopy() { if (copyIn != null) { try { flush(); } finally { try { copyIn.endCopy(); } catch (Exception ex) { throw getPlatform().getSqlTemplate().translate(ex); } finally { copyIn = null; } } } }
public int getFieldFormat(int field) { return op.getFieldFormat(field); }
public int getFormat() { return op.getFormat(); }
public int getFieldCount() { return op.getFieldCount(); }
public long endCopy() throws SQLException { if(at > 0) { op.writeToCopy(copyBuffer, 0, at); } op.endCopy(); return getHandledRowCount(); }
public void writeToCopy(byte[] buf, int off, int siz) throws SQLException { if (at > 0 && siz > copyBuffer.length - at) { // would not fit into rest of our buf, so flush buf op.writeToCopy(copyBuffer, 0, at); at = 0; } if (siz > copyBuffer.length) { // would still not fit into buf, so just pass it through op.writeToCopy(buf, off, siz); } else { // fits into our buf, so save it there System.arraycopy(buf, off, copyBuffer, at, siz); at += siz; } }
public void cancelCopy() throws SQLException { op.cancelCopy(); }
public void flushCopy() throws SQLException { op.flushCopy(); }
public boolean isActive() { return op.isActive(); }
public int getFieldFormat(int field) { return op.getFieldFormat(field); }
public int getFormat() { return op.getFormat(); }
public int getFieldCount() { return op.getFieldCount(); }