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