All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.undefinedlabs.scope.rules.sql.provider.internal.h2.JdbcPreparedStatementQueryProvider Maven / Gradle / Ivy

Go to download

Scope is a APM for tests to give engineering teams unprecedented visibility into their CI process to quickly identify, troubleshoot and fix failed builds. This artifact contains the classes to instrument the Java SQL package.

There is a newer version: 0.15.1-beta.2
Show newest version
package com.undefinedlabs.scope.rules.sql.provider.internal.h2;

import com.undefinedlabs.scope.rules.sql.model.PreparedStatementQuery;
import com.undefinedlabs.scope.rules.sql.model.PreparedStatementQueryParameter;
import com.undefinedlabs.scope.rules.sql.provider.PreparedStatementQueryProvider;
import com.undefinedlabs.scope.rules.sql.provider.internal.PreparedStatementQueryUtils;
import com.undefinedlabs.scope.tracing.jdk.reflection.ReflectionContext;
import org.apache.commons.lang3.reflect.FieldUtils;
import org.apache.commons.lang3.reflect.MethodUtils;

import java.sql.ParameterMetaData;
import java.sql.PreparedStatement;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class JdbcPreparedStatementQueryProvider implements PreparedStatementQueryProvider {

    public static final String JDBC_PREPARED_STATEMENT_CLASS_NAME = "org.h2.jdbc.JdbcPreparedStatement";

    @Override
    public PreparedStatementQuery create(final PreparedStatement preparedStatement) {
        if(preparedStatement == null || !(ReflectionContext.INSTANCE.getScopeClass(JDBC_PREPARED_STATEMENT_CLASS_NAME).isInstance(preparedStatement))) {
            return PreparedStatementQuery.EMPTY;
        }

        try {
            final ParameterMetaData parameterMetaData = preparedStatement.getParameterMetaData();
            final String sqlStatement = (String) FieldUtils.readField(preparedStatement, "sqlStatement", true);
            final String sqlMethod = PreparedStatementQueryUtils.INSTANCE.extractSqlMethod(sqlStatement);
            final Object command = FieldUtils.readField(preparedStatement, "command", true);
            final List parametersList = (List) MethodUtils.invokeMethod(command, "getParameters");

            final Map parametersMap = new HashMap<>();
            for(int i = 0; i < parametersList.size(); i++){
                final Object paramValue = MethodUtils.invokeMethod(parametersList.get(i), "getParamValue");
                final Object paramType = MethodUtils.invokeMethod(paramValue, "getType");

                final int paramIndex = i + 1;
                final String paramKey = PreparedStatementQueryUtils.INSTANCE.generateParamKey(paramIndex); //SQL params start by 1.
                final String paramTypeSQL = (MethodUtils.invokeMethod(paramType, "getSQL", new StringBuilder())).toString();
                final String paramValueSQL = (String) MethodUtils.invokeMethod(paramValue, "getSQL");
                final PreparedStatementQueryParameter param = new PreparedStatementQueryParameter(paramTypeSQL, parameterMetaData.getParameterClassName(paramIndex), paramValueSQL);
                parametersMap.put(paramKey, param);
            }

            return new PreparedStatementQuery(sqlMethod, sqlStatement, parametersMap);

        } catch(Exception e){
            throw new RuntimeException(e);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy