@@ -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