Search in sources :

Example 1 with LargeObjectManager

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

the class PostgresSequentialSequentialFileDriver method createFile.

@Override
public void createFile(JDBCSequentialFile file) throws SQLException {
    synchronized (connection) {
        try {
            connection.setAutoCommit(false);
            LargeObjectManager lobjManager = ((PGConnection) connection).getLargeObjectAPI();
            long oid = lobjManager.createLO();
            createFile.setString(1, file.getFileName());
            createFile.setString(2, file.getExtension());
            createFile.setLong(3, oid);
            createFile.executeUpdate();
            try (ResultSet keys = createFile.getGeneratedKeys()) {
                keys.next();
                file.setId(keys.getLong(1));
            }
            connection.commit();
        } catch (SQLException e) {
            connection.rollback();
            throw e;
        }
    }
}
Also used : PGConnection(org.postgresql.PGConnection) LargeObjectManager(org.postgresql.largeobject.LargeObjectManager) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet)

Example 2 with LargeObjectManager

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

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

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

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

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