001/* 002 * Copyright (c) 2004-2013 Tada AB and other contributors, as listed below. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the The BSD 3-Clause License 006 * which accompanies this distribution, and is available at 007 * http://opensource.org/licenses/BSD-3-Clause 008 * 009 * Contributors: 010 * Tada AB 011 */ 012package org.postgresql.pljava.example; 013 014import java.io.ByteArrayInputStream; 015import java.io.ByteArrayOutputStream; 016import java.io.DataOutputStream; 017import java.io.IOException; 018import java.sql.ResultSet; 019import java.sql.SQLException; 020 021import org.postgresql.pljava.ResultSetProvider; 022 023/** 024 * Example using {@code ResultSetProvider} to return 100 rows of two 025 * {@code bytea} columns each, which should be equal in each row, one being 026 * set by {@link java.sql.ResultSet#updateBinaryStream updateBinaryStream} 027 * and the other by {@link java.sql.ResultSet#updateBytes updateBytes}. 028 */ 029public class BinaryColumnTest implements ResultSetProvider { 030 public static ResultSetProvider getBinaryPairs() { 031 return new BinaryColumnTest(); 032 } 033 034 @Override 035 public boolean assignRowValues(ResultSet rs, int rowCount) 036 throws SQLException { 037 try { 038 if (rowCount >= 100) 039 return false; 040 041 int offset = rowCount * 100; 042 ByteArrayOutputStream bld = new ByteArrayOutputStream(); 043 DataOutputStream da = new DataOutputStream(bld); 044 for (int idx = 0; idx < 100; ++idx) 045 da.writeInt(offset + idx); 046 byte[] bytes = bld.toByteArray(); 047 ByteArrayInputStream input = new ByteArrayInputStream(bytes); 048 rs.updateBinaryStream(1, input, bytes.length); 049 rs.updateBytes(2, bytes); 050 return true; 051 } catch (IOException e) { 052 throw new SQLException(e.getMessage()); 053 } 054 } 055 056 @Override 057 public void close() throws SQLException { 058 } 059}