dbforum.java
来自「Jive是基于JSP/JAVA技术构架的一个大型BBS论坛系统,这是Jive论坛」· Java 代码 · 共 1,659 行 · 第 1/5 页
JAVA
1,659 行
query.append(", jiveThreadProp propTable"); } // WHERE BLOCK query.append(" WHERE jiveThread.forumID=").append(this.id); // User if (filterUser || (!countQuery && sortField == JiveGlobals.THREAD_NAME)) { query.append(" AND jiveThread.threadID=jiveMessage.threadID"); query.append(" AND jiveMessage.parentMessageID IS NULL"); } if (filterUser) { query.append(" AND jiveMessage.userID=").append(resultFilter.getUserID()); } // Properties for (int i=0; i<propertyCount; i++) { query.append(" AND jiveThread.threadID=p").append(i).append(".threadID"); query.append(" AND p").append(i).append(".name='"); query.append(resultFilter.getPropertyName(i)); query.append("' AND p").append(i).append(".propValue='"); query.append(resultFilter.getPropertyValue(i)).append("'"); } // Sort on thread name if (!countQuery && sortField == JiveGlobals.THREAD_NAME) { query.append(" AND jiveThread.threadID=jiveMessage.threadID"); } // Sort on properties if (sortField == JiveGlobals.EXTENDED_PROPERTY) { query.append(" AND jiveThread.threadID=propTable.threadID"); query.append(" AND propTable.name='"); query.append(resultFilter.getSortPropertyName()).append("'"); } // Creation date range if (filterCreationDate) { if (resultFilter.getCreationDateRangeMin() != null) { query.append(" AND jiveThread.creationDate >= '"); query.append(StringUtils.dateToMillis( resultFilter.getCreationDateRangeMin())); query.append("'"); } if (resultFilter.getCreationDateRangeMax() != null) { query.append(" AND jiveThread.creationDate <= '"); query.append(StringUtils.dateToMillis( resultFilter.getCreationDateRangeMax())); query.append("'"); } } // Modified date range if (filterModifiedDate) { if (resultFilter.getModifiedDateRangeMin() != null) { query.append(" AND jiveThread.modifiedDate >= '"); query.append(StringUtils.dateToMillis( resultFilter.getModifiedDateRangeMin())); query.append("'"); } if (resultFilter.getModifiedDateRangeMax() != null) { query.append(" AND jiveThread.modifiedDate <= '"); query.append(StringUtils.dateToMillis( resultFilter.getModifiedDateRangeMax())); query.append("'"); } } // Moderation range (but only if moderation hasn't been disabled at // the database level for performance reasons. if (!factory.moderationDisabled()) { int moderationRangeMin = resultFilter.getModerationRangeMin(); // If the minimum moderation value was not set, it should default // to the minimum threshold for the forum. if (moderationRangeMin == ResultFilter.NULL_INT) { moderationRangeMin = getModerationMinThreadValue(); } int moderationRangeMax = resultFilter.getModerationRangeMax(); if (moderationRangeMin == moderationRangeMax) { query.append(" AND jiveThread.modValue = "); query.append(moderationRangeMin); } else { // Ignore any min less than negative 1 million. if (moderationRangeMin > -1000000) { query.append(" AND jiveThread.modValue >= "); query.append(moderationRangeMin); } // Use a max value if it's defined. if (moderationRangeMax != ResultFilter.NULL_INT) { query.append(" AND jiveThread.modValue <= "); query.append(moderationRangeMax); } } } // ORDER BY if (!countQuery) { switch(sortField) { case JiveGlobals.THREAD_NAME: query.append(" ORDER BY jiveMessage.subject"); break; case JiveGlobals.MODIFIED_DATE: query.append(" ORDER BY jiveThread.modifiedDate"); break; case JiveGlobals.CREATION_DATE: query.append(" ORDER BY jiveThread.creationDate"); break; case JiveGlobals.EXTENDED_PROPERTY: query.append(" ORDER BY propTable.propValue"); break; } if (resultFilter.getSortOrder() == ResultFilter.DESCENDING) { query.append(" DESC"); } else { query.append(" ASC"); } } return query.toString(); } /** * Returns the SQL statement corresponding to a ResultFilter for messages. */ protected String getMessageListSQL(ResultFilter resultFilter, boolean countQuery) { int sortField = resultFilter.getSortField(); // Make sure the sort field is valid. if (!countQuery && !( sortField == JiveGlobals.MODIFIED_DATE || sortField == JiveGlobals.CREATION_DATE || sortField == JiveGlobals.MESSAGE_SUBJECT || sortField == JiveGlobals.MESSAGE_BODY || ( sortField == JiveGlobals.EXTENDED_PROPERTY && resultFilter.getSortPropertyName() != null ) ) ) { throw new IllegalArgumentException("The specified sort field is not valid."); } // We'll accumlate the query in a StringBuffer. StringBuffer query = new StringBuffer(80); if (!countQuery) { query.append("SELECT jiveMessage.messageID"); } else { query.append("SELECT count(1)"); } boolean filterUser = resultFilter.getUserID() != ResultFilter.NULL_INT; boolean filterCreationDate = resultFilter.getCreationDateRangeMin() != null || resultFilter.getCreationDateRangeMax() != null; boolean filterModifiedDate = resultFilter.getModifiedDateRangeMin() != null || resultFilter.getModifiedDateRangeMax() != null; int propertyCount = resultFilter.getPropertyCount(); // SELECT -- need to add value that we sort on if (!countQuery) { switch(sortField) { case JiveGlobals.MESSAGE_SUBJECT: query.append(", subject"); break; case JiveGlobals.MODIFIED_DATE: query.append(", modifiedDate"); break; case JiveGlobals.CREATION_DATE: query.append(", creationDate"); break; case JiveGlobals.EXTENDED_PROPERTY: query.append(", propTable.propValue"); break; } } // FROM -- values (in addition to jiveThread) query.append(" FROM jiveMessage"); for (int i=0; i<propertyCount; i++) { query.append(", jiveMessageProp p").append(i); } if (resultFilter.getSortField() == JiveGlobals.EXTENDED_PROPERTY) { query.append(", jiveMessageProp propTable"); } // WHERE BLOCK query.append(" WHERE forumID=").append(this.id); // User if (filterUser) { query.append(" AND userID=").append(resultFilter.getUserID()); } // Properties for (int i=0; i<propertyCount; i++) { query.append(" AND jiveMessage.messageID=p").append(i).append(".messageID"); query.append(" AND p").append(i).append(".name='"); query.append(resultFilter.getPropertyName(i)); query.append("' AND p").append(i).append(".propValue='"); query.append(resultFilter.getPropertyValue(i)).append("'"); } // Sort on properties if (sortField == JiveGlobals.EXTENDED_PROPERTY) { query.append(" AND jiveMessage.messageID=propTable.messageID"); query.append(" AND propTable.name='"); query.append(resultFilter.getSortPropertyName()).append("'"); } // Creation date range if (filterCreationDate) { if (resultFilter.getCreationDateRangeMin() != null) { query.append(" AND creationDate >= '"); query.append(StringUtils.dateToMillis( resultFilter.getCreationDateRangeMin())); query.append("'"); } if (resultFilter.getCreationDateRangeMax() != null) { query.append(" AND creationDate <= '"); query.append(StringUtils.dateToMillis( resultFilter.getCreationDateRangeMax())); query.append("'"); } } // Modified date range if (filterModifiedDate) { if (resultFilter.getModifiedDateRangeMin() != null) { query.append(" AND modifiedDate >= '"); query.append(StringUtils.dateToMillis( resultFilter.getModifiedDateRangeMin())); query.append("'"); } if (resultFilter.getModifiedDateRangeMax() != null) { query.append(" AND modifiedDate <= '"); query.append(StringUtils.dateToMillis( resultFilter.getModifiedDateRangeMax())); query.append("'"); } } // Moderation range (but only if moderation hasn't been disabled at // the database level for performance reasons. if (!factory.moderationDisabled()) { int moderationRangeMin = resultFilter.getModerationRangeMin(); // If the minimum moderation value was not set, it should default // to the minimum threshold for the forum. if (moderationRangeMin == ResultFilter.NULL_INT) { moderationRangeMin = getModerationMinMessageValue(); } int moderationRangeMax = resultFilter.getModerationRangeMax(); if (moderationRangeMin == moderationRangeMax) { query.append(" AND modValue = "); query.append(moderationRangeMin); } else { // Ignore any min less than negative 1 million. if (moderationRangeMin > -1000000) { query.append(" AND modValue >= "); query.append(moderationRangeMin); } // Use a max value if it's defined. if (moderationRangeMax != ResultFilter.NULL_INT) { query.append(" AND modValue <= "); query.append(moderationRangeMax); } } } // ORDER BY if (!countQuery) { switch(sortField) { case JiveGlobals.MESSAGE_SUBJECT: query.append(" ORDER BY subject"); break; case JiveGlobals.MODIFIED_DATE: query.append(" ORDER BY modifiedDate"); break; case JiveGlobals.CREATION_DATE: query.append(" ORDER BY creationDate"); break; case JiveGlobals.EXTENDED_PROPERTY: query.append(" ORDER BY propTable.propValue"); break; } if (resultFilter.getSortOrder() == ResultFilter.DESCENDING) { query.append(" DESC"); } else { query.append(" ASC"); } } return query.toString(); } /** * Returns a block of objectID's (threads or messages) from a query and * performs transparent caching of those blocks. The two parameters specify * a database query and a startIndex for the results in that query. * * @param query the SQL message list query to cache blocks from. * @param startIndex the startIndex in the list to get a block for. */ protected long [] getBlock(String query, int startIndex) { // First, find what block number the results will be in. int blockID = startIndex / BLOCK_SIZE; int blockStart = blockID * BLOCK_SIZE; // Now, check cache to see if the block is already cached. QueryCacheKey key = new QueryCacheKey(JiveGlobals.FORUM, id, query, blockID); // Synchronize on the sql query so that only one thread can try to load a // block at once. This should help to prevent the database from // becoming overloaded on busy sites. StringBuffer internKey = new StringBuffer(query.length()+4).append(query).append(blockID); synchronized (internKey.toString().intern()) { // First, look in long-term cache. long [] objectIDs = (long [])factory.cacheManager.queryCache.get(key); // If lookup in long-term cache failed, look in short-term cache. if (objectIDs == null && factory.cacheManager.isShortTermQueryCacheEnabled()) { objectIDs = (long [])factory.cacheManager.shortTermQueryCache.get(key); } // If already in cache, return the block. if (objectIDs != null) { /** * The actual block may be smaller than MESSAGE_BLOCK_SIZE. If * that's the case, it means two things: * 1) We're at the end boundary of all the results. * 2) If the start index is greater than the length of the current * block, than there aren't really any results to return. */ if (startIndex >= blockStart + objectIDs.length) { // Return an empty array return EMPTY_BLOCK; } else { return objectIDs; } } // Otherwise, we have to load up the block from the database. else { LongList objectList = new LongList(BLOCK_SIZE);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?