001/*
002 * Copyright (c) 2004-2016 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 *   Purdue University
012 */
013package org.postgresql.pljava.example.annotation;
014
015import java.sql.Connection;
016import java.sql.DriverManager;
017import java.sql.ResultSet;
018import java.sql.SQLException;
019import java.sql.Statement;
020import java.util.ArrayList;
021import java.util.Iterator;
022
023import org.postgresql.pljava.annotation.Function;
024
025/**
026 * This implementation uses another function that returns a set of a complex
027 * type, concatenates the name and value of that type and returns this as
028 * a set of a scalar type. Somewhat cumbersome way to display properties
029 * but it's a good test.
030 *
031 * @author Thomas Hallgren
032 */
033public class UsingPropertiesAsScalarSet
034{
035    @Function
036    public static Iterator<String> getProperties()
037    throws SQLException
038    {
039        StringBuilder bld = new StringBuilder();
040        ArrayList<String> list = new ArrayList<>();
041        Connection conn = DriverManager.getConnection("jdbc:default:connection");
042        Statement  stmt = conn.createStatement();
043        try
044        {
045            ResultSet rs = stmt.executeQuery(
046                "SELECT name, value FROM propertyExampleAnno()");
047            try
048            {
049                while(rs.next())
050                {
051                    bld.setLength(0);
052                    bld.append(rs.getString(1));
053                    bld.append(" = ");
054                    bld.append(rs.getString(2));
055                    list.add(bld.toString());
056                }
057                return list.iterator();
058            }
059            finally
060            {
061                try { rs.close(); } catch(SQLException e) {}
062            }
063        }
064        finally
065        {
066            try { stmt.close(); } catch(SQLException e) {}
067        }
068    }
069}