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