1 28 package net.sf.jasperreports.engine.util; 29 30 import java.math.BigDecimal; 31 import java.sql.Connection; 32 import java.sql.PreparedStatement; 33 import java.sql.SQLException; 34 import java.sql.Types; 35 import java.util.ArrayList; 36 import java.util.HashMap; 37 import java.util.List; 38 import java.util.Map; 39 40 import net.sf.jasperreports.engine.JRException; 41 import net.sf.jasperreports.engine.JRParameter; 42 import net.sf.jasperreports.engine.JRQuery; 43 import net.sf.jasperreports.engine.JRQueryChunk; 44 45 46 50 public class JRQueryExecuter 51 { 52 53 54 57 private JRQuery query = null; 58 private Map parametersMap = new HashMap(); 59 private Map parameterValues = null; 60 private String queryString = ""; 61 private List parameterNames = new ArrayList(); 62 63 64 67 protected JRQueryExecuter(JRQuery query, Map parameters, Map values) 68 { 69 this.query = query; 70 this.parametersMap = parameters; 71 this.parameterValues = values; 72 73 this.parseQuery(); 74 } 75 76 77 80 public static PreparedStatement getStatement( 81 JRQuery query, 82 Map parameters, 83 Map values, 84 Connection conn 85 ) throws JRException 86 { 87 PreparedStatement pstmt = null; 88 89 if (conn != null) 90 { 91 JRQueryExecuter queryExecuter = 92 new JRQueryExecuter( 93 query, 94 parameters, 95 values 96 ); 97 98 pstmt = queryExecuter.getStatement(conn); 99 } 100 101 return pstmt; 102 } 103 104 105 108 private void parseQuery() 109 { 110 queryString = ""; 111 parameterNames = new ArrayList(); 112 113 if (query != null) 114 { 115 JRQueryChunk[] chunks = query.getChunks(); 116 if (chunks != null && chunks.length > 0) 117 { 118 StringBuffer sbuffer = new StringBuffer(); 119 JRQueryChunk chunk = null; 120 for(int i = 0; i < chunks.length; i++) 121 { 122 chunk = chunks[i]; 123 switch (chunk.getType()) 124 { 125 case JRQueryChunk.TYPE_PARAMETER_CLAUSE : 126 { 127 String parameterName = chunk.getText(); 128 Object parameterValue = parameterValues.get(parameterName); 129 sbuffer.append(String.valueOf(parameterValue)); 130 break; 132 } 133 case JRQueryChunk.TYPE_PARAMETER : 134 { 135 sbuffer.append("?"); 136 parameterNames.add(chunk.getText()); 137 break; 138 } 139 case JRQueryChunk.TYPE_TEXT : 140 default : 141 { 142 sbuffer.append(chunk.getText()); 143 break; 144 } 145 } 146 } 147 148 queryString = sbuffer.toString(); 149 } 150 } 151 } 152 153 154 157 private PreparedStatement getStatement(Connection conn) throws JRException 158 { 159 PreparedStatement pstmt = null; 160 161 if (queryString != null && queryString.trim().length() > 0) 162 { 163 try 164 { 165 pstmt = conn.prepareStatement(queryString); 166 167 if (parameterNames != null && parameterNames.size() > 0) 168 { 169 JRParameter parameter = null; 170 String parameterName = null; 171 Class clazz = null; 172 Object parameterValue = null; 173 for(int i = 0; i < parameterNames.size(); i++) 174 { 175 parameterName = (String)parameterNames.get(i); 176 parameter = (JRParameter)parametersMap.get(parameterName); 177 clazz = parameter.getValueClass(); 178 parameterValue = parameterValues.get(parameterName); 180 181 if ( clazz.equals(java.lang.Object.class) ) 182 { 183 if (parameterValue == null) 184 { 185 pstmt.setNull(i + 1, Types.JAVA_OBJECT); 186 } 187 else 188 { 189 pstmt.setObject(i + 1, parameterValue); 190 } 191 } 192 else if ( clazz.equals(java.lang.Boolean.class) ) 193 { 194 if (parameterValue == null) 195 { 196 pstmt.setNull(i + 1, Types.BIT); 197 } 198 else 199 { 200 pstmt.setBoolean(i + 1, ((Boolean)parameterValue).booleanValue()); 201 } 202 } 203 else if ( clazz.equals(java.lang.Byte.class) ) 204 { 205 if (parameterValue == null) 206 { 207 pstmt.setNull(i + 1, Types.TINYINT); 208 } 209 else 210 { 211 pstmt.setByte(i + 1, ((Byte)parameterValue).byteValue()); 212 } 213 } 214 else if ( clazz.equals(java.lang.Double.class) ) 215 { 216 if (parameterValue == null) 217 { 218 pstmt.setNull(i + 1, Types.DOUBLE); 219 } 220 else 221 { 222 pstmt.setDouble(i + 1, ((Double)parameterValue).doubleValue()); 223 } 224 } 225 else if ( clazz.equals(java.lang.Float.class) ) 226 { 227 if (parameterValue == null) 228 { 229 pstmt.setNull(i + 1, Types.FLOAT); 230 } 231 else 232 { 233 pstmt.setFloat(i + 1, ((Float)parameterValue).floatValue()); 234 } 235 } 236 else if ( clazz.equals(java.lang.Integer.class) ) 237 { 238 if (parameterValue == null) 239 { 240 pstmt.setNull(i + 1, Types.INTEGER); 241 } 242 else 243 { 244 pstmt.setInt(i + 1, ((Integer)parameterValue).intValue()); 245 } 246 } 247 else if ( clazz.equals(java.lang.Long.class) ) 248 { 249 if (parameterValue == null) 250 { 251 pstmt.setNull(i + 1, Types.BIGINT); 252 } 253 else 254 { 255 pstmt.setLong(i + 1, ((Long)parameterValue).longValue()); 256 } 257 } 258 else if ( clazz.equals(java.lang.Short.class) ) 259 { 260 if (parameterValue == null) 261 { 262 pstmt.setNull(i + 1, Types.SMALLINT); 263 } 264 else 265 { 266 pstmt.setShort(i + 1, ((Short)parameterValue).shortValue()); 267 } 268 } 269 else if ( clazz.equals(java.math.BigDecimal.class) ) 270 { 271 if (parameterValue == null) 272 { 273 pstmt.setNull(i + 1, Types.DECIMAL); 274 } 275 else 276 { 277 pstmt.setBigDecimal(i + 1, (BigDecimal)parameterValue); 278 } 279 } 280 else if ( clazz.equals(java.lang.String.class) ) 281 { 282 if (parameterValue == null) 283 { 284 pstmt.setNull(i + 1, Types.VARCHAR); 285 } 286 else 287 { 288 pstmt.setString(i + 1, parameterValue.toString()); 289 } 290 } 291 else if ( clazz.equals(java.util.Date.class) ) 292 { 293 if (parameterValue == null) 294 { 295 pstmt.setNull(i + 1, Types.DATE); 296 } 297 else 298 { 299 pstmt.setDate( i + 1, new java.sql.Date( ((java.util.Date)parameterValue).getTime() ) ); 300 } 301 } 302 else if ( clazz.equals(java.sql.Timestamp.class) ) 303 { 304 if (parameterValue == null) 305 { 306 pstmt.setNull(i + 1, Types.TIMESTAMP); 307 } 308 else 309 { 310 pstmt.setTimestamp( i + 1, (java.sql.Timestamp)parameterValue ); 311 } 312 } 313 else if ( clazz.equals(java.sql.Time.class) ) 314 { 315 if (parameterValue == null) 316 { 317 pstmt.setNull(i + 1, Types.TIME); 318 } 319 else 320 { 321 pstmt.setTime( i + 1, (java.sql.Time)parameterValue ); 322 } 323 } 324 else 325 { 326 throw new JRException("Parameter type not supported in query : " + parameterName + " class " + clazz.getName()); 327 } 328 } 329 } 330 } 331 catch (SQLException e) 332 { 333 throw new JRException("Error preparing statement for executing the report query : " + "\n\n" + queryString + "\n\n", e); 334 } 335 } 336 337 return pstmt; 338 } 339 340 341 } 342 | Popular Tags |