Skip to content

Commit 6812cb9

Browse files
fix: The database type detection mechanism has been redesigned.
1 parent 4eaa87c commit 6812cb9

2 files changed

Lines changed: 23 additions & 16 deletions

File tree

changelog.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ <h1>
4444
REST API Plugin Changelog
4545
</h1>
4646

47+
<p><b>1.12.1</b> October 9, 2025</p>
48+
<ul>
49+
<li>Ensure cross-database compatibility for unread message count query</li>
50+
</ul>
51+
4752
<p><b>1.12.0</b> July 4, 2025</p>
4853
<ul>
4954
<li>Now requires Openfire 5.0.0 or later</li>

src/java/org/jivesoftware/openfire/plugin/rest/controller/MsgArchiveController.java

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,29 +36,30 @@ public class MsgArchiveController {
3636

3737
/** The Constant INSTANCE. */
3838
public static final MsgArchiveController INSTANCE = new MsgArchiveController();
39+
3940
/**
4041
* Builds the SQL query for counting unread messages based on the underlying database type.
4142
*
42-
* @param con the database connection used to detect the database product name
43+
* @param databaseType the database connection used to detect the database product name
4344
* @return the database-specific SQL query string for counting unread messages
4445
* @throws SQLException if a database access error occurs while retrieving metadata
4546
*/
46-
private String buildUserMessageCountQuery(Connection con) throws SQLException {
47-
String db = con.getMetaData().getDatabaseProductName().toLowerCase();
47+
private String buildUserMessageCountQuery(DatabaseType databaseType) throws SQLException {
4848
String castExpr;
49-
50-
if (db.contains("mysql")) {
51-
castExpr = "CAST(p.offlineDate AS SIGNED)";
52-
} else if (db.contains("oracle")) {
53-
castExpr = "CAST(p.offlineDate AS NUMBER)";
54-
} else {
55-
// PostgreSQL, SQL Server, Sybase — all understand BIGINT in CAST
56-
castExpr = "CAST(p.offlineDate AS BIGINT)";
49+
switch (databaseType) {
50+
case mysql:
51+
castExpr = "CAST(p.offlineDate AS SIGNED)"; break;
52+
case oracle:
53+
castExpr = "CAST(p.offlineDate AS NUMBER)"; break;
54+
default:
55+
// PostgreSQL, SQL Server, Sybase — all understand BIGINT in CAST
56+
castExpr = "CAST(p.offlineDate AS BIGINT)";
57+
break;
5758
}
58-
59+
5960
return "SELECT COUNT(1) FROM ofMessageArchive a " +
60-
"JOIN ofPresence p ON (a.sentDate > " + castExpr + ") " +
61-
"WHERE a.toJID = ? AND p.username = ?";
61+
"JOIN ofPresence p ON (a.sentDate > " + castExpr + ") " +
62+
"WHERE a.toJID = ? AND p.username = ?";
6263
}
6364
/**
6465
* Gets the single instance of MsgArchiveController.
@@ -88,8 +89,9 @@ public int getUnReadMessagesCount(JID jid) {
8889
ResultSet rs = null;
8990
try {
9091
con = DbConnectionManager.getConnection();
91-
String userMessageCount = buildUserMessageCountQuery(con);
92-
pstmt = con.prepareStatement(userMessageCount);
92+
DatabaseType databaseType = DbConnectionManager.getDatabaseType();
93+
String userMessageCountQuery = buildUserMessageCountQuery(databaseType);
94+
pstmt = con.prepareStatement(userMessageCountQuery);
9395
pstmt.setString(1, jid.toBareJID());
9496
pstmt.setString(2, jid.getNode());
9597
rs = pstmt.executeQuery();

0 commit comments

Comments
 (0)