1 28 package net.sf.jasperreports.engine.query; 29 30 import java.math.BigDecimal ; 31 import java.sql.Connection ; 32 import java.sql.PreparedStatement ; 33 import java.sql.ResultSet ; 34 import java.sql.SQLException ; 35 import java.sql.Types ; 36 import java.util.List ; 37 import java.util.Map ; 38 39 import net.sf.jasperreports.engine.JRDataSource; 40 import net.sf.jasperreports.engine.JRDataset; 41 import net.sf.jasperreports.engine.JRException; 42 import net.sf.jasperreports.engine.JRParameter; 43 import net.sf.jasperreports.engine.JRResultSetDataSource; 44 import net.sf.jasperreports.engine.JRValueParameter; 45 import net.sf.jasperreports.engine.util.JRProperties; 46 47 import org.apache.commons.logging.Log; 48 import org.apache.commons.logging.LogFactory; 49 50 51 59 public class JRJdbcQueryExecuter extends JRAbstractQueryExecuter 60 { 61 private static final Log log = LogFactory.getLog(JRJdbcQueryExecuter.class); 62 63 64 private Connection connection; 65 66 69 private PreparedStatement statement; 70 71 private ResultSet resultSet; 72 73 74 public JRJdbcQueryExecuter(JRDataset dataset, Map parameters) 75 { 76 super(dataset, parameters); 77 78 connection = (Connection ) getParameterValue(JRParameter.REPORT_CONNECTION); 79 80 if (connection == null) 81 { 82 if (log.isWarnEnabled()) 83 log.warn("The supplied java.sql.Connection object is null."); 84 } 85 86 parseQuery(); 87 } 88 89 90 protected String getParameterReplacement(String parameterName) 91 { 92 return "?"; 93 } 94 95 96 99 public JRDataSource createDatasource() throws JRException 100 { 101 JRDataSource dataSource = null; 102 103 createStatement(); 104 105 if (statement != null) 106 { 107 try 108 { 109 Integer reportMaxCount = (Integer ) getParameterValue(JRParameter.REPORT_MAX_COUNT); 110 if (reportMaxCount != null) 111 { 112 statement.setMaxRows(reportMaxCount.intValue()); 113 } 114 115 resultSet = statement.executeQuery(); 116 117 dataSource = new JRResultSetDataSource(resultSet); 118 } 119 catch (SQLException e) 120 { 121 throw new JRException("Error executing SQL statement for : " + dataset.getName(), e); 122 } 123 } 124 125 return dataSource; 126 } 127 128 129 private void createStatement() throws JRException 130 { 131 String queryString = getQueryString(); 132 133 if (log.isDebugEnabled()) 134 { 135 log.debug("SQL query string: " + queryString); 136 } 137 138 if (connection != null && queryString != null && queryString.trim().length() > 0) 139 { 140 try 141 { 142 statement = connection.prepareStatement(queryString); 143 144 int fetchSize = JRProperties.getIntegerProperty(dataset.getPropertiesMap(), 145 JRJdbcQueryExecuterFactory.PROPERTY_JDBC_FETCH_SIZE, 146 0); 147 if (fetchSize > 0) 148 { 149 statement.setFetchSize(fetchSize); 150 } 151 152 List parameterNames = getCollectedParameterNames(); 153 if (!parameterNames.isEmpty()) 154 { 155 for(int i = 0; i < parameterNames.size(); i++) 156 { 157 String parameterName = (String )parameterNames.get(i); 158 setStatementParameter(i + 1, parameterName); 159 } 160 } 161 } 162 catch (SQLException e) 163 { 164 throw new JRException("Error preparing statement for executing the report query : " + "\n\n" + queryString + "\n\n", e); 165 } 166 } 167 } 168 169 170 protected void setStatementParameter(int parameterIndex, String parameterName) throws SQLException , JRException 171 { 172 JRValueParameter parameter = getValueParameter(parameterName); 173 Class clazz = parameter.getValueClass(); 174 Object parameterValue = parameter.getValue(); 175 176 if (log.isDebugEnabled()) 177 { 178 log.debug("Parameter #" + parameterIndex + " (" + parameterName + " of type " + clazz.getName() + "): " + parameterValue); 179 } 180 181 if ( clazz.equals(java.lang.Object .class) ) 182 { 183 if (parameterValue == null) 184 { 185 statement.setNull(parameterIndex, Types.JAVA_OBJECT); 186 } 187 else 188 { 189 statement.setObject(parameterIndex, parameterValue); 190 } 191 } 192 else if ( clazz.equals(java.lang.Boolean .class) ) 193 { 194 if (parameterValue == null) 195 { 196 statement.setNull(parameterIndex, Types.BIT); 197 } 198 else 199 { 200 statement.setBoolean(parameterIndex, ((Boolean )parameterValue).booleanValue()); 201 } 202 } 203 else if ( clazz.equals(java.lang.Byte .class) ) 204 { 205 if (parameterValue == null) 206 { 207 statement.setNull(parameterIndex, Types.TINYINT); 208 } 209 else 210 { 211 statement.setByte(parameterIndex, ((Byte )parameterValue).byteValue()); 212 } 213 } 214 else if ( clazz.equals(java.lang.Double .class) ) 215 { 216 if (parameterValue == null) 217 { 218 statement.setNull(parameterIndex, Types.DOUBLE); 219 } 220 else 221 { 222 statement.setDouble(parameterIndex, ((Double )parameterValue).doubleValue()); 223 } 224 } 225 else if ( clazz.equals(java.lang.Float .class) ) 226 { 227 if (parameterValue == null) 228 { 229 statement.setNull(parameterIndex, Types.FLOAT); 230 } 231 else 232 { 233 statement.setFloat(parameterIndex, ((Float )parameterValue).floatValue()); 234 } 235 } 236 else if ( clazz.equals(java.lang.Integer .class) ) 237 { 238 if (parameterValue == null) 239 { 240 statement.setNull(parameterIndex, Types.INTEGER); 241 } 242 else 243 { 244 statement.setInt(parameterIndex, ((Integer )parameterValue).intValue()); 245 } 246 } 247 else if ( clazz.equals(java.lang.Long .class) ) 248 { 249 if (parameterValue == null) 250 { 251 statement.setNull(parameterIndex, Types.BIGINT); 252 } 253 else 254 { 255 statement.setLong(parameterIndex, ((Long )parameterValue).longValue()); 256 } 257 } 258 else if ( clazz.equals(java.lang.Short .class) ) 259 { 260 if (parameterValue == null) 261 { 262 statement.setNull(parameterIndex, Types.SMALLINT); 263 } 264 else 265 { 266 statement.setShort(parameterIndex, ((Short )parameterValue).shortValue()); 267 } 268 } 269 else if ( clazz.equals(java.math.BigDecimal .class) ) 270 { 271 if (parameterValue == null) 272 { 273 statement.setNull(parameterIndex, Types.DECIMAL); 274 } 275 else 276 { 277 statement.setBigDecimal(parameterIndex, (BigDecimal )parameterValue); 278 } 279 } 280 else if ( clazz.equals(java.lang.String .class) ) 281 { 282 if (parameterValue == null) 283 { 284 statement.setNull(parameterIndex, Types.VARCHAR); 285 } 286 else 287 { 288 statement.setString(parameterIndex, parameterValue.toString()); 289 } 290 } 291 else if ( clazz.equals(java.util.Date .class) ) 292 { 293 if (parameterValue == null) 294 { 295 statement.setNull(parameterIndex, Types.DATE); 296 } 297 else 298 { 299 statement.setDate( parameterIndex, 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 statement.setNull(parameterIndex, Types.TIMESTAMP); 307 } 308 else 309 { 310 statement.setTimestamp( parameterIndex, (java.sql.Timestamp )parameterValue ); 311 } 312 } 313 else if ( clazz.equals(java.sql.Time .class) ) 314 { 315 if (parameterValue == null) 316 { 317 statement.setNull(parameterIndex, Types.TIME); 318 } 319 else 320 { 321 statement.setTime( parameterIndex, (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 334 public synchronized void close() 335 { 336 if (resultSet != null) 337 { 338 try 339 { 340 resultSet.close(); 341 } 342 catch (SQLException e) 343 { 344 log.error("Error while closing result set.", e); 345 } 346 finally 347 { 348 resultSet = null; 349 } 350 } 351 352 if (statement != null) 353 { 354 try 355 { 356 statement.close(); 357 } 358 catch (SQLException e) 359 { 360 log.error("Error while closing statement.", e); 361 } 362 finally 363 { 364 statement = null; 365 } 366 } 367 } 368 369 372 public synchronized boolean cancelQuery() throws JRException 373 { 374 if (statement != null) 375 { 376 try 377 { 378 statement.cancel(); 379 return true; 380 } 381 catch (Throwable t) 382 { 383 throw new JRException("Error cancelling SQL statement", t); 384 } 385 } 386 387 return false; 388 } 389 } 390 | Popular Tags |