001/*
002 * Copyright (c) 2019-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.sql.SQLXML;
015
016import java.sql.SQLException;
017
018import org.postgresql.pljava.annotation.Function;
019import org.postgresql.pljava.annotation.SQLAction;
020import org.postgresql.pljava.annotation.SQLType;
021
022import static org.postgresql.pljava.example.LoggerTest.logMessage;
023
024/**
025 * Class illustrating use of {@link SQLXML} to operate on non-XML data types
026 * for which PL/Java provides an XML rendering.
027 *<p>
028 * Everything mentioning the type XML here needs a conditional implementor tag
029 * in case of being loaded into a PostgreSQL instance built without that type.
030 */
031@SQLAction(implementor="postgresql_xml", requires="pgNodeTreeAsXML", install=
032"WITH" +
033"  a(t) AS (SELECT adbin FROM pg_catalog.pg_attrdef LIMIT 1)" +
034" SELECT" +
035"   CASE WHEN pgNodeTreeAsXML(t) IS DOCUMENT" +
036"    THEN javatest.logmessage('INFO', 'pgNodeTreeAsXML ok')" +
037"    ELSE javatest.logmessage('WARNING', 'pgNodeTreeAsXML ng')" +
038"   END" +
039"  FROM a"
040)
041public class XMLRenderedTypes
042{
043    @Function(
044        schema="javatest", implementor="postgresql_xml",
045        provides="pgNodeTreeAsXML"
046    )
047    public static SQLXML pgNodeTreeAsXML(@SQLType("pg_node_tree") SQLXML pgt)
048    throws SQLException
049    {
050        return pgt;
051    }
052}