1 package net.sf.saxon.sql; 2 import net.sf.saxon.expr.Expression; 3 import net.sf.saxon.expr.SimpleExpression; 4 import net.sf.saxon.expr.StaticProperty; 5 import net.sf.saxon.expr.XPathContext; 6 import net.sf.saxon.instruct.Executable; 7 import net.sf.saxon.om.Item; 8 import net.sf.saxon.style.ExtensionInstruction; 9 import net.sf.saxon.trans.XPathException; 10 import net.sf.saxon.value.ObjectValue; 11 12 import java.sql.Connection ; 13 import java.sql.SQLException ; 14 15 18 19 public class SQLClose extends ExtensionInstruction { 20 21 Expression connection = null; 22 23 public void prepareAttributes() throws XPathException { 24 String connectAtt = getAttributeList().getValue("", "connection"); 25 if (connectAtt==null) { 26 reportAbsence("connection"); 27 } else { 28 connection = makeExpression(connectAtt); 29 } 30 } 31 32 public void validate() throws XPathException { 33 super.validate(); 34 connection = typeCheck("connection", connection); 35 } 36 37 public Expression compile(Executable exec) throws XPathException { 38 return new CloseInstruction(connection); 39 } 40 41 private static class CloseInstruction extends SimpleExpression { 42 43 public static final int CONNECTION = 0; 44 45 public CloseInstruction(Expression connect) { 46 Expression[] sub = {connect}; 47 setArguments(sub); 48 } 49 53 54 public int getImplementationMethod() { 55 return Expression.EVALUATE_METHOD; 56 } 57 58 public String getExpressionType() { 59 return "sql:close"; 60 } 61 62 public int computeCardinality() { 63 return StaticProperty.ALLOWS_ZERO_OR_ONE; 64 } 65 66 public Item evaluateItem(XPathContext context) throws XPathException { 67 Item conn = arguments[CONNECTION].evaluateItem(context); 68 if (!(conn instanceof ObjectValue && ((ObjectValue)conn).getObject() instanceof Connection ) ) { 69 dynamicError("Value of connection expression is not a JDBC Connection", context); 70 } 71 Connection connection = (Connection )((ObjectValue)conn).getObject(); 72 try { 73 connection.close(); 74 } catch (SQLException ex) { 75 dynamicError("(SQL) Failed to close connection: " + ex.getMessage(), context); 76 } 77 return null; 78 } 79 } 80 } 81 82 | Popular Tags |