1 29 30 package com.caucho.jstl.el; 31 32 import com.caucho.el.Expr; 33 import com.caucho.jstl.ResultImpl; 34 import com.caucho.log.Log; 35 import com.caucho.util.L10N; 36 37 import javax.el.ELContext; 38 import javax.naming.InitialContext ; 39 import javax.naming.NamingException ; 40 import javax.servlet.jsp.JspException ; 41 import javax.servlet.jsp.PageContext ; 42 import javax.servlet.jsp.jstl.core.Config; 43 import javax.servlet.jsp.jstl.sql.Result; 44 import javax.servlet.jsp.jstl.sql.SQLExecutionTag; 45 import javax.servlet.jsp.tagext.BodyTagSupport ; 46 import javax.sql.DataSource ; 47 import java.sql.Connection ; 48 import java.sql.PreparedStatement ; 49 import java.sql.ResultSet ; 50 import java.sql.Statement ; 51 import java.sql.Types ; 52 import java.util.ArrayList ; 53 import java.util.logging.Level ; 54 import java.util.logging.Logger ; 55 56 public class SqlQueryTag extends BodyTagSupport implements SQLExecutionTag { 57 private static final Logger log = Log.open(SqlQueryTag.class); 58 private static final L10N L = new L10N(SqlQueryTag.class); 59 60 private Expr _sql; 61 private String _var; 62 private String _scope; 63 private Expr _dataSource; 64 private Expr _maxRows; 65 private Expr _startRow; 66 67 private ArrayList <Object > _params; 68 69 72 public void setSql(Expr sql) 73 { 74 _sql = sql; 75 } 76 77 80 public void setVar(String var) 81 { 82 _var = var; 83 } 84 85 88 public void setScope(String scope) 89 { 90 _scope = scope; 91 } 92 93 96 public void setDataSource(Expr dataSource) 97 { 98 _dataSource = dataSource; 99 } 100 101 104 public void setMaxRows(Expr maxRows) 105 { 106 _maxRows = maxRows; 107 } 108 109 112 public void setStartRow(Expr startRow) 113 { 114 _startRow = startRow; 115 } 116 117 120 public void addSQLParameter(Object value) 121 { 122 if (_params == null) 123 _params = new ArrayList <Object >(); 124 125 _params.add(value); 126 } 127 128 public int doEndTag() throws JspException 129 { 130 Connection conn = null; 131 boolean isTransaction = false; 132 ELContext env = pageContext.getELContext(); 133 134 try { 135 String sql; 136 137 if (_sql != null) 138 sql = _sql.evalString(env); 139 else 140 sql = bodyContent.getString(); 141 142 conn = (Connection ) pageContext.getAttribute("caucho.jstl.sql.conn"); 143 if (conn != null) 144 isTransaction = true; 145 146 if (! isTransaction) { 147 DataSource ds; 148 149 if (_dataSource != null) 150 ds = getDataSource(pageContext, _dataSource.evalObject(env)); 151 else 152 ds = getDataSource(pageContext, null); 153 154 conn = ds.getConnection(); 155 } 156 157 Object value = null; 158 159 ResultSet rs; 160 161 ArrayList params = _params; 162 _params = null; 163 Statement stmt; 164 165 if (params == null) { 166 stmt = conn.createStatement(); 167 rs = stmt.executeQuery(sql); 168 } 169 else { 170 PreparedStatement pstmt = conn.prepareStatement(sql); 171 stmt = pstmt; 172 173 for (int i = 0; i < params.size(); i++) { 174 Object paramValue = params.get(i); 175 176 if (paramValue == null) 177 pstmt.setNull(i + 1, Types.VARCHAR); 178 else 179 pstmt.setObject(i + 1, paramValue); 180 } 181 182 rs = pstmt.executeQuery(); 183 } 184 185 if (_startRow != null) { 186 int startRow = (int) _startRow.evalLong(env); 187 188 while (startRow-- > 0 && rs.next()) { 189 } 190 } 191 192 Result result; 193 if (_maxRows != null) 194 result = new ResultImpl(rs, (int) _maxRows.evalLong(env)); 195 else 196 result = new ResultImpl(rs, -1); 197 198 rs.close(); 199 stmt.close(); 200 201 CoreSetTag.setValue(pageContext, _var, _scope, result); 202 } catch (Exception e) { 203 throw new JspException (e); 204 } finally { 205 if (! isTransaction && conn != null) { 206 try { 207 conn.close(); 208 } catch (Exception e) { 209 log.log(Level.FINE, e.toString(), e); 210 } 211 } 212 } 213 214 return EVAL_PAGE; 215 } 216 217 public static DataSource getDataSource(PageContext pageContext, 218 Object ds) 219 throws JspException 220 { 221 if (ds == null) 222 ds = Config.find(pageContext, Config.SQL_DATA_SOURCE); 223 224 if (ds instanceof DataSource ) 225 return (DataSource ) ds; 226 else if (! (ds instanceof String )) 227 throw new JspException (L.l("`{0}' is an invalid DataSource.", ds)); 228 229 String key = (String ) ds; 230 231 try { 232 String jndiName; 233 234 if (key.startsWith("java:comp/")) 235 jndiName = key; 236 else 237 jndiName = "java:comp/env/" + key; 238 239 Object value = new InitialContext ().lookup(jndiName); 240 241 if (value instanceof DataSource ) 242 return (DataSource ) value; 243 } catch (NamingException e) { 244 } 245 246 throw new JspException (L.l("`{0}' is an invalid DataSource.", ds)); 247 } 248 } 249 | Popular Tags |