Search in sources :

Example 6 with LargeObject

use of org.postgresql.largeobject.LargeObject in project syndesis by syndesisio.

the class SqlFileStore method doReadPostgres.

/**
 * Postgres does not allow to read from the large object after the connection has been closed.
 */
private InputStream doReadPostgres(String path) {
    Handle h = dbi.open();
    try {
        h.getConnection().setAutoCommit(false);
        List<Map<String, Object>> res = h.select("SELECT data FROM filestore WHERE path=?", path);
        Optional<Long> oid = res.stream().map(row -> row.get("data")).map(Long.class::cast).findFirst();
        if (oid.isPresent()) {
            LargeObjectManager lobj = getPostgresConnection(h.getConnection()).getLargeObjectAPI();
            LargeObject obj = lobj.open(oid.get(), LargeObjectManager.READ);
            return new HandleCloserInputStream(h, obj.getInputStream());
        } else {
            h.close();
            return null;
        }
    } catch (SQLException e) {
        IOUtils.closeQuietly(h);
        throw DaoException.launderThrowable(e);
    }
}
Also used : LargeObjectManager(org.postgresql.largeobject.LargeObjectManager) SQLException(java.sql.SQLException) LargeObject(org.postgresql.largeobject.LargeObject) Map(java.util.Map) Handle(org.skife.jdbi.v2.Handle)

Aggregations

LargeObject (org.postgresql.largeobject.LargeObject)6 LargeObjectManager (org.postgresql.largeobject.LargeObjectManager)6 SQLException (java.sql.SQLException)5 PGConnection (org.postgresql.PGConnection)4 IOException (java.io.IOException)2 Resource (com.revolsys.spring.resource.Resource)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 Blob (java.sql.Blob)1 Map (java.util.Map)1 Handle (org.skife.jdbi.v2.Handle)1