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.XPathContext; 5 import net.sf.saxon.instruct.Executable; 6 import net.sf.saxon.om.Axis; 7 import net.sf.saxon.om.AxisIterator; 8 import net.sf.saxon.om.Item; 9 import net.sf.saxon.om.NodeInfo; 10 import net.sf.saxon.style.ExtensionInstruction; 11 import net.sf.saxon.trans.XPathException; 12 import net.sf.saxon.value.AtomicValue; 13 import net.sf.saxon.value.ObjectValue; 14 15 import java.sql.Connection ; 16 import java.sql.PreparedStatement ; 17 import java.sql.SQLException ; 18 import java.util.ArrayList ; 19 import java.util.List ; 20 23 24 public class SQLInsert extends ExtensionInstruction { 25 26 Expression connection; 27 String table; 28 29 public void prepareAttributes() throws XPathException { 30 31 table = getAttributeList().getValue("", "table"); 32 if (table==null) { 33 reportAbsence("table"); 34 } 35 String connectAtt = getAttributeList().getValue("", "connection"); 36 if (connectAtt==null) { 37 reportAbsence("connection"); 38 } else { 39 connection = makeExpression(connectAtt); 40 } 41 } 42 43 public void validate() throws XPathException { 44 super.validate(); 45 connection = typeCheck("connection", connection); 46 } 47 48 public Expression compile(Executable exec) throws XPathException { 49 50 52 StringBuffer statement = new StringBuffer (120); 53 statement.append("INSERT INTO " + table + " ("); 54 55 AxisIterator kids = iterateAxis(Axis.CHILD); 56 NodeInfo child; 57 int cols = 0; 58 while (true) { 59 child = (NodeInfo)kids.next(); 60 if (child == null) { 61 break; 62 } 63 if (child instanceof SQLColumn) { 64 if (cols++ > 0) statement.append(','); 65 String colname = ((SQLColumn)child).getColumnName(); 66 statement.append(colname); 67 } 68 } 69 statement.append(") VALUES ("); 70 71 73 for(int i=0; i<cols; i++) { 74 if (i!=0) { 75 statement.append(','); 76 } 77 statement.append('?'); 78 }; 79 80 statement.append(')'); 81 82 return new InsertInstruction(connection, statement.toString(), getColumnInstructions(exec)); 83 } 84 85 public List getColumnInstructions(Executable exec) throws XPathException { 86 List list = new ArrayList (10); 87 88 AxisIterator kids = iterateAxis(Axis.CHILD); 89 NodeInfo child; 90 while (true) { 91 child = (NodeInfo)kids.next(); 92 if (child == null) { 93 break; 94 } 95 if (child instanceof SQLColumn) { 96 list.add(((SQLColumn)child).compile(exec)); 97 } 98 } 99 100 return list; 101 } 102 103 private static class InsertInstruction extends SimpleExpression { 104 105 public static final int CONNECTION = 0; 106 public static final int FIRST_COLUMN = 1; 107 String statement; 108 109 public InsertInstruction(Expression connection, String statement, List columnInstructions) { 110 Expression[] sub = new Expression[columnInstructions.size() + 1]; 111 sub[CONNECTION] = connection; 112 for (int i=0; i<columnInstructions.size(); i++) { 113 sub[i+FIRST_COLUMN] = (Expression)columnInstructions.get(i); 114 } 115 this.statement = statement; 116 setArguments(sub); 117 } 118 119 123 124 public int getImplementationMethod() { 125 return Expression.EVALUATE_METHOD; 126 } 127 128 public String getExpressionType() { 129 return "sql:insert"; 130 } 131 132 public Item evaluateItem(XPathContext context) throws XPathException { 133 134 136 Item conn = arguments[CONNECTION].evaluateItem(context); 137 if (!(conn instanceof ObjectValue && ((ObjectValue)conn).getObject() instanceof Connection ) ) { 138 dynamicError("Value of connection expression is not a JDBC Connection", context); 139 } 140 Connection connection = (Connection )((ObjectValue)conn).getObject(); 141 PreparedStatement ps = null; 142 143 try { 144 ps=connection.prepareStatement(statement); 145 146 148 int i = 1; 149 for (int c=FIRST_COLUMN; c<arguments.length; c++) { 150 AtomicValue v = (AtomicValue)((SQLColumn.ColumnInstruction)arguments[c]).getSelectValue(context); 151 152 String val = v.getStringValue(); 154 155 if (val.length()==1) val += " "; 157 ps.setObject(i++, val); 159 160 } 161 162 ps.executeUpdate(); 163 if (!connection.getAutoCommit()) { 164 connection.commit(); 165 } 166 167 } catch (SQLException ex) { 168 dynamicError("(SQL INSERT) " + ex.getMessage(), context); 169 } finally { 170 if (ps != null) { 171 try { 172 ps.close(); 173 } catch (SQLException ignore) {} 174 } 175 } 176 177 return null; 178 } 179 180 } 181 182 183 } 184 185 | Popular Tags |