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 java.text.DateFormat; 24 import java.util.ArrayList; 25 import java.util.Iterator; 26 import java.util.List; 27 28 import org.apache.log4j.Logger; 29 import org.enableit.db.beans.Column; 30 31 32 /*** 33 * Common ancestor for both queries and updates. 34 * @author Tim Stephenson 35 */ 36 public abstract class DBStatement extends Object { 37 /* 38 * Properties 39 */ 40 protected static final String SPACE = " "; 41 42 /*** 43 * CVS info about this class and its current version 44 */ 45 public static final String ABOUT = "$Revision $"; 46 47 /*** 48 * The Log4J <code>Logger</code> doing the logging. 49 */ 50 private static Logger logger = Logger.getLogger(DBStatement.class); 51 private DateFormat dateFormat = DateFormat.getDateInstance(); 52 private DateFormat timeFormat = DateFormat.getTimeInstance(); 53 private DateFormat dateTimeFormat = DateFormat.getDateTimeInstance(); 54 55 /*** 56 * The filters to apply to the base query. 57 */ 58 private List filters = new ArrayList(); 59 60 /*** 61 * The base SQL statement, does not include a WHERE statement, 62 * ORDER BY or GROUP BY. 63 */ 64 private String baseSql; 65 private StringBuffer whereClause; 66 protected List cols; 67 protected List vals; 68 69 /* 70 * Constructors 71 */ 72 73 /*** 74 * Default Constructor 75 */ 76 public DBStatement() { 77 cols = new ArrayList(); 78 vals = new ArrayList(); 79 } 80 81 /*** 82 * Construct instance and set the base SQL statement. 83 * @param baseSql The underlying SQL statement. 84 */ 85 public DBStatement(String baseSql) { 86 this(); 87 setBaseSql(baseSql); 88 } 89 90 /* 91 * Methods 92 */ 93 94 /*** 95 * @return baseSql The base SQL. 96 */ 97 public String getBaseSql() { 98 return baseSql; 99 } 100 101 /*** 102 * @param baseSql The base SQL. 103 */ 104 public void setBaseSql(String baseSql) { 105 int where = baseSql.indexOf("WHERE"); 106 107 if (where > 0) { 108 this.baseSql = baseSql.substring(0, where).trim(); 109 setWhereClause(baseSql.substring(where + 5).trim()); 110 } else { 111 this.baseSql = baseSql.trim(); 112 setWhereClause(""); 113 } 114 } 115 116 public void setDateFormat(DateFormat fmt) { 117 logger.info("METHOD_ENTRY setDateFormat, format: " + fmt); 118 dateFormat = fmt; 119 logger.info("METHOD_EXIT setDateFormat"); 120 } 121 122 public void setTimeFormat(DateFormat fmt) { 123 logger.info("METHOD_ENTRY setTimeFormat, format: " + fmt); 124 timeFormat = fmt; 125 logger.info("METHOD_EXIT setTimeFormat"); 126 } 127 128 public void setDateTimeFormat(DateFormat fmt) { 129 logger.info("METHOD_ENTRY setDateTimeFormat, format: " + fmt); 130 dateTimeFormat = fmt; 131 logger.info("METHOD_EXIT setDateTimeFormat"); 132 } 133 134 /*** 135 * @return filters Iterator over the DBFilters in this statement. 136 */ 137 public Iterator getFilterIterator() { 138 return this.filters.iterator(); 139 } 140 141 /*** 142 * @param filter Additional where clause. 143 */ 144 public void addFilter(DBFilter filter) { 145 logger.info("METHOD_ENTRY addFilter, filter:" + filter); 146 this.filters.add(filter); 147 logger.info("METHOD_EXIT addFilter"); 148 } 149 150 /*** 151 * @return SQL with placeholders for any parameters. 152 */ 153 public String getSql() { 154 logger.info("METHOD_ENTRY: getSql"); 155 156 StringBuffer sql = new StringBuffer(getBaseSql()); 157 158 sql.append(getWhereClause()); 159 160 logger.info("METHOD_EXIT: getSql, sql=" + sql); 161 162 return sql.toString(); 163 } 164 165 /*** 166 * @param string 167 */ 168 protected void appendWhereClause(String s) { 169 if (whereClause.length() <= 0) { 170 whereClause.append(s); 171 } else { 172 whereClause.append(SPACE + "AND" + SPACE + s.trim()); 173 } 174 } 175 176 /*** 177 * @return List of variables to bind to the placeholders in the SQL. 178 */ 179 public List getParameters() { 180 List params = new ArrayList(); 181 182 // Append values of any filters 183 for (Iterator i = filters.iterator(); i.hasNext();) { 184 DBFilter filter = (DBFilter) i.next(); 185 186 if ((filter.getMatchType() != DBFilter.IS_NULL) 187 && (filter.getMatchType() != DBFilter.IS_NOT_NULL)) { 188 Object obj = filter.getValue(); 189 190 params.add(obj); 191 } 192 } 193 194 return params; 195 } 196 197 protected Object getParam(Column col, Object val) { 198 Object obj = null; 199 200 if ((val == null) 201 || (val instanceof String 202 && (((String) val).trim().length() == 0))) { 203 obj = new SqlType(col); 204 } else { 205 try { 206 // Check if need to attempt type conversion 207 SqlType sqlType = new SqlType(col); 208 209 switch (sqlType.getDataType()) { 210 case SqlType.SQLTIMESTAMP: 211 212 //dateTimeFormat. 213 System.out.println("found datetime: " + col.getColType() 214 + "=" + val); 215 obj = new java.sql.Timestamp(dateTimeFormat.parse( 216 val.toString()).getTime()); 217 218 break; 219 case SqlType.SQLDATE: 220 System.out.println("found date: " + col.getColType() + "=" 221 + val); 222 obj = new java.sql.Date(dateFormat.parse(val.toString()) 223 .getTime()); 224 225 break; 226 case SqlType.SQLTIME: 227 System.out.println("found time: " + col.getColType() + "=" 228 + val); 229 obj = new java.sql.Time(timeFormat.parse(val.toString()) 230 .getTime()); 231 232 break; 233 default: 234 logger.info("Treating " + col.getColType() 235 + " as string type"); 236 237 /* 238 * Take a chance with String representation 239 * and any RDBMS implicit casting 240 */ 241 obj = val; 242 } 243 } catch (java.text.ParseException e) { 244 logger.warn(e.getMessage()); 245 246 /* 247 * Take a chance with String representation 248 * and any RDBMS implicit casting 249 */ 250 obj = val; 251 } 252 } 253 254 return obj; 255 } 256 257 protected void setWhereClause(String whereClause) { 258 this.whereClause = new StringBuffer(whereClause); 259 } 260 261 protected String getWhereClause() { 262 // Initialise from the declared where clause... 263 StringBuffer sb; 264 265 if (whereClause.length() > 0) { 266 sb = new StringBuffer(" WHERE " + whereClause.toString()); 267 } else { 268 sb = new StringBuffer(); 269 } 270 271 // ... then add any filters ` 272 for (Iterator it = filters.iterator(); it.hasNext();) { 273 DBFilter filter = (DBFilter) it.next(); 274 275 if (sb.indexOf("WHERE") > 0) { 276 sb.append(" AND "); 277 } else { 278 sb.append(" WHERE "); 279 } 280 281 sb.append(filter.getName()); 282 sb.append(filter.getMatch()); 283 sb.append("?"); 284 } 285 286 return sb.toString(); 287 } 288 }

This page was automatically generated by Maven