1 package dst.ass1.jpa.interceptor;
3 import org.hibernate.EmptyInterceptor;
5 import java.util.concurrent.atomic.AtomicLong;
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);
13 public static void resetCounter() {
14 SQLInterceptor.counter.set(0);
17 public static int getSelectCount() {
18 return (int) SQLInterceptor.counter.get();
22 * If the verbose argument is set, the interceptor prints the intercepted SQL statements to System.out.
24 * @param verbose whether or not to be verbose
26 public static void setVerbose(boolean verbose) {
27 SQLInterceptor.verbose = verbose;
31 public String onPrepareStatement(String sql) {
32 if (verbose) System.out.println("SQL: " + sql);
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);