1 28 29 package com.caucho.jstl.el; 30 31 import com.caucho.el.Expr; 32 import com.caucho.jsp.PageContextImpl; 33 import com.caucho.log.Log; 34 import com.caucho.util.L10N; 35 36 import javax.el.ELContext; 37 import javax.servlet.jsp.JspException ; 38 import javax.servlet.jsp.jstl.sql.SQLExecutionTag; 39 import javax.servlet.jsp.tagext.BodyTagSupport ; 40 import javax.sql.DataSource ; 41 import java.sql.Connection ; 42 import java.sql.PreparedStatement ; 43 import java.sql.ResultSet ; 44 import java.sql.Statement ; 45 import java.sql.Types ; 46 import java.util.ArrayList ; 47 import java.util.logging.Level ; 48 import java.util.logging.Logger ; 49 50 public class SqlUpdateTag extends BodyTagSupport implements SQLExecutionTag { 51 private static final Logger log = Log.open(SqlUpdateTag.class); 52 private static final L10N L = new L10N(SqlUpdateTag.class); 53 54 private Expr _sql; 55 private String _var; 56 private String _scope; 57 private Expr _dataSource; 58 59 private ArrayList <Object > _params; 60 61 64 public void setSql(Expr sql) 65 { 66 _sql = sql; 67 } 68 69 72 public void setVar(String var) 73 { 74 _var = var; 75 } 76 77 80 public void setScope(String scope) 81 { 82 _scope = scope; 83 } 84 85 88 public void setDataSource(Expr dataSource) 89 { 90 _dataSource = dataSource; 91 } 92 93 96 public void addSQLParameter(Object value) 97 { 98 if (_params == null) 99 _params = new ArrayList <Object >(); 100 101 _params.add(value); 102 } 103 104 public int doEndTag() throws JspException 105 { 106 Connection conn = null; 107 boolean isTransaction = false; 108 PageContextImpl pageContext = (PageContextImpl) this.pageContext; 109 ELContext env = pageContext.getELContext(); 110 111 try { 112 String sql; 113 114 if (_sql != null) 115 sql = _sql.evalString(env); 116 else 117 sql = bodyContent.getString(); 118 119 conn = (Connection ) pageContext.getAttribute("caucho.jstl.sql.conn"); 120 if (conn != null) 121 isTransaction = true; 122 123 if (! isTransaction) { 124 DataSource ds; 125 126 if (_dataSource != null) 127 ds = SqlQueryTag.getDataSource(pageContext, 128 _dataSource.evalObject(env)); 129 else 130 ds = SqlQueryTag.getDataSource(pageContext, null); 131 132 conn = ds.getConnection(); 133 } 134 135 Object value = null; 136 137 ResultSet rs; 138 139 ArrayList params = _params; 140 _params = null; 141 Statement stmt; 142 143 int rows = 0; 144 145 if (params == null) { 146 stmt = conn.createStatement(); 147 rows = stmt.executeUpdate(sql); 148 } 149 else { 150 PreparedStatement pstmt = conn.prepareStatement(sql); 151 stmt = pstmt; 152 153 for (int i = 0; i < params.size(); i++) { 154 Object paramValue = params.get(i); 155 156 if (paramValue == null) 157 pstmt.setNull(i + 1, Types.VARCHAR); 158 else 159 pstmt.setObject(i + 1, paramValue); 160 } 161 162 rows = pstmt.executeUpdate(); 163 } 164 165 stmt.close(); 166 167 CoreSetTag.setValue(pageContext, _var, _scope, new Integer (rows)); 168 } catch (Exception e) { 169 throw new JspException (e); 170 } finally { 171 if (! isTransaction && conn != null) { 172 try { 173 conn.close(); 174 } catch (Exception e) { 175 log.log(Level.FINE, e.toString(), e); 176 } 177 } 178 } 179 180 return EVAL_PAGE; 181 } 182 } 183 | Popular Tags |