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.sql.ResultSet;
015import java.sql.SQLException;
016import java.sql.Timestamp;
017
018import org.postgresql.pljava.ResultSetProvider;
019
020/**
021 * Illustrates various methods of returning composite values.
022 * @author Thomas Hallgren
023 */
024public class TupleReturn implements ResultSetProvider {
025    public static String makeString(ResultSet _testSetReturn)
026            throws SQLException {
027        int base = _testSetReturn.getInt(1);
028        int incbase = _testSetReturn.getInt(2);
029        Timestamp ctime = _testSetReturn.getTimestamp(3);
030        return "Base = \"" + base + "\", incbase = \"" + incbase
031                + "\", ctime = \"" + ctime + "\"";
032    }
033
034    public static ResultSetProvider setReturn(int base, int increment)
035            throws SQLException {
036        return new TupleReturn(base, increment);
037    }
038
039    public static boolean tupleReturn(int base, int increment,
040            ResultSet receiver) throws SQLException {
041        receiver.updateInt(1, base);
042        receiver.updateInt(2, base + increment);
043        receiver.updateTimestamp(3, new Timestamp(System.currentTimeMillis()));
044        return true;
045    }
046
047    public static boolean tupleReturn(Integer base, Integer increment,
048            ResultSet receiver) throws SQLException {
049        if (base == null) {
050            receiver.updateNull(1);
051            receiver.updateNull(2);
052        } else {
053            receiver.updateInt(1, base.intValue());
054            if (increment == null)
055                receiver.updateNull(2);
056            else
057                receiver.updateInt(2, base.intValue() + increment.intValue());
058        }
059        receiver.updateTimestamp(3, new Timestamp(System.currentTimeMillis()));
060        return true;
061    }
062
063    private final int m_base;
064
065    private final int m_increment;
066
067    public TupleReturn(int base, int increment) {
068        m_base = base;
069        m_increment = increment;
070    }
071
072    @Override
073    public boolean assignRowValues(ResultSet receiver, int currentRow)
074            throws SQLException {
075        // Stop when we reach 12 rows.
076        //
077        if (currentRow >= 12)
078            return false;
079
080        receiver.updateInt(1, m_base);
081        receiver.updateInt(2, m_base + m_increment * currentRow);
082        receiver.updateTimestamp(3, new Timestamp(System.currentTimeMillis()));
083        return true;
084    }
085
086    @Override
087    public void close() {
088    }
089}