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