@@ -341,10 +341,9 @@ public int getBookID(String coverImage) {
341341 String sql = "select book_id from books\n "
342342 + "where cover_image = ? " ;
343343 try (
344- Connection conn = DBConnection .getInstance ().getConnection ();
345- PreparedStatement ps = conn .prepareStatement (sql )){
346- ps .setString (1 , coverImage );
347- ResultSet rs = ps .executeQuery () ;
344+ Connection conn = DBConnection .getInstance ().getConnection (); PreparedStatement ps = conn .prepareStatement (sql )) {
345+ ps .setString (1 , coverImage );
346+ ResultSet rs = ps .executeQuery ();
348347 while (rs .next ()) {
349348 return rs .getInt ("book_id" );
350349 }
@@ -354,4 +353,105 @@ public int getBookID(String coverImage) {
354353 return -1 ;
355354 }
356355
356+ @ Override
357+ public List <Book > getBooksByCursor (int cursor , int limit ) {
358+ List <Book > list = new ArrayList <>();
359+
360+ String sql = "SELECT b.book_id, b.title, b.slug, b.author, b.quantity, "
361+ + "c.category_id AS category_ID, c.name AS category_name, b.cover_image "
362+ + "FROM books b "
363+ + "LEFT JOIN categories c ON b.category_id = c.category_id "
364+ + "WHERE b.book_id > ? "
365+ + "ORDER BY b.book_id "
366+ + "OFFSET 0 ROWS FETCH NEXT ? ROWS ONLY" ;
367+
368+ logger .debug ("Executing Cursor Pagination SQL: {}" , sql );
369+
370+ try (Connection conn = DBConnection .getInstance ().getConnection (); PreparedStatement ps = conn .prepareStatement (sql )) {
371+
372+ ps .setInt (1 , cursor ); // ID cuối của trang trước (0 = trang đầu)
373+ ps .setInt (2 , limit ); // số bản ghi muốn lấy
374+
375+ try (ResultSet rs = ps .executeQuery ()) {
376+ while (rs .next ()) {
377+ Book b = new Book ();
378+ b .setBookID (rs .getInt ("book_id" ));
379+ b .setTitle (rs .getString ("title" ));
380+ b .setSlug (rs .getString ("slug" ));
381+ b .setAuthor (rs .getString ("author" ));
382+ b .setQuantity (rs .getInt ("quantity" ));
383+ b .setCoverImage (rs .getString ("cover_image" ));
384+
385+ Category category = new Category ();
386+ category .setCategoryID (rs .getInt ("category_ID" ));
387+ category .setType (BookType .convert (rs .getString ("category_name" )));
388+ b .setCategory (category );
389+
390+ list .add (b );
391+ }
392+ }
393+
394+ logger .info ("Cursor loaded {} books (cursor={}, limit={})" ,
395+ list .size (), cursor , limit );
396+
397+ } catch (SQLException e ) {
398+ logger .error ("Error retrieving books using cursor pagination" , e );
399+ }
400+
401+ return list ;
402+ }
403+
404+ @ Override
405+ public List <Book > searchBookByCursor (String query , int cursor , int limit ) {
406+ List <Book > list = new ArrayList <>();
407+
408+ String sql = "SELECT books.*, categories.* "
409+ + "FROM books "
410+ + "JOIN categories ON books.category_id = categories.category_id "
411+ + "WHERE (title_unaccented LIKE ? OR title LIKE ?) "
412+ + "AND books.book_id > ? "
413+ + "ORDER BY books.book_id "
414+ + "OFFSET 0 ROWS FETCH NEXT ? ROWS ONLY" ;
415+
416+ logger .info ("Searching books with cursor. Query={}, Cursor={}, Limit={}" , query , cursor , limit );
417+
418+ try (Connection conn = DBConnection .getInstance ().getConnection (); PreparedStatement ps = conn .prepareStatement (sql )) {
419+
420+ String key = "%" + query + "%" ;
421+
422+ ps .setString (1 , key );
423+ ps .setString (2 , key );
424+ ps .setInt (3 , cursor ); // KEYSET pagination
425+ ps .setInt (4 , limit );
426+
427+ try (ResultSet rs = ps .executeQuery ()) {
428+ while (rs .next ()) {
429+
430+ Book b = new Book ();
431+ b .setBookID (rs .getInt ("book_id" ));
432+ b .setSlug (rs .getString ("slug" ));
433+ b .setAuthor (rs .getString ("author" ));
434+ b .setTitle (rs .getString ("title" ));
435+ b .setQuantity (rs .getInt ("quantity" ));
436+ b .setDescription (rs .getString ("description" ));
437+ b .setCoverImage (rs .getString ("cover_image" ));
438+
439+ Category category = new Category ();
440+ category .setCategoryID (rs .getInt ("category_id" ));
441+ category .setType (BookType .convert (rs .getString ("name" )));
442+ b .setCategory (category );
443+
444+ list .add (b );
445+ }
446+ }
447+
448+ logger .info ("Loaded {} search results (cursor mode)" , list .size ());
449+
450+ } catch (SQLException s ) {
451+ logger .error ("Error executing search with cursor: {}" , s .getMessage (), s );
452+ }
453+
454+ return list ;
455+ }
456+
357457}
0 commit comments