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;
017import java.util.Random;
018import java.util.logging.Logger;
019
020import org.postgresql.pljava.ResultSetProvider;
021
022/**
023 * Example implementing {@code ResultSetProvider} to provide a function that
024 * generates and returns a lot of rows (caller passes the desired row count)
025 * each containing the row number, a random integer, and a timestamp.
026 */
027public class HugeResultSet implements ResultSetProvider {
028    public static ResultSetProvider executeSelect(int rowCount)
029            throws SQLException {
030        return new HugeResultSet(rowCount);
031    }
032
033    private final int m_rowCount;
034
035    private final Random m_random;
036
037    public HugeResultSet(int rowCount) throws SQLException {
038        m_rowCount = rowCount;
039        m_random = new Random(System.currentTimeMillis());
040    }
041
042    @Override
043    public boolean assignRowValues(ResultSet receiver, int currentRow)
044            throws SQLException {
045        // Stop when we reach rowCount rows.
046        //
047        if (currentRow >= m_rowCount) {
048            Logger.getAnonymousLogger().info("HugeResultSet ends");
049            return false;
050        }
051
052        receiver.updateInt(1, currentRow);
053        receiver.updateInt(2, m_random.nextInt());
054        receiver.updateTimestamp(3, new Timestamp(System.currentTimeMillis()));
055        return true;
056    }
057
058    @Override
059    public void close() {
060    }
061}