View Javadoc
1 /* 2 * PROJECT : DAR Runtime and Tools 3 * COPYRIGHT : Copyright (C) 1999-2004 tim.stephenson@enableit.org 4 * LICENSE : GNU LESSER GENERAL PUBLIC LICENSE 5 * Version 2.1, February 1999 6 * 7 * This program is free software; you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License as published by 9 * the Free Software Foundation; either version 2 of the License, or 10 * (at your option) any later version. 11 * 12 * This program is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License 18 * along with this program; if not, write to the Free Software 19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 20 */ 21 package org.enableit.db; 22 23 import org.apache.log4j.Logger; 24 import org.enableit.db.beans.ForeignKey; 25 26 27 /*** 28 * @author Tim Stephenson 29 */ 30 public class DBQuery extends DBStatement { 31 /*** 32 * CVS info about current version of this class. 33 */ 34 public static final String ABOUT = "$Revision: 1.8 $"; 35 36 /*** 37 * String employed in decomposing SQL statements. 38 */ 39 private static final String FROM = "FROM"; 40 41 /*** 42 * String employed in decomposing SQL statements. 43 */ 44 private static final String WHERE = "WHERE"; 45 46 /*** 47 * Hold the platform specific End Of Line character. 48 */ 49 private static final String EOL = System.getProperty("line.separator"); 50 51 /*** 52 * The Log4J <code>Logger</code> doing the logging. 53 */ 54 private static Logger logger = Logger.getLogger(DBQuery.class); 55 56 /*** 57 * The ORDER BY statement to append. 58 */ 59 private String orderBy; 60 private StringBuffer drivingTable; 61 private StringBuffer fromClause; 62 private StringBuffer selectClause; 63 64 /*** 65 * Default constructor. 66 */ 67 public DBQuery() { 68 super(); 69 } 70 71 /*** 72 * Construct instance and set the SQL. 73 * @param sql A SQL statement, optionally containing GROUP BY and / or 74 * ORDER BY clauses. The GROUP BY should <em>preceed</em> any 75 * ORDER BY. 76 */ 77 public DBQuery(String sql) { 78 // If both present, group by SHOULD be first 79 int orderBy = sql.indexOf("ORDER BY"); 80 int groupBy = sql.indexOf("GROUP BY"); 81 82 if (groupBy > -1) { 83 setBaseSql(sql.substring(0, groupBy)); 84 setOrderBy(sql.substring(groupBy)); 85 } else if (orderBy > -1) { 86 setBaseSql(sql.substring(0, orderBy)); 87 setOrderBy(sql.substring(orderBy)); 88 } else { 89 setBaseSql(sql); 90 } 91 } 92 93 /*** 94 * Constructor that trusts the caller to set up the clauses correctly. 95 * @param newSelectClause Specifies the new SELECT clause. 96 * @param newFromClause Specifies the new FROM clause. 97 * @param newOrderBy Specifies the new ORDER BY clause, 98 * actually includes anything after WHERE clause (e.g. GROUP BY). 99 */ 100 public DBQuery(String newSelectClause, String newFromClause, 101 String newOrderBy) { 102 this(); 103 this.setSelectClause(newSelectClause); 104 this.setFromClause(newFromClause); 105 this.setOrderBy(newOrderBy); 106 } 107 108 /*** 109 * Construct instance and set both the baseSql and orderBy phrases of 110 * a SQL statement. 111 * @param newBaseSql The base SQL statement (not including group or order by). 112 * @param newOrderBy The remainder of the SQL statement. 113 */ 114 public DBQuery(String newBaseSql, String newOrderBy) { 115 setBaseSql(newBaseSql); 116 setOrderBy(newOrderBy); 117 } 118 119 /*** 120 * @return baseSql The base SQL. 121 */ 122 public String getBaseSql() { 123 return getSelectClause() + getFromClause(); 124 } 125 126 /*** 127 * @param baseSql The base SQL. 128 */ 129 public void setBaseSql(String baseSql) { 130 //baseSql = baseSql.toUpperCase() ; 131 int from = baseSql.indexOf(FROM); 132 133 if (from > 0) { 134 setSelectClause(baseSql.substring(0, from).trim()); 135 } else { 136 setSelectClause(baseSql.substring(0).trim()); 137 } 138 139 int where = baseSql.indexOf(WHERE); 140 141 if (where > 0) { 142 setFromClause(baseSql.substring(from + FROM.length(), where).trim()); 143 setWhereClause(baseSql.substring(where + WHERE.length()).trim()); 144 } else { 145 setFromClause(baseSql.substring(from + FROM.length()).trim()); 146 setWhereClause(""); 147 } 148 } 149 150 /*** 151 * @param orderBy The ORDER BY clause. 152 */ 153 public void setOrderBy(String orderBy) { 154 this.orderBy = orderBy; 155 } 156 157 /*** 158 * @return SQL with placeholders for any parameters. 159 */ 160 public String getSql() { 161 logger.info("METHOD_ENTRY: getSql"); 162 163 //appendJoinSql(); 164 StringBuffer sql = new StringBuffer(getSelectClause()); 165 166 sql.append(getFromClause()); 167 sql.append(getWhereClause()); 168 169 // Append order by (if any) 170 if (orderBy != null) { 171 //sql.append(orderBy) ; 172 if (!orderBy.startsWith(EOL)) { 173 sql.append(EOL); 174 } 175 176 sql.append(orderBy); 177 } 178 179 logger.info("METHOD_EXIT: getSql, sql=" + sql); 180 181 return sql.toString(); 182 } 183 184 /*** 185 * @param string 186 */ 187 private void appendFromClause(String s) { 188 if (fromClause.length() <= 0) { 189 fromClause.append(s); 190 } else { 191 fromClause.append("," + SPACE + s.trim()); 192 } 193 } 194 195 private void setSelectClause(String selectClause) { 196 this.selectClause = new StringBuffer(selectClause); 197 } 198 199 private String getSelectClause() { 200 return selectClause.toString(); 201 } 202 203 /* public List getSelectedCols() { 204 List selectedCols = new ArrayList() ; 205 206 String s = getSelectClause().substring(getSelectClause().indexOf("SELECT") + 6) ; 207 StringTokenizer st = new StringTokenizer(s, ",") ; 208 while (st.hasMoreElements()) { 209 String col = (String) st.nextElement() ; 210 logger.error("col: " + col) ; 211 int i = col.indexOf("AS") ; 212 if (i > 0) { 213 col = col.substring(i + 2) ; 214 col.replaceAll("'", "") ; 215 } 216 logger.error("col: " + col) ; 217 selectedCols.add(col.trim()) ; 218 } 219 return selectedCols ; 220 } */ 221 private void setFromClause(String fromClause) { 222 int comma = fromClause.indexOf(","); 223 224 if (comma > 0) { 225 drivingTable = new StringBuffer(fromClause.substring(0, comma)); 226 } else { 227 drivingTable = new StringBuffer(fromClause); 228 } 229 230 this.fromClause = new StringBuffer(fromClause); 231 } 232 233 private String getFromClause() { 234 if (fromClause.length() > 0) { 235 return " FROM " + fromClause.toString(); 236 } else { 237 // TODO: Reckon this wont ever happen 238 return ""; 239 } 240 } 241 242 private String getDrivingTable() { 243 return drivingTable.toString(); 244 } 245 246 /*** 247 * @param fk Encapsulates information on what to join to. 248 */ 249 public void addJoin(ForeignKey fk) { 250 appendFromClause(fk.getFkTableName()); 251 appendWhereClause(getDrivingTable() + "." 252 + fk.getDataColumn().getColName() + " *= " + fk.getFkTableName() 253 + "." + fk.getDataColumn().getColName()); 254 } 255 256 /*private void appendJoinSql() { 257 // Append sql for Joins 258 for (Iterator it = joins.iterator() ; it.hasNext() ;) { 259 ForeignKey fk = (ForeignKey) it.next() ; 260 appendFromClause(fk.getFkTableName()) ; 261 appendWhereClause( 262 getDrivingTable() + "." + fk.getDataColumn().getColName() 263 + " = " + fk.getFkTableName() + "." + fk.getDataColumn().getColName()) ; 264 } 265 }*/ 266 }

This page was automatically generated by Maven