001/* 002 * Copyright 2018 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.ametys.plugins.core.impl.datasource; 017 018import java.io.ByteArrayInputStream; 019import java.io.InputStream; 020import java.io.UnsupportedEncodingException; 021import java.sql.PreparedStatement; 022import java.sql.ResultSet; 023import java.sql.SQLException; 024import java.sql.Types; 025 026/** 027 * Specifically handle Derby connections, allowing to actually shut the database down; 028 */ 029public class PostgreSQLDatabaseType extends StaticSQLDatabaseType 030{ 031 @Override 032 public InputStream getBlob(ResultSet resultSet, String columnName) throws SQLException 033 { 034 return resultSet.getBinaryStream(columnName); 035 } 036 @Override 037 public InputStream getBlob(ResultSet resultSet, int pos) throws SQLException 038 { 039 return resultSet.getBinaryStream(pos); 040 } 041 042 @Override 043 public void setBlob(PreparedStatement statement, int pos, String blob) throws SQLException, UnsupportedEncodingException 044 { 045 if (blob == null) 046 { 047 statement.setNull(pos, Types.BINARY); 048 } 049 else 050 { 051 byte[] bytes = blob.getBytes("UTF-8"); 052 setBlob(statement, pos, bytes); 053 } 054 } 055 056 @Override 057 public void setBlob(PreparedStatement statement, int pos, byte[] bytes) throws SQLException 058 { 059 if (bytes == null) 060 { 061 statement.setNull(pos, Types.BINARY); 062 } 063 else 064 { 065 ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes); 066 setBlob(statement, pos, byteArrayInputStream, bytes.length); 067 } 068 } 069 070 @Override 071 public void setBlob(PreparedStatement statement, int pos, InputStream is, long length) throws SQLException 072 { 073 if (is == null) 074 { 075 statement.setNull(pos, Types.BINARY); 076 } 077 else 078 { 079 statement.setBinaryStream(pos, is, length); 080 } 081 } 082}