001: package net.sf.saxon.sql;
002:
003: import net.sf.saxon.expr.Expression;
004: import net.sf.saxon.expr.SimpleExpression;
005: import net.sf.saxon.expr.StaticProperty;
006: import net.sf.saxon.expr.XPathContext;
007: import net.sf.saxon.instruct.Executable;
008: import net.sf.saxon.om.Item;
009: import net.sf.saxon.style.ExtensionInstruction;
010: import net.sf.saxon.trans.XPathException;
011: import net.sf.saxon.value.ObjectValue;
012:
013: import java.sql.Connection;
014: import java.sql.SQLException;
015:
016: /**
017: * An sql:close element in the stylesheet.
018: */
019:
020: public class SQLClose extends ExtensionInstruction {
021:
022: Expression connection = null;
023:
024: public void prepareAttributes() throws XPathException {
025: String connectAtt = getAttributeList().getValue("",
026: "connection");
027: if (connectAtt == null) {
028: reportAbsence("connection");
029: } else {
030: connection = makeExpression(connectAtt);
031: }
032: }
033:
034: public void validate() throws XPathException {
035: super .validate();
036: connection = typeCheck("connection", connection);
037: }
038:
039: public Expression compile(Executable exec) throws XPathException {
040: return new CloseInstruction(connection);
041: }
042:
043: private static class CloseInstruction extends SimpleExpression {
044:
045: public static final int CONNECTION = 0;
046:
047: public CloseInstruction(Expression connect) {
048: Expression[] sub = { connect };
049: setArguments(sub);
050: }
051:
052: /**
053: * A subclass must provide one of the methods evaluateItem(), iterate(), or process().
054: * This method indicates which of the three is provided.
055: */
056:
057: public int getImplementationMethod() {
058: return Expression.EVALUATE_METHOD;
059: }
060:
061: public String getExpressionType() {
062: return "sql:close";
063: }
064:
065: public int computeCardinality() {
066: return StaticProperty.ALLOWS_ZERO_OR_ONE;
067: }
068:
069: public Item evaluateItem(XPathContext context)
070: throws XPathException {
071: Item conn = arguments[CONNECTION].evaluateItem(context);
072: if (!(conn instanceof ObjectValue && ((ObjectValue) conn)
073: .getObject() instanceof Connection)) {
074: dynamicError(
075: "Value of connection expression is not a JDBC Connection",
076: context);
077: }
078: Connection connection = (Connection) ((ObjectValue) conn)
079: .getObject();
080: try {
081: connection.close();
082: } catch (SQLException ex) {
083: dynamicError("(SQL) Failed to close connection: "
084: + ex.getMessage(), context);
085: }
086: return null;
087: }
088: }
089: }
090:
091: //
092: // The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
093: // you may not use this file except in compliance with the License. You may obtain a copy of the
094: // License at http://www.mozilla.org/MPL/
095: //
096: // Software distributed under the License is distributed on an "AS IS" basis,
097: // WITHOUT WARRANTY OF ANY KIND, either express or implied.
098: // See the License for the specific language governing rights and limitations under the License.
099: //
100: // The Original Code is: all this file.
101: //
102: // The Initial Developer of the Original Code is Michael H. Kay.
103: //
104: // Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
105: //
106: // Additional Contributor(s): Rick Bonnett [rbonnett@acadia.net]
107: //
|