public CopyManager getCopyAPI() throws SQLException { checkClosed(); if (copyManager == null) { copyManager = new CopyManager(this); } return copyManager; }
public CopyManager getCopyAPI() throws SQLException { checkClosed(); if (copyManager == null) copyManager = new CopyManager(this); return copyManager; } }
CopyManager copyManager = new CopyManager((BaseConnection) con);
public CopyManager getCopyAPI() throws SQLException { if (copyManager == null) copyManager = new CopyManager(this); return copyManager; } }
public CopyManager getCopyAPI() throws SQLException { checkClosed(); if (copyManager == null) copyManager = new CopyManager(this); return copyManager; } }
/** * 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())); }
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 );
LOG.info(copySql); CopyManager copyManager = new CopyManager(connection.unwrap(BaseConnection.class)); tableLoadResult.rowCount = (int) copyManager.copyOut(copySql, protectedOut); if (tableLoadResult.rowCount == 0) {
/** * Loads a table from a COPY file. * * @param dbCtx * The database connection. * @param copyFile * The file to be loaded. * @param tableName * The table to load the data into. */ public void loadCopyFile(DatabaseContext dbCtx, File copyFile, String tableName) { try (InputStream bufferedInStream = new BufferedInputStream(new FileInputStream(copyFile), 65536)) { CopyManager copyManager = new CopyManager((BaseConnection) dbCtx.getConnection()); copyManager.copyIn("COPY " + tableName + " FROM STDIN", bufferedInStream); } catch (IOException e) { throw new OsmosisRuntimeException("Unable to process COPY file " + copyFile + ".", e); } catch (SQLException e) { throw new OsmosisRuntimeException("Unable to process COPY file " + copyFile + ".", e); } }
Connection conn = DataSourceUtils.getConnection(dataSource); try { copyManager = new CopyManager(conn.unwrap(BaseConnection.class));
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); } }
// your code CopyManager copyManager = new CopyManager((BaseConnection) new DataSource().connect()); FileReader from = ... // different name! int bufferSize = 65536; // here starts the copy of the driver's implementation of the copyIn() method. char[] cbuf = new char[bufferSize]; int len; // if you store the instance of the CopyIn interface in an instance variable you // should be able to call cancelCopy() on it CopyIn cp = copyManager.copyIn(sql); try { while ( (len = from.read(cbuf)) > 0) { byte[] buf = encoding.encode(new String(cbuf, 0, len)); cp.writeToCopy(buf, 0, buf.length); } return cp.endCopy(); } finally { // see to it that we do not leave the connection locked if(cp.isActive()) cp.cancelCopy(); }
private long handleCopyIn(Connection conn, ParametersContext context, String std) throws SQLException, IOException{ SqlParameterCollection sqlParameters = createSqlParameterCollection(context); BaseConnection connection=conn.unwrap(BaseConnection.class); AbstractJdbc<?> logConnection=conn.unwrap(AbstractJdbc.class); Object input=sqlParameters.getInputStream(); CopyManager copyManager = new CopyManager(connection); String sql=sqlParameters.getSql(); long start = System.currentTimeMillis(); long result=-1; try{ if ("STDIN".equals(std)){ if (input instanceof Reader){ result= copyManager.copyIn(sql, (Reader)input); }else if (input instanceof InputStream){ result= copyManager.copyIn(sql, (InputStream)input); } else{ result= copyManager.copyIn(sql, System.in); } } else{ CopyIn copyIn=copyManager.copyIn(sql); result=copyIn.getHandledRowCount(); } JdbcLogUtils.info(logConnection, "rowCount="+result); return result; } finally{ long end = System.currentTimeMillis(); JdbcLogUtils.logSql(logConnection, sql, start, end); } }
AbstractJdbc<?> logConnection=conn.unwrap(AbstractJdbc.class); Object output=sqlParameters.getOutputStream(); CopyManager copyManager = new CopyManager(connection); String sql=sqlParameters.getSql(); long result=-1;
@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); } }