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.DriverManager;
015import java.sql.ResultSet;
016import java.sql.SQLException;
017import java.sql.Statement;
018
019import org.postgresql.pljava.ResultSetHandle;
020
021/**
022 * Uses the {@code ResultSetHandle} interface to implement two functions,
023 * {@link #listNonSupers listNonSupers} and {@link #listSupers listSupers},
024 * returning the corresponding subsets of rows from {@code pg_user}.
025 */
026public class Users implements ResultSetHandle {
027    public static ResultSetHandle listNonSupers() {
028        return new Users("usesuper = false");
029    }
030
031    public static ResultSetHandle listSupers() {
032        return new Users("usesuper = true");
033    }
034
035    private final String m_filter;
036
037    private Statement m_statement;
038
039    public Users(String filter) {
040        m_filter = filter;
041    }
042
043    @Override
044    public void close() throws SQLException {
045        m_statement.close();
046    }
047
048    @Override
049    public ResultSet getResultSet() throws SQLException {
050        m_statement = DriverManager.getConnection("jdbc:default:connection")
051                .createStatement();
052        return m_statement.executeQuery("SELECT * FROM pg_user WHERE "
053                + m_filter);
054    }
055}