001/*
002 * Copyright (c) 2015-2023 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 *   Chapman Flack
011 */
012package org.postgresql.pljava.example.annotation;
013
014import java.util.Iterator;
015import java.util.Arrays;
016
017import org.postgresql.pljava.annotation.SQLAction;
018import org.postgresql.pljava.annotation.SQLType;
019import org.postgresql.pljava.annotation.Function;
020
021/**
022 * Confirms the mapping of PG enum and Java String, and arrays of each, as
023 * parameter and return types.
024 */
025@SQLAction(provides="mood type",
026    install="CREATE TYPE mood AS ENUM ('sad', 'ok', 'happy')",
027    remove="DROP TYPE mood"
028)
029@SQLAction(
030    requires={"textToMood", "moodToText", "textsToMoods", "moodsToTexts"},
031    install={
032        "SELECT textToMood('happy')",
033        "SELECT moodToText('happy'::mood)",
034        "SELECT textsToMoods(array['happy','happy','sad','ok'])",
035        "SELECT moodsToTexts(array['happy','happy','sad','ok']::mood[])"
036    }
037)
038public class Enumeration
039{
040    @Function(requires="mood type", provides="textToMood", type="mood")
041    public static String textToMood(String s)
042    {
043        return s;
044    }
045    @Function(requires="mood type", provides="moodToText")
046    public static String moodToText(@SQLType("mood")String s)
047    {
048        return s;
049    }
050    @Function(requires="mood type", provides="textsToMoods", type="mood")
051    public static Iterator<String> textsToMoods(String[] ss)
052    {
053        return Arrays.asList(ss).iterator();
054    }
055    @Function(requires="mood type", provides="moodsToTexts")
056    public static Iterator<String> moodsToTexts(@SQLType("mood[]")String[] ss)
057    {
058        return Arrays.asList(ss).iterator();
059    }
060}