1 16 package org.apache.cocoon.components.language.markup.xsp; 17 18 import org.apache.avalon.framework.logger.AbstractLogEnabled; 19 20 import java.sql.Connection ; 21 import java.sql.PreparedStatement ; 22 import java.sql.CallableStatement ; 23 import java.sql.ResultSet ; 24 import java.sql.ResultSetMetaData ; 25 import java.sql.SQLException ; 26 import java.util.ArrayList ; 27 28 34 public abstract class AbstractEsqlQuery extends AbstractLogEnabled { 35 private int maxRows = -1; 36 private int skipRows = 0; 37 private int rowCount = -1; 38 private int position = -1; 39 private String query = null; 40 private Connection connection = null; 41 private ResultSetMetaData resultSetMetaData = null; 42 private PreparedStatement preparedStatement = null; 43 private ResultSet resultSet = null; 44 private boolean hasResultSet = false; 45 private boolean keepgoing = true; 46 47 private int queryResultsCount = 0; 48 private int updateResultsCount = 0; 49 private int updateCount = -2; 50 51 private ArrayList groups = null; 52 private int groupLevel = -1; 53 private int changeLevel = -1; 54 55 56 62 protected AbstractEsqlQuery(Connection connection, String query) { 63 this.connection = connection; 64 this.query = query; 65 } 66 67 71 protected AbstractEsqlQuery(final ResultSet resultSet) { 72 this.connection = null; 73 this.query = null; 74 this.resultSet = resultSet; 75 this.hasResultSet = (resultSet != null); 76 } 77 78 82 public abstract AbstractEsqlQuery newInstance(final ResultSet resultSet); 83 84 85 113 public String getQueryString() throws SQLException { 114 return (query); 115 } 116 117 122 public PreparedStatement prepareStatement() throws SQLException { 123 preparedStatement = connection.prepareStatement(getQueryString()); 124 return (preparedStatement); 125 } 126 127 132 public CallableStatement prepareCall() throws SQLException { 133 preparedStatement = connection.prepareCall(getQueryString()); 134 return ((CallableStatement ) preparedStatement); 135 } 136 137 138 147 public int getRowCount() throws SQLException { 148 if (rowCount < 0) { 149 String lowerQuery = query.toLowerCase(); 150 int from = lowerQuery.indexOf(" from "); 151 152 int groupby = lowerQuery.indexOf(" group by "); 153 int orderby = lowerQuery.indexOf(" order by "); 154 155 int min = Math.min(groupby, orderby); 156 157 String countQuery; 158 if (min > -1) { 159 countQuery = "select count(*)" + String.valueOf(query).substring(from, min); 160 } 161 else { 162 countQuery = "select count(*)" + String.valueOf(query).substring(from); 163 } 164 165 if (getLogger().isDebugEnabled()) getLogger().debug("executing [" + String.valueOf(query) + "]"); 166 167 ResultSet rs = preparedStatement.executeQuery(countQuery); 168 try { 169 if (rs.first()) { 170 rowCount = rs.getInt(1); 171 if (getLogger().isDebugEnabled()) getLogger().debug("count = " + rowCount); 172 } 173 } 174 finally { 175 rs.close(); 176 } 177 } 178 179 return (rowCount); 180 } 181 182 189 public void getResultRows() throws SQLException { 190 if (skipRows > 0) { 191 while (resultSet.next()) { 192 position++; 193 if (position >= skipRows) { 194 break; 195 } 196 } 197 } 198 } 199 200 205 public void cleanUp() { 206 this.resultSetMetaData = null; 207 if (this.resultSet != null){ 208 try { 209 this.resultSet.close(); 210 this.resultSet = null; 211 } catch (SQLException e) { 212 } 214 } 215 if (this.preparedStatement != null){ 216 try { 217 this.preparedStatement.close(); 218 this.preparedStatement = null; 219 } catch (SQLException e) { 220 } 222 } 223 } 224 225 226 227 protected final void setPosition(int p) { 228 position = p; 229 } 230 231 protected final PreparedStatement setPreparedStatement(final PreparedStatement ps) { 232 preparedStatement = ps; 233 return (preparedStatement); 234 } 235 236 public final Connection getConnection() { 237 return (connection); 238 } 239 240 public final int getSkipRows() { 241 return (skipRows); 242 } 243 244 public final void setSkipRows(int i) { 245 skipRows = i; 246 } 247 248 public final int getMaxRows() { 249 return (maxRows); 250 } 251 252 public final void setMaxRows(int i) { 253 maxRows = i; 254 } 255 256 public final ResultSetMetaData getResultSetMetaData() { 257 return (resultSetMetaData); 258 } 259 260 public final PreparedStatement getPreparedStatement() { 261 return (preparedStatement); 262 } 263 264 public final CallableStatement getCallableStatement() { 265 return ((CallableStatement ) preparedStatement); 266 } 267 268 public final ResultSet getResultSet() { 269 return (resultSet); 270 } 271 272 public final boolean nextRow() throws SQLException { 273 position++; 274 return (resultSet.next()); 275 } 276 277 public final int getCurrentRow() { 278 return (position); 279 } 280 281 282 public final boolean execute(int resultSetFromObject) throws SQLException { 283 if (preparedStatement != null) { 284 hasResultSet = preparedStatement.execute(); 285 if (hasResultSet) { 286 resultSet = (ResultSet ) ((CallableStatement ) preparedStatement).getObject(resultSetFromObject); 287 queryResultsCount++; 288 return (true); 289 } 290 else { 291 updateResultsCount++; 292 updateCount = preparedStatement.getUpdateCount(); 293 return (updateCount > -1); 294 } 295 } 296 else { 297 return (false); 298 } 299 } 300 301 public final boolean execute() throws SQLException { 302 if (preparedStatement != null) { 303 hasResultSet = preparedStatement.execute(); 304 if (hasResultSet) { 305 resultSet = preparedStatement.getResultSet(); 306 resultSetMetaData = resultSet.getMetaData(); 307 queryResultsCount++; 308 return (true); 309 } 310 else { 311 updateResultsCount++; 312 updateCount = preparedStatement.getUpdateCount(); 313 return (updateCount > -1); 314 } 315 } 316 else { 317 return (false); 318 } 319 } 320 321 public final boolean executeQuery() throws SQLException { 322 if (preparedStatement != null) { 323 resultSet = preparedStatement.executeQuery(); 324 if (resultSet != null) { 325 resultSetMetaData = resultSet.getMetaData(); 326 queryResultsCount++; 327 hasResultSet = true; 328 return (true); 329 } 330 else { 331 return (false); 332 } 333 } 334 else { 335 return (false); 336 } 337 } 338 339 345 public final boolean getMoreResults() throws SQLException { 346 if (preparedStatement != null) { 347 hasResultSet = preparedStatement.getMoreResults(); 348 if (hasResultSet) { 349 resultSet = preparedStatement.getResultSet(); 350 resultSetMetaData = resultSet.getMetaData(); 351 queryResultsCount++; 352 return (true); 353 } 354 else { 355 updateResultsCount++; 356 updateCount = preparedStatement.getUpdateCount(); 357 return (updateCount > -1); 358 } 359 } 360 else { 361 return (false); 362 } 363 } 364 365 366 public final boolean hasResultSet() { 367 return (hasResultSet); 368 } 369 370 371 374 public final int getUpdateCount() { 375 return (updateCount); 376 } 377 378 381 public final int getQueryResultsCount() { 382 return (queryResultsCount); 383 } 384 385 388 public final int getUpdateResultsCount() { 389 return (updateResultsCount); 390 } 391 392 393 public final boolean keepGoing() { 394 return (keepgoing); 395 } 396 397 public final void setKeepGoing(boolean still) { 398 keepgoing = still; 399 } 400 401 402 403 public final void incGroupLevel() { 404 groupLevel++; 405 } 406 407 public final void decGroupLevel() { 408 groupLevel--; 409 } 410 411 public final boolean groupLevelExists() { 412 return (groups != null && groups.size() >= groupLevel + 1 && groups.get(groupLevel) != null); 413 } 414 415 public final void setGroupingVar(String key) throws SQLException { 416 if (groups == null) groups = new ArrayList (groupLevel); 417 groups.ensureCapacity(groupLevel); 418 groups.add(groupLevel, new EsqlGroup(key, getResultSet().getObject(key)) 419 ); 420 } 421 422 public final boolean hasGroupingVarChanged() throws SQLException { 423 if (changeLevel != -1) { 424 if (changeLevel < groupLevel) { 425 return (true); 426 } 427 else { 428 changeLevel = -1; 429 return (true); 430 } 431 } 432 else { 433 boolean result = false; 434 for (int i = 0; i <= groupLevel; i++) { 436 Object tmp = getResultSet().getObject(((EsqlGroup) groups.get(i)).var); 437 if (!tmp.equals(((EsqlGroup) groups.get(i)).value)) { 438 ((EsqlGroup) groups.get(i)).value = tmp; 439 result = true; 440 if (changeLevel == -1 && groupLevel != i) 441 changeLevel = i; 442 } 443 } 444 return (result); 445 } 446 } 447 448 final static class EsqlGroup { 449 public String var = null; 450 public Object value = null; 451 452 EsqlGroup(String var, Object value) { 453 this.var = var; 454 this.value = value; 455 } 456 } 457 458 } 459 | Popular Tags |