Search in sources :

Example 6 with LargeObjectManager

use of org.postgresql.largeobject.LargeObjectManager in project com.revolsys.open by revolsys.

the class PostgreSQLJdbcBlobFieldDefinition method setPreparedStatementValue.

@Override
public int setPreparedStatementValue(final PreparedStatement statement, final int parameterIndex, final Object value) throws SQLException {
    if (value == null) {
        final int sqlType = getSqlType();
        statement.setNull(parameterIndex, sqlType);
    } else {
        Blob blob;
        if (value instanceof Blob) {
            blob = (Blob) value;
            statement.setBlob(parameterIndex, blob);
        } else {
            InputStream in;
            if (value instanceof InputStream) {
                in = (InputStream) value;
            } else if (value instanceof byte[]) {
                final byte[] bytes = (byte[]) value;
                in = new ByteArrayInputStream(bytes);
            } else if (value instanceof CharSequence) {
                final String string = ((CharSequence) value).toString();
                final byte[] bytes = string.getBytes(StandardCharsets.UTF_8);
                in = new ByteArrayInputStream(bytes);
            } else {
                try {
                    final Resource resource = Resource.getResource(value);
                    in = resource.newBufferedInputStream();
                } catch (final IllegalArgumentException e) {
                    throw new IllegalArgumentException(value.getClass() + " not valid for a blob column");
                }
            }
            try {
                final PGConnection pgConnection = (PGConnection) ((DelegatingConnection<?>) statement.getConnection()).getInnermostDelegate();
                final LargeObjectManager lobManager = pgConnection.getLargeObjectAPI();
                final long lobId = lobManager.createLO(LargeObjectManager.READ | LargeObjectManager.WRITE);
                final LargeObject lob = lobManager.open(lobId, LargeObjectManager.WRITE);
                try {
                    final byte[] buffer = new byte[2048];
                    int readCount = 0;
                    while ((readCount = in.read(buffer, 0, 2048)) > 0) {
                        lob.write(buffer, 0, readCount);
                    }
                } finally {
                    lob.close();
                }
                statement.setLong(parameterIndex, lobId);
            } catch (final IOException e) {
                Exceptions.throwUncheckedException(e);
            }
        }
    }
    return parameterIndex + 1;
}
Also used : Blob(java.sql.Blob) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Resource(com.revolsys.spring.resource.Resource) IOException(java.io.IOException) PGConnection(org.postgresql.PGConnection) ByteArrayInputStream(java.io.ByteArrayInputStream) LargeObjectManager(org.postgresql.largeobject.LargeObjectManager) LargeObject(org.postgresql.largeobject.LargeObject)

Example 7 with LargeObjectManager

use of org.postgresql.largeobject.LargeObjectManager 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

LargeObjectManager (org.postgresql.largeobject.LargeObjectManager)7 SQLException (java.sql.SQLException)6 LargeObject (org.postgresql.largeobject.LargeObject)6 PGConnection (org.postgresql.PGConnection)5 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 ResultSet (java.sql.ResultSet)1 Map (java.util.Map)1 Handle (org.skife.jdbi.v2.Handle)1