Search in sources :

Example 1 with LargeObject

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

the class SqlFileStore method doWritePostgres.

private void doWritePostgres(Handle h, String path, InputStream file) {
    doDelete(h, path);
    try {
        LargeObjectManager lobj = getPostgresConnection(h.getConnection()).getLargeObjectAPI();
        long oid = lobj.createLO();
        LargeObject obj = lobj.open(oid, LargeObjectManager.WRITE);
        try (OutputStream lob = obj.getOutputStream()) {
            IOUtils.copy(file, lob);
        }
        h.insert("INSERT INTO filestore(path, data) values (?,?)", path, oid);
    } catch (IOException | SQLException ex) {
        throw DaoException.launderThrowable(ex);
    }
}
Also used : LargeObjectManager(org.postgresql.largeobject.LargeObjectManager) SQLException(java.sql.SQLException) OutputStream(java.io.OutputStream) IOException(java.io.IOException) LargeObject(org.postgresql.largeobject.LargeObject)

Example 2 with LargeObject

use of org.postgresql.largeobject.LargeObject in project activemq-artemis by apache.

the class PostgresSequentialSequentialFileDriver method getPostGresLargeObjectSize.

private int getPostGresLargeObjectSize(JDBCSequentialFile file) throws SQLException {
    LargeObjectManager lobjManager = ((PGConnection) connection).getLargeObjectAPI();
    int size = 0;
    Long oid = getOID(file);
    if (oid != null) {
        synchronized (connection) {
            try {
                connection.setAutoCommit(false);
                LargeObject largeObject = lobjManager.open(oid, LargeObjectManager.READ);
                size = largeObject.size();
                largeObject.close();
                connection.commit();
            } catch (SQLException e) {
                connection.rollback();
                throw e;
            }
        }
    }
    return size;
}
Also used : PGConnection(org.postgresql.PGConnection) LargeObjectManager(org.postgresql.largeobject.LargeObjectManager) SQLException(java.sql.SQLException) LargeObject(org.postgresql.largeobject.LargeObject)

Example 3 with LargeObject

use of org.postgresql.largeobject.LargeObject in project activemq-artemis by apache.

the class PostgresSequentialSequentialFileDriver method writeToFile.

@Override
public int writeToFile(JDBCSequentialFile file, byte[] data) throws SQLException {
    synchronized (connection) {
        LargeObjectManager lobjManager = ((PGConnection) connection).getLargeObjectAPI();
        LargeObject largeObject = null;
        Long oid = getOID(file);
        try {
            connection.setAutoCommit(false);
            largeObject = lobjManager.open(oid, LargeObjectManager.WRITE);
            largeObject.seek(largeObject.size());
            largeObject.write(data);
            largeObject.close();
            connection.commit();
        } catch (Exception e) {
            connection.rollback();
            throw e;
        }
        return data.length;
    }
}
Also used : PGConnection(org.postgresql.PGConnection) LargeObjectManager(org.postgresql.largeobject.LargeObjectManager) LargeObject(org.postgresql.largeobject.LargeObject) SQLException(java.sql.SQLException)

Example 4 with LargeObject

use of org.postgresql.largeobject.LargeObject in project activemq-artemis by apache.

the class PostgresSequentialSequentialFileDriver method readFromFile.

@Override
public int readFromFile(JDBCSequentialFile file, ByteBuffer bytes) throws SQLException {
    LargeObjectManager lobjManager = ((PGConnection) connection).getLargeObjectAPI();
    LargeObject largeObject = null;
    long oid = getOID(file);
    synchronized (connection) {
        try {
            connection.setAutoCommit(false);
            largeObject = lobjManager.open(oid, LargeObjectManager.READ);
            int readLength = (int) calculateReadLength(largeObject.size(), bytes.remaining(), file.position());
            if (readLength > 0) {
                if (file.position() > 0)
                    largeObject.seek((int) file.position());
                byte[] data = largeObject.read(readLength);
                bytes.put(data);
            }
            largeObject.close();
            connection.commit();
            return readLength;
        } catch (SQLException e) {
            connection.rollback();
            throw e;
        }
    }
}
Also used : PGConnection(org.postgresql.PGConnection) LargeObjectManager(org.postgresql.largeobject.LargeObjectManager) SQLException(java.sql.SQLException) LargeObject(org.postgresql.largeobject.LargeObject)

Example 5 with LargeObject

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

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