package com.github.bedrin.jdbc.sniffer;

import java.lang.reflect.Proxy;
import java.sql.*;
import java.util.Properties;
import java.util.logging.Logger;

/**
 * Enable JDBC Sniffer by adding a {@code sniffer:} prefix to your JDBC URL.
 * For example:
 * {@code sniffer:jdbc:h2:mem:}
 *
 * After that you'll be able to verify the number of executed statements using the {@link Sniffer} class
 * @see Sniffer
 */
public class MockDriver implements Driver {

    private static final MockDriver INSTANCE = new MockDriver();

    static {
        load();
    }

    private static void load() {
        try {
            DriverManager.registerDriver(INSTANCE);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public Connection connect(String url, Properties info) throws SQLException {
        String originUrl = extractOriginUrl(url);
        Driver originDriver = DriverManager.getDriver(originUrl);
        Connection delegateConnection = originDriver.connect(originUrl, info);

        return Connection.class.cast(Proxy.newProxyInstance(
                MockDriver.class.getClassLoader(),
                new Class[]{Connection.class},
                new ConnectionInvocationHandler(delegateConnection)
        ));
    }

    private Driver getOriginDriver(String url) throws SQLException {
        String originUrl = extractOriginUrl(url);
        return DriverManager.getDriver(originUrl);
    }

    private static String extractOriginUrl(String url) {
        if (null == url) return null;
        if (url.startsWith(Constants.DRIVER_PREFIX)) return url.substring(Constants.DRIVER_PREFIX.length());
        return url;
    }

    @Override
    public boolean acceptsURL(String url) throws SQLException {
        return null != url && url.startsWith(Constants.DRIVER_PREFIX);
    }

    @Override
    public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws SQLException {
        Driver originDriver = getOriginDriver(url);
        return originDriver.getPropertyInfo(url, info);
    }

    @Override
    public int getMajorVersion() {
        return Constants.MAJOR_VERSION;
    }

    @Override
    public int getMinorVersion() {
        return Constants.MINOR_VERSION;
    }

    @Override
    public boolean jdbcCompliant() {
        return true;
    }

    @Override
    public Logger getParentLogger() throws SQLFeatureNotSupportedException {
        throw new SQLFeatureNotSupportedException();
    }
}

@@ -34,16 +34,75 @@
 iffer;%0A%0A
+import com.github.bedrin.jdbc.sniffer.util.ExceptionUtil;%0A%0A
 import j
@@ -125,16 +125,16 @@
 .Proxy;%0A
-
 import j
@@ -791,38 +791,24 @@
    %7D%0A    %7D%0A%0A
-    @Override%0A
     public C
@@ -1722,38 +1722,24 @@
 url;%0A    %7D%0A%0A
-    @Override%0A
     public b
@@ -1864,38 +1864,24 @@
 IX);%0A    %7D%0A%0A
-    @Override%0A
     public D
@@ -2078,38 +2078,24 @@
 fo);%0A    %7D%0A%0A
-    @Override%0A
     public i
@@ -2160,38 +2160,24 @@
 ION;%0A    %7D%0A%0A
-    @Override%0A
     public i
@@ -2242,38 +2242,24 @@
 ION;%0A    %7D%0A%0A
-    @Override%0A
     public b
@@ -2315,22 +2315,8 @@
  %7D%0A%0A
-    @Override%0A
     
@@ -2351,106 +2351,294 @@
 r() 
-throws SQLFeatureNotSupportedException %7B%0A        throw new SQLFeatureNotSupportedException();%0A    %7D
+%7B%0A        String message = %22getParentLogger() method is not implemented in JDBC Sniffer%22;%0A        if (!ExceptionUtil.throwException(%22java.sql.SQLFeatureNotSupportedException%22, message)) %7B%0A            throw new UnsupportedOperationException(message);%0A        %7D%0A        return null;%0A    %7D%0A
 %0A%7D%0A

Support old JRE's; removed @Override from interface methods implementation; removed reference to SQLFeatureNotSupportedException since it is java 8+ only