1 21 22 package org.opensubsystems.patterns.listdata.persist.db; 23 24 import java.sql.Connection ; 25 import java.sql.ResultSet ; 26 import java.sql.SQLException ; 27 import java.sql.Statement ; 28 import java.util.List ; 29 import java.util.logging.Logger ; 30 31 import org.opensubsystems.core.error.OSSDatabaseAccessException; 32 import org.opensubsystems.core.error.OSSException; 33 import org.opensubsystems.core.persist.db.DatabaseConnectionFactoryImpl; 34 import org.opensubsystems.core.persist.db.DatabaseImpl; 35 import org.opensubsystems.core.util.CallContext; 36 import org.opensubsystems.core.util.DatabaseUtils; 37 import org.opensubsystems.core.util.GlobalConstants; 38 import org.opensubsystems.core.util.Log; 39 import org.opensubsystems.core.util.StringUtils; 40 import org.opensubsystems.patterns.listdata.data.DataCondition; 41 import org.opensubsystems.patterns.listdata.data.ListOptions; 42 import org.opensubsystems.patterns.listdata.data.SimpleRule; 43 44 53 public class MySQLListDatabaseUtils extends ListDatabaseUtils 54 { 55 57 60 private static Logger s_logger = Log.getInstance(MySQLListDatabaseUtils.class); 61 62 64 67 public MySQLListDatabaseUtils() 68 { 69 super(); 70 } 71 72 74 77 public String getWhereClause( 78 String strTableName, 79 int[] arrIds, 80 SimpleRule securityFilter, 81 ListDatabaseSchema schema 82 ) throws OSSException 83 { 84 90 Connection cntConnection = null; 91 ResultSet rsQueryResults = null; 92 Statement stmQuery = null; 93 94 int iIndex = 0; 95 96 if (GlobalConstants.ERROR_CHECKING) 97 { 98 assert securityFilter != null 99 : "If I don't have rights for action, this shouldn't be called"; 100 } 101 102 StringBuffer buffer = new StringBuffer (); 103 StringBuffer bufferInnerQuery = new StringBuffer (); 104 105 buffer.append(" where "); 106 107 if (schema.isInDomain()) 108 { 109 buffer.append(strTableName); 112 buffer.append(".DOMAIN_ID = ? "); 113 } 114 115 if ((arrIds != null) && (arrIds.length > 0)) 116 { 117 if (schema.isInDomain()) 120 { 121 buffer.append(" and "); 122 } 123 buffer.append(strTableName); 124 buffer.append(".ID in ("); 125 buffer.append(StringUtils.parseIntArrayToString(arrIds, ",")); 126 buffer.append(")"); 127 } 128 129 List securityConditions = null; 132 133 securityConditions = securityFilter.getConditions(); 134 if (securityConditions != null) 135 { 136 String [] otherFromWhere; 140 141 buffer.append(" and "); 143 buffer.append(strTableName); 144 buffer.append(".ID in ("); 145 146 bufferInnerQuery.append("select "); 147 bufferInnerQuery.append(strTableName); 148 bufferInnerQuery.append(".ID from "); 149 bufferInnerQuery.append(strTableName); 150 otherFromWhere = schema.getJoinFromWhere(securityConditions, null); 153 if (otherFromWhere != null) 154 { 155 bufferInnerQuery.append(otherFromWhere[0]); 156 } 157 bufferInnerQuery.append(" where "); 159 if (schema.isInDomain()) 160 { 161 bufferInnerQuery.append(strTableName); 162 bufferInnerQuery.append(".DOMAIN_ID="); 163 bufferInnerQuery.append(CallContext.getInstance().getCurrentDomainId()); 164 } 165 else 168 { 169 bufferInnerQuery.append(strTableName); 170 bufferInnerQuery.append(".ID is not null "); 171 } 172 173 if ((otherFromWhere != null) && (otherFromWhere[1].length() > 0)) 174 { 175 bufferInnerQuery.append(" and "); 176 bufferInnerQuery.append(otherFromWhere[1]); 177 } 178 bufferInnerQuery.append(" and ("); 179 180 for (int iCount = 0; iCount < securityConditions.size(); iCount++) 182 { 183 if (iCount > 0) 184 { 185 bufferInnerQuery.append(" or "); 186 } 187 parseCondition(bufferInnerQuery, (DataCondition) securityConditions.get(iCount), 188 schema); 189 } 190 bufferInnerQuery.append(")"); 191 192 try 195 { 196 cntConnection = DatabaseConnectionFactoryImpl.getInstance().requestConnection(true); 198 stmQuery = cntConnection.createStatement(); 199 rsQueryResults = stmQuery.executeQuery(bufferInnerQuery.toString()); 200 201 bufferInnerQuery.delete(0, bufferInnerQuery.length()); 202 while (rsQueryResults.next()) 203 { 204 if (iIndex > 0) 205 { 206 bufferInnerQuery.append(", "); 207 } 208 bufferInnerQuery.append(rsQueryResults.getInt(1)); 209 iIndex++; 210 } 211 if (iIndex == 0) 213 { 214 bufferInnerQuery.append("-1"); 215 } 216 } 217 catch (SQLException sqleExc) 218 { 219 throw new OSSDatabaseAccessException( 220 "Failed to retrieve specified data by subselect " + 221 " from the database.", sqleExc); 222 } 223 finally 224 { 225 DatabaseUtils.closeResultSetAndStatement(rsQueryResults, stmQuery); 226 DatabaseConnectionFactoryImpl.getInstance().returnConnection(cntConnection); 227 } 228 229 buffer.append(bufferInnerQuery); 231 buffer.append(")"); 232 } 233 234 return buffer.toString(); 235 } 236 237 240 public List getObjectList( 241 ListOptions options, 242 String query, 243 ListDatabaseFactory factory, 244 ListDatabaseSchema schema 245 ) throws OSSException 246 { 247 return super.getObjectList(options, query, factory, schema); 248 249 418 } 419 420 422 425 protected String preprocessSelectQuery( 426 String inputQuery, 427 ListOptions options, 428 ListDatabaseSchema schema 429 ) throws OSSException 430 { 431 if (DatabaseImpl.getInstance().hasSelectListRangeSupport()) 433 { 434 442 StringBuffer buffer = new StringBuffer (); 443 StringBuffer bufferUsedIndex = new StringBuffer (); 444 445 String strQueryLowerCase = inputQuery.toLowerCase(); 450 451 String strTableName = ""; 452 String strColumnName = ""; 453 String strFromTables = ""; 454 String strIndexName = ""; 455 456 boolean bCanUseIndex = false; 457 boolean bCanUseTableName = false; 458 459 int iSortColumnStart = strQueryLowerCase.indexOf(" order by "); 462 int iSortColumnEnd = strQueryLowerCase.lastIndexOf(" asc"); 463 int iSortColumnEndDesc = strQueryLowerCase.lastIndexOf(" desc"); 464 int iFromWhereColumnStart = -1; 465 int iFromWhereColumnEnd = -1; 466 int ihelpIndex = -1; 467 int iBeginPosition = 0; 468 469 if (iSortColumnEndDesc > iSortColumnEnd) 471 { 472 iSortColumnEnd = iSortColumnEndDesc; 473 } 474 475 if (iSortColumnStart != -1) 477 { 478 if (GlobalConstants.ERROR_CHECKING) 479 { 480 assert iSortColumnEnd != -1 481 : "There should be set up value for ASC/DESC position."; 482 } 483 484 strColumnName = inputQuery.substring(iSortColumnStart + " order by ".length(), 488 iSortColumnEnd); 489 if (strColumnName.indexOf(",") == -1) 492 { 493 iFromWhereColumnStart = strQueryLowerCase.indexOf(" from "); 496 iFromWhereColumnEnd = strQueryLowerCase.indexOf(" where "); 497 498 if (strColumnName.indexOf(".") != -1) 503 { 504 strTableName = strColumnName.substring(0, strColumnName.indexOf(".")); 505 strColumnName = strColumnName.substring(strColumnName.indexOf(".") + 1, 506 strColumnName.length()); 507 bCanUseIndex = true; 508 bCanUseTableName = true; 509 } 510 else 511 { 512 516 strFromTables = inputQuery.substring(iFromWhereColumnStart + " from ".length(), 519 iFromWhereColumnEnd); 520 521 if (strFromTables.indexOf(",") == -1) 522 { 523 bCanUseIndex = true; 525 526 strTableName = strFromTables; 528 } 529 else 530 { 531 536 if (GlobalConstants.ERROR_CHECKING) 541 { 542 assert true 543 : "There should be defined belonging table name" + 544 " to the column within the ORDER BY-ASC/DESC" + 545 " clause while there are used more tables " + 546 " within the FROM-WHERE clause."; 547 } 548 } 549 } 550 } 551 else 552 { 553 s_logger.info("There will be not used index because there are more columns " + 556 "within the ORDER BY-ASC/DESC clause."); 557 } 558 } 559 560 buffer.append(inputQuery); 561 569 if (bCanUseIndex) 571 { 572 strIndexName = getConstructListIndexName(strTableName, strColumnName); 574 if (schema.isExistingIndex(strIndexName)) 576 { 577 bufferUsedIndex.append(" use index ("); 579 bufferUsedIndex.append(strIndexName); 580 bufferUsedIndex.append(") "); 581 } 582 583 if (bCanUseTableName) 584 { 585 588 ihelpIndex = buffer.indexOf(strTableName, iFromWhereColumnStart); 591 buffer.insert(ihelpIndex + strTableName.length() + 1, bufferUsedIndex); 592 } 593 else 594 { 595 buffer.insert(iFromWhereColumnStart, bufferUsedIndex); 598 } 599 } 600 601 610 buffer.append(" limit "); 611 if (options.getBeginPosition() > 0) 612 { 613 iBeginPosition = options.getBeginPosition() - 1; 614 } 615 buffer.append(iBeginPosition); 616 buffer.append(", "); 617 buffer.append(options.getPageSize()); 618 619 return buffer.toString(); 620 } 621 else 622 { 623 return super.preprocessSelectQuery(inputQuery, options, schema); 626 } 627 } 628 629 632 protected String preprocessSelectQueryForCreationDate( 633 String inputQuery, 634 ListOptions options, 635 ListDatabaseSchema schema 636 ) throws OSSException 637 { 638 return ListQueryPreprocessor.preprocessSelectQueryForCreationDate( 639 inputQuery, options, schema); 640 } 641 } 642 | Popular Tags |