From d6b2e1347c950e99ff8e77e3358076c3de390fce Mon Sep 17 00:00:00 2001 From: Jan Vales Date: Wed, 28 Mar 2018 16:45:03 +0200 Subject: [PATCH] [1.3.3] Interceptor seems to work. --- .../ass1/jpa/interceptor/SQLInterceptor.java | 27 ++++++++++++------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/ass1-jpa/src/main/java/dst/ass1/jpa/interceptor/SQLInterceptor.java b/ass1-jpa/src/main/java/dst/ass1/jpa/interceptor/SQLInterceptor.java index 16baccb..170eb0f 100644 --- a/ass1-jpa/src/main/java/dst/ass1/jpa/interceptor/SQLInterceptor.java +++ b/ass1-jpa/src/main/java/dst/ass1/jpa/interceptor/SQLInterceptor.java @@ -2,23 +2,20 @@ package dst.ass1.jpa.interceptor; import org.hibernate.EmptyInterceptor; -public class SQLInterceptor extends EmptyInterceptor { +import java.util.concurrent.atomic.AtomicLong; +public class SQLInterceptor extends EmptyInterceptor { private static final long serialVersionUID = -3082243834965597947L; + private static boolean verbose = false; + private static AtomicLong counter = new AtomicLong(0); - @Override - public String onPrepareStatement(String sql) { - // TODO - return sql; - } public static void resetCounter() { - // TODO + SQLInterceptor.counter.set(0); } public static int getSelectCount() { - // TODO - return -1; + return (int) SQLInterceptor.counter.get(); } /** @@ -27,7 +24,17 @@ public class SQLInterceptor extends EmptyInterceptor { * @param verbose whether or not to be verbose */ public static void setVerbose(boolean verbose) { - // TODO + SQLInterceptor.verbose = verbose; } + @Override + public String onPrepareStatement(String sql) { + if (verbose) System.out.println("SQL: " + sql); + + if (sql.toLowerCase().matches("select.*from.*lecturer.*") || sql.toLowerCase().matches("select.*from.*lesson.*")) { + long newval = SQLInterceptor.counter.incrementAndGet(); + if (verbose) System.out.println("Count increased to: " + newval); + } + return sql; + } } -- 2.43.0