1 21 22 package org.opensubsystems.patterns.listdata.persist.db; 23 24 import java.sql.CallableStatement ; 25 import java.sql.Connection ; 26 import java.sql.PreparedStatement ; 27 import java.sql.ResultSet ; 28 import java.sql.SQLException ; 29 import java.util.List ; 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.DatabaseUtils; 36 import org.opensubsystems.core.util.GlobalConstants; 37 import org.opensubsystems.patterns.listdata.data.ListOptions; 38 39 48 public class SybaseListDatabaseUtils extends ListDatabaseUtils 49 { 50 52 55 public SybaseListDatabaseUtils() 56 { 57 super(); 58 } 59 60 62 65 public List getObjectList( 66 ListOptions options, 67 String query, 68 ListDatabaseFactory factory, 69 ListDatabaseSchema schema 70 ) throws OSSException 71 { 72 List lstObjects = null; 73 Connection cntConnection = null; 74 ResultSet rsQueryResults = null; 75 PreparedStatement pstmQuery = null; 76 CallableStatement selectStatement = null; 78 try 79 { 80 cntConnection = DatabaseConnectionFactoryImpl.getInstance().requestConnection(true); 82 83 int iCount = 0; 84 85 if ((!DatabaseImpl.getInstance().hasAbsolutePositioningSupport()) 86 || (DatabaseImpl.getInstance().preferCountToLast())) 87 { 88 94 iCount = getSelectCount(cntConnection, query, schema, options.getDomainId()); 96 97 setOptionPositioning(iCount, options); 99 100 lstObjects = allocateObjectList(options, iCount); 102 103 query = preprocessSelectQuery(query, options, schema); 106 124 selectStatement = cntConnection.prepareCall("EXEC GET_LIST ?, ?"); 125 selectStatement.setInt(1, options.getEndPosition()); 126 selectStatement.setString(2, query); 127 128 rsQueryResults = selectStatement.executeQuery(); 129 130 if (lstObjects != null) 131 { 132 if (DatabaseImpl.getInstance().hasSelectListRangeSupport()) 136 { 137 rsQueryResults.next(); 138 } 139 else 140 { 141 rsQueryResults.absolute(options.getBeginPosition()); 142 } 143 } 146 } 147 else 148 { 149 155 pstmQuery = cntConnection.prepareStatement( 158 query, 159 DatabaseImpl.getInstance().getSelectListResultSetType(), 160 DatabaseImpl.getInstance().getSelectListResultSetConcurrency() 161 ); 162 if (schema.isInDomain()) 163 { 164 pstmQuery.setInt(1, options.getDomainId()); 165 } 166 168 169 173 iCount = getTotalRecords(rsQueryResults, options); 176 178 setOptionPositioning(iCount, options); 180 181 lstObjects = allocateObjectList(options, iCount); 183 184 if (lstObjects != null) 185 { 186 rsQueryResults.absolute(options.getBeginPosition()); 188 } 190 } 191 192 194 if (lstObjects != null) 198 { 199 if (GlobalConstants.ERROR_CHECKING) 200 { 201 assert iCount > 0 202 : "List should be null for empty result"; 203 } 204 205 int iHelpCounter = 0; 206 int[] columnCodes = options.getRetrieveColumnCodes(); 208 209 while ((iHelpCounter == 0 || rsQueryResults.next()) 212 && ((iHelpCounter < options.getPageSize()) 213 || (options.getPageSize() == ListOptions.PAGE_SIZE_ALL))) 214 { 215 iHelpCounter++; 216 lstObjects.add( 217 factory.load(rsQueryResults, columnCodes, 1)); 218 } 219 } 221 222 224 lstObjects = getObjectListKeepSelected(factory, cntConnection, query, 227 options, lstObjects, schema); 228 } 229 catch (SQLException sqleExc) 230 { 231 throw new OSSDatabaseAccessException( 232 "Failed to retrieve specified list of data objects" + 233 " from the database.", sqleExc); 234 } 235 finally 236 { 237 DatabaseUtils.closeStatement(selectStatement); 238 DatabaseUtils.closeResultSetAndStatement(rsQueryResults, pstmQuery); 239 DatabaseConnectionFactoryImpl.getInstance().returnConnection(cntConnection); 240 } 241 242 return lstObjects; 243 } 244 245 248 public String preprocessSelectQuery( 249 String inputQuery, 250 ListOptions options, 251 ListDatabaseSchema schema 252 ) throws OSSException 253 { 254 if (DatabaseImpl.getInstance().hasSelectListRangeSupport()) 256 { 257 265 273 279 295 307 313 int iFromStart = 0; 314 int iActualPosition = -1; 315 int iIDCount = 1; 316 317 StringBuffer buffer = new StringBuffer (); 318 StringBuffer bufferRetrievedColumns = new StringBuffer (); 319 StringBuffer strAllColumnNames = new StringBuffer (); 320 321 String strQueryLowerCase = inputQuery.toLowerCase(); 326 327 iFromStart = strQueryLowerCase.indexOf(" from "); 328 String strColumnName = ""; 329 String strTemp = ""; 330 strAllColumnNames.append(inputQuery.substring( 332 strQueryLowerCase.indexOf("select ") + "select ".length(), 333 iFromStart)); 334 335 while (strAllColumnNames.indexOf(",") != -1) 339 { 340 iActualPosition = strAllColumnNames.indexOf(","); 343 strTemp = strAllColumnNames.substring(0, iActualPosition); 344 if (strTemp.indexOf(".") != -1) 347 { 348 strColumnName = strTemp.substring(strTemp.indexOf(".") + 1, strTemp.length()); 350 } 351 else 352 { 353 strColumnName = strTemp; 355 } 356 357 if (strColumnName.equalsIgnoreCase("ID")) 358 { 359 bufferRetrievedColumns.append("convert(NUMERIC(10, 0), "); 361 bufferRetrievedColumns.append(strTemp); 362 bufferRetrievedColumns.append(") as ID"); 363 bufferRetrievedColumns.append(iIDCount++); 364 } 365 else 366 { 367 bufferRetrievedColumns.append(strTemp); 368 } 369 bufferRetrievedColumns.append(", "); 370 371 strAllColumnNames.delete(0, iActualPosition + 1); 373 } 374 375 if (strAllColumnNames.indexOf(".") != -1) 378 { 379 strColumnName = strAllColumnNames.substring(strAllColumnNames.indexOf(".") + 1, 381 strAllColumnNames.length()); 382 } 383 else 384 { 385 strColumnName = strAllColumnNames.toString(); 387 } 388 389 if (strColumnName.equalsIgnoreCase("ID")) 390 { 391 bufferRetrievedColumns.append("convert(NUMERIC(10, 0), "); 393 bufferRetrievedColumns.append(strTemp); 394 bufferRetrievedColumns.append(") as ID"); 395 bufferRetrievedColumns.append(iIDCount++); 396 } 397 else 398 { 399 bufferRetrievedColumns.append(strAllColumnNames); 400 } 401 402 403 buffer.append("select "); 404 buffer.append(bufferRetrievedColumns); 405 buffer.append(", rownum = identity(9) into #tempA"); 406 buffer.append(inputQuery.substring(iFromStart, inputQuery.length())); 407 buffer.append(" select * from #tempA where rownum between "); 408 buffer.append(options.getBeginPosition()); 409 buffer.append(" and "); 410 buffer.append(options.getEndPosition()); 411 buffer.append(" order by rownum "); 412 413 int iIndex = -1; 418 iIndex = buffer.indexOf("?"); 419 if (iIndex > -1) 420 { 421 buffer.replace(iIndex, iIndex + 1, String.valueOf(options.getDomainId())); 422 } 423 424 return buffer.toString(); 425 426 } 431 else 432 { 433 return super.preprocessSelectQuery(inputQuery, options, schema); 435 } 436 } 437 } 438 | Popular Tags |