|
| 1 | +/* |
| 2 | + * Copyright (C) 2014, Evolved Binary Ltd |
| 3 | + * |
| 4 | + * This file was originally ported from FusionDB to eXist-db by |
| 5 | + * Evolved Binary, for the benefit of the eXist-db Open Source community. |
| 6 | + * Only the ported code as it appears in this file, at the time that |
| 7 | + * it was contributed to eXist-db, was re-licensed under The GNU |
| 8 | + * Lesser General Public License v2.1 only for use in eXist-db. |
| 9 | + * |
| 10 | + * This license grant applies only to a snapshot of the code as it |
| 11 | + * appeared when ported, it does not offer or infer any rights to either |
| 12 | + * updates of this source code or access to the original source code. |
| 13 | + * |
| 14 | + * The GNU Lesser General Public License v2.1 only license follows. |
| 15 | + * |
| 16 | + * --------------------------------------------------------------------- |
| 17 | + * |
| 18 | + * Copyright (C) 2014, Evolved Binary Ltd |
| 19 | + * |
| 20 | + * This library is free software; you can redistribute it and/or |
| 21 | + * modify it under the terms of the GNU Lesser General Public |
| 22 | + * License as published by the Free Software Foundation; version 2.1. |
| 23 | + * |
| 24 | + * This library is distributed in the hope that it will be useful, |
| 25 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 26 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 27 | + * Lesser General Public License for more details. |
| 28 | + * |
| 29 | + * You should have received a copy of the GNU Lesser General Public |
| 30 | + * License along with this library; if not, write to the Free Software |
| 31 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 32 | + */ |
| 33 | +package org.exist.xquery.modules.sql; |
| 34 | + |
| 35 | +import org.apache.logging.log4j.LogManager; |
| 36 | +import org.apache.logging.log4j.Logger; |
| 37 | + |
| 38 | +import org.exist.xquery.BasicFunction; |
| 39 | +import org.exist.xquery.FunctionSignature; |
| 40 | +import org.exist.xquery.XPathException; |
| 41 | +import org.exist.xquery.XQueryContext; |
| 42 | +import org.exist.xquery.value.*; |
| 43 | + |
| 44 | +import java.sql.Connection; |
| 45 | +import java.sql.SQLException; |
| 46 | + |
| 47 | +import static org.exist.xquery.FunctionDSL.*; |
| 48 | +import static org.exist.xquery.modules.sql.SQLModule.functionSignature; |
| 49 | + |
| 50 | + |
| 51 | +/** |
| 52 | + * SQL Module Extension function for XQuery to explicitly close a connection. |
| 53 | + * |
| 54 | + * @author <a href="mailto:[email protected]">Adam Retter</a> |
| 55 | + */ |
| 56 | +public class CloseConnectionFunction extends BasicFunction { |
| 57 | + |
| 58 | + private static final Logger LOGGER = LogManager.getLogger(GetConnectionFunction.class); |
| 59 | + |
| 60 | + private static final String FN_CLOSE_CONNECTION = "close-connection"; |
| 61 | + public static final FunctionSignature FS_CLOSE_CONNECTION = functionSignature( |
| 62 | + FN_CLOSE_CONNECTION, |
| 63 | + "Closes a connection to a SQL Database, or if the connection was taken from a connection pool then it is returned to the pool", |
| 64 | + returns(Type.BOOLEAN, "true if the connection was closed, false if there was no such connection"), |
| 65 | + param("connection-handle", Type.LONG, "an xs:long representing the connection handle") |
| 66 | + ); |
| 67 | + |
| 68 | + public CloseConnectionFunction(final XQueryContext context, final FunctionSignature signature) { |
| 69 | + super(context, signature); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Evaluate the call to the xquery close-connection(). |
| 74 | + * |
| 75 | + * @param args arguments from the close-connection() function call |
| 76 | + * @param contextSequence the Context Sequence to operate on (not used here internally!) |
| 77 | + * |
| 78 | + * @return An empty sequence |
| 79 | + * |
| 80 | + * @throws XPathException if an error occurs. |
| 81 | + */ |
| 82 | + @Override |
| 83 | + public Sequence eval(final Sequence[] args, final Sequence contextSequence) throws XPathException { |
| 84 | + final IntegerValue connectionHandle = (IntegerValue) args[0].itemAt(0); |
| 85 | + final long connectionUid = connectionHandle.toJavaObject(long.class); |
| 86 | + |
| 87 | + final Connection connection = SQLModule.removeConnection(context, connectionUid); |
| 88 | + if (connection == null) { |
| 89 | + return BooleanValue.FALSE; |
| 90 | + } |
| 91 | + |
| 92 | + try { |
| 93 | + if (connection.isClosed()) { |
| 94 | + LOGGER.warn("sql:close-connection() Cannot close connection with handle: {}, as it is already closed!", connectionUid); |
| 95 | + return BooleanValue.FALSE; |
| 96 | + } |
| 97 | + |
| 98 | + connection.close(); |
| 99 | + return BooleanValue.TRUE; |
| 100 | + |
| 101 | + } catch (final SQLException e) { |
| 102 | + throw new XPathException(this, "Unable to close connection with handle: " + connectionUid + ". " + e.getMessage()); |
| 103 | + } |
| 104 | + } |
| 105 | +} |
0 commit comments