001
014
015 package com.liferay.portal.dao.jdbc.postgresql;
016
017 import java.sql.Connection;
018 import java.sql.PreparedStatement;
019 import java.sql.ResultSet;
020 import java.sql.SQLException;
021 import java.sql.Statement;
022
023 import org.postgresql.PGConnection;
024 import org.postgresql.PGStatement;
025 import org.postgresql.largeobject.LargeObject;
026 import org.postgresql.largeobject.LargeObjectManager;
027
028
031 public class PostgreSQLJDBCUtil {
032
033 public static byte[] getLargeObject(ResultSet resultSet, String name)
034 throws SQLException {
035
036 Statement statement = resultSet.getStatement();
037
038 Connection connection = statement.getConnection();
039
040 connection.setAutoCommit(false);
041
042 try {
043 PGConnection pgConnection = connection.unwrap(PGConnection.class);
044
045 LargeObjectManager largeObjectManager =
046 pgConnection.getLargeObjectAPI();
047
048 long id = resultSet.getLong(name);
049
050 LargeObject largeObject = largeObjectManager.open(
051 id, LargeObjectManager.READ);
052
053 byte[] bytes = new byte[largeObject.size()];
054
055 largeObject.read(bytes, 0, largeObject.size());
056
057 largeObject.close();
058
059 return bytes;
060 }
061 finally {
062 connection.setAutoCommit(true);
063 }
064 }
065
066 public static boolean isPGStatement(Statement statement)
067 throws SQLException {
068
069 if (statement.isWrapperFor(PGStatement.class)) {
070 return true;
071 }
072
073 return false;
074 }
075
076 public static void setLargeObject(
077 PreparedStatement preparedStatement, int index, byte[] bytes)
078 throws SQLException {
079
080 Connection connection = preparedStatement.getConnection();
081
082 connection.setAutoCommit(false);
083
084 try {
085 PGConnection pgConnection = connection.unwrap(PGConnection.class);
086
087 LargeObjectManager largeObjectManager =
088 pgConnection.getLargeObjectAPI();
089
090 long id = largeObjectManager.createLO(
091 LargeObjectManager.READ | LargeObjectManager.WRITE);
092
093 LargeObject largeObject = largeObjectManager.open(
094 id, LargeObjectManager.WRITE);
095
096 largeObject.write(bytes);
097
098 largeObject.close();
099
100 preparedStatement.setLong(index, id);
101 }
102 finally {
103 connection.setAutoCommit(true);
104 }
105 }
106
107 }