/** * Use COPY FROM STDIN for very fast copying from a Reader into a database table. * * @param sql COPY FROM STDIN statement * @param from a CSV file or such * @return number of rows updated for server 8.2 or newer; -1 for older * @throws SQLException on database usage issues * @throws IOException upon reader or database connection failure */ public long copyIn(final String sql, Reader from) throws SQLException, IOException { return copyIn(sql, from, DEFAULT_BUFFER_SIZE); }
CopyManager copyManager = new CopyManager((BaseConnection) con); copyManager.copyIn("COPY t FROM STDIN", fileReader );
/** * Uses given connection for specified COPY TO STDOUT operation. * * @param connection database connection to use for copying (protocol version 3 required) * @param sql COPY TO STDOUT statement * @throws SQLException if initializing the operation fails */ public PGCopyInputStream(PGConnection connection, String sql) throws SQLException { this(connection.getCopyAPI().copyOut(sql)); }
AbstractJdbc<?> logConnection=conn.unwrap(AbstractJdbc.class); Object output=sqlParameters.getOutputStream(); CopyManager copyManager = new CopyManager(connection); String sql=sqlParameters.getSql(); long result=-1; if ("STDOUT".equals(std)){ if (output instanceof Writer){ result= copyManager.copyOut(sql, (Writer)output); }else if (output instanceof OutputStream){ result= copyManager.copyOut(sql, (OutputStream)output); } else{ result= copyManager.copyOut(sql, System.out); CopyOut copyOut= copyManager.copyOut(sql); result=copyOut.getHandledRowCount();
public CopyManager getCopyAPI() throws SQLException { checkClosed(); if (copyManager == null) { copyManager = new CopyManager(this); } return copyManager; }
LOG.info(copySql); CopyManager copyManager = new CopyManager(connection.unwrap(BaseConnection.class)); tableLoadResult.rowCount = (int) copyManager.copyOut(copySql, protectedOut); if (tableLoadResult.rowCount == 0) {
public CopyManager getCopyAPI() throws SQLException { checkClosed(); if (copyManager == null) copyManager = new CopyManager(this); return copyManager; } }
/** * 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 * @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) throws SQLException, IOException { return copyIn(sql, from, DEFAULT_BUFFER_SIZE); }
SessionFactory factory = HibernateUtil.getSessionFactory(); Session session = factory.openSession(); SessionImplementor sessImpl = (SessionImplementor) session; Connection conn = null; conn = sessImpl.getJdbcConnectionAccess().obtainConnection(); CopyManager copyManager = new CopyManager((BaseConnection) conn); File tf =File.createTempFile("temp-file", "tmp"); String tempPath =tf.getParent(); File tempFile = new File(tempPath + File.separator + filename); FileReader fileReader = new FileReader(tempFile); copyManager.copyIn("copy testdata (col1, col2, col3) from STDIN with csv", fileReader );
/** * Uses given connection for specified COPY TO STDOUT operation * @param connection database connection to use for copying (protocol version 3 required) * @param sql COPY TO STDOUT statement * @throws SQLException if initializing the operation fails */ public PGCopyInputStream(PGConnection connection, String sql) throws SQLException { this(connection.getCopyAPI().copyOut(sql)); }
public CopyManager getCopyAPI() throws SQLException { if (copyManager == null) copyManager = new CopyManager(this); return copyManager; } }
/** * 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 * @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) throws SQLException, IOException { return copyIn(sql, from, DEFAULT_BUFFER_SIZE); }
public synchronized long copyFromReader(TableSchema tableSchema, Reader reader) { try { String query = tableSchema.copyFromStdin(); CopyManager copyManager = new CopyManager((BaseConnection) currentConnection); long numRows = copyManager.copyIn(query, reader); return numRows; } catch (Exception e) { throw new EmissionException(e); } }
/** * Pass results of a COPY TO STDOUT query from database into an OutputStream. * @param sql COPY TO STDOUT statement * @param to the stream to write the results to (row by row) * @return number of rows updated for server 8.2 or newer; -1 for older * @throws SQLException on database usage errors * @throws IOException upon output stream or database connection failure */ public long copyOut(final String sql, OutputStream to) throws SQLException, IOException { byte[] buf; CopyOut cp = copyOut(sql); try { while( (buf = cp.readFromCopy()) != null ) { to.write(buf); } return cp.getHandledRowCount(); } finally { // see to it that we do not leave the connection locked if(cp.isActive()) cp.cancelCopy(); } }
public CopyManager getCopyAPI() throws SQLException { checkClosed(); if (copyManager == null) copyManager = new CopyManager(this); return copyManager; } }
/** * Use COPY FROM STDIN for very fast copying from a Reader into a database table. * @param sql COPY FROM STDIN statement * @param from a CSV file or such * @return number of rows updated for server 8.2 or newer; -1 for older * @throws SQLException on database usage issues * @throws IOException upon reader or database connection failure */ public long copyIn(final String sql, Reader from) throws SQLException, IOException { return copyIn(sql, from, DEFAULT_BUFFER_SIZE); }
/** * Method that uses the PostgreSQL-specific copy from file command to load csv data into a table on the provided * connection. NOTE: This method does not commit the transaction or close the connection. */ public static void copyFromFile(Connection connection, File file, String targetTableName) throws IOException, SQLException { // Allows sending over network. This is only slightly slower than a local file copy. final String copySql = String.format("copy %s from stdin", targetTableName); // FIXME we should be reading the COPY text from a stream in parallel, not from a temporary text file. InputStream stream = new BufferedInputStream(new FileInputStream(file.getAbsolutePath())); // Our connection pool wraps the Connection objects, so we need to unwrap the Postgres connection interface. CopyManager copyManager = new CopyManager(connection.unwrap(BaseConnection.class)); copyManager.copyIn(copySql, stream, 1024*1024); stream.close(); // It is also possible to load from local file if this code is running on the database server. // statement.execute(String.format("copy %s from '%s'", table.name, tempTextFile.getAbsolutePath())); }
/** * Pass results of a COPY TO STDOUT query from database into a Writer. * @param sql COPY TO STDOUT statement * @param to the stream to write the results to (row by row) * @return number of rows updated for server 8.2 or newer; -1 for older * @throws SQLException on database usage errors * @throws IOException upon writer or database connection failure */ public long copyOut(final String sql, Writer to) throws SQLException, IOException { byte[] buf; CopyOut cp = copyOut(sql); try { while ( (buf = cp.readFromCopy()) != null ) { to.write(encoding.decode(buf)); } return cp.getHandledRowCount(); } finally { // see to it that we do not leave the connection locked if(cp.isActive()) cp.cancelCopy(); } }
@Override public void open(DataContext context) { super.open(context); try { JdbcSqlTransaction jdbcTransaction = (JdbcSqlTransaction) transaction; Connection conn = jdbcExtractor.getNativeConnection(jdbcTransaction.getConnection()); copyManager = new CopyManager((BaseConnection) conn); } catch (Exception ex) { throw getPlatform().getSqlTemplate().translate(ex); } }
/** * Uses given connection for specified COPY FROM STDIN operation. * * @param connection database connection to use for copying (protocol version 3 required) * @param sql COPY FROM STDIN statement * @param bufferSize try to send this many bytes at a time * @throws SQLException if initializing the operation fails */ public PGCopyOutputStream(PGConnection connection, String sql, int bufferSize) throws SQLException { this(connection.getCopyAPI().copyIn(sql), bufferSize); }