1 23 package org.dbforms.taglib; 24 import org.apache.commons.logging.Log; 25 import org.apache.commons.logging.LogFactory; 26 27 import org.dbforms.config.DbEventInterceptorData; 28 import org.dbforms.config.ResultSetVector; 29 30 import org.dbforms.util.StringUtil; 31 32 import java.sql.Connection ; 33 import java.sql.PreparedStatement ; 34 import java.sql.SQLException ; 35 36 import java.util.List ; 37 import java.util.Vector ; 38 39 import javax.servlet.http.HttpServletRequest ; 40 41 42 43 54 public class TableData extends EmbeddedData 55 implements javax.servlet.jsp.tagext.TryCatchFinally { 56 private static Log logCat = LogFactory.getLog(TableData.class.getName()); 57 58 private String foreignTable; 60 private String orderBy; 61 private String storeField; 62 private String visibleFields; 63 64 69 public void setForeignTable(String foreignTable) { 70 this.foreignTable = foreignTable; 71 } 72 73 74 79 public String getForeignTable() { 80 return foreignTable; 81 } 82 83 84 89 public void setOrderBy(String orderBy) { 90 this.orderBy = orderBy; 91 logCat.info("setOrderBy(\"" + orderBy + "\")"); 92 } 93 94 95 100 public String getOrderBy() { 101 return orderBy; 102 } 103 104 105 110 public void setStoreField(String storeField) { 111 this.storeField = storeField; 112 } 113 114 115 120 public String getStoreField() { 121 return storeField; 122 } 123 124 125 130 public void setVisibleFields(String visibleFields) { 131 this.visibleFields = visibleFields; 132 } 133 134 135 140 public String getVisibleFields() { 141 return visibleFields; 142 } 143 144 145 148 public void doCatch(Throwable t) throws Throwable { 149 throw t; 150 } 151 152 153 156 public void doFinally() { 157 foreignTable = null; 158 visibleFields = null; 159 storeField = null; 160 orderBy = null; 161 super.doFinally(); 162 } 163 164 165 175 protected List fetchData(Connection con) throws SQLException { 176 Vector vf = StringUtil.splitString(visibleFields, ",;~"); 177 178 StringBuffer queryBuf = new StringBuffer (); 179 180 queryBuf.append("SELECT "); 181 queryBuf.append(storeField); 182 queryBuf.append(", "); 183 184 for (int i = 0; i < vf.size(); i++) { 185 queryBuf.append((String ) vf.elementAt(i)); 186 187 if (i < (vf.size() - 1)) { 188 queryBuf.append(", "); 189 } 190 } 191 192 queryBuf.append(" FROM "); 193 queryBuf.append(foreignTable); 194 195 if (orderBy != null) { 196 queryBuf.append(" ORDER BY "); 197 queryBuf.append(orderBy); 198 } 199 200 logCat.info("about to execute:" + queryBuf.toString()); 201 202 PreparedStatement ps = con.prepareStatement(queryBuf.toString()); 203 ResultSetVector rsv = null; 204 205 try { 206 rsv = new ResultSetVector(); 207 208 HttpServletRequest request = (HttpServletRequest ) pageContext 209 .getRequest(); 210 DbEventInterceptorData data = new DbEventInterceptorData(request, 211 getConfig(), con, null); 212 data.setAttribute(DbEventInterceptorData.PAGECONTEXT, 213 pageContext); 214 rsv.addResultSet(data, ps.executeQuery()); 215 } finally { 216 ps.close(); } 218 219 return formatEmbeddedResultRows(rsv); 220 } 221 } 222 | Popular Tags |