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 import net.sf.saxon.value.StringValue; 12 13 import java.sql.Connection ; 14 import java.sql.DriverManager ; 15 16 19 20 public class SQLConnect extends ExtensionInstruction { 21 22 Expression database; 23 Expression driver; 24 Expression user; 25 Expression password; 26 27 public boolean mayContainSequenceConstructor() { 28 return false; 29 } 30 31 public void prepareAttributes() throws XPathException { 32 33 35 String dbAtt = attributeList.getValue("", "database"); 36 if (dbAtt==null) { 37 reportAbsence("database"); 38 dbAtt = ""; } 40 database = makeAttributeValueTemplate(dbAtt); 41 42 44 String dbDriver = attributeList.getValue("", "driver"); 45 if (dbDriver==null) { 46 if (dbAtt.length()>9 && dbAtt.substring(0,9).equals("jdbc:odbc")) { 47 dbDriver = "sun.jdbc.odbc.JdbcOdbcDriver"; 48 } else { 49 reportAbsence("driver"); 50 } 51 } 52 driver = makeAttributeValueTemplate(dbDriver); 53 54 55 57 String userAtt = attributeList.getValue("", "user"); 58 if (userAtt==null) { 59 user = StringValue.EMPTY_STRING; 60 } else { 61 user = makeAttributeValueTemplate(userAtt); 62 } 63 64 66 String pwdAtt = attributeList.getValue("", "password"); 67 if (pwdAtt==null) { 68 password = StringValue.EMPTY_STRING; 69 } else { 70 password = makeAttributeValueTemplate(pwdAtt); 71 } 72 } 73 74 public void validate() throws XPathException { 75 super.validate(); 76 database = typeCheck("database", database); 77 driver = typeCheck("driver", driver); 78 user = typeCheck("user", user); 79 password = typeCheck("password", password); 80 } 81 82 public Expression compile(Executable exec) throws XPathException { 83 return new ConnectInstruction(database, driver, user, password); 84 } 85 86 private static class ConnectInstruction extends SimpleExpression { 87 88 public static final int DATABASE = 0; 89 public static final int DRIVER = 1; 90 public static final int USER = 2; 91 public static final int PASSWORD = 3; 92 93 public ConnectInstruction(Expression database, 94 Expression driver, Expression user, Expression password) { 95 96 Expression[] subs = {database, driver, user, password}; 97 setArguments(subs); 98 }; 99 100 104 105 public int getImplementationMethod() { 106 return Expression.EVALUATE_METHOD; 107 } 108 109 public int computeCardinality() { 110 return StaticProperty.EXACTLY_ONE; 111 } 112 113 public String getExpressionType() { 114 return "sql:connect"; 115 } 116 117 public Item evaluateItem(XPathContext context) throws XPathException { 118 119 121 Connection connection = null; 123 String dbString = arguments[DATABASE].evaluateAsString(context); 124 String dbDriverString = arguments[DRIVER].evaluateAsString(context); 125 String userString = arguments[USER].evaluateAsString(context); 126 String pwdString = arguments[PASSWORD].evaluateAsString(context); 127 128 try { 129 Class.forName(dbDriverString); 131 connection = DriverManager.getConnection(dbString, userString, pwdString); 132 } catch (Exception ex) { 133 dynamicError("JDBC Connection Failure: " + ex.getMessage(), context); 134 } 135 136 return new ObjectValue(connection); 137 138 } 139 } 140 } 141 142 | Popular Tags |