From 2a5b4f9d483b3553b7953658f9ab502a53a7c053 Mon Sep 17 00:00:00 2001
From: Dessan Hemrayev <43703214+dessanhemrayev@users.noreply.github.com>
Date: Thu, 9 Oct 2025 11:25:54 +0300
Subject: [PATCH 1/2] fix: Ensure cross-database compatibility for unread
message count query
In the "ofpresence" table, the "offlineDate" field has the data type "VARCHAR/CHAR" depending on the
database, and in the query we are trying to compare this field with the "sentDate" field, which has
the data type "BIGINT/INTEGER". This results in the following error:
```
ERROR: operator does not exist: bigint > character varying
LINE 3: JOIN ofPresence p ON (a.sentDate > p.offlineDate)
HINT: No operator matches the given name and argument types. You might need to add explicit type casts.
```
This change replaces implicit comparison with explicit, database-specific CAST expressions.
The correct CAST is selected at runtime based on the underlying database (MySQL, PostgreSQL,
SQL Server, Oracle, Sybase), ensuring reliable and portable behavior across all Openfire-supported DBs.
---
changelog.html | 5 ++++
plugin.xml | 2 +-
.../rest/controller/MsgArchiveController.java | 30 +++++++++++++++----
3 files changed, 31 insertions(+), 6 deletions(-)
diff --git a/changelog.html b/changelog.html
index 2a0aa6a72..d83f73c80 100644
--- a/changelog.html
+++ b/changelog.html
@@ -44,6 +44,11 @@
REST API Plugin Changelog
+1.12.2 October 9, 2025
+
+ - Ensure cross-database compatibility for unread message count query
+
+
1.12.1 (to be determined)
- [#213] - Improve setting a subject in a chat room
diff --git a/plugin.xml b/plugin.xml
index cf76295bb..d4c3d310a 100644
--- a/plugin.xml
+++ b/plugin.xml
@@ -6,7 +6,7 @@
Allows administration over a RESTful API.
Roman Soldatow
${project.version}
- 2025-10-02
+ 2025-10-09
5.0.0
diff --git a/src/java/org/jivesoftware/openfire/plugin/rest/controller/MsgArchiveController.java b/src/java/org/jivesoftware/openfire/plugin/rest/controller/MsgArchiveController.java
index f87ad819c..004a3abc0 100644
--- a/src/java/org/jivesoftware/openfire/plugin/rest/controller/MsgArchiveController.java
+++ b/src/java/org/jivesoftware/openfire/plugin/rest/controller/MsgArchiveController.java
@@ -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;
@@ -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.
*
@@ -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();
From 2d7fada728e77fb4f0bdb352571526c71132fde9 Mon Sep 17 00:00:00 2001
From: Dessan Hemrayev <43703214+dessanhemrayev@users.noreply.github.com>
Date: Fri, 10 Oct 2025 17:24:25 +0300
Subject: [PATCH 2/2] Update changelog.html
Co-authored-by: Dan Caseley
---
changelog.html | 6 +-----
1 file changed, 1 insertion(+), 5 deletions(-)
diff --git a/changelog.html b/changelog.html
index d83f73c80..5192092ce 100644
--- a/changelog.html
+++ b/changelog.html
@@ -44,15 +44,11 @@
REST API Plugin Changelog
-1.12.2 October 9, 2025
-
- - Ensure cross-database compatibility for unread message count query
-
-
1.12.1 (to be determined)
- [#213] - Improve setting a subject in a chat room
- [#217] - Add Hurl e2e tests, and CI to run them
+ - Ensure cross-database compatibility for unread message count query
1.12.0 July 4, 2025