]> git.somenet.org - pub/jan/dst18.git/blob - ass1-jpa/src/main/java/dst/ass1/jpa/interceptor/SQLInterceptor.java
[1.3.3] Interceptor seems to work.
[pub/jan/dst18.git] / ass1-jpa / src / main / java / dst / ass1 / jpa / interceptor / SQLInterceptor.java
1 package dst.ass1.jpa.interceptor;
2
3 import org.hibernate.EmptyInterceptor;
4
5 import java.util.concurrent.atomic.AtomicLong;
6
7 public class SQLInterceptor extends EmptyInterceptor {
8     private static final long serialVersionUID = -3082243834965597947L;
9     private static boolean verbose = false;
10     private static AtomicLong counter = new AtomicLong(0);
11
12
13     public static void resetCounter() {
14         SQLInterceptor.counter.set(0);
15     }
16
17     public static int getSelectCount() {
18         return (int) SQLInterceptor.counter.get();
19     }
20
21     /**
22      * If the verbose argument is set, the interceptor prints the intercepted SQL statements to System.out.
23      *
24      * @param verbose whether or not to be verbose
25      */
26     public static void setVerbose(boolean verbose) {
27         SQLInterceptor.verbose = verbose;
28     }
29
30     @Override
31     public String onPrepareStatement(String sql) {
32         if (verbose) System.out.println("SQL: " + sql);
33
34         if (sql.toLowerCase().matches("select.*from.*lecturer.*") || sql.toLowerCase().matches("select.*from.*lesson.*")) {
35             long newval = SQLInterceptor.counter.incrementAndGet();
36             if (verbose) System.out.println("Count increased to: " + newval);
37         }
38         return sql;
39     }
40 }