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

com.undefinedlabs.scope.rules.sql.provider.internal.PreparedStatementQueryUtils 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;

import com.undefinedlabs.scope.rules.sql.model.PreparedStatementQueryParameter;
import com.undefinedlabs.scope.sender.internal.serializers.ObjectMapperFactory;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.util.Map;

public enum PreparedStatementQueryUtils {

    INSTANCE;

    public String generateParamKey(final int sqlParamIndex) {
        return "?".concat(String.valueOf(sqlParamIndex));
    }

    public String extractSqlMethod(final String sqlStatement) {
        if(StringUtils.isEmpty(sqlStatement)){
            return null;
        }

        final String[] chuncks = sqlStatement.split(" ", 2);
        return chuncks.length > 0 ? chuncks[0] : null;
    }

    public String generateSqlParamsAsString(final Map map) {
        if(map == null || map.isEmpty()){
            return null;
        }

        try {
            return ObjectMapperFactory.newConfiguredObjectMapper().writeValueAsString(map);
        } catch(IOException e){
            throw new RuntimeException(e);
        }
    }

    public String buildSql(String sqlStatement, Map parametersMap) {
        if(StringUtils.isEmpty(sqlStatement)){
            return null;
        }

        if(parametersMap == null || parametersMap.isEmpty()){
            return sqlStatement;
        }

        String sql = sqlStatement;
        for(PreparedStatementQueryParameter sqlParam : parametersMap.values()){
            sql = sql.replaceFirst("\\?", sqlParam.getValue());
        }


        return sql;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy