From 15c83639a7e93efcd3f73dc8e2ca89d2e099fbe2 Mon Sep 17 00:00:00 2001 From: jarvis2f <137974272+jarvis2f@users.noreply.github.com> Date: Sat, 4 Jan 2025 16:41:32 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix:=20Fixed=20the=20query=20pro?= =?UTF-8?q?blem=20of=20stored=20file=20list.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../files/repository/impl/FileRepositoryImpl.java | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/api/src/main/java/telegram/files/repository/impl/FileRepositoryImpl.java b/api/src/main/java/telegram/files/repository/impl/FileRepositoryImpl.java index a487cff..a4806ce 100644 --- a/api/src/main/java/telegram/files/repository/impl/FileRepositoryImpl.java +++ b/api/src/main/java/telegram/files/repository/impl/FileRepositoryImpl.java @@ -106,10 +106,6 @@ public class FileRepositoryImpl implements FileRepository { whereClause += " AND (file_name LIKE #{search} OR caption LIKE #{search})"; params.put("search", "%%" + search + "%%"); } - if (fromMessageId > 0) { - whereClause += " AND message_id > #{fromMessageId}"; - params.put("fromMessageId", fromMessageId); - } if (StrUtil.isNotBlank(type)) { if (Objects.equals(type, "media")) { whereClause += " AND type IN ('photo', 'video')"; @@ -118,10 +114,15 @@ public class FileRepositoryImpl implements FileRepository { params.put("type", type); } } + String countClause = whereClause; + if (fromMessageId > 0) { + whereClause += " AND message_id < #{fromMessageId}"; + params.put("fromMessageId", fromMessageId); + } return Future.all( SqlTemplate .forQuery(pool, """ - SELECT * FROM file_record WHERE %s ORDER BY date DESC LIMIT 20 + SELECT * FROM file_record WHERE %s ORDER BY message_id desc LIMIT 20 """.formatted(whereClause)) .mapTo(FileRecord.ROW_MAPPER) .execute(params) @@ -131,7 +132,7 @@ public class FileRepositoryImpl implements FileRepository { SqlTemplate .forQuery(pool, """ SELECT COUNT(*) FROM file_record WHERE %s - """.formatted(whereClause)) + """.formatted(countClause)) .mapTo(rs -> rs.getLong(0)) .execute(params) .onFailure(err -> log.error("Failed to get file record count: %s".formatted(err.getMessage())))