001/*
002 * Copyright (c) 2004-2020 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 *   Chapman Flack
012 */
013package org.postgresql.pljava.example.annotation;
014
015import java.lang.reflect.Array;
016import java.sql.SQLException;
017import java.util.logging.Logger;
018
019import org.postgresql.pljava.annotation.Function;
020import static org.postgresql.pljava.annotation.Function.Effects.IMMUTABLE;
021import static
022    org.postgresql.pljava.annotation.Function.OnNullInput.RETURNS_NULL;
023import org.postgresql.pljava.annotation.SQLAction;
024import org.postgresql.pljava.annotation.SQLType;
025
026/**
027 * Provides example methods to illustrate the polymorphic types {@code any},
028 * {@code anyarray}, and {@code anyelement}.
029 */
030public class AnyTest {
031    private static Logger s_logger = Logger.getAnonymousLogger();
032
033    /**
034     * Log (at INFO level) the Java class received for the passed argument.
035     */
036    @Function(schema="javatest", effects=IMMUTABLE, onNullInput=RETURNS_NULL)
037    public static void logAny(@SQLType("pg_catalog.any") Object param)
038    throws SQLException
039    {
040        s_logger.info("logAny received an object of class " + param.getClass());
041    }
042
043    /**
044     * Log (at INFO level) the Java class received for the passed argument, and
045     * return the same value.
046     */
047    @Function(schema="javatest", effects=IMMUTABLE, onNullInput=RETURNS_NULL,
048        type="pg_catalog.anyelement")
049    public static Object logAnyElement(
050        @SQLType("pg_catalog.anyelement") Object param)
051    throws SQLException
052    {
053        s_logger.info("logAnyElement received an object of class "
054                + param.getClass());
055        return param;
056    }
057
058    /**
059     * Return the Java object received for the passed argument, wrapped in a
060     * one-element array with the object's class as its element type.
061     */
062    @Function(schema="javatest", effects=IMMUTABLE, onNullInput=RETURNS_NULL,
063        type="pg_catalog.anyarray")
064    public static Object[] makeArray(
065        @SQLType("pg_catalog.anyelement") Object param)
066    {
067        Object[] result = (Object[]) Array.newInstance(param.getClass(), 1);
068        result[0] = param;
069        return result;
070    }
071}