Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.html
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ <h1>
<p><b>1.12.1</b> (to be determined)</p>
<ul>
<li>Now requires Openfire 5.1.0 or later</li>
<li>[<a href="https://github.com/igniterealtime/openfire-restAPI-plugin/issues/265">#265</a>] - Ensure cross-database compatibility for unread message count query</li>
<li>[<a href="https://github.com/igniterealtime/openfire-restAPI-plugin/issues/261">#261</a>] - Remove the deprecated 'userservice' endpoint</li>
<li>[<a href="https://github.com/igniterealtime/openfire-restAPI-plugin/issues/259">#259</a>] - Prevent system property requests from affecting properties other than the one requested</li>
<li>[<a href="https://github.com/igniterealtime/openfire-restAPI-plugin/issues/256">#256</a>] - Record configuration changes in audit log</li>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.sql.SQLException;

import org.jivesoftware.database.DbConnectionManager;
import org.jivesoftware.database.DbConnectionManager.DatabaseType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xmpp.packet.JID;
Expand All @@ -36,12 +37,29 @@ public class MsgArchiveController {

/** The Constant INSTANCE. */
public static final MsgArchiveController INSTANCE = new MsgArchiveController();
/**
* Builds the SQL query for counting unread messages based on the underlying database type.
*
* @param databaseType the database connection used to detect the database product name
* @return the database-specific SQL query string for counting unread messages
*/
private String buildUserMessageCountQuery(DatabaseType databaseType) {
String castExpr;
switch (databaseType) {
case mysql:
castExpr = "CAST(p.offlineDate AS SIGNED)"; break;
case oracle:
castExpr = "CAST(p.offlineDate AS NUMBER)"; break;
default:
// PostgreSQL, SQL Server, Sybase — all understand BIGINT in CAST
castExpr = "CAST(p.offlineDate AS BIGINT)";
break;
}

/** The Constant USER_MESSAGE_COUNT. */
private static final String USER_MESSAGE_COUNT = "select COUNT(1) from ofMessageArchive a " +
"join ofPresence p on (a.sentDate > p.offlineDate) " +
return "SELECT COUNT(1) FROM ofMessageArchive a " +
"JOIN ofPresence p ON (a.sentDate > " + castExpr + ") " +
"WHERE a.toJID = ? AND p.username = ?";

}
/**
* Gets the single instance of MsgArchiveController.
*
Expand Down Expand Up @@ -70,7 +88,9 @@ public int getUnReadMessagesCount(JID jid) {
ResultSet rs = null;
try {
con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(USER_MESSAGE_COUNT);
DatabaseType databaseType = DbConnectionManager.getDatabaseType();
String userMessageCountQuery = buildUserMessageCountQuery(databaseType);
pstmt = con.prepareStatement(userMessageCountQuery);
pstmt.setString(1, jid.toBareJID());
pstmt.setString(2, jid.getNode());
rs = pstmt.executeQuery();
Expand Down
Loading