package dev.elaine.datalab.mybatis;

import java.sql.Connection;
import java.util.concurrent.atomic.AtomicInteger;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Signature;

@Intercepts(@Signature(
    type = StatementHandler.class,
    method = "prepare",
    args = {Connection.class, Integer.class}))
public final class SqlCountingPlugin implements Interceptor {
  private final AtomicInteger preparations = new AtomicInteger();

  @Override
  public Object intercept(Invocation invocation) throws Throwable {
    preparations.incrementAndGet();
    return invocation.proceed();
  }

  public int preparations() {
    return preparations.get();
  }
}
